17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * main.c - Point-to-Point Protocol main module
37c478bd9Sstevel@tonic-gate  *
4f53eecf5SJames Carlson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software and its
87c478bd9Sstevel@tonic-gate  * documentation is hereby granted, provided that the above copyright
97c478bd9Sstevel@tonic-gate  * notice appears in all copies.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
127c478bd9Sstevel@tonic-gate  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
137c478bd9Sstevel@tonic-gate  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
147c478bd9Sstevel@tonic-gate  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  SUN SHALL NOT BE LIABLE FOR
157c478bd9Sstevel@tonic-gate  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
167c478bd9Sstevel@tonic-gate  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * Copyright (c) 1989 Carnegie Mellon University.
197c478bd9Sstevel@tonic-gate  * All rights reserved.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
227c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
237c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
247c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
257c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
267c478bd9Sstevel@tonic-gate  * by Carnegie Mellon University.  The name of the
277c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
287c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
297c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
307c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
317c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
327c478bd9Sstevel@tonic-gate  */
33*48bbca81SDaniel Hoffman /*
34*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
35*48bbca81SDaniel Hoffman  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <signal.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <syslog.h>
457c478bd9Sstevel@tonic-gate #include <netdb.h>
467c478bd9Sstevel@tonic-gate #include <pwd.h>
477c478bd9Sstevel@tonic-gate #include <setjmp.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <sys/wait.h>
517c478bd9Sstevel@tonic-gate #include <sys/time.h>
527c478bd9Sstevel@tonic-gate #include <sys/resource.h>
537c478bd9Sstevel@tonic-gate #include <sys/stat.h>
547c478bd9Sstevel@tonic-gate #include <sys/socket.h>
557c478bd9Sstevel@tonic-gate #include <netinet/in.h>
567c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include "pppd.h"
597c478bd9Sstevel@tonic-gate #include "magic.h"
607c478bd9Sstevel@tonic-gate #include "fsm.h"
617c478bd9Sstevel@tonic-gate #include "lcp.h"
627c478bd9Sstevel@tonic-gate #include "ipcp.h"
637c478bd9Sstevel@tonic-gate #ifdef INET6
647c478bd9Sstevel@tonic-gate #include "ipv6cp.h"
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate #include "upap.h"
677c478bd9Sstevel@tonic-gate #include "chap.h"
687c478bd9Sstevel@tonic-gate #include "ccp.h"
697c478bd9Sstevel@tonic-gate #include "pathnames.h"
707c478bd9Sstevel@tonic-gate #include "patchlevel.h"
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
737c478bd9Sstevel@tonic-gate #include "tdb.h"
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef CBCP_SUPPORT
777c478bd9Sstevel@tonic-gate #include "cbcp.h"
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #ifdef IPX_CHANGE
817c478bd9Sstevel@tonic-gate #include "ipxcp.h"
827c478bd9Sstevel@tonic-gate #endif /* IPX_CHANGE */
837c478bd9Sstevel@tonic-gate #ifdef AT_CHANGE
847c478bd9Sstevel@tonic-gate #include "atcp.h"
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* interface vars */
887c478bd9Sstevel@tonic-gate char ifname[32];		/* Interface name */
897c478bd9Sstevel@tonic-gate int ifunit = -1;		/* Interface unit number */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate char *progname;			/* Name of this program */
927c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN+1]; /* Our hostname */
937c478bd9Sstevel@tonic-gate static char pidfilename[MAXPATHLEN]; /* name of pid file */
947c478bd9Sstevel@tonic-gate static char linkpidfile[MAXPATHLEN]; /* name of linkname pid file */
957c478bd9Sstevel@tonic-gate char ppp_devnam[MAXPATHLEN];	/* name of PPP tty (maybe ttypx) */
967c478bd9Sstevel@tonic-gate static uid_t uid;		/* Our real user-id */
977c478bd9Sstevel@tonic-gate static int conn_running;	/* we have a [dis]connector running */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int ttyfd;			/* Serial port file descriptor */
1007c478bd9Sstevel@tonic-gate mode_t tty_mode = (mode_t)-1;	/* Original access permissions to tty */
1017c478bd9Sstevel@tonic-gate int baud_rate;			/* Actual bits/second for serial device */
1027c478bd9Sstevel@tonic-gate bool hungup;			/* terminal has been hung up */
1037c478bd9Sstevel@tonic-gate bool privileged;		/* we're running as real uid root */
1047c478bd9Sstevel@tonic-gate bool need_holdoff;		/* need holdoff period before restarting */
1057c478bd9Sstevel@tonic-gate bool detached;			/* have detached from terminal */
1067c478bd9Sstevel@tonic-gate struct stat devstat;		/* result of stat() on devnam */
1077c478bd9Sstevel@tonic-gate bool prepass = 0;		/* doing prepass to find device name */
1087c478bd9Sstevel@tonic-gate int devnam_fixed;		/* set while in options.ttyxx file */
1097c478bd9Sstevel@tonic-gate volatile int status;		/* exit status for pppd */
1107c478bd9Sstevel@tonic-gate int unsuccess;			/* # unsuccessful connection attempts */
1117c478bd9Sstevel@tonic-gate int do_callback;		/* != 0 if we should do callback next */
1127c478bd9Sstevel@tonic-gate int doing_callback;		/* != 0 if we are doing callback */
1137c478bd9Sstevel@tonic-gate char *callback_script;		/* script for doing callback */
1147c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
1157c478bd9Sstevel@tonic-gate TDB_CONTEXT *pppdb;		/* database for storing status etc. */
1167c478bd9Sstevel@tonic-gate char db_key[32];
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * For plug-in usage:
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  *	holdoff_hook - Can be used to change the demand-dial hold-off
1237c478bd9Sstevel@tonic-gate  *		time dynamically.  This is normally set by the
1247c478bd9Sstevel@tonic-gate  *		"holdoff" option, and is 30 seconds by default.
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  *	new_phase_hook - This is called for each change in the PPP
1277c478bd9Sstevel@tonic-gate  *		phase (per RFC 1661).  This can be used to log
1287c478bd9Sstevel@tonic-gate  *		progress.
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  *	check_options_hook - This is called before doing sys_init()
1317c478bd9Sstevel@tonic-gate  *		and allows the plugin to verify the selected options.
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  *	updown_script_hook - This is called with the proposed
1347c478bd9Sstevel@tonic-gate  *		command-line arguments for any of the
1357c478bd9Sstevel@tonic-gate  *		/etc/ppp/{ip,ipv6,ipx,auth}-{up,down} scripts before
1367c478bd9Sstevel@tonic-gate  *		fork/exec.  It can be used to add or change arguments.
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  *	device_pipe_hook - If this is set, then an extra fd (3) is
1397c478bd9Sstevel@tonic-gate  *		passed to the connect/disconnect script.  This extra
1407c478bd9Sstevel@tonic-gate  *		fd is the write side of a pipe, and the read side is
1417c478bd9Sstevel@tonic-gate  *		passed to this routine.  This can be used to pass
1427c478bd9Sstevel@tonic-gate  *		arbitrary data from the script back to pppd.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate int (*holdoff_hook) __P((void)) = NULL;
1457c478bd9Sstevel@tonic-gate int (*new_phase_hook) __P((int new, int old)) = NULL;
1467c478bd9Sstevel@tonic-gate int (*check_options_hook) __P((uid_t uid)) = NULL;
1477c478bd9Sstevel@tonic-gate int (*updown_script_hook) __P((const char ***argsp)) = NULL;
1487c478bd9Sstevel@tonic-gate void (*device_pipe_hook) __P((int pipefd)) = NULL;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static int fd_ppp = -1;		/* fd for talking PPP */
1517c478bd9Sstevel@tonic-gate static int fd_loop;		/* fd for getting demand-dial packets */
1527c478bd9Sstevel@tonic-gate static int pty_master;		/* fd for master side of pty */
1537c478bd9Sstevel@tonic-gate int pty_slave = -1;		/* fd for slave side of pty */
1547c478bd9Sstevel@tonic-gate static int real_ttyfd;		/* fd for actual serial port (not pty) */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int phase;			/* where the link is at */
1577c478bd9Sstevel@tonic-gate int kill_link;
1587c478bd9Sstevel@tonic-gate int open_ccp_flag;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static int waiting;		/* for input from peer or timer expiration */
1617c478bd9Sstevel@tonic-gate static sigjmp_buf sigjmp;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate char **script_env;		/* Env. variable values for scripts */
1647c478bd9Sstevel@tonic-gate int s_env_nalloc;		/* # words avail at script_env */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate u_char outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for outgoing packet */
1677c478bd9Sstevel@tonic-gate u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */
1687c478bd9Sstevel@tonic-gate u_char nak_buffer[PPP_MRU];	/* where we construct a nak packet */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate static int n_children;		/* # child processes still running */
1717c478bd9Sstevel@tonic-gate static bool got_sigchld;	/* set if we have received a SIGCHLD */
1727c478bd9Sstevel@tonic-gate static sigset_t main_sigmask;	/* signals blocked while dispatching */
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static bool locked;		/* lock() has succeeded */
1757c478bd9Sstevel@tonic-gate static bool privopen;		/* don't lock, open device as root */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n";
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate GIDSET_TYPE groups[NGROUPS_MAX];/* groups the user is in */
1807c478bd9Sstevel@tonic-gate int ngroups;			/* How many groups valid in groups */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate static struct timeval start_time; /* Time when link was started. */
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate struct pppd_stats link_stats;
1857c478bd9Sstevel@tonic-gate int link_connect_time;
1867c478bd9Sstevel@tonic-gate bool link_stats_valid;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static pid_t charshunt_pid;	/* Process ID for charshunt */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate extern option_t general_options[];
1917c478bd9Sstevel@tonic-gate extern option_t auth_options[];
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * We maintain a list of child process pids and
1957c478bd9Sstevel@tonic-gate  * functions to call when they exit.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate struct subprocess {
1987c478bd9Sstevel@tonic-gate     pid_t	pid;
1997c478bd9Sstevel@tonic-gate     char	*prog;
2007c478bd9Sstevel@tonic-gate     void	(*done) __P((void *, int));
2017c478bd9Sstevel@tonic-gate     void	*arg;
2027c478bd9Sstevel@tonic-gate     struct subprocess *next;
2037c478bd9Sstevel@tonic-gate };
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static struct subprocess *children;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /* Prototypes for procedures local to this file. */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate static void setup_signals __P((void));
2107c478bd9Sstevel@tonic-gate static void create_pidfile __P((void));
2117c478bd9Sstevel@tonic-gate static void create_linkpidfile __P((void));
2127c478bd9Sstevel@tonic-gate static void cleanup __P((void));
2137c478bd9Sstevel@tonic-gate static void close_tty __P((void));
2147c478bd9Sstevel@tonic-gate static void get_input __P((void));
2157c478bd9Sstevel@tonic-gate static void calltimeout __P((void));
2167c478bd9Sstevel@tonic-gate static struct timeval *timeleft __P((struct timeval *));
2177c478bd9Sstevel@tonic-gate static void kill_my_pg __P((int));
2187c478bd9Sstevel@tonic-gate static void hup __P((int));
2197c478bd9Sstevel@tonic-gate static void term __P((int));
2207c478bd9Sstevel@tonic-gate static void chld __P((int));
2217c478bd9Sstevel@tonic-gate static void toggle_debug __P((int));
2227c478bd9Sstevel@tonic-gate static void open_ccp __P((int));
2237c478bd9Sstevel@tonic-gate static void bad_signal __P((int));
2247c478bd9Sstevel@tonic-gate static void holdoff_end __P((void *));
2257c478bd9Sstevel@tonic-gate static int device_script __P((char *, int, int, int, char *));
2267c478bd9Sstevel@tonic-gate static int reap_kids __P((int waitfor));
2277c478bd9Sstevel@tonic-gate static void record_child __P((pid_t, char *, void (*) (void *, int), void *));
2287c478bd9Sstevel@tonic-gate static int open_socket __P((char *));
2297c478bd9Sstevel@tonic-gate static int start_charshunt __P((int, int));
2307c478bd9Sstevel@tonic-gate static void charshunt_done __P((void *, int));
2317c478bd9Sstevel@tonic-gate static void charshunt __P((int, int, char *));
2327c478bd9Sstevel@tonic-gate static int record_write __P((FILE *, int code, u_char *buf, int nb,
2337c478bd9Sstevel@tonic-gate     struct timeval *));
2347c478bd9Sstevel@tonic-gate static void final_reap __P((void));
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
2377c478bd9Sstevel@tonic-gate static void update_db_entry __P((void));
2387c478bd9Sstevel@tonic-gate static void add_db_key __P((const char *));
2397c478bd9Sstevel@tonic-gate static void delete_db_key __P((const char *));
2407c478bd9Sstevel@tonic-gate static void cleanup_db __P((void));
2417c478bd9Sstevel@tonic-gate #endif
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate int main __P((int, char *[]));
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate #ifdef ultrix
2467c478bd9Sstevel@tonic-gate #undef	O_NONBLOCK
2477c478bd9Sstevel@tonic-gate #define	O_NONBLOCK	O_NDELAY
2487c478bd9Sstevel@tonic-gate #endif
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate #ifdef ULTRIX
2517c478bd9Sstevel@tonic-gate #define setlogmask(x)	0
2527c478bd9Sstevel@tonic-gate #endif
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /* Backward compatibility for Linux */
2557c478bd9Sstevel@tonic-gate #ifndef RECMARK_TIMESTART
2567c478bd9Sstevel@tonic-gate #define	RECMARK_STARTSEND	1
2577c478bd9Sstevel@tonic-gate #define	RECMARK_STARTRECV	2
2587c478bd9Sstevel@tonic-gate #define	RECMARK_ENDSEND		3
2597c478bd9Sstevel@tonic-gate #define	RECMARK_ENDRECV		4
2607c478bd9Sstevel@tonic-gate #define	RECMARK_TIMEDELTA32	5
2617c478bd9Sstevel@tonic-gate #define	RECMARK_TIMEDELTA8	6
2627c478bd9Sstevel@tonic-gate #define	RECMARK_TIMESTART	7
2637c478bd9Sstevel@tonic-gate #endif
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * PPP Data Link Layer "protocol" table.
2677c478bd9Sstevel@tonic-gate  * One entry per supported protocol.
2687c478bd9Sstevel@tonic-gate  * The last entry must be NULL.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate struct protent *protocols[] = {
2717c478bd9Sstevel@tonic-gate     &lcp_protent,
2727c478bd9Sstevel@tonic-gate     &pap_protent,
2737c478bd9Sstevel@tonic-gate     &chap_protent,
2747c478bd9Sstevel@tonic-gate #ifdef CBCP_SUPPORT
2757c478bd9Sstevel@tonic-gate     &cbcp_protent,
2767c478bd9Sstevel@tonic-gate #endif
2777c478bd9Sstevel@tonic-gate     &ipcp_protent,
2787c478bd9Sstevel@tonic-gate #ifdef INET6
2797c478bd9Sstevel@tonic-gate     &ipv6cp_protent,
2807c478bd9Sstevel@tonic-gate #endif
2817c478bd9Sstevel@tonic-gate     &ccp_protent,
2827c478bd9Sstevel@tonic-gate #ifdef IPX_CHANGE
2837c478bd9Sstevel@tonic-gate     &ipxcp_protent,
2847c478bd9Sstevel@tonic-gate #endif
2857c478bd9Sstevel@tonic-gate #ifdef AT_CHANGE
2867c478bd9Sstevel@tonic-gate     &atcp_protent,
2877c478bd9Sstevel@tonic-gate #endif
2887c478bd9Sstevel@tonic-gate     NULL
2897c478bd9Sstevel@tonic-gate };
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate int
main(argc,argv)2927c478bd9Sstevel@tonic-gate main(argc, argv)
2937c478bd9Sstevel@tonic-gate     int argc;
2947c478bd9Sstevel@tonic-gate     char *argv[];
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate     int i, fdflags, t;
2977c478bd9Sstevel@tonic-gate     char *p, *connector;
2987c478bd9Sstevel@tonic-gate     struct passwd *pw;
2997c478bd9Sstevel@tonic-gate     struct timeval timo;
3007c478bd9Sstevel@tonic-gate     struct protent *protp;
3017c478bd9Sstevel@tonic-gate     struct stat statbuf;
3027c478bd9Sstevel@tonic-gate     char numbuf[16];
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate     ifname[0] = '\0';
3057c478bd9Sstevel@tonic-gate     new_phase(PHASE_INITIALIZE);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate     /*
3087c478bd9Sstevel@tonic-gate      * Ensure that fds 0, 1, 2 are open, to /dev/null if nowhere else.
3097c478bd9Sstevel@tonic-gate      * This way we can close 0, 1, 2 in detach() without clobbering
3107c478bd9Sstevel@tonic-gate      * a fd that we are using.
3117c478bd9Sstevel@tonic-gate      */
3127c478bd9Sstevel@tonic-gate     if ((i = open(_PATH_DEVNULL, O_RDWR)) >= 0) {
3137c478bd9Sstevel@tonic-gate 	while (0 <= i && i <= 2)
3147c478bd9Sstevel@tonic-gate 	    i = dup(i);
3157c478bd9Sstevel@tonic-gate 	if (i >= 0)
3167c478bd9Sstevel@tonic-gate 	    (void) close(i);
3177c478bd9Sstevel@tonic-gate     }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate     script_env = NULL;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate     /* Initialize syslog facilities */
3227c478bd9Sstevel@tonic-gate     reopen_log();
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate     if (gethostname(hostname, MAXHOSTNAMELEN+1) < 0 ) {
3257c478bd9Sstevel@tonic-gate 	option_error("Couldn't get hostname: %m");
3267c478bd9Sstevel@tonic-gate 	exit(1);
3277c478bd9Sstevel@tonic-gate     }
3287c478bd9Sstevel@tonic-gate     hostname[MAXHOSTNAMELEN] = '\0';
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate     /* make sure we don't create world or group writable files. */
3317c478bd9Sstevel@tonic-gate     (void) umask(umask(0777) | 022);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate     uid = getuid();
3347c478bd9Sstevel@tonic-gate     privileged = (uid == 0);
3357c478bd9Sstevel@tonic-gate     (void) slprintf(numbuf, sizeof(numbuf), "%d", uid);
3367c478bd9Sstevel@tonic-gate     script_setenv("ORIG_UID", numbuf, 0);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate     ngroups = getgroups(NGROUPS_MAX, groups);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate     /*
3417c478bd9Sstevel@tonic-gate      * Initialize magic number generator now so that protocols may
3427c478bd9Sstevel@tonic-gate      * use magic numbers in initialization.
3437c478bd9Sstevel@tonic-gate      */
3447c478bd9Sstevel@tonic-gate     magic_init();
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate     progname = *argv;
3477c478bd9Sstevel@tonic-gate     prepass = 0;
3487c478bd9Sstevel@tonic-gate     /*
3497c478bd9Sstevel@tonic-gate      * Initialize to the standard option set, then parse, in order, the
3507c478bd9Sstevel@tonic-gate      * system options file, the user's options file, the tty's options file,
3517c478bd9Sstevel@tonic-gate      * and the command line arguments.  At last, install the options declared
3527c478bd9Sstevel@tonic-gate      * by each protocol into the extra_option list.
3537c478bd9Sstevel@tonic-gate      */
3547c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i) {
3557c478bd9Sstevel@tonic-gate         (*protp->init)(0);
3567c478bd9Sstevel@tonic-gate 	if (protp->options != NULL) {
3577c478bd9Sstevel@tonic-gate 	    add_options(protp->options);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate     }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate     /*
3627c478bd9Sstevel@tonic-gate      * Install "generic" options into the extra_options list.
3637c478bd9Sstevel@tonic-gate      */
3647c478bd9Sstevel@tonic-gate     add_options(auth_options);
3657c478bd9Sstevel@tonic-gate     add_options(general_options);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate     /* Install any system-specific options (or remove unusable ones) */
3687c478bd9Sstevel@tonic-gate     sys_options();
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate     if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1)
3717c478bd9Sstevel@tonic-gate 	|| !options_from_user())
3727c478bd9Sstevel@tonic-gate 	exit(EXIT_OPTION_ERROR);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate     /* scan command line and options files to find device name */
3757c478bd9Sstevel@tonic-gate     prepass = 1;
3767c478bd9Sstevel@tonic-gate     (void) parse_args(argc-1, argv+1);
3777c478bd9Sstevel@tonic-gate     prepass = 0;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate     /*
3807c478bd9Sstevel@tonic-gate      * Work out the device name, if it hasn't already been specified.
3817c478bd9Sstevel@tonic-gate      */
3827c478bd9Sstevel@tonic-gate     using_pty = notty || ptycommand != NULL || pty_socket != NULL;
3837c478bd9Sstevel@tonic-gate     if (!using_pty && default_device && !direct_tty) {
3847c478bd9Sstevel@tonic-gate 	char *p;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (!isatty(0) || (p = ttyname(0)) == NULL) {
3877c478bd9Sstevel@tonic-gate 	    option_error("no device specified and stdin is not a tty");
3887c478bd9Sstevel@tonic-gate 	    exit(EXIT_OPTION_ERROR);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 	(void) strlcpy(devnam, p, sizeof(devnam));
3917c478bd9Sstevel@tonic-gate 	if (stat(devnam, &devstat) < 0)
3927c478bd9Sstevel@tonic-gate 	    fatal("Couldn't stat default device %s: %m", devnam);
3937c478bd9Sstevel@tonic-gate     }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate     /*
3967c478bd9Sstevel@tonic-gate      * Parse the tty options file and the command line.
3977c478bd9Sstevel@tonic-gate      * The per-tty options file should not change
3987c478bd9Sstevel@tonic-gate      * ptycommand, pty_socket, notty or devnam.
3997c478bd9Sstevel@tonic-gate      */
4007c478bd9Sstevel@tonic-gate     devnam_fixed = 1;
4017c478bd9Sstevel@tonic-gate     if (!using_pty && !direct_tty) {
4027c478bd9Sstevel@tonic-gate 	if (!options_for_tty())
4037c478bd9Sstevel@tonic-gate 	    exit(EXIT_OPTION_ERROR);
4047c478bd9Sstevel@tonic-gate     }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate     devnam_fixed = 0;
4077c478bd9Sstevel@tonic-gate     if (!parse_args(argc-1, argv+1))
4087c478bd9Sstevel@tonic-gate 	exit(EXIT_OPTION_ERROR);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate     /*
4117c478bd9Sstevel@tonic-gate      * Check that we are running as root.
4127c478bd9Sstevel@tonic-gate      */
4137c478bd9Sstevel@tonic-gate     if (geteuid() != 0) {
4147c478bd9Sstevel@tonic-gate 	option_error("must be root to run %s, since it is not setuid-root",
4157c478bd9Sstevel@tonic-gate 		     argv[0]);
4167c478bd9Sstevel@tonic-gate 	exit(EXIT_NOT_ROOT);
4177c478bd9Sstevel@tonic-gate     }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate     if (!ppp_available()) {
4207c478bd9Sstevel@tonic-gate 	option_error(no_ppp_msg);
4217c478bd9Sstevel@tonic-gate 	exit(EXIT_NO_KERNEL_SUPPORT);
4227c478bd9Sstevel@tonic-gate     }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate     /*
4257c478bd9Sstevel@tonic-gate      * Check that the options given are valid and consistent.
4267c478bd9Sstevel@tonic-gate      */
4277c478bd9Sstevel@tonic-gate     if (!sys_check_options())
4287c478bd9Sstevel@tonic-gate 	exit(EXIT_OPTION_ERROR);
4297c478bd9Sstevel@tonic-gate     auth_check_options();
4307c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
4317c478bd9Sstevel@tonic-gate     mp_check_options();
4327c478bd9Sstevel@tonic-gate #endif
4337c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
4347c478bd9Sstevel@tonic-gate 	if (protp->enabled_flag && protp->check_options != NULL)
4357c478bd9Sstevel@tonic-gate 	    (*protp->check_options)();
4367c478bd9Sstevel@tonic-gate     if (demand && (connect_script == NULL)) {
4377c478bd9Sstevel@tonic-gate 	option_error("connect script is required for demand-dialling\n");
4387c478bd9Sstevel@tonic-gate 	exit(EXIT_OPTION_ERROR);
4397c478bd9Sstevel@tonic-gate     }
4407c478bd9Sstevel@tonic-gate     if (updetach && (nodetach || demand)) {
4417c478bd9Sstevel@tonic-gate 	option_error("updetach cannot be used with %s",
4427c478bd9Sstevel@tonic-gate 	    nodetach ? "nodetach" : "demand");
4437c478bd9Sstevel@tonic-gate 	exit(EXIT_OPTION_ERROR);
4447c478bd9Sstevel@tonic-gate     }
4457c478bd9Sstevel@tonic-gate     /* default holdoff to 0 if no connect script has been given */
4467c478bd9Sstevel@tonic-gate     if ((connect_script == NULL) && !holdoff_specified)
4477c478bd9Sstevel@tonic-gate 	holdoff = 0;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate     if (using_pty || direct_tty) {
4507c478bd9Sstevel@tonic-gate 	if (!default_device) {
4517c478bd9Sstevel@tonic-gate 	    option_error("%s option precludes specifying device name",
4527c478bd9Sstevel@tonic-gate 			 notty? "notty": "pty");
4537c478bd9Sstevel@tonic-gate 	    exit(EXIT_OPTION_ERROR);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	if (ptycommand != NULL && (notty || direct_tty)) {
4567c478bd9Sstevel@tonic-gate 	    option_error("pty option is incompatible with notty option");
4577c478bd9Sstevel@tonic-gate 	    exit(EXIT_OPTION_ERROR);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	if (pty_socket != NULL && (ptycommand != NULL || notty ||
4607c478bd9Sstevel@tonic-gate 	    direct_tty)) {
4617c478bd9Sstevel@tonic-gate 	    option_error("socket option is incompatible with pty and notty");
4627c478bd9Sstevel@tonic-gate 	    exit(EXIT_OPTION_ERROR);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	default_device = notty || direct_tty;
4657c478bd9Sstevel@tonic-gate 	lockflag = 0;
4667c478bd9Sstevel@tonic-gate 	modem = 0;
4677c478bd9Sstevel@tonic-gate 	if (default_device && log_to_fd <= 1)
4687c478bd9Sstevel@tonic-gate 	    log_to_fd = -1;
4697c478bd9Sstevel@tonic-gate     } else {
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * If the user has specified a device which is the same as
4727c478bd9Sstevel@tonic-gate 	 * the one on stdin, pretend they didn't specify any.
4737c478bd9Sstevel@tonic-gate 	 * If the device is already open read/write on stdin,
4747c478bd9Sstevel@tonic-gate 	 * we assume we don't need to lock it, and we can open it as root.
4757c478bd9Sstevel@tonic-gate 	 */
4767c478bd9Sstevel@tonic-gate 	if (fstat(0, &statbuf) >= 0 && S_ISCHR(statbuf.st_mode)
4777c478bd9Sstevel@tonic-gate 	    && statbuf.st_rdev == devstat.st_rdev) {
4787c478bd9Sstevel@tonic-gate 	    default_device = 1;
4797c478bd9Sstevel@tonic-gate 	    fdflags = fcntl(0, F_GETFL);
4807c478bd9Sstevel@tonic-gate 	    if (fdflags != -1 && (fdflags & O_ACCMODE) == O_RDWR)
4817c478bd9Sstevel@tonic-gate 		privopen = 1;
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate     }
4847c478bd9Sstevel@tonic-gate     if (default_device)
4857c478bd9Sstevel@tonic-gate 	nodetach = 1;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate     /*
4887c478bd9Sstevel@tonic-gate      * Don't send log messages to the serial port, it tends to
4897c478bd9Sstevel@tonic-gate      * confuse the peer. :-)
4907c478bd9Sstevel@tonic-gate      */
4917c478bd9Sstevel@tonic-gate     if (log_to_fd >= 0 && fstat(log_to_fd, &statbuf) >= 0
4927c478bd9Sstevel@tonic-gate 	&& S_ISCHR(statbuf.st_mode) && statbuf.st_rdev == devstat.st_rdev)
4937c478bd9Sstevel@tonic-gate 	log_to_fd = -1;
4947c478bd9Sstevel@tonic-gate     early_log = 0;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate     if (debug)
4977c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_DEBUG));
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate     /*
5007c478bd9Sstevel@tonic-gate      * Initialize system-dependent stuff.
5017c478bd9Sstevel@tonic-gate      */
5027c478bd9Sstevel@tonic-gate     if (check_options_hook != NULL &&
5037c478bd9Sstevel@tonic-gate 	(*check_options_hook)(uid) == -1) {
5047c478bd9Sstevel@tonic-gate 	exit(EXIT_OPTION_ERROR);
5057c478bd9Sstevel@tonic-gate     }
5067c478bd9Sstevel@tonic-gate     sys_init(!devnam_info.priv && !privopen);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
5097c478bd9Sstevel@tonic-gate     pppdb = tdb_open(_PATH_PPPDB, 0, 0, O_RDWR|O_CREAT, 0644);
5107c478bd9Sstevel@tonic-gate     if (pppdb != NULL) {
5117c478bd9Sstevel@tonic-gate 	(void) slprintf(db_key, sizeof(db_key), "pppd%d", getpid());
5127c478bd9Sstevel@tonic-gate 	update_db_entry();
5137c478bd9Sstevel@tonic-gate     } else {
5147c478bd9Sstevel@tonic-gate 	warn("Warning: couldn't open ppp database %s", _PATH_PPPDB);
5157c478bd9Sstevel@tonic-gate 	if (multilink) {
5167c478bd9Sstevel@tonic-gate 	    warn("Warning: disabling multilink");
5177c478bd9Sstevel@tonic-gate 	    multilink = 0;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate     }
5207c478bd9Sstevel@tonic-gate #endif
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate     /*
5237c478bd9Sstevel@tonic-gate      * Detach ourselves from the terminal, if required, and identify
5247c478bd9Sstevel@tonic-gate      * who is running us.  Printing to stderr stops here unless
5257c478bd9Sstevel@tonic-gate      * nodetach or updetach is set.
5267c478bd9Sstevel@tonic-gate      */
5277c478bd9Sstevel@tonic-gate     if (!nodetach && !updetach)
5287c478bd9Sstevel@tonic-gate 	detach();
5297c478bd9Sstevel@tonic-gate     p = getlogin();
5307c478bd9Sstevel@tonic-gate     if (p == NULL) {
5317c478bd9Sstevel@tonic-gate 	pw = getpwuid(uid);
5327c478bd9Sstevel@tonic-gate 	if (pw != NULL && pw->pw_name != NULL)
5337c478bd9Sstevel@tonic-gate 	    p = pw->pw_name;
5347c478bd9Sstevel@tonic-gate 	else
5357c478bd9Sstevel@tonic-gate 	    p = "(unknown)";
5367c478bd9Sstevel@tonic-gate     }
5377c478bd9Sstevel@tonic-gate     syslog(LOG_NOTICE, "pppd %s.%d%s started by %s, uid %d",
5387c478bd9Sstevel@tonic-gate 	   VERSION, PATCHLEVEL, IMPLEMENTATION, p, uid);
5397c478bd9Sstevel@tonic-gate     script_setenv("PPPLOGNAME", p, 0);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate     if (devnam[0] != '\0')
5427c478bd9Sstevel@tonic-gate 	script_setenv("DEVICE", devnam, 1);
5437c478bd9Sstevel@tonic-gate     (void) slprintf(numbuf, sizeof(numbuf), "%d", getpid());
5447c478bd9Sstevel@tonic-gate     script_setenv("PPPD_PID", numbuf, 1);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate     setup_signals();
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate     waiting = 0;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate     create_linkpidfile();
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate     /*
5537c478bd9Sstevel@tonic-gate      * If we're doing dial-on-demand, set up the interface now.
5547c478bd9Sstevel@tonic-gate      */
5557c478bd9Sstevel@tonic-gate     if (demand) {
5567c478bd9Sstevel@tonic-gate 	/*
5577c478bd9Sstevel@tonic-gate 	 * Open the loopback channel and set it up to be the ppp interface.
5587c478bd9Sstevel@tonic-gate 	 */
5597c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
5607c478bd9Sstevel@tonic-gate 	(void) tdb_writelock(pppdb);
5617c478bd9Sstevel@tonic-gate #endif
5627c478bd9Sstevel@tonic-gate 	set_ifunit(1);
5637c478bd9Sstevel@tonic-gate 	fd_loop = open_ppp_loopback();
5647c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
5657c478bd9Sstevel@tonic-gate 	(void) tdb_writeunlock(pppdb);
5667c478bd9Sstevel@tonic-gate #endif
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/*
5697c478bd9Sstevel@tonic-gate 	 * Configure the interface and mark it up, etc.
5707c478bd9Sstevel@tonic-gate 	 */
5717c478bd9Sstevel@tonic-gate 	demand_conf();
5727c478bd9Sstevel@tonic-gate     }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate     new_phase(PHASE_INITIALIZED);
5757c478bd9Sstevel@tonic-gate     do_callback = 0;
5767c478bd9Sstevel@tonic-gate     for (;;) {
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	need_holdoff = 1;
5797c478bd9Sstevel@tonic-gate 	ttyfd = -1;
5807c478bd9Sstevel@tonic-gate 	real_ttyfd = -1;
5817c478bd9Sstevel@tonic-gate 	status = EXIT_OK;
5827c478bd9Sstevel@tonic-gate 	++unsuccess;
5837c478bd9Sstevel@tonic-gate 	doing_callback = do_callback;
5847c478bd9Sstevel@tonic-gate 	do_callback = 0;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (demand && !doing_callback) {
5877c478bd9Sstevel@tonic-gate 	    /*
5887c478bd9Sstevel@tonic-gate 	     * Don't do anything until we see some activity.
5897c478bd9Sstevel@tonic-gate 	     */
5907c478bd9Sstevel@tonic-gate 	    kill_link = 0;
5917c478bd9Sstevel@tonic-gate 	    new_phase(PHASE_DORMANT);
5927c478bd9Sstevel@tonic-gate 	    demand_unblock();
5937c478bd9Sstevel@tonic-gate 	    add_fd(fd_loop);
5947c478bd9Sstevel@tonic-gate 	    for (;;) {
5957c478bd9Sstevel@tonic-gate 		if (sigsetjmp(sigjmp, 1) == 0) {
5967c478bd9Sstevel@tonic-gate 		    (void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL);
5977c478bd9Sstevel@tonic-gate 		    if (kill_link || got_sigchld) {
5987c478bd9Sstevel@tonic-gate 			(void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL);
5997c478bd9Sstevel@tonic-gate 		    } else {
6007c478bd9Sstevel@tonic-gate 			waiting = 1;
6017c478bd9Sstevel@tonic-gate 			(void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL);
6027c478bd9Sstevel@tonic-gate 			wait_input(timeleft(&timo));
6037c478bd9Sstevel@tonic-gate 		    }
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		waiting = 0;
6067c478bd9Sstevel@tonic-gate 		calltimeout();
6077c478bd9Sstevel@tonic-gate 		if (kill_link) {
6087c478bd9Sstevel@tonic-gate 		    if (!persist)
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 		    kill_link = 0;
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		if (get_loop_output())
6137c478bd9Sstevel@tonic-gate 		    break;
6147c478bd9Sstevel@tonic-gate 		if (got_sigchld)
6157c478bd9Sstevel@tonic-gate 		    (void) reap_kids(0);
6167c478bd9Sstevel@tonic-gate 	    }
6177c478bd9Sstevel@tonic-gate 	    remove_fd(fd_loop);
6187c478bd9Sstevel@tonic-gate 	    if (kill_link && !persist)
6197c478bd9Sstevel@tonic-gate 		break;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	    /*
6227c478bd9Sstevel@tonic-gate 	     * Now we want to bring up the link.
6237c478bd9Sstevel@tonic-gate 	     */
6247c478bd9Sstevel@tonic-gate 	    demand_block();
6257c478bd9Sstevel@tonic-gate 	    info("Starting link");
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	new_phase(doing_callback ? PHASE_CALLINGBACK : PHASE_SERIALCONN);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/*
6317c478bd9Sstevel@tonic-gate 	 * Get a pty master/slave pair if the pty, notty, socket,
6327c478bd9Sstevel@tonic-gate 	 * or record options were specified.
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 	(void) strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam));
6357c478bd9Sstevel@tonic-gate 	pty_master = -1;
6367c478bd9Sstevel@tonic-gate 	pty_slave = -1;
6377c478bd9Sstevel@tonic-gate 	if (using_pty || record_file != NULL) {
6387c478bd9Sstevel@tonic-gate 	    if (!get_pty(&pty_master, &pty_slave, ppp_devnam, uid)) {
6397c478bd9Sstevel@tonic-gate 		error("Couldn't allocate pseudo-tty");
6407c478bd9Sstevel@tonic-gate 		status = EXIT_FATAL_ERROR;
6417c478bd9Sstevel@tonic-gate 		goto fail;
6427c478bd9Sstevel@tonic-gate 	    }
6437c478bd9Sstevel@tonic-gate 	    set_up_tty(pty_slave, 1);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	/*
6477c478bd9Sstevel@tonic-gate 	 * Lock the device if we've been asked to.
6487c478bd9Sstevel@tonic-gate 	 */
6497c478bd9Sstevel@tonic-gate 	status = EXIT_LOCK_FAILED;
6507c478bd9Sstevel@tonic-gate 	if (lockflag && !privopen && !direct_tty) {
6517c478bd9Sstevel@tonic-gate 	    if (lock(devnam) < 0)
6527c478bd9Sstevel@tonic-gate 		goto fail;
6537c478bd9Sstevel@tonic-gate 	    locked = 1;
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * Open the serial device and set it up to be the ppp interface.
6587c478bd9Sstevel@tonic-gate 	 * First we open it in non-blocking mode so we can set the
6597c478bd9Sstevel@tonic-gate 	 * various termios flags appropriately.  If we aren't dialling
6607c478bd9Sstevel@tonic-gate 	 * out and we want to use the modem lines, we reopen it later
6617c478bd9Sstevel@tonic-gate 	 * in order to wait for the carrier detect signal from the modem.
6627c478bd9Sstevel@tonic-gate 	 */
6637c478bd9Sstevel@tonic-gate 	hungup = 0;
6647c478bd9Sstevel@tonic-gate 	kill_link = 0;
6657c478bd9Sstevel@tonic-gate 	connector = doing_callback? callback_script: connect_script;
6667c478bd9Sstevel@tonic-gate 	if (direct_tty) {
6677c478bd9Sstevel@tonic-gate 	    ttyfd = 0;
6687c478bd9Sstevel@tonic-gate 	} else if (devnam[0] != '\0') {
6697c478bd9Sstevel@tonic-gate 	    for (;;) {
6707c478bd9Sstevel@tonic-gate 		/* If the user specified the device name, become the
6717c478bd9Sstevel@tonic-gate 		   user before opening it. */
6727c478bd9Sstevel@tonic-gate 		int err;
6737c478bd9Sstevel@tonic-gate 		if (!devnam_info.priv && !privopen)
6747c478bd9Sstevel@tonic-gate 		    (void) seteuid(uid);
6757c478bd9Sstevel@tonic-gate 		if ((ttyfd = sys_extra_fd()) < 0)
6767c478bd9Sstevel@tonic-gate 		    ttyfd = open(devnam, O_NONBLOCK | O_RDWR);
6777c478bd9Sstevel@tonic-gate 		err = errno;
6787c478bd9Sstevel@tonic-gate 		if (!devnam_info.priv && !privopen)
6797c478bd9Sstevel@tonic-gate 		    (void) seteuid(0);
6807c478bd9Sstevel@tonic-gate 		if (ttyfd >= 0)
6817c478bd9Sstevel@tonic-gate 		    break;
6827c478bd9Sstevel@tonic-gate 		errno = err;
6837c478bd9Sstevel@tonic-gate 		if (err != EINTR) {
6847c478bd9Sstevel@tonic-gate 		    error("Failed to open %s: %m", devnam);
6857c478bd9Sstevel@tonic-gate 		    status = EXIT_OPEN_FAILED;
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 		if (!persist || err != EINTR)
6887c478bd9Sstevel@tonic-gate 		    goto fail;
6897c478bd9Sstevel@tonic-gate 	    }
6907c478bd9Sstevel@tonic-gate 	    if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1
6917c478bd9Sstevel@tonic-gate 		|| fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
6927c478bd9Sstevel@tonic-gate 		warn("Couldn't reset non-blocking mode on device: %m");
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	    /*
6957c478bd9Sstevel@tonic-gate 	     * Do the equivalent of `mesg n' to stop broadcast messages.
6967c478bd9Sstevel@tonic-gate 	     */
6977c478bd9Sstevel@tonic-gate 	    if (fstat(ttyfd, &statbuf) < 0
6987c478bd9Sstevel@tonic-gate 		|| fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) {
6997c478bd9Sstevel@tonic-gate 		warn("Couldn't restrict write permissions to %s: %m", devnam);
7007c478bd9Sstevel@tonic-gate 	    } else
7017c478bd9Sstevel@tonic-gate 		tty_mode = statbuf.st_mode;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	    /*
7047c478bd9Sstevel@tonic-gate 	     * Set line speed, flow control, etc.
7057c478bd9Sstevel@tonic-gate 	     * If we have a non-null connection or initializer script,
7067c478bd9Sstevel@tonic-gate 	     * on most systems we set CLOCAL for now so that we can talk
7077c478bd9Sstevel@tonic-gate 	     * to the modem before carrier comes up.  But this has the
7087c478bd9Sstevel@tonic-gate 	     * side effect that we might miss it if CD drops before we
7097c478bd9Sstevel@tonic-gate 	     * get to clear CLOCAL below.  On systems where we can talk
7107c478bd9Sstevel@tonic-gate 	     * successfully to the modem with CLOCAL clear and CD down,
7117c478bd9Sstevel@tonic-gate 	     * we could clear CLOCAL at this point.
7127c478bd9Sstevel@tonic-gate 	     */
7137c478bd9Sstevel@tonic-gate 	    set_up_tty(ttyfd, ((connector != NULL && connector[0] != '\0')
7147c478bd9Sstevel@tonic-gate 			       || initializer != NULL));
7157c478bd9Sstevel@tonic-gate 	    real_ttyfd = ttyfd;
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * If the pty, socket, notty and/or record option was specified,
7207c478bd9Sstevel@tonic-gate 	 * start up the character shunt now.
7217c478bd9Sstevel@tonic-gate 	 */
7227c478bd9Sstevel@tonic-gate 	status = EXIT_PTYCMD_FAILED;
7237c478bd9Sstevel@tonic-gate 	if (ptycommand != NULL) {
7247c478bd9Sstevel@tonic-gate 	    if (record_file != NULL) {
7257c478bd9Sstevel@tonic-gate 		int ipipe[2], opipe[2], ok;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		if (pipe(ipipe) < 0 || pipe(opipe) < 0)
7287c478bd9Sstevel@tonic-gate 		    fatal("Couldn't create pipes for record option: %m");
7297c478bd9Sstevel@tonic-gate 		dbglog("starting charshunt for pty option");
7307c478bd9Sstevel@tonic-gate 		ok = device_script(ptycommand, opipe[0], ipipe[1], 1,
7317c478bd9Sstevel@tonic-gate 		    "record") == 0 && start_charshunt(ipipe[0], opipe[1]);
7327c478bd9Sstevel@tonic-gate 		(void) close(ipipe[0]);
7337c478bd9Sstevel@tonic-gate 		(void) close(ipipe[1]);
7347c478bd9Sstevel@tonic-gate 		(void) close(opipe[0]);
7357c478bd9Sstevel@tonic-gate 		(void) close(opipe[1]);
7367c478bd9Sstevel@tonic-gate 		if (!ok)
7377c478bd9Sstevel@tonic-gate 		    goto fail;
7387c478bd9Sstevel@tonic-gate 	    } else {
7397c478bd9Sstevel@tonic-gate 		if (device_script(ptycommand, pty_master, pty_master, 1,
7407c478bd9Sstevel@tonic-gate 		    "pty") < 0)
7417c478bd9Sstevel@tonic-gate 		    goto fail;
7427c478bd9Sstevel@tonic-gate 		ttyfd = pty_slave;
7437c478bd9Sstevel@tonic-gate 		(void) close(pty_master);
7447c478bd9Sstevel@tonic-gate 		pty_master = -1;
7457c478bd9Sstevel@tonic-gate 	    }
7467c478bd9Sstevel@tonic-gate 	} else if (pty_socket != NULL) {
7477c478bd9Sstevel@tonic-gate 	    int fd = open_socket(pty_socket);
7487c478bd9Sstevel@tonic-gate 	    if (fd < 0)
7497c478bd9Sstevel@tonic-gate 		goto fail;
7507c478bd9Sstevel@tonic-gate 	    dbglog("starting charshunt for socket option");
7517c478bd9Sstevel@tonic-gate 	    if (!start_charshunt(fd, fd))
7527c478bd9Sstevel@tonic-gate 		goto fail;
7537c478bd9Sstevel@tonic-gate 	} else if (notty) {
7547c478bd9Sstevel@tonic-gate 	    dbglog("starting charshunt for notty option");
7557c478bd9Sstevel@tonic-gate 	    if (!start_charshunt(0, 1))
7567c478bd9Sstevel@tonic-gate 		goto fail;
7577c478bd9Sstevel@tonic-gate 	} else if (record_file != NULL) {
7587c478bd9Sstevel@tonic-gate 	    dbglog("starting charshunt for record option");
7597c478bd9Sstevel@tonic-gate 	    if (!start_charshunt(ttyfd, ttyfd))
7607c478bd9Sstevel@tonic-gate 		goto fail;
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/* run connection script */
7647c478bd9Sstevel@tonic-gate 	if (((connector != NULL) && (connector[0] != '\0')) || initializer) {
7657c478bd9Sstevel@tonic-gate 	    if (real_ttyfd != -1) {
7667c478bd9Sstevel@tonic-gate 		/* XXX do this if doing_callback == CALLBACK_DIALIN? */
7677c478bd9Sstevel@tonic-gate 		if (!default_device && modem && !direct_tty) {
7687c478bd9Sstevel@tonic-gate 		    setdtr(real_ttyfd, 0);	/* in case modem is off hook */
7697c478bd9Sstevel@tonic-gate 		    (void) sleep(1);
7707c478bd9Sstevel@tonic-gate 		    setdtr(real_ttyfd, 1);
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 	    }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	    if ((initializer != NULL) && (initializer[0] != '\0')) {
7757c478bd9Sstevel@tonic-gate 		if (device_script(initializer, ttyfd, ttyfd, 0, "init") < 0) {
7767c478bd9Sstevel@tonic-gate 		    error("Initializer script failed");
7777c478bd9Sstevel@tonic-gate 		    status = EXIT_INIT_FAILED;
7787c478bd9Sstevel@tonic-gate 		    goto fail;
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 		if (kill_link)
7817c478bd9Sstevel@tonic-gate 		    goto disconnect;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		info("Serial port initialized.");
7847c478bd9Sstevel@tonic-gate 	    }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	    if ((connector != NULL) && (connector[0] != '\0')) {
7877c478bd9Sstevel@tonic-gate 		if (device_script(connector, ttyfd, ttyfd, 0, "connect") < 0) {
7887c478bd9Sstevel@tonic-gate 		    error("Connect script failed");
7897c478bd9Sstevel@tonic-gate 		    status = EXIT_CONNECT_FAILED;
7907c478bd9Sstevel@tonic-gate 		    goto fail;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 		if (kill_link)
7937c478bd9Sstevel@tonic-gate 		    goto disconnect;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		info("Serial connection established.");
7967c478bd9Sstevel@tonic-gate 	    }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	    /*
7997c478bd9Sstevel@tonic-gate 	     * Clear CLOCAL if modem option -- we now have carrier
8007c478bd9Sstevel@tonic-gate 	     * established, and we should respect loss of carrier.
8017c478bd9Sstevel@tonic-gate 	     */
8027c478bd9Sstevel@tonic-gate 	    if (real_ttyfd != -1)
8037c478bd9Sstevel@tonic-gate 		set_up_tty(real_ttyfd, 0);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	    if (doing_callback == CALLBACK_DIALIN)
8067c478bd9Sstevel@tonic-gate 		connector = NULL;
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	/* reopen tty if necessary to wait for carrier */
8107c478bd9Sstevel@tonic-gate 	if (connector == NULL && modem && devnam[0] != '\0' && !direct_tty) {
8117c478bd9Sstevel@tonic-gate 	    for (;;) {
8127c478bd9Sstevel@tonic-gate 		if ((i = open(devnam, O_RDWR)) >= 0)
8137c478bd9Sstevel@tonic-gate 		    break;
8147c478bd9Sstevel@tonic-gate 		if (errno != EINTR) {
8157c478bd9Sstevel@tonic-gate 		    error("Failed to reopen %s: %m", devnam);
8167c478bd9Sstevel@tonic-gate 		    status = EXIT_OPEN_FAILED;
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 		if (!persist || errno != EINTR || hungup || kill_link)
8197c478bd9Sstevel@tonic-gate 		    goto fail;
8207c478bd9Sstevel@tonic-gate 	    }
8217c478bd9Sstevel@tonic-gate 	    (void) close(i);
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	(void) slprintf(numbuf, sizeof(numbuf), "%d", baud_rate);
8257c478bd9Sstevel@tonic-gate 	script_setenv("SPEED", numbuf, 0);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	/* run welcome script, if any */
8287c478bd9Sstevel@tonic-gate 	if ((welcomer != NULL) && (welcomer[0] != '\0')) {
8297c478bd9Sstevel@tonic-gate 	    if (device_script(welcomer, ttyfd, ttyfd, 0, "welcome") < 0)
8307c478bd9Sstevel@tonic-gate 		warn("Welcome script failed");
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	/* set up the serial device as a ppp interface */
8347c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
8357c478bd9Sstevel@tonic-gate 	(void) tdb_writelock(pppdb);
8367c478bd9Sstevel@tonic-gate #endif
8377c478bd9Sstevel@tonic-gate 	fd_ppp = establish_ppp(ttyfd);
8387c478bd9Sstevel@tonic-gate 	if (fd_ppp < 0) {
8397c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
8407c478bd9Sstevel@tonic-gate 	    (void) tdb_writeunlock(pppdb);
8417c478bd9Sstevel@tonic-gate #endif
8427c478bd9Sstevel@tonic-gate 	    status = EXIT_FATAL_ERROR;
8437c478bd9Sstevel@tonic-gate 	    goto disconnect;
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if (!demand && ifunit >= 0)
8477c478bd9Sstevel@tonic-gate 	    set_ifunit(1);
8487c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
8497c478bd9Sstevel@tonic-gate 	(void) tdb_writeunlock(pppdb);
8507c478bd9Sstevel@tonic-gate #endif
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	/*
8537c478bd9Sstevel@tonic-gate 	 * Start opening the connection and wait for
8547c478bd9Sstevel@tonic-gate 	 * incoming events (reply, timeout, etc.).
8557c478bd9Sstevel@tonic-gate 	 */
8567c478bd9Sstevel@tonic-gate 	notice("Connect: %s <--> %s", ifname, ppp_devnam);
8577c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&start_time, NULL);
8587c478bd9Sstevel@tonic-gate 	link_stats_valid = 0;
8597c478bd9Sstevel@tonic-gate 	script_unsetenv("CONNECT_TIME");
8607c478bd9Sstevel@tonic-gate 	script_unsetenv("BYTES_SENT");
8617c478bd9Sstevel@tonic-gate 	script_unsetenv("BYTES_RCVD");
8627c478bd9Sstevel@tonic-gate 	lcp_lowerup(0);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/* Mostly for accounting purposes */
8657c478bd9Sstevel@tonic-gate 	new_phase(PHASE_CONNECTED);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/*
8687c478bd9Sstevel@tonic-gate 	 * If we are initiating this connection, wait for a short
8697c478bd9Sstevel@tonic-gate 	 * time for something from the peer.  This can avoid bouncing
870*48bbca81SDaniel Hoffman 	 * our packets off its tty before it has set up the tty.
8717c478bd9Sstevel@tonic-gate 	 */
8727c478bd9Sstevel@tonic-gate 	add_fd(fd_ppp);
8737c478bd9Sstevel@tonic-gate 	if (connect_delay != 0 && (connector != NULL || ptycommand != NULL)) {
8747c478bd9Sstevel@tonic-gate 	    struct timeval t;
8757c478bd9Sstevel@tonic-gate 	    t.tv_sec = connect_delay / 1000;
8767c478bd9Sstevel@tonic-gate 	    t.tv_usec = connect_delay % 1000;
8777c478bd9Sstevel@tonic-gate 	    wait_input(&t);
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	lcp_open(0);		/* Start protocol */
8817c478bd9Sstevel@tonic-gate 	open_ccp_flag = 0;
8827c478bd9Sstevel@tonic-gate 	status = EXIT_NEGOTIATION_FAILED;
8837c478bd9Sstevel@tonic-gate 	new_phase(PHASE_ESTABLISH);
8847c478bd9Sstevel@tonic-gate 	while (phase != PHASE_DEAD) {
8857c478bd9Sstevel@tonic-gate 	    if (sigsetjmp(sigjmp, 1) == 0) {
8867c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL);
8877c478bd9Sstevel@tonic-gate 		if (kill_link || open_ccp_flag || got_sigchld) {
8887c478bd9Sstevel@tonic-gate 		    (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL);
8897c478bd9Sstevel@tonic-gate 		} else {
8907c478bd9Sstevel@tonic-gate 		    waiting = 1;
8917c478bd9Sstevel@tonic-gate 		    (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL);
8927c478bd9Sstevel@tonic-gate 		    wait_input(timeleft(&timo));
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 	    }
8957c478bd9Sstevel@tonic-gate 	    waiting = 0;
8967c478bd9Sstevel@tonic-gate 	    calltimeout();
8977c478bd9Sstevel@tonic-gate 	    get_input();
8987c478bd9Sstevel@tonic-gate 	    if (kill_link) {
8997c478bd9Sstevel@tonic-gate 		lcp_close(0, "User request");
9007c478bd9Sstevel@tonic-gate 		kill_link = 0;
9017c478bd9Sstevel@tonic-gate 	    }
9027c478bd9Sstevel@tonic-gate 	    if (open_ccp_flag) {
9037c478bd9Sstevel@tonic-gate 		if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) {
9047c478bd9Sstevel@tonic-gate 		    /* Uncloak ourselves. */
9057c478bd9Sstevel@tonic-gate 		    ccp_fsm[0].flags &= ~OPT_SILENT;
9067c478bd9Sstevel@tonic-gate 		    (*ccp_protent.open)(0);
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 		open_ccp_flag = 0;
9097c478bd9Sstevel@tonic-gate 	    }
9107c478bd9Sstevel@tonic-gate 	    if (got_sigchld)
9117c478bd9Sstevel@tonic-gate 		(void) reap_kids(0);	/* Don't leave dead kids lying around */
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/*
9157c478bd9Sstevel@tonic-gate 	 * Print connect time and statistics.
9167c478bd9Sstevel@tonic-gate 	 */
9177c478bd9Sstevel@tonic-gate 	if (link_stats_valid) {
9187c478bd9Sstevel@tonic-gate 	    int t = (link_connect_time + 5) / 6;    /* 1/10ths of minutes */
9197c478bd9Sstevel@tonic-gate 	    info("Connect time %d.%d minutes.", t/10, t%10);
9207c478bd9Sstevel@tonic-gate 	    info("Sent %" PPP_COUNTER_F " bytes (%" PPP_COUNTER_F
9217c478bd9Sstevel@tonic-gate 		" packets), received %" PPP_COUNTER_F " bytes (%" PPP_COUNTER_F
9227c478bd9Sstevel@tonic-gate 		" packets).",
9237c478bd9Sstevel@tonic-gate 		 link_stats.bytes_out, link_stats.pkts_out,
9247c478bd9Sstevel@tonic-gate 		 link_stats.bytes_in, link_stats.pkts_in);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/*
9287c478bd9Sstevel@tonic-gate 	 * Delete pid file before disestablishing ppp.  Otherwise it
9297c478bd9Sstevel@tonic-gate 	 * can happen that another pppd gets the same unit and then
9307c478bd9Sstevel@tonic-gate 	 * we delete its pid file.
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	if (!demand) {
9337c478bd9Sstevel@tonic-gate 	    if (pidfilename[0] != '\0'
9347c478bd9Sstevel@tonic-gate 		&& unlink(pidfilename) < 0 && errno != ENOENT)
9357c478bd9Sstevel@tonic-gate 		warn("unable to delete pid file %s: %m", pidfilename);
9367c478bd9Sstevel@tonic-gate 	    pidfilename[0] = '\0';
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	/*
9407c478bd9Sstevel@tonic-gate 	 * If we may want to bring the link up again, transfer
9417c478bd9Sstevel@tonic-gate 	 * the ppp unit back to the loopback.  Set the
9427c478bd9Sstevel@tonic-gate 	 * real serial device back to its normal mode of operation.
9437c478bd9Sstevel@tonic-gate 	 */
9447c478bd9Sstevel@tonic-gate 	remove_fd(fd_ppp);
9457c478bd9Sstevel@tonic-gate 	clean_check();
9467c478bd9Sstevel@tonic-gate 	if (demand)
9477c478bd9Sstevel@tonic-gate 	    restore_loop();
9487c478bd9Sstevel@tonic-gate 	disestablish_ppp(ttyfd);
9497c478bd9Sstevel@tonic-gate 	fd_ppp = -1;
9507c478bd9Sstevel@tonic-gate 	if (!hungup)
9517c478bd9Sstevel@tonic-gate 	    lcp_lowerdown(0);
9527c478bd9Sstevel@tonic-gate 	if (!demand)
9537c478bd9Sstevel@tonic-gate 	    script_unsetenv("IFNAME");
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	/*
9567c478bd9Sstevel@tonic-gate 	 * Run disconnector script, if requested.
9577c478bd9Sstevel@tonic-gate 	 * XXX we may not be able to do this if the line has hung up!
9587c478bd9Sstevel@tonic-gate 	 */
9597c478bd9Sstevel@tonic-gate     disconnect:
9607c478bd9Sstevel@tonic-gate 	if ((disconnect_script != NULL) && (disconnect_script[0] != '\0') &&
9617c478bd9Sstevel@tonic-gate 	    !hungup) {
9627c478bd9Sstevel@tonic-gate 	    new_phase(PHASE_DISCONNECT);
9637c478bd9Sstevel@tonic-gate 	    if (real_ttyfd >= 0)
9647c478bd9Sstevel@tonic-gate 		set_up_tty(real_ttyfd, 1);
9657c478bd9Sstevel@tonic-gate 	    if (device_script(disconnect_script, ttyfd, ttyfd, 0,
9667c478bd9Sstevel@tonic-gate 		"disconnect") < 0) {
9677c478bd9Sstevel@tonic-gate 		warn("disconnect script failed");
9687c478bd9Sstevel@tonic-gate 	    } else {
9697c478bd9Sstevel@tonic-gate 		info("Serial link disconnected.");
9707c478bd9Sstevel@tonic-gate 	    }
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate     fail:
9747c478bd9Sstevel@tonic-gate 	if (pty_master >= 0)
9757c478bd9Sstevel@tonic-gate 	    (void) close(pty_master);
9767c478bd9Sstevel@tonic-gate 	if (pty_slave >= 0) {
9777c478bd9Sstevel@tonic-gate 	    (void) close(pty_slave);
9787c478bd9Sstevel@tonic-gate 	    pty_slave = -1;
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	if (real_ttyfd >= 0)
9817c478bd9Sstevel@tonic-gate 	    close_tty();
9827c478bd9Sstevel@tonic-gate 	if (locked) {
9837c478bd9Sstevel@tonic-gate 	    locked = 0;
9847c478bd9Sstevel@tonic-gate 	    unlock();
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if (!demand) {
9887c478bd9Sstevel@tonic-gate 	    if (pidfilename[0] != '\0'
9897c478bd9Sstevel@tonic-gate 		&& unlink(pidfilename) < 0 && errno != ENOENT)
9907c478bd9Sstevel@tonic-gate 		warn("unable to delete pid file %s: %m", pidfilename);
9917c478bd9Sstevel@tonic-gate 	    pidfilename[0] = '\0';
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	if (!persist || (maxfail > 0 && unsuccess >= maxfail))
9957c478bd9Sstevel@tonic-gate 	    break;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	kill_link = 0;
9987c478bd9Sstevel@tonic-gate 	if (demand)
9997c478bd9Sstevel@tonic-gate 	    demand_discard();
10007c478bd9Sstevel@tonic-gate 	t = need_holdoff? holdoff: 0;
10017c478bd9Sstevel@tonic-gate 	if (holdoff_hook != NULL)
10027c478bd9Sstevel@tonic-gate 	    t = (*holdoff_hook)();
10037c478bd9Sstevel@tonic-gate 	if (t > 0) {
10047c478bd9Sstevel@tonic-gate 	    new_phase(PHASE_HOLDOFF);
10057c478bd9Sstevel@tonic-gate 	    TIMEOUT(holdoff_end, NULL, t);
10067c478bd9Sstevel@tonic-gate 	    do {
10077c478bd9Sstevel@tonic-gate 		if (sigsetjmp(sigjmp, 1) == 0) {
10087c478bd9Sstevel@tonic-gate 		    (void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL);
10097c478bd9Sstevel@tonic-gate 		    if (kill_link || got_sigchld) {
10107c478bd9Sstevel@tonic-gate 			(void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL);
10117c478bd9Sstevel@tonic-gate 		    } else {
10127c478bd9Sstevel@tonic-gate 			waiting = 1;
10137c478bd9Sstevel@tonic-gate 			(void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL);
10147c478bd9Sstevel@tonic-gate 			wait_input(timeleft(&timo));
10157c478bd9Sstevel@tonic-gate 		    }
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 		waiting = 0;
10187c478bd9Sstevel@tonic-gate 		calltimeout();
10197c478bd9Sstevel@tonic-gate 		if (kill_link) {
10207c478bd9Sstevel@tonic-gate 		    kill_link = 0;
10217c478bd9Sstevel@tonic-gate 		    new_phase(PHASE_DORMANT); /* allow signal to end holdoff */
10227c478bd9Sstevel@tonic-gate 		}
10237c478bd9Sstevel@tonic-gate 		if (got_sigchld)
10247c478bd9Sstevel@tonic-gate 		    (void) reap_kids(0);
10257c478bd9Sstevel@tonic-gate 	    } while (phase == PHASE_HOLDOFF);
10267c478bd9Sstevel@tonic-gate 	    if (!persist)
10277c478bd9Sstevel@tonic-gate 		break;
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate     }
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate     /* Wait for scripts to finish */
10327c478bd9Sstevel@tonic-gate     final_reap();
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate     die(status);
10357c478bd9Sstevel@tonic-gate     return (0);
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate /*
10397c478bd9Sstevel@tonic-gate  * setup_signals - initialize signal handling.
10407c478bd9Sstevel@tonic-gate  */
10417c478bd9Sstevel@tonic-gate static void
setup_signals()10427c478bd9Sstevel@tonic-gate setup_signals()
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate     struct sigaction sa;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate     /*
10477c478bd9Sstevel@tonic-gate      * Compute mask of all interesting signals and install signal handlers
10487c478bd9Sstevel@tonic-gate      * for each.  Only one signal handler may be active at a time.  Therefore,
10497c478bd9Sstevel@tonic-gate      * all other signals should be masked when any handler is executing.
10507c478bd9Sstevel@tonic-gate      */
10517c478bd9Sstevel@tonic-gate     (void) sigemptyset(&main_sigmask);
10527c478bd9Sstevel@tonic-gate     (void) sigaddset(&main_sigmask, SIGHUP);
10537c478bd9Sstevel@tonic-gate     (void) sigaddset(&main_sigmask, SIGINT);
10547c478bd9Sstevel@tonic-gate     (void) sigaddset(&main_sigmask, SIGTERM);
10557c478bd9Sstevel@tonic-gate     (void) sigaddset(&main_sigmask, SIGCHLD);
10567c478bd9Sstevel@tonic-gate     (void) sigaddset(&main_sigmask, SIGUSR2);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate #define SIGNAL(s, handler)	if (1) { \
10597c478bd9Sstevel@tonic-gate 	sa.sa_handler = handler; \
10607c478bd9Sstevel@tonic-gate 	if (sigaction(s, &sa, NULL) < 0) \
10617c478bd9Sstevel@tonic-gate 	    fatal("Couldn't establish signal handler (%d): %m", s); \
10627c478bd9Sstevel@tonic-gate     } else ((void)0)
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate     sa.sa_mask = main_sigmask;
10657c478bd9Sstevel@tonic-gate     sa.sa_flags = 0;
10667c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGHUP, hup);		/* Hangup */
10677c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGINT, term);		/* Interrupt */
10687c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGTERM, term);		/* Terminate */
10697c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGCHLD, chld);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGUSR1, toggle_debug);	/* Toggle debug flag */
10727c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGUSR2, open_ccp);	/* Reopen CCP */
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate     /*
10757c478bd9Sstevel@tonic-gate      * Install a handler for other signals which would otherwise
10767c478bd9Sstevel@tonic-gate      * cause pppd to exit without cleaning up.
10777c478bd9Sstevel@tonic-gate      */
10787c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGALRM, bad_signal);
1079f53eecf5SJames Carlson /*CONSTANTCONDITION*/ SIGNAL(SIGQUIT, bad_signal);
1080f53eecf5SJames Carlson 
1081f53eecf5SJames Carlson /* Do not hook any of these signals on Solaris; allow core dump instead */
1082f53eecf5SJames Carlson #ifndef SOL2
1083f53eecf5SJames Carlson /*CONSTANTCONDITION*/ SIGNAL(SIGABRT, bad_signal);
10847c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGFPE, bad_signal);
10857c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGILL, bad_signal);
10867c478bd9Sstevel@tonic-gate #ifndef DEBUG
10877c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGSEGV, bad_signal);
10887c478bd9Sstevel@tonic-gate #endif
10897c478bd9Sstevel@tonic-gate #ifdef SIGBUS
10907c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGBUS, bad_signal);
10917c478bd9Sstevel@tonic-gate #endif
10927c478bd9Sstevel@tonic-gate #ifdef SIGEMT
10937c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGEMT, bad_signal);
10947c478bd9Sstevel@tonic-gate #endif
10957c478bd9Sstevel@tonic-gate #ifdef SIGPOLL
10967c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGPOLL, bad_signal);
10977c478bd9Sstevel@tonic-gate #endif
10987c478bd9Sstevel@tonic-gate #ifdef SIGPROF
10997c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGPROF, bad_signal);
11007c478bd9Sstevel@tonic-gate #endif
11017c478bd9Sstevel@tonic-gate #ifdef SIGSYS
11027c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGSYS, bad_signal);
11037c478bd9Sstevel@tonic-gate #endif
11047c478bd9Sstevel@tonic-gate #ifdef SIGTRAP
11057c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGTRAP, bad_signal);
11067c478bd9Sstevel@tonic-gate #endif
11077c478bd9Sstevel@tonic-gate #ifdef SIGVTALRM
11087c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGVTALRM, bad_signal);
11097c478bd9Sstevel@tonic-gate #endif
11107c478bd9Sstevel@tonic-gate #ifdef SIGXCPU
11117c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGXCPU, bad_signal);
11127c478bd9Sstevel@tonic-gate #endif
11137c478bd9Sstevel@tonic-gate #ifdef SIGXFSZ
11147c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGXFSZ, bad_signal);
1115f53eecf5SJames Carlson #endif
11167c478bd9Sstevel@tonic-gate #endif
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate     /*
11197c478bd9Sstevel@tonic-gate      * Apparently we can get a SIGPIPE when we call syslog, if
11207c478bd9Sstevel@tonic-gate      * syslogd has died and been restarted.  Ignoring it seems
11217c478bd9Sstevel@tonic-gate      * be sufficient.
11227c478bd9Sstevel@tonic-gate      */
11237c478bd9Sstevel@tonic-gate     (void) signal(SIGPIPE, SIG_IGN);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate /*
11277c478bd9Sstevel@tonic-gate  * set_ifunit - do things we need to do once we know which ppp
11287c478bd9Sstevel@tonic-gate  * unit we are using.
11297c478bd9Sstevel@tonic-gate  */
11307c478bd9Sstevel@tonic-gate void
set_ifunit(iskey)11317c478bd9Sstevel@tonic-gate set_ifunit(iskey)
11327c478bd9Sstevel@tonic-gate     int iskey;
11337c478bd9Sstevel@tonic-gate {
11347c478bd9Sstevel@tonic-gate     sys_ifname();
11357c478bd9Sstevel@tonic-gate     info("Using interface %s", ifname);
11367c478bd9Sstevel@tonic-gate     script_setenv("IFNAME", ifname, iskey);
11377c478bd9Sstevel@tonic-gate     if (iskey) {
11387c478bd9Sstevel@tonic-gate 	create_pidfile();	/* write pid to file */
11397c478bd9Sstevel@tonic-gate 	create_linkpidfile();
11407c478bd9Sstevel@tonic-gate     }
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate /*
11447c478bd9Sstevel@tonic-gate  * detach - detach us from the controlling terminal.
11457c478bd9Sstevel@tonic-gate  */
11467c478bd9Sstevel@tonic-gate void
detach()11477c478bd9Sstevel@tonic-gate detach()
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate     pid_t pid;
11507c478bd9Sstevel@tonic-gate     char numbuf[16];
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate     if (detached)
11537c478bd9Sstevel@tonic-gate 	return;
11547c478bd9Sstevel@tonic-gate     if ((pid = fork()) == (pid_t)-1) {
11557c478bd9Sstevel@tonic-gate 	error("Couldn't detach (fork failed: %m)");
11567c478bd9Sstevel@tonic-gate 	die(1);			/* or just return? */
11577c478bd9Sstevel@tonic-gate     }
11587c478bd9Sstevel@tonic-gate     if (pid != (pid_t)0) {
11597c478bd9Sstevel@tonic-gate 	/* parent */
11607c478bd9Sstevel@tonic-gate 	if (locked)
11617c478bd9Sstevel@tonic-gate 	    (void) relock(pid);
11627c478bd9Sstevel@tonic-gate 	exit(0);		/* parent dies */
11637c478bd9Sstevel@tonic-gate     }
11647c478bd9Sstevel@tonic-gate     (void) setsid();
11657c478bd9Sstevel@tonic-gate 	/*
11667c478bd9Sstevel@tonic-gate 	 * Fork again to relinquish session leadership. This is needed
11677c478bd9Sstevel@tonic-gate 	 * to prevent the daemon from acquiring controlling terminal.
11687c478bd9Sstevel@tonic-gate 	 */
11697c478bd9Sstevel@tonic-gate     if ((pid = fork()) == (pid_t)-1) {
11707c478bd9Sstevel@tonic-gate 	error("Couldn't detach (second fork failed: %m)");
11717c478bd9Sstevel@tonic-gate 	die(1);			/* or just return? */
11727c478bd9Sstevel@tonic-gate     }
11737c478bd9Sstevel@tonic-gate     if (pid != (pid_t)0) {
11747c478bd9Sstevel@tonic-gate 	/* parent */
11757c478bd9Sstevel@tonic-gate 	if (locked)
11767c478bd9Sstevel@tonic-gate 	    (void) relock(pid);
11777c478bd9Sstevel@tonic-gate 	exit(0);		/* parent dies */
11787c478bd9Sstevel@tonic-gate     }
11797c478bd9Sstevel@tonic-gate     (void) chdir("/");
11807c478bd9Sstevel@tonic-gate     (void) close(0);
11817c478bd9Sstevel@tonic-gate     (void) close(1);
11827c478bd9Sstevel@tonic-gate     (void) close(2);
11837c478bd9Sstevel@tonic-gate     detached = 1;
11847c478bd9Sstevel@tonic-gate     if (!log_to_file && !log_to_specific_fd)
11857c478bd9Sstevel@tonic-gate 	log_to_fd = -1;
11867c478bd9Sstevel@tonic-gate     /* update pid files if they have been written already */
11877c478bd9Sstevel@tonic-gate     if (pidfilename[0] != '\0')
11887c478bd9Sstevel@tonic-gate 	create_pidfile();
11897c478bd9Sstevel@tonic-gate     if (linkpidfile[0] != '\0')
11907c478bd9Sstevel@tonic-gate 	create_linkpidfile();
11917c478bd9Sstevel@tonic-gate     (void) slprintf(numbuf, sizeof(numbuf), "%d", getpid());
11927c478bd9Sstevel@tonic-gate     script_setenv("PPPD_PID", numbuf, 1);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate /*
11967c478bd9Sstevel@tonic-gate  * reopen_log - (re)open our connection to syslog.
11977c478bd9Sstevel@tonic-gate  */
11987c478bd9Sstevel@tonic-gate void
reopen_log()11997c478bd9Sstevel@tonic-gate reopen_log()
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate #ifdef ULTRIX
12027c478bd9Sstevel@tonic-gate     openlog("pppd", LOG_PID);
12037c478bd9Sstevel@tonic-gate #else
12047c478bd9Sstevel@tonic-gate     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
12057c478bd9Sstevel@tonic-gate     (void) setlogmask(LOG_UPTO(LOG_INFO));
12067c478bd9Sstevel@tonic-gate #endif
12077c478bd9Sstevel@tonic-gate }
1208