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 }
12087c478bd9Sstevel@tonic-gate
12097c478bd9Sstevel@tonic-gate /*
12107c478bd9Sstevel@tonic-gate * Create a file containing our process ID.
12117c478bd9Sstevel@tonic-gate */
12127c478bd9Sstevel@tonic-gate static void
create_pidfile()12137c478bd9Sstevel@tonic-gate create_pidfile()
12147c478bd9Sstevel@tonic-gate {
12157c478bd9Sstevel@tonic-gate FILE *pidfile;
12167c478bd9Sstevel@tonic-gate
12177c478bd9Sstevel@tonic-gate (void) slprintf(pidfilename, sizeof(pidfilename), "%s%s.pid",
12187c478bd9Sstevel@tonic-gate _PATH_VARRUN, ifname);
12197c478bd9Sstevel@tonic-gate if ((pidfile = fopen(pidfilename, "w")) != NULL) {
12207c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%u\n", (unsigned)getpid());
12217c478bd9Sstevel@tonic-gate (void) fclose(pidfile);
12227c478bd9Sstevel@tonic-gate } else {
12237c478bd9Sstevel@tonic-gate error("Failed to create pid file %s: %m", pidfilename);
12247c478bd9Sstevel@tonic-gate pidfilename[0] = '\0';
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate
12287c478bd9Sstevel@tonic-gate static void
create_linkpidfile()12297c478bd9Sstevel@tonic-gate create_linkpidfile()
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate FILE *pidfile;
12327c478bd9Sstevel@tonic-gate
12337c478bd9Sstevel@tonic-gate if (linkname[0] == '\0')
12347c478bd9Sstevel@tonic-gate return;
12357c478bd9Sstevel@tonic-gate script_setenv("LINKNAME", linkname, 1);
12367c478bd9Sstevel@tonic-gate (void) slprintf(linkpidfile, sizeof(linkpidfile), "%sppp-%s.pid",
12377c478bd9Sstevel@tonic-gate _PATH_VARRUN, linkname);
12387c478bd9Sstevel@tonic-gate if ((pidfile = fopen(linkpidfile, "w")) != NULL) {
12397c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%u\n", (unsigned)getpid());
12407c478bd9Sstevel@tonic-gate if (ifname[0] != '\0')
12417c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%s\n", ifname);
12427c478bd9Sstevel@tonic-gate (void) fclose(pidfile);
12437c478bd9Sstevel@tonic-gate } else {
12447c478bd9Sstevel@tonic-gate error("Failed to create pid file %s: %m", linkpidfile);
12457c478bd9Sstevel@tonic-gate linkpidfile[0] = '\0';
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate
12497c478bd9Sstevel@tonic-gate /*
12507c478bd9Sstevel@tonic-gate * holdoff_end - called via a timeout when the holdoff period ends.
12517c478bd9Sstevel@tonic-gate */
12527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12537c478bd9Sstevel@tonic-gate static void
holdoff_end(arg)12547c478bd9Sstevel@tonic-gate holdoff_end(arg)
12557c478bd9Sstevel@tonic-gate void *arg;
12567c478bd9Sstevel@tonic-gate {
12577c478bd9Sstevel@tonic-gate new_phase(PHASE_DORMANT);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate
12607c478bd9Sstevel@tonic-gate /* List of protocol names, to make our messages a little more informative. */
12617c478bd9Sstevel@tonic-gate struct protocol_list {
12627c478bd9Sstevel@tonic-gate u_short proto;
12637c478bd9Sstevel@tonic-gate const char *name;
12647c478bd9Sstevel@tonic-gate } protocol_list[] = {
12657c478bd9Sstevel@tonic-gate { 0x21, "IP" },
12667c478bd9Sstevel@tonic-gate { 0x23, "OSI Network Layer" },
12677c478bd9Sstevel@tonic-gate { 0x25, "Xerox NS IDP" },
12687c478bd9Sstevel@tonic-gate { 0x27, "DECnet Phase IV" },
12697c478bd9Sstevel@tonic-gate { 0x29, "Appletalk" },
12707c478bd9Sstevel@tonic-gate { 0x2b, "Novell IPX" },
12717c478bd9Sstevel@tonic-gate { 0x2d, "VJ compressed TCP/IP" },
12727c478bd9Sstevel@tonic-gate { 0x2f, "VJ uncompressed TCP/IP" },
12737c478bd9Sstevel@tonic-gate { 0x31, "Bridging PDU" },
12747c478bd9Sstevel@tonic-gate { 0x33, "Stream Protocol ST-II" },
12757c478bd9Sstevel@tonic-gate { 0x35, "Banyan Vines" },
12767c478bd9Sstevel@tonic-gate { 0x37, "Old VJ compressed TCP/IP" },
12777c478bd9Sstevel@tonic-gate { 0x39, "AppleTalk EDDP" },
12787c478bd9Sstevel@tonic-gate { 0x3b, "AppleTalk SmartBuffered" },
12797c478bd9Sstevel@tonic-gate { 0x3d, "Multilink" },
12807c478bd9Sstevel@tonic-gate { 0x3f, "NetBIOS Frame" },
12817c478bd9Sstevel@tonic-gate { 0x41, "Cisco LAN Extension" },
12827c478bd9Sstevel@tonic-gate { 0x43, "Ascom Timeplex" },
12837c478bd9Sstevel@tonic-gate { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" },
12847c478bd9Sstevel@tonic-gate { 0x47, "DCA Remote Lan" },
12857c478bd9Sstevel@tonic-gate { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" },
12867c478bd9Sstevel@tonic-gate { 0x4b, "SNA over 802.2" },
12877c478bd9Sstevel@tonic-gate { 0x4d, "SNA" },
12887c478bd9Sstevel@tonic-gate { 0x4f, "IP6 Header Compression" },
12897c478bd9Sstevel@tonic-gate { 0x51, "KNX Bridging" },
12907c478bd9Sstevel@tonic-gate { 0x53, "Encrypted" },
12917c478bd9Sstevel@tonic-gate { 0x55, "per-link encrypted" },
12927c478bd9Sstevel@tonic-gate { 0x57, "IPv6" },
12937c478bd9Sstevel@tonic-gate { 0x59, "PPP Muxing" },
12947c478bd9Sstevel@tonic-gate { 0x6f, "Stampede Bridging" },
12957c478bd9Sstevel@tonic-gate { 0x73, "MP+" },
12967c478bd9Sstevel@tonic-gate { 0xc1, "STMF" },
12977c478bd9Sstevel@tonic-gate { 0xfb, "per-link compressed" },
12987c478bd9Sstevel@tonic-gate { 0xfd, "compressed datagram" },
12997c478bd9Sstevel@tonic-gate { 0x0201, "802.1d Hello Packets" },
13007c478bd9Sstevel@tonic-gate { 0x0203, "IBM Source Routing BPDU" },
13017c478bd9Sstevel@tonic-gate { 0x0205, "DEC LANBridge100 Spanning Tree" },
13027c478bd9Sstevel@tonic-gate { 0x0207, "Cisco Discovery Protocol" },
13037c478bd9Sstevel@tonic-gate { 0x0231, "Luxcom" },
13047c478bd9Sstevel@tonic-gate { 0x0233, "Sigma Network Systems" },
13057c478bd9Sstevel@tonic-gate { 0x0235, "Apple Client Server Protocol" },
13067c478bd9Sstevel@tonic-gate { 0x0281, "MPLS Unicast" },
13077c478bd9Sstevel@tonic-gate { 0x0283, "MPLS Multicast" },
13087c478bd9Sstevel@tonic-gate { 0x0285, "IEEE p1284.4" },
13097c478bd9Sstevel@tonic-gate { 0x0287, "ETSI TETRA TNP1" },
13107c478bd9Sstevel@tonic-gate { 0x4021, "Stacker LZS" },
13117c478bd9Sstevel@tonic-gate { 0x8021, "Internet Protocol Control Protocol" },
13127c478bd9Sstevel@tonic-gate { 0x8023, "OSI Network Layer Control Protocol" },
13137c478bd9Sstevel@tonic-gate { 0x8025, "Xerox NS IDP Control Protocol" },
13147c478bd9Sstevel@tonic-gate { 0x8027, "DECnet Phase IV Control Protocol" },
13157c478bd9Sstevel@tonic-gate { 0x8029, "Appletalk Control Protocol" },
13167c478bd9Sstevel@tonic-gate { 0x802b, "Novell IPX Control Protocol" },
13177c478bd9Sstevel@tonic-gate { 0x8031, "Bridging Control Protocol" },
13187c478bd9Sstevel@tonic-gate { 0x8033, "Stream Protocol Control Protocol" },
13197c478bd9Sstevel@tonic-gate { 0x8035, "Banyan Vines Control Protocol" },
13207c478bd9Sstevel@tonic-gate { 0x803f, "NetBIOS Frames Control Protocol" },
13217c478bd9Sstevel@tonic-gate { 0x8041, "Cisco LAN Extension Control Protocol" },
13227c478bd9Sstevel@tonic-gate { 0x8043, "Ascom Timeplex Control Protocol" },
13237c478bd9Sstevel@tonic-gate { 0x8045, "Fujitsu LBLB Control Protocol" },
13247c478bd9Sstevel@tonic-gate { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" },
13257c478bd9Sstevel@tonic-gate { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" },
13267c478bd9Sstevel@tonic-gate { 0x804b, "SNA over 802.2 Control Protocol" },
13277c478bd9Sstevel@tonic-gate { 0x804d, "SNA Control Protocol" },
13287c478bd9Sstevel@tonic-gate { 0x8051, "KNX Bridging Control Protocol" },
13297c478bd9Sstevel@tonic-gate { 0x8053, "Encryption Control Protocol" },
13307c478bd9Sstevel@tonic-gate { 0x8055, "Per-link Encryption Control Protocol" },
13317c478bd9Sstevel@tonic-gate { 0x8057, "IPv6 Control Protocol" },
13327c478bd9Sstevel@tonic-gate { 0x806f, "Stampede Bridging Control Protocol" },
13337c478bd9Sstevel@tonic-gate { 0x80c1, "STMF Control Protocol" },
13347c478bd9Sstevel@tonic-gate { 0x80fb, "Per-link Compression Control Protocol" },
13357c478bd9Sstevel@tonic-gate { 0x80fd, "Compression Control Protocol" },
13367c478bd9Sstevel@tonic-gate { 0x8207, "Cisco Discovery Control Protocol" },
13377c478bd9Sstevel@tonic-gate { 0x8235, "Apple Client Server Control Protocol" },
13387c478bd9Sstevel@tonic-gate { 0x8281, "MPLS Control Protocol" },
13397c478bd9Sstevel@tonic-gate { 0x8287, "ETSI TETRA TNP1 Control Protocol" },
13407c478bd9Sstevel@tonic-gate { 0xc021, "Link Control Protocol" },
13417c478bd9Sstevel@tonic-gate { 0xc023, "Password Authentication Protocol" },
13427c478bd9Sstevel@tonic-gate { 0xc025, "Link Quality Report" },
13437c478bd9Sstevel@tonic-gate { 0xc027, "Shiva Password Authentication Protocol" },
13447c478bd9Sstevel@tonic-gate { 0xc029, "CallBack Control Protocol (CBCP)" },
13457c478bd9Sstevel@tonic-gate { 0xc02b, "Bandwidth Allocation Control Protocol" },
13467c478bd9Sstevel@tonic-gate { 0xc02d, "BAP" },
13477c478bd9Sstevel@tonic-gate { 0xc081, "Container Control Protocol" },
13487c478bd9Sstevel@tonic-gate { 0xc223, "Challenge Handshake Authentication Protocol" },
13497c478bd9Sstevel@tonic-gate { 0xc227, "Extensible Authentication Protocol" },
13507c478bd9Sstevel@tonic-gate { 0xc281, "Funk Proprietary Authentication Protocol" },
13517c478bd9Sstevel@tonic-gate { 0, NULL },
13527c478bd9Sstevel@tonic-gate };
13537c478bd9Sstevel@tonic-gate
13547c478bd9Sstevel@tonic-gate /*
13557c478bd9Sstevel@tonic-gate * protocol_name - find a name for a PPP protocol.
13567c478bd9Sstevel@tonic-gate */
13577c478bd9Sstevel@tonic-gate const char *
protocol_name(proto)13587c478bd9Sstevel@tonic-gate protocol_name(proto)
13597c478bd9Sstevel@tonic-gate int proto;
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate struct protocol_list *lp;
13627c478bd9Sstevel@tonic-gate
13637c478bd9Sstevel@tonic-gate for (lp = protocol_list; lp->proto != 0; ++lp)
13647c478bd9Sstevel@tonic-gate if (proto == lp->proto)
13657c478bd9Sstevel@tonic-gate return (lp->name);
13667c478bd9Sstevel@tonic-gate return (NULL);
13677c478bd9Sstevel@tonic-gate }
13687c478bd9Sstevel@tonic-gate
13697c478bd9Sstevel@tonic-gate static const char *phase_names[] = { PHASE__NAMES };
13707c478bd9Sstevel@tonic-gate
13717c478bd9Sstevel@tonic-gate const char *
phase_name(pval)13727c478bd9Sstevel@tonic-gate phase_name(pval)
13737c478bd9Sstevel@tonic-gate int pval;
13747c478bd9Sstevel@tonic-gate {
13757c478bd9Sstevel@tonic-gate static char buf[32];
13767c478bd9Sstevel@tonic-gate
13777c478bd9Sstevel@tonic-gate if (pval < 0 || pval >= Dim(phase_names)) {
13787c478bd9Sstevel@tonic-gate (void) slprintf(buf, sizeof (buf), "unknown %d", pval);
13797c478bd9Sstevel@tonic-gate return ((const char *)buf);
13807c478bd9Sstevel@tonic-gate }
13817c478bd9Sstevel@tonic-gate return (phase_names[pval]);
13827c478bd9Sstevel@tonic-gate }
13837c478bd9Sstevel@tonic-gate
13847c478bd9Sstevel@tonic-gate /*
13857c478bd9Sstevel@tonic-gate * get_input - called when incoming data is available.
13867c478bd9Sstevel@tonic-gate */
13877c478bd9Sstevel@tonic-gate static void
get_input()13887c478bd9Sstevel@tonic-gate get_input()
13897c478bd9Sstevel@tonic-gate {
13907c478bd9Sstevel@tonic-gate int len, i;
13917c478bd9Sstevel@tonic-gate u_char *p;
13927c478bd9Sstevel@tonic-gate u_short protocol;
13937c478bd9Sstevel@tonic-gate struct protent *protp;
13947c478bd9Sstevel@tonic-gate const char *pname;
13957c478bd9Sstevel@tonic-gate
13967c478bd9Sstevel@tonic-gate p = inpacket_buf; /* point to beginning of packet buffer */
13977c478bd9Sstevel@tonic-gate
13987c478bd9Sstevel@tonic-gate len = read_packet(inpacket_buf);
13997c478bd9Sstevel@tonic-gate if (len < 0)
14007c478bd9Sstevel@tonic-gate return;
14017c478bd9Sstevel@tonic-gate
14027c478bd9Sstevel@tonic-gate if (len == 0) {
14037c478bd9Sstevel@tonic-gate notice("Modem hangup");
14047c478bd9Sstevel@tonic-gate hungup = 1;
14057c478bd9Sstevel@tonic-gate status = EXIT_HANGUP;
14067c478bd9Sstevel@tonic-gate lcp_lowerdown(0); /* serial link is no longer available */
14077c478bd9Sstevel@tonic-gate link_terminated(0);
14087c478bd9Sstevel@tonic-gate return;
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate
14117c478bd9Sstevel@tonic-gate if (debug /*&& (debugflags & DBG_INPACKET)*/)
14127c478bd9Sstevel@tonic-gate dbglog("rcvd %P", p, len);
14137c478bd9Sstevel@tonic-gate
14147c478bd9Sstevel@tonic-gate if (len < PPP_HDRLEN) {
14157c478bd9Sstevel@tonic-gate dbglog("Discarded short packet (%d < %d)", len, PPP_HDRLEN);
14167c478bd9Sstevel@tonic-gate return;
14177c478bd9Sstevel@tonic-gate }
14187c478bd9Sstevel@tonic-gate
14197c478bd9Sstevel@tonic-gate p += 2; /* Skip address and control */
14207c478bd9Sstevel@tonic-gate GETSHORT(protocol, p);
14217c478bd9Sstevel@tonic-gate len -= PPP_HDRLEN;
14227c478bd9Sstevel@tonic-gate
14237c478bd9Sstevel@tonic-gate pname = debug ? NULL : protocol_name(protocol);
14247c478bd9Sstevel@tonic-gate
14257c478bd9Sstevel@tonic-gate /*
14267c478bd9Sstevel@tonic-gate * Toss all non-LCP packets unless LCP is in Opened state and
14277c478bd9Sstevel@tonic-gate * discard non-authentication protocols if we're not yet
14287c478bd9Sstevel@tonic-gate * authenticated.
14297c478bd9Sstevel@tonic-gate */
14307c478bd9Sstevel@tonic-gate if ((protocol != PPP_LCP &&
14317c478bd9Sstevel@tonic-gate (phase < PHASE_AUTHENTICATE || phase > PHASE_RUNNING)) ||
14327c478bd9Sstevel@tonic-gate (phase <= PHASE_AUTHENTICATE &&
14337c478bd9Sstevel@tonic-gate !(protocol == PPP_LCP || protocol == PPP_LQR ||
14347c478bd9Sstevel@tonic-gate protocol == PPP_PAP || protocol == PPP_CHAP))) {
14357c478bd9Sstevel@tonic-gate if (pname == NULL)
14367c478bd9Sstevel@tonic-gate dbglog("Discarded proto 0x%x in %s phase",
14377c478bd9Sstevel@tonic-gate protocol, phase_name(phase));
14387c478bd9Sstevel@tonic-gate else
14397c478bd9Sstevel@tonic-gate dbglog("Discarded %s (0x%x) in %s phase",
14407c478bd9Sstevel@tonic-gate pname, protocol, phase_name(phase));
14417c478bd9Sstevel@tonic-gate return;
14427c478bd9Sstevel@tonic-gate }
14437c478bd9Sstevel@tonic-gate
14447c478bd9Sstevel@tonic-gate /*
14457c478bd9Sstevel@tonic-gate * Upcall the proper protocol input routine.
14467c478bd9Sstevel@tonic-gate */
14477c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) {
14487c478bd9Sstevel@tonic-gate if (protp->protocol == protocol && protp->enabled_flag) {
14497c478bd9Sstevel@tonic-gate (*protp->input)(0, p, len);
14507c478bd9Sstevel@tonic-gate return;
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag
14537c478bd9Sstevel@tonic-gate && protp->datainput != NULL) {
14547c478bd9Sstevel@tonic-gate (*protp->datainput)(0, p, len);
14557c478bd9Sstevel@tonic-gate return;
14567c478bd9Sstevel@tonic-gate }
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate
14597c478bd9Sstevel@tonic-gate if (debug) {
14607c478bd9Sstevel@tonic-gate if (pname != NULL)
14617c478bd9Sstevel@tonic-gate warn("Unsupported protocol '%s' (0x%x) received", pname, protocol);
14627c478bd9Sstevel@tonic-gate else
14637c478bd9Sstevel@tonic-gate warn("Unsupported protocol 0x%x received", protocol);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate
14687c478bd9Sstevel@tonic-gate /*
14697c478bd9Sstevel@tonic-gate * new_phase - signal the start of a new phase of pppd's operation.
14707c478bd9Sstevel@tonic-gate */
14717c478bd9Sstevel@tonic-gate void
new_phase(p)14727c478bd9Sstevel@tonic-gate new_phase(p)
14737c478bd9Sstevel@tonic-gate int p;
14747c478bd9Sstevel@tonic-gate {
14757c478bd9Sstevel@tonic-gate if (new_phase_hook != NULL)
14767c478bd9Sstevel@tonic-gate (*new_phase_hook)(p, phase);
14777c478bd9Sstevel@tonic-gate phase = p;
14787c478bd9Sstevel@tonic-gate }
14797c478bd9Sstevel@tonic-gate
14807c478bd9Sstevel@tonic-gate /*
14817c478bd9Sstevel@tonic-gate * die - clean up state and exit with the specified status.
14827c478bd9Sstevel@tonic-gate */
14837c478bd9Sstevel@tonic-gate void
die(status)14847c478bd9Sstevel@tonic-gate die(status)
14857c478bd9Sstevel@tonic-gate int status;
14867c478bd9Sstevel@tonic-gate {
14877c478bd9Sstevel@tonic-gate cleanup();
14887c478bd9Sstevel@tonic-gate if (phase != PHASE_EXIT) {
14897c478bd9Sstevel@tonic-gate syslog(LOG_INFO, "Exit.");
14907c478bd9Sstevel@tonic-gate new_phase(PHASE_EXIT);
14917c478bd9Sstevel@tonic-gate }
14927c478bd9Sstevel@tonic-gate exit(status);
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate * cleanup - restore anything which needs to be restored before we exit
14977c478bd9Sstevel@tonic-gate */
14987c478bd9Sstevel@tonic-gate static void
cleanup()14997c478bd9Sstevel@tonic-gate cleanup()
15007c478bd9Sstevel@tonic-gate {
15017c478bd9Sstevel@tonic-gate sys_cleanup(); /* XXX: Need to check if this is okay after close_tty */
15027c478bd9Sstevel@tonic-gate
15037c478bd9Sstevel@tonic-gate if (fd_ppp >= 0) {
15047c478bd9Sstevel@tonic-gate fd_ppp = -1;
15057c478bd9Sstevel@tonic-gate disestablish_ppp(ttyfd);
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0)
15087c478bd9Sstevel@tonic-gate close_tty();
15097c478bd9Sstevel@tonic-gate
15107c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0' && unlink(pidfilename) < 0 && errno != ENOENT)
15117c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", pidfilename);
15127c478bd9Sstevel@tonic-gate pidfilename[0] = '\0';
15137c478bd9Sstevel@tonic-gate if (linkpidfile[0] != '\0' && unlink(linkpidfile) < 0 && errno != ENOENT)
15147c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", linkpidfile);
15157c478bd9Sstevel@tonic-gate linkpidfile[0] = '\0';
15167c478bd9Sstevel@tonic-gate
15177c478bd9Sstevel@tonic-gate if (locked) {
15187c478bd9Sstevel@tonic-gate locked = 0;
15197c478bd9Sstevel@tonic-gate unlock();
15207c478bd9Sstevel@tonic-gate }
15217c478bd9Sstevel@tonic-gate
15227c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
15237c478bd9Sstevel@tonic-gate if (pppdb != NULL) {
15247c478bd9Sstevel@tonic-gate cleanup_db();
15257c478bd9Sstevel@tonic-gate pppdb = NULL;
15267c478bd9Sstevel@tonic-gate }
15277c478bd9Sstevel@tonic-gate #endif
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate
15307c478bd9Sstevel@tonic-gate /*
15317c478bd9Sstevel@tonic-gate * close_tty - restore the terminal device and close it.
15327c478bd9Sstevel@tonic-gate */
15337c478bd9Sstevel@tonic-gate static void
close_tty()15347c478bd9Sstevel@tonic-gate close_tty()
15357c478bd9Sstevel@tonic-gate {
15367c478bd9Sstevel@tonic-gate int fd = real_ttyfd;
15377c478bd9Sstevel@tonic-gate
15387c478bd9Sstevel@tonic-gate real_ttyfd = -1;
15397c478bd9Sstevel@tonic-gate
15407c478bd9Sstevel@tonic-gate /* drop dtr to hang up */
15417c478bd9Sstevel@tonic-gate if (!default_device && modem) {
15427c478bd9Sstevel@tonic-gate setdtr(fd, 0);
15437c478bd9Sstevel@tonic-gate /*
15447c478bd9Sstevel@tonic-gate * This sleep is in case the serial port has CLOCAL set by default,
15457c478bd9Sstevel@tonic-gate * and consequently will reassert DTR when we close the device.
15467c478bd9Sstevel@tonic-gate */
15477c478bd9Sstevel@tonic-gate (void) sleep(1);
15487c478bd9Sstevel@tonic-gate }
15497c478bd9Sstevel@tonic-gate
15507c478bd9Sstevel@tonic-gate restore_tty(fd);
15517c478bd9Sstevel@tonic-gate
15527c478bd9Sstevel@tonic-gate if (tty_mode != (mode_t) -1) {
15537c478bd9Sstevel@tonic-gate if (fchmod(fd, tty_mode) != 0) {
15547c478bd9Sstevel@tonic-gate /* XXX if devnam is a symlink, this will change the link */
15557c478bd9Sstevel@tonic-gate if (chmod(devnam, tty_mode) != 0) {
15567c478bd9Sstevel@tonic-gate error("Unable to chmod file %s: %m", devnam);
15577c478bd9Sstevel@tonic-gate }
15587c478bd9Sstevel@tonic-gate }
15597c478bd9Sstevel@tonic-gate }
15607c478bd9Sstevel@tonic-gate
15617c478bd9Sstevel@tonic-gate (void) close(fd);
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate
15647c478bd9Sstevel@tonic-gate /*
15657c478bd9Sstevel@tonic-gate * update_link_stats - get stats at link termination.
15667c478bd9Sstevel@tonic-gate */
15677c478bd9Sstevel@tonic-gate void
update_link_stats(u)15687c478bd9Sstevel@tonic-gate update_link_stats(u)
15697c478bd9Sstevel@tonic-gate int u;
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate struct timeval now;
15727c478bd9Sstevel@tonic-gate char numbuf[32];
15737c478bd9Sstevel@tonic-gate
15747c478bd9Sstevel@tonic-gate if (gettimeofday(&now, NULL) >= 0) {
15757c478bd9Sstevel@tonic-gate link_connect_time = now.tv_sec - start_time.tv_sec;
15767c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time);
15777c478bd9Sstevel@tonic-gate script_setenv("CONNECT_TIME", numbuf, 0);
15787c478bd9Sstevel@tonic-gate } else {
15797c478bd9Sstevel@tonic-gate link_connect_time = 0;
15807c478bd9Sstevel@tonic-gate }
15817c478bd9Sstevel@tonic-gate
15827c478bd9Sstevel@tonic-gate if (get_ppp_stats(u, &link_stats)) {
15837c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F,
15847c478bd9Sstevel@tonic-gate link_stats.bytes_out);
15857c478bd9Sstevel@tonic-gate script_setenv("BYTES_SENT", numbuf, 0);
15867c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F,
15877c478bd9Sstevel@tonic-gate link_stats.bytes_in);
15887c478bd9Sstevel@tonic-gate script_setenv("BYTES_RCVD", numbuf, 0);
15897c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F,
15907c478bd9Sstevel@tonic-gate link_stats.pkts_in);
15917c478bd9Sstevel@tonic-gate script_setenv("PKTS_RCVD", numbuf, 0);
15927c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F,
15937c478bd9Sstevel@tonic-gate link_stats.pkts_out);
15947c478bd9Sstevel@tonic-gate script_setenv("PKTS_SENT", numbuf, 0);
15957c478bd9Sstevel@tonic-gate link_stats_valid = 1;
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate
15997c478bd9Sstevel@tonic-gate
16007c478bd9Sstevel@tonic-gate struct callout {
16017c478bd9Sstevel@tonic-gate struct timeval c_time; /* time at which to call routine */
16027c478bd9Sstevel@tonic-gate void *c_arg; /* argument to routine */
16037c478bd9Sstevel@tonic-gate void (*c_func) __P((void *)); /* routine */
16047c478bd9Sstevel@tonic-gate struct callout *c_next;
16057c478bd9Sstevel@tonic-gate };
16067c478bd9Sstevel@tonic-gate
16077c478bd9Sstevel@tonic-gate static struct callout *callout = NULL; /* Callout list */
16087c478bd9Sstevel@tonic-gate static struct timeval timenow; /* Current time */
16097c478bd9Sstevel@tonic-gate
16107c478bd9Sstevel@tonic-gate /*
16117c478bd9Sstevel@tonic-gate * timeout - Schedule a timeout.
16127c478bd9Sstevel@tonic-gate *
16137c478bd9Sstevel@tonic-gate * Note that this timeout takes the number of seconds, NOT hz (as in
16147c478bd9Sstevel@tonic-gate * the kernel).
16157c478bd9Sstevel@tonic-gate */
16167c478bd9Sstevel@tonic-gate void
16177c478bd9Sstevel@tonic-gate timeout(func, arg, time)
16187c478bd9Sstevel@tonic-gate void (*func) __P((void *));
16197c478bd9Sstevel@tonic-gate void *arg;
16207c478bd9Sstevel@tonic-gate int time;
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate struct callout *newp, *p, **pp;
16237c478bd9Sstevel@tonic-gate
16247c478bd9Sstevel@tonic-gate MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time));
16257c478bd9Sstevel@tonic-gate
16267c478bd9Sstevel@tonic-gate /*
16277c478bd9Sstevel@tonic-gate * Allocate timeout.
16287c478bd9Sstevel@tonic-gate */
16297c478bd9Sstevel@tonic-gate if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL)
16307c478bd9Sstevel@tonic-gate novm("callout structure for timeout.");
16317c478bd9Sstevel@tonic-gate newp->c_arg = arg;
16327c478bd9Sstevel@tonic-gate newp->c_func = func;
16337c478bd9Sstevel@tonic-gate (void) gettimeofday(&timenow, NULL);
16347c478bd9Sstevel@tonic-gate newp->c_time.tv_sec = timenow.tv_sec + time;
16357c478bd9Sstevel@tonic-gate newp->c_time.tv_usec = timenow.tv_usec;
16367c478bd9Sstevel@tonic-gate
16377c478bd9Sstevel@tonic-gate /*
16387c478bd9Sstevel@tonic-gate * Find correct place and link it in.
16397c478bd9Sstevel@tonic-gate */
16407c478bd9Sstevel@tonic-gate for (pp = &callout; (p = *pp) != NULL; pp = &p->c_next)
16417c478bd9Sstevel@tonic-gate if (newp->c_time.tv_sec < p->c_time.tv_sec
16427c478bd9Sstevel@tonic-gate || (newp->c_time.tv_sec == p->c_time.tv_sec
16437c478bd9Sstevel@tonic-gate && newp->c_time.tv_usec < p->c_time.tv_usec))
16447c478bd9Sstevel@tonic-gate break;
16457c478bd9Sstevel@tonic-gate newp->c_next = p;
16467c478bd9Sstevel@tonic-gate *pp = newp;
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate
16497c478bd9Sstevel@tonic-gate
16507c478bd9Sstevel@tonic-gate /*
16517c478bd9Sstevel@tonic-gate * untimeout - Unschedule a timeout.
16527c478bd9Sstevel@tonic-gate */
16537c478bd9Sstevel@tonic-gate void
16547c478bd9Sstevel@tonic-gate untimeout(func, arg)
16557c478bd9Sstevel@tonic-gate void (*func) __P((void *));
16567c478bd9Sstevel@tonic-gate void *arg;
16577c478bd9Sstevel@tonic-gate {
16587c478bd9Sstevel@tonic-gate struct callout **copp, *freep;
16597c478bd9Sstevel@tonic-gate
16607c478bd9Sstevel@tonic-gate MAINDEBUG(("Untimeout %p:%p.", func, arg));
16617c478bd9Sstevel@tonic-gate
16627c478bd9Sstevel@tonic-gate /*
16637c478bd9Sstevel@tonic-gate * Find first matching timeout and remove it from the list.
16647c478bd9Sstevel@tonic-gate */
16657c478bd9Sstevel@tonic-gate for (copp = &callout; (freep = *copp) != NULL; copp = &freep->c_next)
16667c478bd9Sstevel@tonic-gate if (freep->c_func == func && freep->c_arg == arg) {
16677c478bd9Sstevel@tonic-gate *copp = freep->c_next;
16687c478bd9Sstevel@tonic-gate free((char *) freep);
16697c478bd9Sstevel@tonic-gate break;
16707c478bd9Sstevel@tonic-gate }
16717c478bd9Sstevel@tonic-gate }
16727c478bd9Sstevel@tonic-gate
16737c478bd9Sstevel@tonic-gate
16747c478bd9Sstevel@tonic-gate /*
16757c478bd9Sstevel@tonic-gate * calltimeout - Call any timeout routines which are now due.
16767c478bd9Sstevel@tonic-gate */
16777c478bd9Sstevel@tonic-gate static void
calltimeout()16787c478bd9Sstevel@tonic-gate calltimeout()
16797c478bd9Sstevel@tonic-gate {
16807c478bd9Sstevel@tonic-gate struct callout *p;
16817c478bd9Sstevel@tonic-gate
16827c478bd9Sstevel@tonic-gate while (callout != NULL) {
16837c478bd9Sstevel@tonic-gate p = callout;
16847c478bd9Sstevel@tonic-gate
16857c478bd9Sstevel@tonic-gate if (gettimeofday(&timenow, NULL) < 0)
16867c478bd9Sstevel@tonic-gate fatal("Failed to get time of day: %m");
16877c478bd9Sstevel@tonic-gate if (!(p->c_time.tv_sec < timenow.tv_sec
16887c478bd9Sstevel@tonic-gate || (p->c_time.tv_sec == timenow.tv_sec
16897c478bd9Sstevel@tonic-gate && p->c_time.tv_usec <= timenow.tv_usec)))
16907c478bd9Sstevel@tonic-gate break; /* no, it's not time yet */
16917c478bd9Sstevel@tonic-gate
16927c478bd9Sstevel@tonic-gate callout = p->c_next;
16937c478bd9Sstevel@tonic-gate (*p->c_func)(p->c_arg);
16947c478bd9Sstevel@tonic-gate
16957c478bd9Sstevel@tonic-gate free((char *) p);
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate
16997c478bd9Sstevel@tonic-gate
17007c478bd9Sstevel@tonic-gate /*
17017c478bd9Sstevel@tonic-gate * timeleft - return the length of time until the next timeout is due.
17027c478bd9Sstevel@tonic-gate */
17037c478bd9Sstevel@tonic-gate static struct timeval *
timeleft(tvp)17047c478bd9Sstevel@tonic-gate timeleft(tvp)
17057c478bd9Sstevel@tonic-gate struct timeval *tvp;
17067c478bd9Sstevel@tonic-gate {
17077c478bd9Sstevel@tonic-gate if (callout == NULL)
17087c478bd9Sstevel@tonic-gate return (NULL);
17097c478bd9Sstevel@tonic-gate
17107c478bd9Sstevel@tonic-gate (void) gettimeofday(&timenow, NULL);
17117c478bd9Sstevel@tonic-gate tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
17127c478bd9Sstevel@tonic-gate tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec;
17137c478bd9Sstevel@tonic-gate if (tvp->tv_usec < 0) {
17147c478bd9Sstevel@tonic-gate tvp->tv_usec += 1000000;
17157c478bd9Sstevel@tonic-gate tvp->tv_sec -= 1;
17167c478bd9Sstevel@tonic-gate }
17177c478bd9Sstevel@tonic-gate if (tvp->tv_sec < 0)
17187c478bd9Sstevel@tonic-gate tvp->tv_sec = tvp->tv_usec = 0;
17197c478bd9Sstevel@tonic-gate
17207c478bd9Sstevel@tonic-gate return (tvp);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate
17237c478bd9Sstevel@tonic-gate
17247c478bd9Sstevel@tonic-gate /*
17257c478bd9Sstevel@tonic-gate * kill_my_pg - send a signal to our process group, and ignore it ourselves.
17267c478bd9Sstevel@tonic-gate */
17277c478bd9Sstevel@tonic-gate static void
kill_my_pg(sig)17287c478bd9Sstevel@tonic-gate kill_my_pg(sig)
17297c478bd9Sstevel@tonic-gate int sig;
17307c478bd9Sstevel@tonic-gate {
17317c478bd9Sstevel@tonic-gate struct sigaction act, oldact;
17327c478bd9Sstevel@tonic-gate sigset_t mask;
17337c478bd9Sstevel@tonic-gate
17347c478bd9Sstevel@tonic-gate BZERO(&act, sizeof (act));
17357c478bd9Sstevel@tonic-gate act.sa_handler = SIG_IGN;
17367c478bd9Sstevel@tonic-gate (void) sigemptyset(&mask);
17377c478bd9Sstevel@tonic-gate (void) sigaddset(&mask, sig);
17387c478bd9Sstevel@tonic-gate /*
17397c478bd9Sstevel@tonic-gate * Ignore signal 'sig' temporarily, before finally re-activating the
17407c478bd9Sstevel@tonic-gate * original handler. We need to do it in the following sequence, since
17417c478bd9Sstevel@tonic-gate * otherwise the signal handler for 'sig' will be called forever.
17427c478bd9Sstevel@tonic-gate */
17437c478bd9Sstevel@tonic-gate if (sigaction(sig, &act, &oldact) < 0) {
17447c478bd9Sstevel@tonic-gate fatal("kill_my_pg: couldn't establish signal handler (%d): %m", sig);
17457c478bd9Sstevel@tonic-gate }
17467c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
17477c478bd9Sstevel@tonic-gate /*
17487c478bd9Sstevel@tonic-gate * Send signal 'sig' to all processes whose process group ID is equal
17497c478bd9Sstevel@tonic-gate * to the process group ID of the sender.
17507c478bd9Sstevel@tonic-gate */
17517c478bd9Sstevel@tonic-gate (void) kill(0, sig);
17527c478bd9Sstevel@tonic-gate if (sigaction(sig, &oldact, NULL) < 0) {
17537c478bd9Sstevel@tonic-gate fatal("kill_my_pg: couldn't establish signal handler (%d): %m", sig);
17547c478bd9Sstevel@tonic-gate }
17557c478bd9Sstevel@tonic-gate }
17567c478bd9Sstevel@tonic-gate
17577c478bd9Sstevel@tonic-gate
17587c478bd9Sstevel@tonic-gate /*
17597c478bd9Sstevel@tonic-gate * hup - Catch SIGHUP signal.
17607c478bd9Sstevel@tonic-gate *
17617c478bd9Sstevel@tonic-gate * Indicates that the physical layer has been disconnected.
17627c478bd9Sstevel@tonic-gate * We don't rely on this indication; if the user has sent this
17637c478bd9Sstevel@tonic-gate * signal, we just take the link down.
17647c478bd9Sstevel@tonic-gate */
17657c478bd9Sstevel@tonic-gate static void
hup(sig)17667c478bd9Sstevel@tonic-gate hup(sig)
17677c478bd9Sstevel@tonic-gate int sig;
17687c478bd9Sstevel@tonic-gate {
17697c478bd9Sstevel@tonic-gate info("Hangup (SIGHUP)");
17707c478bd9Sstevel@tonic-gate kill_link = 1;
17717c478bd9Sstevel@tonic-gate if (status != EXIT_HANGUP)
17727c478bd9Sstevel@tonic-gate status = EXIT_USER_REQUEST;
17737c478bd9Sstevel@tonic-gate if (conn_running > 0)
17747c478bd9Sstevel@tonic-gate /* Send the signal to the [dis]connector process(es) also */
17757c478bd9Sstevel@tonic-gate kill_my_pg(sig);
17767c478bd9Sstevel@tonic-gate if (charshunt_pid)
17777c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, sig);
17787c478bd9Sstevel@tonic-gate if (waiting)
17797c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1);
17807c478bd9Sstevel@tonic-gate }
17817c478bd9Sstevel@tonic-gate
17827c478bd9Sstevel@tonic-gate
17837c478bd9Sstevel@tonic-gate /*
17847c478bd9Sstevel@tonic-gate * term - Catch SIGTERM signal and SIGINT signal (^C/del).
17857c478bd9Sstevel@tonic-gate *
17867c478bd9Sstevel@tonic-gate * Indicates that we should initiate a graceful disconnect and exit.
17877c478bd9Sstevel@tonic-gate */
17887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17897c478bd9Sstevel@tonic-gate static void
term(sig)17907c478bd9Sstevel@tonic-gate term(sig)
17917c478bd9Sstevel@tonic-gate int sig;
17927c478bd9Sstevel@tonic-gate {
17937c478bd9Sstevel@tonic-gate info("Terminating on signal %d.", sig);
17947c478bd9Sstevel@tonic-gate persist = 0; /* don't try to restart */
17957c478bd9Sstevel@tonic-gate kill_link = 1;
17967c478bd9Sstevel@tonic-gate status = EXIT_USER_REQUEST;
17977c478bd9Sstevel@tonic-gate if (conn_running > 0)
17987c478bd9Sstevel@tonic-gate /* Send the signal to the [dis]connector process(es) also */
17997c478bd9Sstevel@tonic-gate kill_my_pg(sig);
18007c478bd9Sstevel@tonic-gate if (charshunt_pid)
18017c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, sig);
18027c478bd9Sstevel@tonic-gate if (waiting)
18037c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1);
18047c478bd9Sstevel@tonic-gate }
18057c478bd9Sstevel@tonic-gate
18067c478bd9Sstevel@tonic-gate
18077c478bd9Sstevel@tonic-gate /*
18087c478bd9Sstevel@tonic-gate * chld - Catch SIGCHLD signal.
18097c478bd9Sstevel@tonic-gate * Sets a flag so we will call reap_kids in the mainline.
18107c478bd9Sstevel@tonic-gate */
18117c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18127c478bd9Sstevel@tonic-gate static void
chld(sig)18137c478bd9Sstevel@tonic-gate chld(sig)
18147c478bd9Sstevel@tonic-gate int sig;
18157c478bd9Sstevel@tonic-gate {
18167c478bd9Sstevel@tonic-gate got_sigchld = 1;
18177c478bd9Sstevel@tonic-gate if (waiting)
18187c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1);
18197c478bd9Sstevel@tonic-gate }
18207c478bd9Sstevel@tonic-gate
18217c478bd9Sstevel@tonic-gate /*
18227c478bd9Sstevel@tonic-gate * toggle_debug - Catch SIGUSR1 signal.
18237c478bd9Sstevel@tonic-gate *
18247c478bd9Sstevel@tonic-gate * Toggle debug flag.
18257c478bd9Sstevel@tonic-gate */
18267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18277c478bd9Sstevel@tonic-gate static void
toggle_debug(sig)18287c478bd9Sstevel@tonic-gate toggle_debug(sig)
18297c478bd9Sstevel@tonic-gate int sig;
18307c478bd9Sstevel@tonic-gate {
18317c478bd9Sstevel@tonic-gate if (debug) {
18327c478bd9Sstevel@tonic-gate print_ncpstate(0, NULL);
18337c478bd9Sstevel@tonic-gate dbglog("debug logging disabled");
18347c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_WARNING));
18357c478bd9Sstevel@tonic-gate debug = 0;
18367c478bd9Sstevel@tonic-gate } else {
18377c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_DEBUG));
18387c478bd9Sstevel@tonic-gate dbglog("debug logging enabled");
18397c478bd9Sstevel@tonic-gate print_ncpstate(0, NULL);
18407c478bd9Sstevel@tonic-gate debug = 1;
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate }
18437c478bd9Sstevel@tonic-gate
18447c478bd9Sstevel@tonic-gate
18457c478bd9Sstevel@tonic-gate /*
18467c478bd9Sstevel@tonic-gate * open_ccp - Catch SIGUSR2 signal.
18477c478bd9Sstevel@tonic-gate *
18487c478bd9Sstevel@tonic-gate * Try to (re)negotiate compression.
18497c478bd9Sstevel@tonic-gate */
18507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18517c478bd9Sstevel@tonic-gate static void
open_ccp(sig)18527c478bd9Sstevel@tonic-gate open_ccp(sig)
18537c478bd9Sstevel@tonic-gate int sig;
18547c478bd9Sstevel@tonic-gate {
18557c478bd9Sstevel@tonic-gate open_ccp_flag = 1;
18567c478bd9Sstevel@tonic-gate if (waiting)
18577c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1);
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate
18607c478bd9Sstevel@tonic-gate
18617c478bd9Sstevel@tonic-gate /*
18627c478bd9Sstevel@tonic-gate * bad_signal - We've caught a fatal signal. Clean up state and exit.
18637c478bd9Sstevel@tonic-gate */
18647c478bd9Sstevel@tonic-gate static void
bad_signal(sig)18657c478bd9Sstevel@tonic-gate bad_signal(sig)
18667c478bd9Sstevel@tonic-gate int sig;
18677c478bd9Sstevel@tonic-gate {
18687c478bd9Sstevel@tonic-gate static int crashed = 0;
18697c478bd9Sstevel@tonic-gate
18707c478bd9Sstevel@tonic-gate if (crashed)
18717c478bd9Sstevel@tonic-gate _exit(127);
18727c478bd9Sstevel@tonic-gate crashed = 1;
18737c478bd9Sstevel@tonic-gate error("Fatal signal %d", sig);
18747c478bd9Sstevel@tonic-gate if (conn_running > 0)
18757c478bd9Sstevel@tonic-gate kill_my_pg(SIGTERM);
18767c478bd9Sstevel@tonic-gate if (charshunt_pid)
18777c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, SIGTERM);
18787c478bd9Sstevel@tonic-gate die(127);
18797c478bd9Sstevel@tonic-gate }
18807c478bd9Sstevel@tonic-gate
18817c478bd9Sstevel@tonic-gate
18827c478bd9Sstevel@tonic-gate /*
18837c478bd9Sstevel@tonic-gate * device_script - run a program to talk to the serial device
18847c478bd9Sstevel@tonic-gate * (e.g. to run the connector or disconnector script).
18857c478bd9Sstevel@tonic-gate */
18867c478bd9Sstevel@tonic-gate static int
device_script(program,in,out,dont_wait,optname)18877c478bd9Sstevel@tonic-gate device_script(program, in, out, dont_wait, optname)
18887c478bd9Sstevel@tonic-gate char *program;
18897c478bd9Sstevel@tonic-gate int in, out;
18907c478bd9Sstevel@tonic-gate int dont_wait;
18917c478bd9Sstevel@tonic-gate char *optname;
18927c478bd9Sstevel@tonic-gate {
18937c478bd9Sstevel@tonic-gate pid_t pid;
18947c478bd9Sstevel@tonic-gate int status = -1;
18957c478bd9Sstevel@tonic-gate int errfd;
18967c478bd9Sstevel@tonic-gate int envpipe[2];
18977c478bd9Sstevel@tonic-gate
18987c478bd9Sstevel@tonic-gate envpipe[0] = envpipe[1] = -1;
18997c478bd9Sstevel@tonic-gate if (!dont_wait && device_pipe_hook != NULL && pipe(envpipe) == -1) {
19007c478bd9Sstevel@tonic-gate error("Cannot create pipe for child: %m");
19017c478bd9Sstevel@tonic-gate return (-1);
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate
19047c478bd9Sstevel@tonic-gate ++conn_running;
19057c478bd9Sstevel@tonic-gate pid = fork();
19067c478bd9Sstevel@tonic-gate
19077c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) {
19087c478bd9Sstevel@tonic-gate --conn_running;
19097c478bd9Sstevel@tonic-gate error("Failed to create child process: %m");
19107c478bd9Sstevel@tonic-gate return (-1);
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate
19137c478bd9Sstevel@tonic-gate if (pid == (pid_t)0) {
19147c478bd9Sstevel@tonic-gate sys_close();
19157c478bd9Sstevel@tonic-gate closelog();
19167c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0) {
19177c478bd9Sstevel@tonic-gate if (envpipe[1] <= 2)
19187c478bd9Sstevel@tonic-gate envpipe[1] = dup(envpipe[1]);
19197c478bd9Sstevel@tonic-gate (void) close(envpipe[0]);
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate if (in == 2) {
19227c478bd9Sstevel@tonic-gate /* aargh!!! */
19237c478bd9Sstevel@tonic-gate int newin = dup(in);
19247c478bd9Sstevel@tonic-gate if (in == out)
19257c478bd9Sstevel@tonic-gate out = newin;
19267c478bd9Sstevel@tonic-gate in = newin;
19277c478bd9Sstevel@tonic-gate } else if (out == 2) {
19287c478bd9Sstevel@tonic-gate out = dup(out);
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate if (log_to_fd >= 0) {
19317c478bd9Sstevel@tonic-gate if (log_to_fd != 2) {
19327c478bd9Sstevel@tonic-gate if (dup2(log_to_fd, 2) < 0)
19337c478bd9Sstevel@tonic-gate error("dup2(log_to_fd, STDERR) failed: %m");
19347c478bd9Sstevel@tonic-gate }
19357c478bd9Sstevel@tonic-gate } else {
19367c478bd9Sstevel@tonic-gate (void) close(2);
19377c478bd9Sstevel@tonic-gate errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600);
19387c478bd9Sstevel@tonic-gate if (errfd >= 0 && errfd != 2) {
19397c478bd9Sstevel@tonic-gate if (dup2(errfd, 2) < 0)
19407c478bd9Sstevel@tonic-gate error("dup2(errfd, STDERR) failed: %m");
19417c478bd9Sstevel@tonic-gate (void) close(errfd);
19427c478bd9Sstevel@tonic-gate }
19437c478bd9Sstevel@tonic-gate }
19447c478bd9Sstevel@tonic-gate if (in != 0) {
19457c478bd9Sstevel@tonic-gate if (out == 0)
19467c478bd9Sstevel@tonic-gate out = dup(out);
19477c478bd9Sstevel@tonic-gate if (dup2(in, 0) < 0)
19487c478bd9Sstevel@tonic-gate error("dup2(in, STDIN) failed: %m");
19497c478bd9Sstevel@tonic-gate }
19507c478bd9Sstevel@tonic-gate if (out != 1) {
19517c478bd9Sstevel@tonic-gate if (dup2(out, 1) < 0)
19527c478bd9Sstevel@tonic-gate error("dup2(out, STDOUT) failed: %m");
19537c478bd9Sstevel@tonic-gate }
19547c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0 && dup2(envpipe[1], 3) < 0)
19557c478bd9Sstevel@tonic-gate error("dup2(pipe, pipeout) failed: %m");
19567c478bd9Sstevel@tonic-gate if (real_ttyfd > 2)
19577c478bd9Sstevel@tonic-gate (void) close(real_ttyfd);
19587c478bd9Sstevel@tonic-gate if (pty_master > 2)
19597c478bd9Sstevel@tonic-gate (void) close(pty_master);
19607c478bd9Sstevel@tonic-gate if (pty_slave > 2) {
19617c478bd9Sstevel@tonic-gate (void) close(pty_slave);
19627c478bd9Sstevel@tonic-gate pty_slave = -1;
19637c478bd9Sstevel@tonic-gate }
19647c478bd9Sstevel@tonic-gate (void) setuid(uid);
19657c478bd9Sstevel@tonic-gate if (getuid() != uid) {
19667c478bd9Sstevel@tonic-gate error("setuid failed");
19677c478bd9Sstevel@tonic-gate exit(1);
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate (void) setgid(getgid());
19707c478bd9Sstevel@tonic-gate if (script_env != NULL) {
19717c478bd9Sstevel@tonic-gate while (*script_env != NULL) {
19727c478bd9Sstevel@tonic-gate if (putenv(*script_env) == -1)
19737c478bd9Sstevel@tonic-gate warn("unable to set %s for %s: %m", *script_env, program);
19747c478bd9Sstevel@tonic-gate script_env++;
19757c478bd9Sstevel@tonic-gate }
19767c478bd9Sstevel@tonic-gate }
19777c478bd9Sstevel@tonic-gate (void) execl("/bin/sh", "sh", "-c", program, (char *)0);
19787c478bd9Sstevel@tonic-gate error("could not exec /bin/sh: %m");
19797c478bd9Sstevel@tonic-gate exit(99);
19807c478bd9Sstevel@tonic-gate /* NOTREACHED */
19817c478bd9Sstevel@tonic-gate }
19827c478bd9Sstevel@tonic-gate
19837c478bd9Sstevel@tonic-gate if (debug)
19847c478bd9Sstevel@tonic-gate dbglog("%s option: '%s' started (pid %d)", optname, program, pid);
19857c478bd9Sstevel@tonic-gate if (dont_wait) {
19867c478bd9Sstevel@tonic-gate record_child(pid, program, NULL, NULL);
19877c478bd9Sstevel@tonic-gate status = 0;
19887c478bd9Sstevel@tonic-gate } else {
19897c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0) {
19907c478bd9Sstevel@tonic-gate (void) close(envpipe[1]);
19917c478bd9Sstevel@tonic-gate (*device_pipe_hook)(envpipe[0]);
19927c478bd9Sstevel@tonic-gate }
19937c478bd9Sstevel@tonic-gate while (waitpid(pid, &status, 0) < 0) {
19947c478bd9Sstevel@tonic-gate if (errno == EINTR)
19957c478bd9Sstevel@tonic-gate continue;
19967c478bd9Sstevel@tonic-gate fatal("error waiting for (dis)connection process: %m");
19977c478bd9Sstevel@tonic-gate }
19987c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0)
19997c478bd9Sstevel@tonic-gate (void) close(envpipe[0]);
20007c478bd9Sstevel@tonic-gate --conn_running;
20017c478bd9Sstevel@tonic-gate }
20027c478bd9Sstevel@tonic-gate
20037c478bd9Sstevel@tonic-gate return (status == 0 ? 0 : -1);
20047c478bd9Sstevel@tonic-gate }
20057c478bd9Sstevel@tonic-gate
20067c478bd9Sstevel@tonic-gate
20077c478bd9Sstevel@tonic-gate /*
20087c478bd9Sstevel@tonic-gate * run-program - execute a program with given arguments,
20097c478bd9Sstevel@tonic-gate * but don't wait for it.
20107c478bd9Sstevel@tonic-gate * If the program can't be executed, logs an error unless
20117c478bd9Sstevel@tonic-gate * must_exist is 0 and the program file doesn't exist.
20127c478bd9Sstevel@tonic-gate * Returns -1 if it couldn't fork, 0 if the file doesn't exist
20137c478bd9Sstevel@tonic-gate * or isn't an executable plain file, or the process ID of the child.
20147c478bd9Sstevel@tonic-gate * If done != NULL, (*done)(arg, int) will be called later (within
20157c478bd9Sstevel@tonic-gate * reap_kids) if this routine returns value > 0.
20167c478bd9Sstevel@tonic-gate */
20177c478bd9Sstevel@tonic-gate pid_t
run_program(prog,args,must_exist,done,arg)20187c478bd9Sstevel@tonic-gate run_program(prog, args, must_exist, done, arg)
20197c478bd9Sstevel@tonic-gate char *prog;
20207c478bd9Sstevel@tonic-gate char **args;
20217c478bd9Sstevel@tonic-gate int must_exist;
20227c478bd9Sstevel@tonic-gate void (*done) __P((void *arg, int status));
20237c478bd9Sstevel@tonic-gate void *arg;
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate pid_t pid;
20267c478bd9Sstevel@tonic-gate struct stat sbuf;
20277c478bd9Sstevel@tonic-gate int retv;
20287c478bd9Sstevel@tonic-gate
20297c478bd9Sstevel@tonic-gate /*
20307c478bd9Sstevel@tonic-gate * First check if the file exists and is executable.
20317c478bd9Sstevel@tonic-gate * We don't use access() because that would use the
20327c478bd9Sstevel@tonic-gate * real user-id, which might not be root, and the script
20337c478bd9Sstevel@tonic-gate * might be accessible only to root.
20347c478bd9Sstevel@tonic-gate */
20357c478bd9Sstevel@tonic-gate errno = EINVAL;
20367c478bd9Sstevel@tonic-gate if (stat(prog, &sbuf) < 0 || !S_ISREG(sbuf.st_mode)
20377c478bd9Sstevel@tonic-gate || (sbuf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) {
20387c478bd9Sstevel@tonic-gate if (must_exist || errno != ENOENT)
20397c478bd9Sstevel@tonic-gate warn("Can't execute %s: %m", prog);
20407c478bd9Sstevel@tonic-gate return (0);
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate
20437c478bd9Sstevel@tonic-gate if (updown_script_hook != NULL) {
20447c478bd9Sstevel@tonic-gate retv = (*updown_script_hook)((const char ***)&args);
20457c478bd9Sstevel@tonic-gate if (retv == -1) {
20467c478bd9Sstevel@tonic-gate return (-1);
20477c478bd9Sstevel@tonic-gate }
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate
20507c478bd9Sstevel@tonic-gate pid = fork();
20517c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) {
20527c478bd9Sstevel@tonic-gate error("Failed to create child process for %s: %m", prog);
20537c478bd9Sstevel@tonic-gate return (-1);
20547c478bd9Sstevel@tonic-gate }
20557c478bd9Sstevel@tonic-gate if (pid == (pid_t)0) {
20567c478bd9Sstevel@tonic-gate int new_fd;
20577c478bd9Sstevel@tonic-gate
20587c478bd9Sstevel@tonic-gate /* Leave the current location */
20597c478bd9Sstevel@tonic-gate (void) setsid(); /* No controlling tty. */
20607c478bd9Sstevel@tonic-gate (void) umask (S_IRWXG|S_IRWXO);
20617c478bd9Sstevel@tonic-gate (void) chdir ("/"); /* no current directory. */
20627c478bd9Sstevel@tonic-gate (void) setuid(0); /* set real UID = root */
20637c478bd9Sstevel@tonic-gate (void) setgid(getegid());
20647c478bd9Sstevel@tonic-gate
20657c478bd9Sstevel@tonic-gate /* Ensure that nothing of our device environment is inherited. */
20667c478bd9Sstevel@tonic-gate sys_close();
20677c478bd9Sstevel@tonic-gate closelog();
20687c478bd9Sstevel@tonic-gate (void) close(0);
20697c478bd9Sstevel@tonic-gate (void) close(1);
20707c478bd9Sstevel@tonic-gate (void) close(2);
20717c478bd9Sstevel@tonic-gate (void) close(ttyfd); /* tty interface to the ppp device */
20727c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0)
20737c478bd9Sstevel@tonic-gate (void) close(real_ttyfd);
20747c478bd9Sstevel@tonic-gate
20757c478bd9Sstevel@tonic-gate /* Don't pass handles to the PPP device, even by accident. */
20767c478bd9Sstevel@tonic-gate new_fd = open (_PATH_DEVNULL, O_RDWR);
20777c478bd9Sstevel@tonic-gate if (new_fd >= 0) {
20787c478bd9Sstevel@tonic-gate if (new_fd != 0) {
20797c478bd9Sstevel@tonic-gate if (dup2(new_fd, 0) < 0) /* stdin <- /dev/null */
20807c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDIN) failed: %m");
20817c478bd9Sstevel@tonic-gate (void) close(new_fd);
20827c478bd9Sstevel@tonic-gate }
20837c478bd9Sstevel@tonic-gate if (dup2(0, 1) < 0) /* stdout -> /dev/null */
20847c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDOUT) failed: %m");
20857c478bd9Sstevel@tonic-gate if (dup2(0, 2) < 0) /* stderr -> /dev/null */
20867c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDERR) failed: %m");
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate
20897c478bd9Sstevel@tonic-gate #ifdef BSD
20907c478bd9Sstevel@tonic-gate /* Force the priority back to zero if pppd is running higher. */
20917c478bd9Sstevel@tonic-gate if (setpriority (PRIO_PROCESS, 0, 0) < 0)
2092*48bbca81SDaniel Hoffman warn("can't reset priority to 0: %m");
20937c478bd9Sstevel@tonic-gate #endif
20947c478bd9Sstevel@tonic-gate
20957c478bd9Sstevel@tonic-gate /* SysV recommends a second fork at this point. */
20967c478bd9Sstevel@tonic-gate
20977c478bd9Sstevel@tonic-gate /* run the program */
20987c478bd9Sstevel@tonic-gate (void) execve(prog, args, script_env);
20997c478bd9Sstevel@tonic-gate if (must_exist || errno != ENOENT) {
21007c478bd9Sstevel@tonic-gate /* have to reopen the log, there's nowhere else
21017c478bd9Sstevel@tonic-gate for the message to go. */
21027c478bd9Sstevel@tonic-gate reopen_log();
21037c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Can't execute %s: %m", prog);
21047c478bd9Sstevel@tonic-gate closelog();
21057c478bd9Sstevel@tonic-gate }
21067c478bd9Sstevel@tonic-gate _exit(-1);
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate
21097c478bd9Sstevel@tonic-gate if (debug)
21107c478bd9Sstevel@tonic-gate dbglog("Script %s started (pid %d)", prog, pid);
21117c478bd9Sstevel@tonic-gate record_child(pid, prog, done, arg);
21127c478bd9Sstevel@tonic-gate
21137c478bd9Sstevel@tonic-gate return (pid);
21147c478bd9Sstevel@tonic-gate }
21157c478bd9Sstevel@tonic-gate
21167c478bd9Sstevel@tonic-gate
21177c478bd9Sstevel@tonic-gate /*
21187c478bd9Sstevel@tonic-gate * record_child - add a child process to the list for reap_kids
21197c478bd9Sstevel@tonic-gate * to use.
21207c478bd9Sstevel@tonic-gate */
21217c478bd9Sstevel@tonic-gate static void
record_child(pid,prog,done,arg)21227c478bd9Sstevel@tonic-gate record_child(pid, prog, done, arg)
21237c478bd9Sstevel@tonic-gate pid_t pid;
21247c478bd9Sstevel@tonic-gate char *prog;
21257c478bd9Sstevel@tonic-gate void (*done) __P((void *, int));
21267c478bd9Sstevel@tonic-gate void *arg;
21277c478bd9Sstevel@tonic-gate {
21287c478bd9Sstevel@tonic-gate struct subprocess *chp;
21297c478bd9Sstevel@tonic-gate
21307c478bd9Sstevel@tonic-gate ++n_children;
21317c478bd9Sstevel@tonic-gate
21327c478bd9Sstevel@tonic-gate chp = (struct subprocess *) malloc(sizeof(struct subprocess));
21337c478bd9Sstevel@tonic-gate if (chp == NULL) {
21347c478bd9Sstevel@tonic-gate warn("losing track of %s process", prog);
21357c478bd9Sstevel@tonic-gate } else {
21367c478bd9Sstevel@tonic-gate chp->pid = pid;
21377c478bd9Sstevel@tonic-gate chp->prog = prog;
21387c478bd9Sstevel@tonic-gate chp->done = done;
21397c478bd9Sstevel@tonic-gate chp->arg = arg;
21407c478bd9Sstevel@tonic-gate chp->next = children;
21417c478bd9Sstevel@tonic-gate children = chp;
21427c478bd9Sstevel@tonic-gate }
21437c478bd9Sstevel@tonic-gate }
21447c478bd9Sstevel@tonic-gate
21457c478bd9Sstevel@tonic-gate
21467c478bd9Sstevel@tonic-gate /*
21477c478bd9Sstevel@tonic-gate * reap_kids - get status from any dead child processes,
21487c478bd9Sstevel@tonic-gate * and log a message for abnormal terminations.
21497c478bd9Sstevel@tonic-gate */
21507c478bd9Sstevel@tonic-gate static int
reap_kids(waitfor)21517c478bd9Sstevel@tonic-gate reap_kids(waitfor)
21527c478bd9Sstevel@tonic-gate int waitfor;
21537c478bd9Sstevel@tonic-gate {
21547c478bd9Sstevel@tonic-gate pid_t pid;
21557c478bd9Sstevel@tonic-gate int status, i;
21567c478bd9Sstevel@tonic-gate struct subprocess *chp, **prevp;
21577c478bd9Sstevel@tonic-gate
21587c478bd9Sstevel@tonic-gate got_sigchld = 0;
21597c478bd9Sstevel@tonic-gate if (n_children == 0)
21607c478bd9Sstevel@tonic-gate return (0);
21617c478bd9Sstevel@tonic-gate
21627c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/
21637c478bd9Sstevel@tonic-gate while (1) {
21647c478bd9Sstevel@tonic-gate pid = waitpid(-1, &status, (waitfor ? 0 : WNOHANG));
21657c478bd9Sstevel@tonic-gate if (pid == 0) {
21667c478bd9Sstevel@tonic-gate break; /* return 0 */
21677c478bd9Sstevel@tonic-gate } else if (pid == -1) {
21687c478bd9Sstevel@tonic-gate if (errno == EINTR)
21697c478bd9Sstevel@tonic-gate continue;
21707c478bd9Sstevel@tonic-gate if (errno != ECHILD)
21717c478bd9Sstevel@tonic-gate error("Error waiting for child process: %m");
21727c478bd9Sstevel@tonic-gate return (-1);
21737c478bd9Sstevel@tonic-gate } else {
21747c478bd9Sstevel@tonic-gate for (prevp = &children; (chp = *prevp) != NULL;
21757c478bd9Sstevel@tonic-gate prevp = &chp->next) {
21767c478bd9Sstevel@tonic-gate if (chp->pid == pid) {
21777c478bd9Sstevel@tonic-gate --n_children;
21787c478bd9Sstevel@tonic-gate *prevp = chp->next;
21797c478bd9Sstevel@tonic-gate break;
21807c478bd9Sstevel@tonic-gate }
21817c478bd9Sstevel@tonic-gate }
21827c478bd9Sstevel@tonic-gate if (WIFSIGNALED(status) || WIFSTOPPED(status)) {
21837c478bd9Sstevel@tonic-gate i = WIFSIGNALED(status) ? WTERMSIG(status) : WSTOPSIG(status);
21847c478bd9Sstevel@tonic-gate warn("Child process %s (pid %d) %s with signal %d (%s)",
21857c478bd9Sstevel@tonic-gate (chp != NULL ? chp->prog : "??"), pid,
21867c478bd9Sstevel@tonic-gate (WIFSIGNALED(status) ? "terminated" : "stopped"),
21877c478bd9Sstevel@tonic-gate i, signal_name(i));
21887c478bd9Sstevel@tonic-gate } else if (debug) {
21897c478bd9Sstevel@tonic-gate dbglog("Child process %s finished (pid %d), status = %d",
21907c478bd9Sstevel@tonic-gate (chp != NULL ? chp->prog: "??"), pid,
21917c478bd9Sstevel@tonic-gate WEXITSTATUS(status));
21927c478bd9Sstevel@tonic-gate }
21937c478bd9Sstevel@tonic-gate if ((chp != NULL) && (chp->done != NULL))
21947c478bd9Sstevel@tonic-gate (*chp->done)(chp->arg, status);
21957c478bd9Sstevel@tonic-gate if (chp != NULL)
21967c478bd9Sstevel@tonic-gate free(chp);
21977c478bd9Sstevel@tonic-gate }
21987c478bd9Sstevel@tonic-gate }
21997c478bd9Sstevel@tonic-gate return (0);
22007c478bd9Sstevel@tonic-gate }
22017c478bd9Sstevel@tonic-gate
22027c478bd9Sstevel@tonic-gate /*
22037c478bd9Sstevel@tonic-gate * infanticide - timeout while waiting for child process.
22047c478bd9Sstevel@tonic-gate */
22057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22067c478bd9Sstevel@tonic-gate static void
infanticide(sig)22077c478bd9Sstevel@tonic-gate infanticide(sig)
22087c478bd9Sstevel@tonic-gate int sig;
22097c478bd9Sstevel@tonic-gate {
22107c478bd9Sstevel@tonic-gate struct subprocess *chp;
22117c478bd9Sstevel@tonic-gate static int runcount = 0;
22127c478bd9Sstevel@tonic-gate
22137c478bd9Sstevel@tonic-gate if (runcount < 2) {
22147c478bd9Sstevel@tonic-gate for (chp = children; chp != NULL; chp = chp->next)
22157c478bd9Sstevel@tonic-gate (void) kill(chp->pid, runcount == 0 ? SIGTERM : SIGKILL);
22167c478bd9Sstevel@tonic-gate } else {
22177c478bd9Sstevel@tonic-gate kill_my_pg(SIGTERM);
22187c478bd9Sstevel@tonic-gate /* Quit and hope for the best. */
22197c478bd9Sstevel@tonic-gate n_children = 0;
22207c478bd9Sstevel@tonic-gate }
22217c478bd9Sstevel@tonic-gate runcount++;
22227c478bd9Sstevel@tonic-gate }
22237c478bd9Sstevel@tonic-gate
22247c478bd9Sstevel@tonic-gate /*
22257c478bd9Sstevel@tonic-gate * Perform final wait before exiting.
22267c478bd9Sstevel@tonic-gate */
22277c478bd9Sstevel@tonic-gate static void
final_reap()22287c478bd9Sstevel@tonic-gate final_reap()
22297c478bd9Sstevel@tonic-gate {
22307c478bd9Sstevel@tonic-gate struct sigaction sa;
22317c478bd9Sstevel@tonic-gate struct subprocess *chp;
22327c478bd9Sstevel@tonic-gate
22337c478bd9Sstevel@tonic-gate if (n_children > 0 && debug) {
22347c478bd9Sstevel@tonic-gate dbglog("Waiting for %d child processes...", n_children);
22357c478bd9Sstevel@tonic-gate for (chp = children; chp != NULL; chp = chp->next)
22367c478bd9Sstevel@tonic-gate dbglog(" pid %d: %s", chp->pid, chp->prog);
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate BZERO(&sa, sizeof (sa));
22397c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGALRM, infanticide);
22407c478bd9Sstevel@tonic-gate while (n_children > 0) {
22417c478bd9Sstevel@tonic-gate (void) alarm(7);
22427c478bd9Sstevel@tonic-gate if (reap_kids(1) < 0)
22437c478bd9Sstevel@tonic-gate break;
22447c478bd9Sstevel@tonic-gate }
22457c478bd9Sstevel@tonic-gate (void) alarm(0);
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate
22487c478bd9Sstevel@tonic-gate /*
22497c478bd9Sstevel@tonic-gate * novm - log an error message saying we ran out of memory, and die.
22507c478bd9Sstevel@tonic-gate */
22517c478bd9Sstevel@tonic-gate void
novm(msg)22527c478bd9Sstevel@tonic-gate novm(msg)
22537c478bd9Sstevel@tonic-gate char *msg;
22547c478bd9Sstevel@tonic-gate {
22557c478bd9Sstevel@tonic-gate fatal("Virtual memory exhausted allocating %s\n", msg);
22567c478bd9Sstevel@tonic-gate }
22577c478bd9Sstevel@tonic-gate
22587c478bd9Sstevel@tonic-gate /*
22597c478bd9Sstevel@tonic-gate * script_setenv - set an environment variable value to be used
22607c478bd9Sstevel@tonic-gate * for scripts that we run (e.g. ip-up, auth-up, etc.)
22617c478bd9Sstevel@tonic-gate */
22627c478bd9Sstevel@tonic-gate void
script_setenv(var,value,iskey)22637c478bd9Sstevel@tonic-gate script_setenv(var, value, iskey)
22647c478bd9Sstevel@tonic-gate const char *var;
22657c478bd9Sstevel@tonic-gate const char *value;
22667c478bd9Sstevel@tonic-gate int iskey;
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate size_t varl = strlen(var);
22697c478bd9Sstevel@tonic-gate size_t vl = varl + strlen(value) + 2;
22707c478bd9Sstevel@tonic-gate int i;
22717c478bd9Sstevel@tonic-gate char *p, *newstring;
22727c478bd9Sstevel@tonic-gate
22737c478bd9Sstevel@tonic-gate /*
22747c478bd9Sstevel@tonic-gate * XXX: Can we assert that a tdb write lock is held here ? It appears that
22757c478bd9Sstevel@tonic-gate * Linux's use of tdb is not safe.
22767c478bd9Sstevel@tonic-gate */
22777c478bd9Sstevel@tonic-gate newstring = (char *) malloc(vl+1);
22787c478bd9Sstevel@tonic-gate if (newstring == NULL) {
22797c478bd9Sstevel@tonic-gate novm("script environment string");
22807c478bd9Sstevel@tonic-gate return;
22817c478bd9Sstevel@tonic-gate }
22827c478bd9Sstevel@tonic-gate *newstring++ = iskey;
22837c478bd9Sstevel@tonic-gate (void) slprintf(newstring, vl, "%s=%s", var, value);
22847c478bd9Sstevel@tonic-gate
22857c478bd9Sstevel@tonic-gate /* check if this variable is already set */
22867c478bd9Sstevel@tonic-gate if (script_env != NULL) {
22877c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) {
22887c478bd9Sstevel@tonic-gate if (strncmp(p, var, varl) == 0 && p[varl] == '=') {
22897c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
22907c478bd9Sstevel@tonic-gate if (p[-1] != '\0' && pppdb != NULL)
22917c478bd9Sstevel@tonic-gate delete_db_key(p);
22927c478bd9Sstevel@tonic-gate #endif
22937c478bd9Sstevel@tonic-gate free(p-1);
22947c478bd9Sstevel@tonic-gate script_env[i] = newstring;
22957c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
22967c478bd9Sstevel@tonic-gate if (iskey && pppdb != NULL)
22977c478bd9Sstevel@tonic-gate add_db_key(newstring);
22987c478bd9Sstevel@tonic-gate update_db_entry();
22997c478bd9Sstevel@tonic-gate #endif
23007c478bd9Sstevel@tonic-gate return;
23017c478bd9Sstevel@tonic-gate }
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate } else {
23047c478bd9Sstevel@tonic-gate /* no space allocated for script env. ptrs. yet */
23057c478bd9Sstevel@tonic-gate i = 0;
23067c478bd9Sstevel@tonic-gate script_env = (char **) malloc(16 * sizeof(char *));
23077c478bd9Sstevel@tonic-gate if (script_env == NULL) {
23087c478bd9Sstevel@tonic-gate novm("script environment variable.");
23097c478bd9Sstevel@tonic-gate return;
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate s_env_nalloc = 16;
23127c478bd9Sstevel@tonic-gate }
23137c478bd9Sstevel@tonic-gate
23147c478bd9Sstevel@tonic-gate /* reallocate script_env with more space if needed */
23157c478bd9Sstevel@tonic-gate if (i + 1 >= s_env_nalloc) {
23167c478bd9Sstevel@tonic-gate int new_n = i + 17;
23177c478bd9Sstevel@tonic-gate char **newenv = (char **) realloc((void *)script_env,
23187c478bd9Sstevel@tonic-gate new_n * sizeof(char *));
23197c478bd9Sstevel@tonic-gate if (newenv == NULL) {
23207c478bd9Sstevel@tonic-gate novm("expanded script environment variable.");
23217c478bd9Sstevel@tonic-gate return;
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate script_env = newenv;
23247c478bd9Sstevel@tonic-gate s_env_nalloc = new_n;
23257c478bd9Sstevel@tonic-gate }
23267c478bd9Sstevel@tonic-gate
23277c478bd9Sstevel@tonic-gate script_env[i] = newstring;
23287c478bd9Sstevel@tonic-gate script_env[i+1] = NULL;
23297c478bd9Sstevel@tonic-gate
23307c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
23317c478bd9Sstevel@tonic-gate if (pppdb != NULL) {
23327c478bd9Sstevel@tonic-gate if (iskey)
23337c478bd9Sstevel@tonic-gate add_db_key(newstring);
23347c478bd9Sstevel@tonic-gate update_db_entry();
23357c478bd9Sstevel@tonic-gate }
23367c478bd9Sstevel@tonic-gate #endif
23377c478bd9Sstevel@tonic-gate }
23387c478bd9Sstevel@tonic-gate
23397c478bd9Sstevel@tonic-gate /*
23407c478bd9Sstevel@tonic-gate * script_unsetenv - remove a variable from the environment
23417c478bd9Sstevel@tonic-gate * for scripts.
23427c478bd9Sstevel@tonic-gate */
23437c478bd9Sstevel@tonic-gate void
script_unsetenv(var)23447c478bd9Sstevel@tonic-gate script_unsetenv(var)
23457c478bd9Sstevel@tonic-gate const char *var;
23467c478bd9Sstevel@tonic-gate {
23477c478bd9Sstevel@tonic-gate int vl = strlen(var);
23487c478bd9Sstevel@tonic-gate int i;
23497c478bd9Sstevel@tonic-gate char *p;
23507c478bd9Sstevel@tonic-gate
23517c478bd9Sstevel@tonic-gate /*
23527c478bd9Sstevel@tonic-gate * XXX: Can we assert that a tdb write lock is held here ? It appears that
23537c478bd9Sstevel@tonic-gate * Linux's use of tdb is not safe.
23547c478bd9Sstevel@tonic-gate */
23557c478bd9Sstevel@tonic-gate if (script_env == NULL)
23567c478bd9Sstevel@tonic-gate return;
23577c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) {
23587c478bd9Sstevel@tonic-gate if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
23597c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
23607c478bd9Sstevel@tonic-gate if (p[-1] != '\0' && pppdb != NULL)
23617c478bd9Sstevel@tonic-gate delete_db_key(p);
23627c478bd9Sstevel@tonic-gate #endif
23637c478bd9Sstevel@tonic-gate free(p-1);
23647c478bd9Sstevel@tonic-gate while ((script_env[i] = script_env[i+1]) != NULL)
23657c478bd9Sstevel@tonic-gate ++i;
23667c478bd9Sstevel@tonic-gate break;
23677c478bd9Sstevel@tonic-gate }
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
23707c478bd9Sstevel@tonic-gate if ((pppdb != NULL) && (p != NULL))
23717c478bd9Sstevel@tonic-gate update_db_entry();
23727c478bd9Sstevel@tonic-gate #endif
23737c478bd9Sstevel@tonic-gate }
23747c478bd9Sstevel@tonic-gate
23757c478bd9Sstevel@tonic-gate /*
23767c478bd9Sstevel@tonic-gate * script_getenv - find a variable in the script environment.
23777c478bd9Sstevel@tonic-gate */
23787c478bd9Sstevel@tonic-gate const char *
script_getenv(var)23797c478bd9Sstevel@tonic-gate script_getenv(var)
23807c478bd9Sstevel@tonic-gate const char *var;
23817c478bd9Sstevel@tonic-gate {
23827c478bd9Sstevel@tonic-gate int vl = strlen(var);
23837c478bd9Sstevel@tonic-gate int i;
23847c478bd9Sstevel@tonic-gate char *p;
23857c478bd9Sstevel@tonic-gate
23867c478bd9Sstevel@tonic-gate if (script_env == NULL)
23877c478bd9Sstevel@tonic-gate return (NULL);
23887c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) {
23897c478bd9Sstevel@tonic-gate if (strncmp(p, var, vl) == 0 && p[vl] == '=')
23907c478bd9Sstevel@tonic-gate return ((const char *)p+vl+1);
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate return (NULL);
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate
23957c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
23967c478bd9Sstevel@tonic-gate /*
23977c478bd9Sstevel@tonic-gate * update_db_entry - update our entry in the database.
23987c478bd9Sstevel@tonic-gate */
23997c478bd9Sstevel@tonic-gate static void
update_db_entry()24007c478bd9Sstevel@tonic-gate update_db_entry()
24017c478bd9Sstevel@tonic-gate {
24027c478bd9Sstevel@tonic-gate TDB_DATA key, dbuf;
24037c478bd9Sstevel@tonic-gate int vlen, i;
24047c478bd9Sstevel@tonic-gate char *p, *q, *vbuf;
24057c478bd9Sstevel@tonic-gate
24067c478bd9Sstevel@tonic-gate if (script_env == NULL)
24077c478bd9Sstevel@tonic-gate return;
24087c478bd9Sstevel@tonic-gate /*
24097c478bd9Sstevel@tonic-gate * vlen needs to be initialized as 1, or otherwise, the last string
24107c478bd9Sstevel@tonic-gate * is truncated by slprintf.
24117c478bd9Sstevel@tonic-gate */
24127c478bd9Sstevel@tonic-gate vlen = 1;
24137c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i)
24147c478bd9Sstevel@tonic-gate vlen += strlen(p) + 1;
24157c478bd9Sstevel@tonic-gate vbuf = malloc(vlen);
24167c478bd9Sstevel@tonic-gate if (vbuf == NULL)
24177c478bd9Sstevel@tonic-gate novm("database entry");
24187c478bd9Sstevel@tonic-gate q = vbuf;
24197c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i)
24207c478bd9Sstevel@tonic-gate q += slprintf(q, vbuf + vlen - q, "%s;", p);
24217c478bd9Sstevel@tonic-gate
24227c478bd9Sstevel@tonic-gate key.dptr = db_key;
24237c478bd9Sstevel@tonic-gate key.dsize = strlen(db_key);
24247c478bd9Sstevel@tonic-gate dbuf.dptr = vbuf;
24257c478bd9Sstevel@tonic-gate dbuf.dsize = vlen;
24267c478bd9Sstevel@tonic-gate if (tdb_store(pppdb, key, dbuf, TDB_REPLACE))
24277c478bd9Sstevel@tonic-gate error("tdb_store failed: %s", tdb_error(pppdb));
24287c478bd9Sstevel@tonic-gate }
24297c478bd9Sstevel@tonic-gate
24307c478bd9Sstevel@tonic-gate /*
24317c478bd9Sstevel@tonic-gate * add_db_key - add a key that we can use to look up our database entry.
24327c478bd9Sstevel@tonic-gate */
24337c478bd9Sstevel@tonic-gate static void
add_db_key(str)24347c478bd9Sstevel@tonic-gate add_db_key(str)
24357c478bd9Sstevel@tonic-gate const char *str;
24367c478bd9Sstevel@tonic-gate {
24377c478bd9Sstevel@tonic-gate TDB_DATA key, dbuf;
24387c478bd9Sstevel@tonic-gate
24397c478bd9Sstevel@tonic-gate key.dptr = (char *) str;
24407c478bd9Sstevel@tonic-gate key.dsize = strlen(str);
24417c478bd9Sstevel@tonic-gate dbuf.dptr = db_key;
24427c478bd9Sstevel@tonic-gate dbuf.dsize = strlen(db_key);
24437c478bd9Sstevel@tonic-gate if (tdb_store(pppdb, key, dbuf, TDB_REPLACE))
24447c478bd9Sstevel@tonic-gate error("tdb_store key failed: %s", tdb_error(pppdb));
24457c478bd9Sstevel@tonic-gate }
24467c478bd9Sstevel@tonic-gate
24477c478bd9Sstevel@tonic-gate /*
24487c478bd9Sstevel@tonic-gate * delete_db_key - delete a key for looking up our database entry.
24497c478bd9Sstevel@tonic-gate */
24507c478bd9Sstevel@tonic-gate static void
delete_db_key(str)24517c478bd9Sstevel@tonic-gate delete_db_key(str)
24527c478bd9Sstevel@tonic-gate const char *str;
24537c478bd9Sstevel@tonic-gate {
24547c478bd9Sstevel@tonic-gate TDB_DATA key;
24557c478bd9Sstevel@tonic-gate
24567c478bd9Sstevel@tonic-gate key.dptr = (char *) str;
24577c478bd9Sstevel@tonic-gate key.dsize = strlen(str);
24587c478bd9Sstevel@tonic-gate (void) tdb_delete(pppdb, key);
24597c478bd9Sstevel@tonic-gate }
24607c478bd9Sstevel@tonic-gate
24617c478bd9Sstevel@tonic-gate /*
24627c478bd9Sstevel@tonic-gate * cleanup_db - delete all the entries we put in the database.
24637c478bd9Sstevel@tonic-gate */
24647c478bd9Sstevel@tonic-gate static void
cleanup_db()24657c478bd9Sstevel@tonic-gate cleanup_db()
24667c478bd9Sstevel@tonic-gate {
24677c478bd9Sstevel@tonic-gate TDB_DATA key;
24687c478bd9Sstevel@tonic-gate int i;
24697c478bd9Sstevel@tonic-gate char *p;
24707c478bd9Sstevel@tonic-gate
24717c478bd9Sstevel@tonic-gate key.dptr = db_key;
24727c478bd9Sstevel@tonic-gate key.dsize = strlen(db_key);
24737c478bd9Sstevel@tonic-gate (void) tdb_delete(pppdb, key);
24747c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i)
24757c478bd9Sstevel@tonic-gate if (p[-1] != '\0')
24767c478bd9Sstevel@tonic-gate delete_db_key(p);
24777c478bd9Sstevel@tonic-gate }
24787c478bd9Sstevel@tonic-gate #endif /* HAVE_MULTILINK */
24797c478bd9Sstevel@tonic-gate
24807c478bd9Sstevel@tonic-gate /*
24817c478bd9Sstevel@tonic-gate * open_socket - establish a stream socket connection to the nominated
24827c478bd9Sstevel@tonic-gate * host and port.
24837c478bd9Sstevel@tonic-gate * XXX: Need IPv6 support for those systems that support it (use getaddrinfo),
24847c478bd9Sstevel@tonic-gate * but requires portability changes.
24857c478bd9Sstevel@tonic-gate */
24867c478bd9Sstevel@tonic-gate static int
open_socket(dest)24877c478bd9Sstevel@tonic-gate open_socket(dest)
24887c478bd9Sstevel@tonic-gate char *dest;
24897c478bd9Sstevel@tonic-gate {
24907c478bd9Sstevel@tonic-gate char *sep, *endp = NULL;
24917c478bd9Sstevel@tonic-gate int sock;
24927c478bd9Sstevel@tonic-gate int port = -1;
24937c478bd9Sstevel@tonic-gate u_int32_t host;
24947c478bd9Sstevel@tonic-gate struct hostent *hent = NULL;
24957c478bd9Sstevel@tonic-gate struct sockaddr_in sad;
24967c478bd9Sstevel@tonic-gate struct servent *se;
24977c478bd9Sstevel@tonic-gate
24987c478bd9Sstevel@tonic-gate /* parse host:port and resolve host to an IP address */
24997c478bd9Sstevel@tonic-gate sep = strchr(dest, ':');
25007c478bd9Sstevel@tonic-gate if (sep != NULL) {
25017c478bd9Sstevel@tonic-gate se = getservbyname((const char *)sep+1, "tcp");
25027c478bd9Sstevel@tonic-gate if (se != NULL) {
25037c478bd9Sstevel@tonic-gate port = ntohs(se->s_port);
25047c478bd9Sstevel@tonic-gate } else {
25057c478bd9Sstevel@tonic-gate port = strtol(sep+1, &endp, 10);
25067c478bd9Sstevel@tonic-gate if (endp == sep+1 || *endp != '\0') {
25077c478bd9Sstevel@tonic-gate error("Can't parse host:port for socket destination");
25087c478bd9Sstevel@tonic-gate return (-1);
25097c478bd9Sstevel@tonic-gate }
25107c478bd9Sstevel@tonic-gate }
25117c478bd9Sstevel@tonic-gate }
25127c478bd9Sstevel@tonic-gate if (port < 0 || port > 65535 || sep == dest) {
25137c478bd9Sstevel@tonic-gate error("Can't parse host:port for socket destination");
25147c478bd9Sstevel@tonic-gate return (-1);
25157c478bd9Sstevel@tonic-gate }
25167c478bd9Sstevel@tonic-gate *sep = '\0';
25177c478bd9Sstevel@tonic-gate host = inet_addr(dest);
25187c478bd9Sstevel@tonic-gate if (host == (u_int32_t) -1) {
25197c478bd9Sstevel@tonic-gate hent = gethostbyname(dest);
25207c478bd9Sstevel@tonic-gate if (hent == NULL) {
25217c478bd9Sstevel@tonic-gate error("%s: unknown host in socket option", dest);
25227c478bd9Sstevel@tonic-gate *sep = ':';
25237c478bd9Sstevel@tonic-gate return (-1);
25247c478bd9Sstevel@tonic-gate }
25257c478bd9Sstevel@tonic-gate BCOPY(hent->h_addr_list[0], &host, sizeof(host));
25267c478bd9Sstevel@tonic-gate hent->h_addr_list++;
25277c478bd9Sstevel@tonic-gate }
25287c478bd9Sstevel@tonic-gate *sep = ':';
25297c478bd9Sstevel@tonic-gate
25307c478bd9Sstevel@tonic-gate for (;;) {
25317c478bd9Sstevel@tonic-gate /* get a socket and connect it to the other end */
25327c478bd9Sstevel@tonic-gate sock = socket(PF_INET, SOCK_STREAM, 0);
25337c478bd9Sstevel@tonic-gate if (sock < 0) {
25347c478bd9Sstevel@tonic-gate error("Can't create socket: %m");
25357c478bd9Sstevel@tonic-gate return (-1);
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate BZERO(&sad, sizeof(sad));
25387c478bd9Sstevel@tonic-gate sad.sin_family = AF_INET;
25397c478bd9Sstevel@tonic-gate sad.sin_port = htons(port);
25407c478bd9Sstevel@tonic-gate sad.sin_addr.s_addr = host;
25417c478bd9Sstevel@tonic-gate if (connect(sock, (struct sockaddr *)&sad, sizeof(sad)) >= 0) {
25427c478bd9Sstevel@tonic-gate break; /* return sock file descriptor */
25437c478bd9Sstevel@tonic-gate }
25447c478bd9Sstevel@tonic-gate if ((hent != NULL) && (hent->h_addr_list != NULL)) {
25457c478bd9Sstevel@tonic-gate BCOPY(hent->h_addr_list[0], &host, sizeof(host));
25467c478bd9Sstevel@tonic-gate hent->h_addr_list++;
25477c478bd9Sstevel@tonic-gate (void) close(sock);
25487c478bd9Sstevel@tonic-gate continue;
25497c478bd9Sstevel@tonic-gate }
25507c478bd9Sstevel@tonic-gate error("Can't connect to %s: %m", dest);
25517c478bd9Sstevel@tonic-gate (void) close(sock);
25527c478bd9Sstevel@tonic-gate return (-1);
25537c478bd9Sstevel@tonic-gate }
25547c478bd9Sstevel@tonic-gate return (sock);
25557c478bd9Sstevel@tonic-gate }
25567c478bd9Sstevel@tonic-gate
25577c478bd9Sstevel@tonic-gate /*
25587c478bd9Sstevel@tonic-gate * print_ncpstate - prints out current NCP state.
25597c478bd9Sstevel@tonic-gate *
25607c478bd9Sstevel@tonic-gate * We're normally called from SIGUSR1 here, but this is safe because
25617c478bd9Sstevel@tonic-gate * these signals are blocked unless we're idle waiting for events.
25627c478bd9Sstevel@tonic-gate * There's no need to otherwise lock the data structures referenced.
25637c478bd9Sstevel@tonic-gate */
25647c478bd9Sstevel@tonic-gate void
print_ncpstate(unit,strptr)25657c478bd9Sstevel@tonic-gate print_ncpstate(unit, strptr)
25667c478bd9Sstevel@tonic-gate int unit;
25677c478bd9Sstevel@tonic-gate FILE *strptr;
25687c478bd9Sstevel@tonic-gate {
25697c478bd9Sstevel@tonic-gate struct protent *protp;
25707c478bd9Sstevel@tonic-gate int i;
25717c478bd9Sstevel@tonic-gate
25727c478bd9Sstevel@tonic-gate (void) flprintf(strptr, "In %s phase\n", phase_name(phase));
25737c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) {
25747c478bd9Sstevel@tonic-gate if (protp->print_stat != NULL)
25757c478bd9Sstevel@tonic-gate (*protp->print_stat)(unit, strptr);
25767c478bd9Sstevel@tonic-gate }
25777c478bd9Sstevel@tonic-gate sys_print_state(strptr);
25787c478bd9Sstevel@tonic-gate }
25797c478bd9Sstevel@tonic-gate
25807c478bd9Sstevel@tonic-gate /*
25817c478bd9Sstevel@tonic-gate * start_charshunt - create a child process to run the character shunt.
25827c478bd9Sstevel@tonic-gate */
25837c478bd9Sstevel@tonic-gate static int
start_charshunt(ifd,ofd)25847c478bd9Sstevel@tonic-gate start_charshunt(ifd, ofd)
25857c478bd9Sstevel@tonic-gate int ifd, ofd;
25867c478bd9Sstevel@tonic-gate {
25877c478bd9Sstevel@tonic-gate pid_t cpid;
25887c478bd9Sstevel@tonic-gate
25897c478bd9Sstevel@tonic-gate cpid = fork();
25907c478bd9Sstevel@tonic-gate if (cpid == (pid_t)-1) {
25917c478bd9Sstevel@tonic-gate error("Can't fork process for character shunt: %m");
25927c478bd9Sstevel@tonic-gate return (0);
25937c478bd9Sstevel@tonic-gate }
25947c478bd9Sstevel@tonic-gate if (cpid == (pid_t)0) {
25957c478bd9Sstevel@tonic-gate /* child */
25967c478bd9Sstevel@tonic-gate (void) close(pty_slave);
25977c478bd9Sstevel@tonic-gate pty_slave = -1;
25987c478bd9Sstevel@tonic-gate (void) setgid(getgid());
25997c478bd9Sstevel@tonic-gate (void) setuid(uid);
26007c478bd9Sstevel@tonic-gate if (getuid() != uid)
26017c478bd9Sstevel@tonic-gate fatal("setuid failed");
26027c478bd9Sstevel@tonic-gate if (!nodetach)
26037c478bd9Sstevel@tonic-gate log_to_fd = -1;
26047c478bd9Sstevel@tonic-gate charshunt(ifd, ofd, record_file);
26057c478bd9Sstevel@tonic-gate exit(0);
26067c478bd9Sstevel@tonic-gate }
26077c478bd9Sstevel@tonic-gate charshunt_pid = cpid;
26087c478bd9Sstevel@tonic-gate (void) close(pty_master);
26097c478bd9Sstevel@tonic-gate pty_master = -1;
26107c478bd9Sstevel@tonic-gate ttyfd = pty_slave;
26117c478bd9Sstevel@tonic-gate record_child(cpid, "pppd (charshunt)", charshunt_done, NULL);
26127c478bd9Sstevel@tonic-gate return (1);
26137c478bd9Sstevel@tonic-gate }
26147c478bd9Sstevel@tonic-gate
26157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26167c478bd9Sstevel@tonic-gate static void
charshunt_done(arg,status)26177c478bd9Sstevel@tonic-gate charshunt_done(arg, status)
26187c478bd9Sstevel@tonic-gate void *arg;
26197c478bd9Sstevel@tonic-gate int status;
26207c478bd9Sstevel@tonic-gate {
26217c478bd9Sstevel@tonic-gate charshunt_pid = (pid_t)0;
26227c478bd9Sstevel@tonic-gate }
26237c478bd9Sstevel@tonic-gate
26247c478bd9Sstevel@tonic-gate static void
reportme(int signo)26257c478bd9Sstevel@tonic-gate reportme(int signo)
26267c478bd9Sstevel@tonic-gate {
26277c478bd9Sstevel@tonic-gate dbglog("charshunt taking signal %d", signo);
26287c478bd9Sstevel@tonic-gate exit(1);
26297c478bd9Sstevel@tonic-gate }
26307c478bd9Sstevel@tonic-gate
26317c478bd9Sstevel@tonic-gate /*
26327c478bd9Sstevel@tonic-gate * charshunt - the character shunt, which passes characters between
26337c478bd9Sstevel@tonic-gate * the pty master side and the serial port (or stdin/stdout).
26347c478bd9Sstevel@tonic-gate * This runs as the user (not as root).
26357c478bd9Sstevel@tonic-gate * (We assume ofd >= ifd which is true the way this gets called. :-).
26367c478bd9Sstevel@tonic-gate */
26377c478bd9Sstevel@tonic-gate static void
charshunt(ifd,ofd,record_file)26387c478bd9Sstevel@tonic-gate charshunt(ifd, ofd, record_file)
26397c478bd9Sstevel@tonic-gate int ifd, ofd;
26407c478bd9Sstevel@tonic-gate char *record_file;
26417c478bd9Sstevel@tonic-gate {
26427c478bd9Sstevel@tonic-gate int n, nfds;
26437c478bd9Sstevel@tonic-gate fd_set ready, writey;
26447c478bd9Sstevel@tonic-gate u_char *ibufp, *obufp;
26457c478bd9Sstevel@tonic-gate int nibuf, nobuf;
26467c478bd9Sstevel@tonic-gate int flags;
26477c478bd9Sstevel@tonic-gate struct timeval lasttime;
26487c478bd9Sstevel@tonic-gate FILE *recordf = NULL;
26497c478bd9Sstevel@tonic-gate int ilevel, olevel, max_level;
26507c478bd9Sstevel@tonic-gate struct timeval levelt, tout, *top;
26517c478bd9Sstevel@tonic-gate
26527c478bd9Sstevel@tonic-gate /*
26537c478bd9Sstevel@tonic-gate * Reset signal handlers.
26547c478bd9Sstevel@tonic-gate */
26557c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); /* Hangup */
26567c478bd9Sstevel@tonic-gate (void) signal(SIGINT, reportme); /* Interrupt */
26577c478bd9Sstevel@tonic-gate (void) signal(SIGTERM, reportme); /* Terminate */
26587c478bd9Sstevel@tonic-gate (void) signal(SIGCHLD, reportme);
26597c478bd9Sstevel@tonic-gate (void) signal(SIGUSR1, reportme);
26607c478bd9Sstevel@tonic-gate (void) signal(SIGUSR2, reportme);
26617c478bd9Sstevel@tonic-gate (void) signal(SIGABRT, reportme);
26627c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, reportme);
26637c478bd9Sstevel@tonic-gate (void) signal(SIGFPE, reportme);
26647c478bd9Sstevel@tonic-gate (void) signal(SIGILL, reportme);
26657c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, reportme);
26667c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, reportme);
26677c478bd9Sstevel@tonic-gate #ifndef DEBUG
26687c478bd9Sstevel@tonic-gate (void) signal(SIGSEGV, reportme);
26697c478bd9Sstevel@tonic-gate #endif
26707c478bd9Sstevel@tonic-gate #ifdef SIGBUS
26717c478bd9Sstevel@tonic-gate (void) signal(SIGBUS, reportme);
26727c478bd9Sstevel@tonic-gate #endif
26737c478bd9Sstevel@tonic-gate #ifdef SIGEMT
26747c478bd9Sstevel@tonic-gate (void) signal(SIGEMT, reportme);
26757c478bd9Sstevel@tonic-gate #endif
26767c478bd9Sstevel@tonic-gate #ifdef SIGPOLL
26777c478bd9Sstevel@tonic-gate (void) signal(SIGPOLL, reportme);
26787c478bd9Sstevel@tonic-gate #endif
26797c478bd9Sstevel@tonic-gate #ifdef SIGPROF
26807c478bd9Sstevel@tonic-gate (void) signal(SIGPROF, reportme);
26817c478bd9Sstevel@tonic-gate #endif
26827c478bd9Sstevel@tonic-gate #ifdef SIGSYS
26837c478bd9Sstevel@tonic-gate (void) signal(SIGSYS, reportme);
26847c478bd9Sstevel@tonic-gate #endif
26857c478bd9Sstevel@tonic-gate #ifdef SIGTRAP
26867c478bd9Sstevel@tonic-gate (void) signal(SIGTRAP, reportme);
26877c478bd9Sstevel@tonic-gate #endif
26887c478bd9Sstevel@tonic-gate #ifdef SIGVTALRM
26897c478bd9Sstevel@tonic-gate (void) signal(SIGVTALRM, reportme);
26907c478bd9Sstevel@tonic-gate #endif
26917c478bd9Sstevel@tonic-gate #ifdef SIGXCPU
26927c478bd9Sstevel@tonic-gate (void) signal(SIGXCPU, reportme);
26937c478bd9Sstevel@tonic-gate #endif
26947c478bd9Sstevel@tonic-gate #ifdef SIGXFSZ
26957c478bd9Sstevel@tonic-gate (void) signal(SIGXFSZ, reportme);
26967c478bd9Sstevel@tonic-gate #endif
26977c478bd9Sstevel@tonic-gate
26987c478bd9Sstevel@tonic-gate /*
26997c478bd9Sstevel@tonic-gate * Open the record file if required.
27007c478bd9Sstevel@tonic-gate */
27017c478bd9Sstevel@tonic-gate if (record_file != NULL) {
27027c478bd9Sstevel@tonic-gate recordf = fopen(record_file, "a");
27037c478bd9Sstevel@tonic-gate if (recordf == NULL)
27047c478bd9Sstevel@tonic-gate error("Couldn't create record file %s: %m", record_file);
27057c478bd9Sstevel@tonic-gate }
27067c478bd9Sstevel@tonic-gate
27077c478bd9Sstevel@tonic-gate /* set all the fds to non-blocking mode */
27087c478bd9Sstevel@tonic-gate flags = fcntl(pty_master, F_GETFL);
27097c478bd9Sstevel@tonic-gate if (flags == -1
27107c478bd9Sstevel@tonic-gate || fcntl(pty_master, F_SETFL, flags | O_NONBLOCK) == -1)
27117c478bd9Sstevel@tonic-gate warn("couldn't set pty master to nonblock: %m");
27127c478bd9Sstevel@tonic-gate flags = fcntl(ifd, F_GETFL);
27137c478bd9Sstevel@tonic-gate if (flags == -1
27147c478bd9Sstevel@tonic-gate || fcntl(ifd, F_SETFL, flags | O_NONBLOCK) == -1)
27157c478bd9Sstevel@tonic-gate warn("couldn't set %s to nonblock: %m", (ifd==0? "stdin": "tty"));
27167c478bd9Sstevel@tonic-gate if (ofd != ifd) {
27177c478bd9Sstevel@tonic-gate flags = fcntl(ofd, F_GETFL);
27187c478bd9Sstevel@tonic-gate if (flags == -1
27197c478bd9Sstevel@tonic-gate || fcntl(ofd, F_SETFL, flags | O_NONBLOCK) == -1)
27207c478bd9Sstevel@tonic-gate warn("couldn't set stdout to nonblock: %m");
27217c478bd9Sstevel@tonic-gate }
27227c478bd9Sstevel@tonic-gate
27237c478bd9Sstevel@tonic-gate nibuf = nobuf = 0;
27247c478bd9Sstevel@tonic-gate ibufp = obufp = NULL;
27257c478bd9Sstevel@tonic-gate
27267c478bd9Sstevel@tonic-gate ilevel = olevel = 0;
27277c478bd9Sstevel@tonic-gate (void) gettimeofday(&levelt, NULL);
27287c478bd9Sstevel@tonic-gate if (max_data_rate) {
27297c478bd9Sstevel@tonic-gate max_level = max_data_rate / 10;
27307c478bd9Sstevel@tonic-gate if (max_level < MAXLEVELMINSIZE)
27317c478bd9Sstevel@tonic-gate max_level = MAXLEVELMINSIZE;
27327c478bd9Sstevel@tonic-gate } else
27337c478bd9Sstevel@tonic-gate max_level = sizeof(inpacket_buf) + 1;
27347c478bd9Sstevel@tonic-gate
27357c478bd9Sstevel@tonic-gate nfds = (ofd > pty_master? ofd: pty_master) + 1;
27367c478bd9Sstevel@tonic-gate if (recordf != NULL) {
27377c478bd9Sstevel@tonic-gate (void) gettimeofday(&lasttime, NULL);
27387c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMESTART, recordf); /* put start marker */
27397c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 24, recordf);
27407c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 16, recordf);
27417c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 8, recordf);
27427c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec, recordf);
27437c478bd9Sstevel@tonic-gate lasttime.tv_usec = 0;
27447c478bd9Sstevel@tonic-gate }
27457c478bd9Sstevel@tonic-gate
27467c478bd9Sstevel@tonic-gate while (nibuf != 0 || nobuf != 0 || ofd >= 0 || pty_master >= 0) {
27477c478bd9Sstevel@tonic-gate top = 0;
27487c478bd9Sstevel@tonic-gate tout.tv_sec = 0;
27497c478bd9Sstevel@tonic-gate tout.tv_usec = 10000;
27507c478bd9Sstevel@tonic-gate FD_ZERO(&ready);
27517c478bd9Sstevel@tonic-gate FD_ZERO(&writey);
27527c478bd9Sstevel@tonic-gate if (nibuf != 0) {
27537c478bd9Sstevel@tonic-gate if (ilevel >= max_level)
27547c478bd9Sstevel@tonic-gate top = &tout;
27557c478bd9Sstevel@tonic-gate else if (pty_master >= 0)
27567c478bd9Sstevel@tonic-gate FD_SET(pty_master, &writey);
27577c478bd9Sstevel@tonic-gate } else if (ifd >= 0)
27587c478bd9Sstevel@tonic-gate FD_SET(ifd, &ready);
27597c478bd9Sstevel@tonic-gate if (nobuf != 0) {
27607c478bd9Sstevel@tonic-gate if (olevel >= max_level)
27617c478bd9Sstevel@tonic-gate top = &tout;
27627c478bd9Sstevel@tonic-gate else if (ofd >= 0)
27637c478bd9Sstevel@tonic-gate FD_SET(ofd, &writey);
27647c478bd9Sstevel@tonic-gate } else {
27657c478bd9Sstevel@tonic-gate /* Don't read from pty if it's gone or it has closed. */
27667c478bd9Sstevel@tonic-gate if (pty_master >= 0 && ofd >= 0)
27677c478bd9Sstevel@tonic-gate FD_SET(pty_master, &ready);
27687c478bd9Sstevel@tonic-gate }
27697c478bd9Sstevel@tonic-gate if (select(nfds, &ready, &writey, NULL, top) < 0) {
27707c478bd9Sstevel@tonic-gate if (errno != EINTR)
27717c478bd9Sstevel@tonic-gate fatal("select");
27727c478bd9Sstevel@tonic-gate continue;
27737c478bd9Sstevel@tonic-gate }
27747c478bd9Sstevel@tonic-gate if (max_data_rate) {
27757c478bd9Sstevel@tonic-gate double dt;
27767c478bd9Sstevel@tonic-gate int nbt;
27777c478bd9Sstevel@tonic-gate struct timeval now;
27787c478bd9Sstevel@tonic-gate
27797c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL);
27807c478bd9Sstevel@tonic-gate dt = (now.tv_sec - levelt.tv_sec
27817c478bd9Sstevel@tonic-gate + (now.tv_usec - levelt.tv_usec) / 1e6);
27827c478bd9Sstevel@tonic-gate nbt = (int)(dt * max_data_rate);
27837c478bd9Sstevel@tonic-gate ilevel = (nbt < 0 || nbt > ilevel)? 0: ilevel - nbt;
27847c478bd9Sstevel@tonic-gate olevel = (nbt < 0 || nbt > olevel)? 0: olevel - nbt;
27857c478bd9Sstevel@tonic-gate levelt = now;
27867c478bd9Sstevel@tonic-gate } else
27877c478bd9Sstevel@tonic-gate ilevel = olevel = 0;
27887c478bd9Sstevel@tonic-gate if (FD_ISSET(ifd, &ready)) {
27897c478bd9Sstevel@tonic-gate ibufp = inpacket_buf;
27907c478bd9Sstevel@tonic-gate nibuf = read(ifd, ibufp, sizeof(inpacket_buf));
27917c478bd9Sstevel@tonic-gate if (nibuf < 0 && errno == EIO)
27927c478bd9Sstevel@tonic-gate nibuf = 0;
27937c478bd9Sstevel@tonic-gate if (nibuf < 0 || pty_master == -1) {
27947c478bd9Sstevel@tonic-gate if (errno != EINTR && errno != EAGAIN) {
27957c478bd9Sstevel@tonic-gate error("Error reading standard input: %m");
27967c478bd9Sstevel@tonic-gate break;
27977c478bd9Sstevel@tonic-gate }
27987c478bd9Sstevel@tonic-gate nibuf = 0;
27997c478bd9Sstevel@tonic-gate } else if (nibuf == 0) {
28007c478bd9Sstevel@tonic-gate /* end of file from stdin */
28017c478bd9Sstevel@tonic-gate (void) close(pty_master);
28027c478bd9Sstevel@tonic-gate pty_master = -1;
28037c478bd9Sstevel@tonic-gate (void) close(ifd);
28047c478bd9Sstevel@tonic-gate ifd = -1;
28057c478bd9Sstevel@tonic-gate if (recordf)
28067c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_ENDRECV, NULL, 0,
28077c478bd9Sstevel@tonic-gate &lasttime))
28087c478bd9Sstevel@tonic-gate recordf = NULL;
28097c478bd9Sstevel@tonic-gate } else {
28107c478bd9Sstevel@tonic-gate FD_SET(pty_master, &writey);
28117c478bd9Sstevel@tonic-gate if (recordf)
28127c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_STARTRECV, ibufp, nibuf,
28137c478bd9Sstevel@tonic-gate &lasttime))
28147c478bd9Sstevel@tonic-gate recordf = NULL;
28157c478bd9Sstevel@tonic-gate }
28167c478bd9Sstevel@tonic-gate }
28177c478bd9Sstevel@tonic-gate if (ofd >= 0 && pty_master >= 0 && FD_ISSET(pty_master, &ready)) {
28187c478bd9Sstevel@tonic-gate obufp = outpacket_buf;
28197c478bd9Sstevel@tonic-gate nobuf = read(pty_master, obufp, sizeof(outpacket_buf));
28207c478bd9Sstevel@tonic-gate if (nobuf < 0 && errno == EIO)
28217c478bd9Sstevel@tonic-gate nobuf = 0;
28227c478bd9Sstevel@tonic-gate if (nobuf < 0 || ofd == -1) {
28237c478bd9Sstevel@tonic-gate if (!(errno == EINTR || errno == EAGAIN)) {
28247c478bd9Sstevel@tonic-gate error("Error reading pseudo-tty master: %m");
28257c478bd9Sstevel@tonic-gate break;
28267c478bd9Sstevel@tonic-gate }
28277c478bd9Sstevel@tonic-gate nobuf = 0;
28287c478bd9Sstevel@tonic-gate } else if (nobuf == 0) {
28297c478bd9Sstevel@tonic-gate /* end of file from the pty - slave side has closed */
28307c478bd9Sstevel@tonic-gate nibuf = 0;
28317c478bd9Sstevel@tonic-gate (void) close(ofd);
28327c478bd9Sstevel@tonic-gate ofd = -1;
28337c478bd9Sstevel@tonic-gate if (recordf)
28347c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_ENDSEND, NULL, 0,
28357c478bd9Sstevel@tonic-gate &lasttime))
28367c478bd9Sstevel@tonic-gate recordf = NULL;
28377c478bd9Sstevel@tonic-gate } else {
28387c478bd9Sstevel@tonic-gate FD_SET(ofd, &writey);
28397c478bd9Sstevel@tonic-gate if (recordf)
28407c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_STARTSEND, obufp, nobuf,
28417c478bd9Sstevel@tonic-gate &lasttime))
28427c478bd9Sstevel@tonic-gate recordf = NULL;
28437c478bd9Sstevel@tonic-gate }
28447c478bd9Sstevel@tonic-gate }
28457c478bd9Sstevel@tonic-gate if (ofd == -1)
28467c478bd9Sstevel@tonic-gate nobuf = 0;
28477c478bd9Sstevel@tonic-gate else if (FD_ISSET(ofd, &writey)) {
28487c478bd9Sstevel@tonic-gate n = nobuf;
28497c478bd9Sstevel@tonic-gate if (olevel + n > max_level)
28507c478bd9Sstevel@tonic-gate n = max_level - olevel;
28517c478bd9Sstevel@tonic-gate n = write(ofd, obufp, n);
28527c478bd9Sstevel@tonic-gate if (n < 0) {
28537c478bd9Sstevel@tonic-gate if (errno == EIO) {
28547c478bd9Sstevel@tonic-gate (void) close(ofd);
28557c478bd9Sstevel@tonic-gate ofd = -1;
28567c478bd9Sstevel@tonic-gate nobuf = 0;
28577c478bd9Sstevel@tonic-gate } else if (errno != EAGAIN && errno != EINTR) {
28587c478bd9Sstevel@tonic-gate error("Error writing standard output: %m");
28597c478bd9Sstevel@tonic-gate break;
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate } else {
28627c478bd9Sstevel@tonic-gate obufp += n;
28637c478bd9Sstevel@tonic-gate nobuf -= n;
28647c478bd9Sstevel@tonic-gate olevel += n;
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate }
28677c478bd9Sstevel@tonic-gate if (pty_master == -1)
28687c478bd9Sstevel@tonic-gate nibuf = 0;
28697c478bd9Sstevel@tonic-gate else if (FD_ISSET(pty_master, &writey)) {
28707c478bd9Sstevel@tonic-gate n = nibuf;
28717c478bd9Sstevel@tonic-gate if (ilevel + n > max_level)
28727c478bd9Sstevel@tonic-gate n = max_level - ilevel;
28737c478bd9Sstevel@tonic-gate n = write(pty_master, ibufp, n);
28747c478bd9Sstevel@tonic-gate if (n < 0) {
28757c478bd9Sstevel@tonic-gate if (errno == EAGAIN || errno == EINTR)
28767c478bd9Sstevel@tonic-gate continue;
28777c478bd9Sstevel@tonic-gate if (errno != EIO) {
28787c478bd9Sstevel@tonic-gate error("Error writing pseudo-tty master: %m");
28797c478bd9Sstevel@tonic-gate break;
28807c478bd9Sstevel@tonic-gate }
28817c478bd9Sstevel@tonic-gate (void) close(pty_master);
28827c478bd9Sstevel@tonic-gate pty_master = -1;
28837c478bd9Sstevel@tonic-gate nibuf = 0;
28847c478bd9Sstevel@tonic-gate } else {
28857c478bd9Sstevel@tonic-gate ibufp += n;
28867c478bd9Sstevel@tonic-gate nibuf -= n;
28877c478bd9Sstevel@tonic-gate ilevel += n;
28887c478bd9Sstevel@tonic-gate }
28897c478bd9Sstevel@tonic-gate }
28907c478bd9Sstevel@tonic-gate }
28917c478bd9Sstevel@tonic-gate exit(0);
28927c478bd9Sstevel@tonic-gate }
28937c478bd9Sstevel@tonic-gate
28947c478bd9Sstevel@tonic-gate static int
record_write(f,code,buf,nb,tp)28957c478bd9Sstevel@tonic-gate record_write(f, code, buf, nb, tp)
28967c478bd9Sstevel@tonic-gate FILE *f;
28977c478bd9Sstevel@tonic-gate int code;
28987c478bd9Sstevel@tonic-gate u_char *buf;
28997c478bd9Sstevel@tonic-gate int nb;
29007c478bd9Sstevel@tonic-gate struct timeval *tp;
29017c478bd9Sstevel@tonic-gate {
29027c478bd9Sstevel@tonic-gate struct timeval now;
29037c478bd9Sstevel@tonic-gate int diff;
29047c478bd9Sstevel@tonic-gate
29057c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL);
29067c478bd9Sstevel@tonic-gate now.tv_usec /= 100000; /* actually 1/10 s, not usec now */
29077c478bd9Sstevel@tonic-gate diff = (now.tv_sec - tp->tv_sec) * 10 + (now.tv_usec - tp->tv_usec);
29087c478bd9Sstevel@tonic-gate if (diff > 0) {
29097c478bd9Sstevel@tonic-gate if (diff > 255) {
29107c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMEDELTA32, f);
29117c478bd9Sstevel@tonic-gate (void) putc(diff >> 24, f);
29127c478bd9Sstevel@tonic-gate (void) putc(diff >> 16, f);
29137c478bd9Sstevel@tonic-gate (void) putc(diff >> 8, f);
29147c478bd9Sstevel@tonic-gate (void) putc(diff, f);
29157c478bd9Sstevel@tonic-gate } else {
29167c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMEDELTA8, f);
29177c478bd9Sstevel@tonic-gate (void) putc(diff, f);
29187c478bd9Sstevel@tonic-gate }
29197c478bd9Sstevel@tonic-gate *tp = now;
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate (void) putc(code, f);
29227c478bd9Sstevel@tonic-gate if (buf != NULL) {
29237c478bd9Sstevel@tonic-gate (void) putc(nb >> 8, f);
29247c478bd9Sstevel@tonic-gate (void) putc(nb, f);
29257c478bd9Sstevel@tonic-gate (void) fwrite(buf, nb, 1, f);
29267c478bd9Sstevel@tonic-gate }
29277c478bd9Sstevel@tonic-gate (void) fflush(f);
29287c478bd9Sstevel@tonic-gate if (ferror(f)) {
29297c478bd9Sstevel@tonic-gate error("Error writing record file: %m");
29307c478bd9Sstevel@tonic-gate return (0);
29317c478bd9Sstevel@tonic-gate }
29327c478bd9Sstevel@tonic-gate return (1);
29337c478bd9Sstevel@tonic-gate }
2934