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 #define RCSID "$Id: main.c,v 1.97 2000/04/24 02:54:16 masputra Exp $" 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #include <stdio.h> 407c478bd9Sstevel@tonic-gate #include <stdlib.h> 417c478bd9Sstevel@tonic-gate #include <string.h> 427c478bd9Sstevel@tonic-gate #include <unistd.h> 437c478bd9Sstevel@tonic-gate #include <signal.h> 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <fcntl.h> 467c478bd9Sstevel@tonic-gate #include <syslog.h> 477c478bd9Sstevel@tonic-gate #include <netdb.h> 487c478bd9Sstevel@tonic-gate #include <pwd.h> 497c478bd9Sstevel@tonic-gate #include <setjmp.h> 507c478bd9Sstevel@tonic-gate #include <sys/param.h> 517c478bd9Sstevel@tonic-gate #include <sys/types.h> 527c478bd9Sstevel@tonic-gate #include <sys/wait.h> 537c478bd9Sstevel@tonic-gate #include <sys/time.h> 547c478bd9Sstevel@tonic-gate #include <sys/resource.h> 557c478bd9Sstevel@tonic-gate #include <sys/stat.h> 567c478bd9Sstevel@tonic-gate #include <sys/socket.h> 577c478bd9Sstevel@tonic-gate #include <netinet/in.h> 587c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #include "pppd.h" 617c478bd9Sstevel@tonic-gate #include "magic.h" 627c478bd9Sstevel@tonic-gate #include "fsm.h" 637c478bd9Sstevel@tonic-gate #include "lcp.h" 647c478bd9Sstevel@tonic-gate #include "ipcp.h" 657c478bd9Sstevel@tonic-gate #ifdef INET6 667c478bd9Sstevel@tonic-gate #include "ipv6cp.h" 677c478bd9Sstevel@tonic-gate #endif 687c478bd9Sstevel@tonic-gate #include "upap.h" 697c478bd9Sstevel@tonic-gate #include "chap.h" 707c478bd9Sstevel@tonic-gate #include "ccp.h" 717c478bd9Sstevel@tonic-gate #include "pathnames.h" 727c478bd9Sstevel@tonic-gate #include "patchlevel.h" 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 757c478bd9Sstevel@tonic-gate #include "tdb.h" 767c478bd9Sstevel@tonic-gate #endif 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #ifdef CBCP_SUPPORT 797c478bd9Sstevel@tonic-gate #include "cbcp.h" 807c478bd9Sstevel@tonic-gate #endif 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate #ifdef IPX_CHANGE 837c478bd9Sstevel@tonic-gate #include "ipxcp.h" 847c478bd9Sstevel@tonic-gate #endif /* IPX_CHANGE */ 857c478bd9Sstevel@tonic-gate #ifdef AT_CHANGE 867c478bd9Sstevel@tonic-gate #include "atcp.h" 877c478bd9Sstevel@tonic-gate #endif 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate #if !defined(lint) && !defined(_lint) 907c478bd9Sstevel@tonic-gate static const char rcsid[] = RCSID; 917c478bd9Sstevel@tonic-gate #endif 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* interface vars */ 947c478bd9Sstevel@tonic-gate char ifname[32]; /* Interface name */ 957c478bd9Sstevel@tonic-gate int ifunit = -1; /* Interface unit number */ 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate char *progname; /* Name of this program */ 987c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN+1]; /* Our hostname */ 997c478bd9Sstevel@tonic-gate static char pidfilename[MAXPATHLEN]; /* name of pid file */ 1007c478bd9Sstevel@tonic-gate static char linkpidfile[MAXPATHLEN]; /* name of linkname pid file */ 1017c478bd9Sstevel@tonic-gate char ppp_devnam[MAXPATHLEN]; /* name of PPP tty (maybe ttypx) */ 1027c478bd9Sstevel@tonic-gate static uid_t uid; /* Our real user-id */ 1037c478bd9Sstevel@tonic-gate static int conn_running; /* we have a [dis]connector running */ 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate int ttyfd; /* Serial port file descriptor */ 1067c478bd9Sstevel@tonic-gate mode_t tty_mode = (mode_t)-1; /* Original access permissions to tty */ 1077c478bd9Sstevel@tonic-gate int baud_rate; /* Actual bits/second for serial device */ 1087c478bd9Sstevel@tonic-gate bool hungup; /* terminal has been hung up */ 1097c478bd9Sstevel@tonic-gate bool privileged; /* we're running as real uid root */ 1107c478bd9Sstevel@tonic-gate bool need_holdoff; /* need holdoff period before restarting */ 1117c478bd9Sstevel@tonic-gate bool detached; /* have detached from terminal */ 1127c478bd9Sstevel@tonic-gate struct stat devstat; /* result of stat() on devnam */ 1137c478bd9Sstevel@tonic-gate bool prepass = 0; /* doing prepass to find device name */ 1147c478bd9Sstevel@tonic-gate int devnam_fixed; /* set while in options.ttyxx file */ 1157c478bd9Sstevel@tonic-gate volatile int status; /* exit status for pppd */ 1167c478bd9Sstevel@tonic-gate int unsuccess; /* # unsuccessful connection attempts */ 1177c478bd9Sstevel@tonic-gate int do_callback; /* != 0 if we should do callback next */ 1187c478bd9Sstevel@tonic-gate int doing_callback; /* != 0 if we are doing callback */ 1197c478bd9Sstevel@tonic-gate char *callback_script; /* script for doing callback */ 1207c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 1217c478bd9Sstevel@tonic-gate TDB_CONTEXT *pppdb; /* database for storing status etc. */ 1227c478bd9Sstevel@tonic-gate char db_key[32]; 1237c478bd9Sstevel@tonic-gate #endif 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * For plug-in usage: 1277c478bd9Sstevel@tonic-gate * 1287c478bd9Sstevel@tonic-gate * holdoff_hook - Can be used to change the demand-dial hold-off 1297c478bd9Sstevel@tonic-gate * time dynamically. This is normally set by the 1307c478bd9Sstevel@tonic-gate * "holdoff" option, and is 30 seconds by default. 1317c478bd9Sstevel@tonic-gate * 1327c478bd9Sstevel@tonic-gate * new_phase_hook - This is called for each change in the PPP 1337c478bd9Sstevel@tonic-gate * phase (per RFC 1661). This can be used to log 1347c478bd9Sstevel@tonic-gate * progress. 1357c478bd9Sstevel@tonic-gate * 1367c478bd9Sstevel@tonic-gate * check_options_hook - This is called before doing sys_init() 1377c478bd9Sstevel@tonic-gate * and allows the plugin to verify the selected options. 1387c478bd9Sstevel@tonic-gate * 1397c478bd9Sstevel@tonic-gate * updown_script_hook - This is called with the proposed 1407c478bd9Sstevel@tonic-gate * command-line arguments for any of the 1417c478bd9Sstevel@tonic-gate * /etc/ppp/{ip,ipv6,ipx,auth}-{up,down} scripts before 1427c478bd9Sstevel@tonic-gate * fork/exec. It can be used to add or change arguments. 1437c478bd9Sstevel@tonic-gate * 1447c478bd9Sstevel@tonic-gate * device_pipe_hook - If this is set, then an extra fd (3) is 1457c478bd9Sstevel@tonic-gate * passed to the connect/disconnect script. This extra 1467c478bd9Sstevel@tonic-gate * fd is the write side of a pipe, and the read side is 1477c478bd9Sstevel@tonic-gate * passed to this routine. This can be used to pass 1487c478bd9Sstevel@tonic-gate * arbitrary data from the script back to pppd. 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate int (*holdoff_hook) __P((void)) = NULL; 1517c478bd9Sstevel@tonic-gate int (*new_phase_hook) __P((int new, int old)) = NULL; 1527c478bd9Sstevel@tonic-gate int (*check_options_hook) __P((uid_t uid)) = NULL; 1537c478bd9Sstevel@tonic-gate int (*updown_script_hook) __P((const char ***argsp)) = NULL; 1547c478bd9Sstevel@tonic-gate void (*device_pipe_hook) __P((int pipefd)) = NULL; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate static int fd_ppp = -1; /* fd for talking PPP */ 1577c478bd9Sstevel@tonic-gate static int fd_loop; /* fd for getting demand-dial packets */ 1587c478bd9Sstevel@tonic-gate static int pty_master; /* fd for master side of pty */ 1597c478bd9Sstevel@tonic-gate int pty_slave = -1; /* fd for slave side of pty */ 1607c478bd9Sstevel@tonic-gate static int real_ttyfd; /* fd for actual serial port (not pty) */ 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate int phase; /* where the link is at */ 1637c478bd9Sstevel@tonic-gate int kill_link; 1647c478bd9Sstevel@tonic-gate int open_ccp_flag; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate static int waiting; /* for input from peer or timer expiration */ 1677c478bd9Sstevel@tonic-gate static sigjmp_buf sigjmp; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate char **script_env; /* Env. variable values for scripts */ 1707c478bd9Sstevel@tonic-gate int s_env_nalloc; /* # words avail at script_env */ 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate u_char outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for outgoing packet */ 1737c478bd9Sstevel@tonic-gate u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */ 1747c478bd9Sstevel@tonic-gate u_char nak_buffer[PPP_MRU]; /* where we construct a nak packet */ 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate static int n_children; /* # child processes still running */ 1777c478bd9Sstevel@tonic-gate static bool got_sigchld; /* set if we have received a SIGCHLD */ 1787c478bd9Sstevel@tonic-gate static sigset_t main_sigmask; /* signals blocked while dispatching */ 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate static bool locked; /* lock() has succeeded */ 1817c478bd9Sstevel@tonic-gate static bool privopen; /* don't lock, open device as root */ 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n"; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate GIDSET_TYPE groups[NGROUPS_MAX];/* groups the user is in */ 1867c478bd9Sstevel@tonic-gate int ngroups; /* How many groups valid in groups */ 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate static struct timeval start_time; /* Time when link was started. */ 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate struct pppd_stats link_stats; 1917c478bd9Sstevel@tonic-gate int link_connect_time; 1927c478bd9Sstevel@tonic-gate bool link_stats_valid; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate static pid_t charshunt_pid; /* Process ID for charshunt */ 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate extern option_t general_options[]; 1977c478bd9Sstevel@tonic-gate extern option_t auth_options[]; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate /* 2007c478bd9Sstevel@tonic-gate * We maintain a list of child process pids and 2017c478bd9Sstevel@tonic-gate * functions to call when they exit. 2027c478bd9Sstevel@tonic-gate */ 2037c478bd9Sstevel@tonic-gate struct subprocess { 2047c478bd9Sstevel@tonic-gate pid_t pid; 2057c478bd9Sstevel@tonic-gate char *prog; 2067c478bd9Sstevel@tonic-gate void (*done) __P((void *, int)); 2077c478bd9Sstevel@tonic-gate void *arg; 2087c478bd9Sstevel@tonic-gate struct subprocess *next; 2097c478bd9Sstevel@tonic-gate }; 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate static struct subprocess *children; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* Prototypes for procedures local to this file. */ 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate static void setup_signals __P((void)); 2167c478bd9Sstevel@tonic-gate static void create_pidfile __P((void)); 2177c478bd9Sstevel@tonic-gate static void create_linkpidfile __P((void)); 2187c478bd9Sstevel@tonic-gate static void cleanup __P((void)); 2197c478bd9Sstevel@tonic-gate static void close_tty __P((void)); 2207c478bd9Sstevel@tonic-gate static void get_input __P((void)); 2217c478bd9Sstevel@tonic-gate static void calltimeout __P((void)); 2227c478bd9Sstevel@tonic-gate static struct timeval *timeleft __P((struct timeval *)); 2237c478bd9Sstevel@tonic-gate static void kill_my_pg __P((int)); 2247c478bd9Sstevel@tonic-gate static void hup __P((int)); 2257c478bd9Sstevel@tonic-gate static void term __P((int)); 2267c478bd9Sstevel@tonic-gate static void chld __P((int)); 2277c478bd9Sstevel@tonic-gate static void toggle_debug __P((int)); 2287c478bd9Sstevel@tonic-gate static void open_ccp __P((int)); 2297c478bd9Sstevel@tonic-gate static void bad_signal __P((int)); 2307c478bd9Sstevel@tonic-gate static void holdoff_end __P((void *)); 2317c478bd9Sstevel@tonic-gate static int device_script __P((char *, int, int, int, char *)); 2327c478bd9Sstevel@tonic-gate static int reap_kids __P((int waitfor)); 2337c478bd9Sstevel@tonic-gate static void record_child __P((pid_t, char *, void (*) (void *, int), void *)); 2347c478bd9Sstevel@tonic-gate static int open_socket __P((char *)); 2357c478bd9Sstevel@tonic-gate static int start_charshunt __P((int, int)); 2367c478bd9Sstevel@tonic-gate static void charshunt_done __P((void *, int)); 2377c478bd9Sstevel@tonic-gate static void charshunt __P((int, int, char *)); 2387c478bd9Sstevel@tonic-gate static int record_write __P((FILE *, int code, u_char *buf, int nb, 2397c478bd9Sstevel@tonic-gate struct timeval *)); 2407c478bd9Sstevel@tonic-gate static void final_reap __P((void)); 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 2437c478bd9Sstevel@tonic-gate static void update_db_entry __P((void)); 2447c478bd9Sstevel@tonic-gate static void add_db_key __P((const char *)); 2457c478bd9Sstevel@tonic-gate static void delete_db_key __P((const char *)); 2467c478bd9Sstevel@tonic-gate static void cleanup_db __P((void)); 2477c478bd9Sstevel@tonic-gate #endif 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate int main __P((int, char *[])); 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate #ifdef ultrix 2527c478bd9Sstevel@tonic-gate #undef O_NONBLOCK 2537c478bd9Sstevel@tonic-gate #define O_NONBLOCK O_NDELAY 2547c478bd9Sstevel@tonic-gate #endif 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate #ifdef ULTRIX 2577c478bd9Sstevel@tonic-gate #define setlogmask(x) 0 2587c478bd9Sstevel@tonic-gate #endif 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate /* Backward compatibility for Linux */ 2617c478bd9Sstevel@tonic-gate #ifndef RECMARK_TIMESTART 2627c478bd9Sstevel@tonic-gate #define RECMARK_STARTSEND 1 2637c478bd9Sstevel@tonic-gate #define RECMARK_STARTRECV 2 2647c478bd9Sstevel@tonic-gate #define RECMARK_ENDSEND 3 2657c478bd9Sstevel@tonic-gate #define RECMARK_ENDRECV 4 2667c478bd9Sstevel@tonic-gate #define RECMARK_TIMEDELTA32 5 2677c478bd9Sstevel@tonic-gate #define RECMARK_TIMEDELTA8 6 2687c478bd9Sstevel@tonic-gate #define RECMARK_TIMESTART 7 2697c478bd9Sstevel@tonic-gate #endif 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate /* 2727c478bd9Sstevel@tonic-gate * PPP Data Link Layer "protocol" table. 2737c478bd9Sstevel@tonic-gate * One entry per supported protocol. 2747c478bd9Sstevel@tonic-gate * The last entry must be NULL. 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate struct protent *protocols[] = { 2777c478bd9Sstevel@tonic-gate &lcp_protent, 2787c478bd9Sstevel@tonic-gate &pap_protent, 2797c478bd9Sstevel@tonic-gate &chap_protent, 2807c478bd9Sstevel@tonic-gate #ifdef CBCP_SUPPORT 2817c478bd9Sstevel@tonic-gate &cbcp_protent, 2827c478bd9Sstevel@tonic-gate #endif 2837c478bd9Sstevel@tonic-gate &ipcp_protent, 2847c478bd9Sstevel@tonic-gate #ifdef INET6 2857c478bd9Sstevel@tonic-gate &ipv6cp_protent, 2867c478bd9Sstevel@tonic-gate #endif 2877c478bd9Sstevel@tonic-gate &ccp_protent, 2887c478bd9Sstevel@tonic-gate #ifdef IPX_CHANGE 2897c478bd9Sstevel@tonic-gate &ipxcp_protent, 2907c478bd9Sstevel@tonic-gate #endif 2917c478bd9Sstevel@tonic-gate #ifdef AT_CHANGE 2927c478bd9Sstevel@tonic-gate &atcp_protent, 2937c478bd9Sstevel@tonic-gate #endif 2947c478bd9Sstevel@tonic-gate NULL 2957c478bd9Sstevel@tonic-gate }; 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate int 2987c478bd9Sstevel@tonic-gate main(argc, argv) 2997c478bd9Sstevel@tonic-gate int argc; 3007c478bd9Sstevel@tonic-gate char *argv[]; 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate int i, fdflags, t; 3037c478bd9Sstevel@tonic-gate char *p, *connector; 3047c478bd9Sstevel@tonic-gate struct passwd *pw; 3057c478bd9Sstevel@tonic-gate struct timeval timo; 3067c478bd9Sstevel@tonic-gate struct protent *protp; 3077c478bd9Sstevel@tonic-gate struct stat statbuf; 3087c478bd9Sstevel@tonic-gate char numbuf[16]; 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate ifname[0] = '\0'; 3117c478bd9Sstevel@tonic-gate new_phase(PHASE_INITIALIZE); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate /* 3147c478bd9Sstevel@tonic-gate * Ensure that fds 0, 1, 2 are open, to /dev/null if nowhere else. 3157c478bd9Sstevel@tonic-gate * This way we can close 0, 1, 2 in detach() without clobbering 3167c478bd9Sstevel@tonic-gate * a fd that we are using. 3177c478bd9Sstevel@tonic-gate */ 3187c478bd9Sstevel@tonic-gate if ((i = open(_PATH_DEVNULL, O_RDWR)) >= 0) { 3197c478bd9Sstevel@tonic-gate while (0 <= i && i <= 2) 3207c478bd9Sstevel@tonic-gate i = dup(i); 3217c478bd9Sstevel@tonic-gate if (i >= 0) 3227c478bd9Sstevel@tonic-gate (void) close(i); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate script_env = NULL; 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate /* Initialize syslog facilities */ 3287c478bd9Sstevel@tonic-gate reopen_log(); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate if (gethostname(hostname, MAXHOSTNAMELEN+1) < 0 ) { 3317c478bd9Sstevel@tonic-gate option_error("Couldn't get hostname: %m"); 3327c478bd9Sstevel@tonic-gate exit(1); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate hostname[MAXHOSTNAMELEN] = '\0'; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* make sure we don't create world or group writable files. */ 3377c478bd9Sstevel@tonic-gate (void) umask(umask(0777) | 022); 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate uid = getuid(); 3407c478bd9Sstevel@tonic-gate privileged = (uid == 0); 3417c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", uid); 3427c478bd9Sstevel@tonic-gate script_setenv("ORIG_UID", numbuf, 0); 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate ngroups = getgroups(NGROUPS_MAX, groups); 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate /* 3477c478bd9Sstevel@tonic-gate * Initialize magic number generator now so that protocols may 3487c478bd9Sstevel@tonic-gate * use magic numbers in initialization. 3497c478bd9Sstevel@tonic-gate */ 3507c478bd9Sstevel@tonic-gate magic_init(); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate progname = *argv; 3537c478bd9Sstevel@tonic-gate prepass = 0; 3547c478bd9Sstevel@tonic-gate /* 3557c478bd9Sstevel@tonic-gate * Initialize to the standard option set, then parse, in order, the 3567c478bd9Sstevel@tonic-gate * system options file, the user's options file, the tty's options file, 3577c478bd9Sstevel@tonic-gate * and the command line arguments. At last, install the options declared 3587c478bd9Sstevel@tonic-gate * by each protocol into the extra_option list. 3597c478bd9Sstevel@tonic-gate */ 3607c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) { 3617c478bd9Sstevel@tonic-gate (*protp->init)(0); 3627c478bd9Sstevel@tonic-gate if (protp->options != NULL) { 3637c478bd9Sstevel@tonic-gate add_options(protp->options); 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate /* 3687c478bd9Sstevel@tonic-gate * Install "generic" options into the extra_options list. 3697c478bd9Sstevel@tonic-gate */ 3707c478bd9Sstevel@tonic-gate add_options(auth_options); 3717c478bd9Sstevel@tonic-gate add_options(general_options); 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate /* Install any system-specific options (or remove unusable ones) */ 3747c478bd9Sstevel@tonic-gate sys_options(); 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1) 3777c478bd9Sstevel@tonic-gate || !options_from_user()) 3787c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate /* scan command line and options files to find device name */ 3817c478bd9Sstevel@tonic-gate prepass = 1; 3827c478bd9Sstevel@tonic-gate (void) parse_args(argc-1, argv+1); 3837c478bd9Sstevel@tonic-gate prepass = 0; 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Work out the device name, if it hasn't already been specified. 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate using_pty = notty || ptycommand != NULL || pty_socket != NULL; 3897c478bd9Sstevel@tonic-gate if (!using_pty && default_device && !direct_tty) { 3907c478bd9Sstevel@tonic-gate char *p; 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate if (!isatty(0) || (p = ttyname(0)) == NULL) { 3937c478bd9Sstevel@tonic-gate option_error("no device specified and stdin is not a tty"); 3947c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate (void) strlcpy(devnam, p, sizeof(devnam)); 3977c478bd9Sstevel@tonic-gate if (stat(devnam, &devstat) < 0) 3987c478bd9Sstevel@tonic-gate fatal("Couldn't stat default device %s: %m", devnam); 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate /* 4027c478bd9Sstevel@tonic-gate * Parse the tty options file and the command line. 4037c478bd9Sstevel@tonic-gate * The per-tty options file should not change 4047c478bd9Sstevel@tonic-gate * ptycommand, pty_socket, notty or devnam. 4057c478bd9Sstevel@tonic-gate */ 4067c478bd9Sstevel@tonic-gate devnam_fixed = 1; 4077c478bd9Sstevel@tonic-gate if (!using_pty && !direct_tty) { 4087c478bd9Sstevel@tonic-gate if (!options_for_tty()) 4097c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate devnam_fixed = 0; 4137c478bd9Sstevel@tonic-gate if (!parse_args(argc-1, argv+1)) 4147c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate /* 4177c478bd9Sstevel@tonic-gate * Check that we are running as root. 4187c478bd9Sstevel@tonic-gate */ 4197c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 4207c478bd9Sstevel@tonic-gate option_error("must be root to run %s, since it is not setuid-root", 4217c478bd9Sstevel@tonic-gate argv[0]); 4227c478bd9Sstevel@tonic-gate exit(EXIT_NOT_ROOT); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate if (!ppp_available()) { 4267c478bd9Sstevel@tonic-gate option_error(no_ppp_msg); 4277c478bd9Sstevel@tonic-gate exit(EXIT_NO_KERNEL_SUPPORT); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * Check that the options given are valid and consistent. 4327c478bd9Sstevel@tonic-gate */ 4337c478bd9Sstevel@tonic-gate if (!sys_check_options()) 4347c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4357c478bd9Sstevel@tonic-gate auth_check_options(); 4367c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 4377c478bd9Sstevel@tonic-gate mp_check_options(); 4387c478bd9Sstevel@tonic-gate #endif 4397c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) 4407c478bd9Sstevel@tonic-gate if (protp->enabled_flag && protp->check_options != NULL) 4417c478bd9Sstevel@tonic-gate (*protp->check_options)(); 4427c478bd9Sstevel@tonic-gate if (demand && (connect_script == NULL)) { 4437c478bd9Sstevel@tonic-gate option_error("connect script is required for demand-dialling\n"); 4447c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate if (updetach && (nodetach || demand)) { 4477c478bd9Sstevel@tonic-gate option_error("updetach cannot be used with %s", 4487c478bd9Sstevel@tonic-gate nodetach ? "nodetach" : "demand"); 4497c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate /* default holdoff to 0 if no connect script has been given */ 4527c478bd9Sstevel@tonic-gate if ((connect_script == NULL) && !holdoff_specified) 4537c478bd9Sstevel@tonic-gate holdoff = 0; 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate if (using_pty || direct_tty) { 4567c478bd9Sstevel@tonic-gate if (!default_device) { 4577c478bd9Sstevel@tonic-gate option_error("%s option precludes specifying device name", 4587c478bd9Sstevel@tonic-gate notty? "notty": "pty"); 4597c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate if (ptycommand != NULL && (notty || direct_tty)) { 4627c478bd9Sstevel@tonic-gate option_error("pty option is incompatible with notty option"); 4637c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate if (pty_socket != NULL && (ptycommand != NULL || notty || 4667c478bd9Sstevel@tonic-gate direct_tty)) { 4677c478bd9Sstevel@tonic-gate option_error("socket option is incompatible with pty and notty"); 4687c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate default_device = notty || direct_tty; 4717c478bd9Sstevel@tonic-gate lockflag = 0; 4727c478bd9Sstevel@tonic-gate modem = 0; 4737c478bd9Sstevel@tonic-gate if (default_device && log_to_fd <= 1) 4747c478bd9Sstevel@tonic-gate log_to_fd = -1; 4757c478bd9Sstevel@tonic-gate } else { 4767c478bd9Sstevel@tonic-gate /* 4777c478bd9Sstevel@tonic-gate * If the user has specified a device which is the same as 4787c478bd9Sstevel@tonic-gate * the one on stdin, pretend they didn't specify any. 4797c478bd9Sstevel@tonic-gate * If the device is already open read/write on stdin, 4807c478bd9Sstevel@tonic-gate * we assume we don't need to lock it, and we can open it as root. 4817c478bd9Sstevel@tonic-gate */ 4827c478bd9Sstevel@tonic-gate if (fstat(0, &statbuf) >= 0 && S_ISCHR(statbuf.st_mode) 4837c478bd9Sstevel@tonic-gate && statbuf.st_rdev == devstat.st_rdev) { 4847c478bd9Sstevel@tonic-gate default_device = 1; 4857c478bd9Sstevel@tonic-gate fdflags = fcntl(0, F_GETFL); 4867c478bd9Sstevel@tonic-gate if (fdflags != -1 && (fdflags & O_ACCMODE) == O_RDWR) 4877c478bd9Sstevel@tonic-gate privopen = 1; 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate if (default_device) 4917c478bd9Sstevel@tonic-gate nodetach = 1; 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate /* 4947c478bd9Sstevel@tonic-gate * Don't send log messages to the serial port, it tends to 4957c478bd9Sstevel@tonic-gate * confuse the peer. :-) 4967c478bd9Sstevel@tonic-gate */ 4977c478bd9Sstevel@tonic-gate if (log_to_fd >= 0 && fstat(log_to_fd, &statbuf) >= 0 4987c478bd9Sstevel@tonic-gate && S_ISCHR(statbuf.st_mode) && statbuf.st_rdev == devstat.st_rdev) 4997c478bd9Sstevel@tonic-gate log_to_fd = -1; 5007c478bd9Sstevel@tonic-gate early_log = 0; 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate if (debug) 5037c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_DEBUG)); 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate /* 5067c478bd9Sstevel@tonic-gate * Initialize system-dependent stuff. 5077c478bd9Sstevel@tonic-gate */ 5087c478bd9Sstevel@tonic-gate if (check_options_hook != NULL && 5097c478bd9Sstevel@tonic-gate (*check_options_hook)(uid) == -1) { 5107c478bd9Sstevel@tonic-gate exit(EXIT_OPTION_ERROR); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate sys_init(!devnam_info.priv && !privopen); 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 5157c478bd9Sstevel@tonic-gate pppdb = tdb_open(_PATH_PPPDB, 0, 0, O_RDWR|O_CREAT, 0644); 5167c478bd9Sstevel@tonic-gate if (pppdb != NULL) { 5177c478bd9Sstevel@tonic-gate (void) slprintf(db_key, sizeof(db_key), "pppd%d", getpid()); 5187c478bd9Sstevel@tonic-gate update_db_entry(); 5197c478bd9Sstevel@tonic-gate } else { 5207c478bd9Sstevel@tonic-gate warn("Warning: couldn't open ppp database %s", _PATH_PPPDB); 5217c478bd9Sstevel@tonic-gate if (multilink) { 5227c478bd9Sstevel@tonic-gate warn("Warning: disabling multilink"); 5237c478bd9Sstevel@tonic-gate multilink = 0; 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate } 5267c478bd9Sstevel@tonic-gate #endif 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate /* 5297c478bd9Sstevel@tonic-gate * Detach ourselves from the terminal, if required, and identify 5307c478bd9Sstevel@tonic-gate * who is running us. Printing to stderr stops here unless 5317c478bd9Sstevel@tonic-gate * nodetach or updetach is set. 5327c478bd9Sstevel@tonic-gate */ 5337c478bd9Sstevel@tonic-gate if (!nodetach && !updetach) 5347c478bd9Sstevel@tonic-gate detach(); 5357c478bd9Sstevel@tonic-gate p = getlogin(); 5367c478bd9Sstevel@tonic-gate if (p == NULL) { 5377c478bd9Sstevel@tonic-gate pw = getpwuid(uid); 5387c478bd9Sstevel@tonic-gate if (pw != NULL && pw->pw_name != NULL) 5397c478bd9Sstevel@tonic-gate p = pw->pw_name; 5407c478bd9Sstevel@tonic-gate else 5417c478bd9Sstevel@tonic-gate p = "(unknown)"; 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate syslog(LOG_NOTICE, "pppd %s.%d%s started by %s, uid %d", 5447c478bd9Sstevel@tonic-gate VERSION, PATCHLEVEL, IMPLEMENTATION, p, uid); 5457c478bd9Sstevel@tonic-gate script_setenv("PPPLOGNAME", p, 0); 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate if (devnam[0] != '\0') 5487c478bd9Sstevel@tonic-gate script_setenv("DEVICE", devnam, 1); 5497c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", getpid()); 5507c478bd9Sstevel@tonic-gate script_setenv("PPPD_PID", numbuf, 1); 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate setup_signals(); 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate waiting = 0; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate create_linkpidfile(); 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate /* 5597c478bd9Sstevel@tonic-gate * If we're doing dial-on-demand, set up the interface now. 5607c478bd9Sstevel@tonic-gate */ 5617c478bd9Sstevel@tonic-gate if (demand) { 5627c478bd9Sstevel@tonic-gate /* 5637c478bd9Sstevel@tonic-gate * Open the loopback channel and set it up to be the ppp interface. 5647c478bd9Sstevel@tonic-gate */ 5657c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 5667c478bd9Sstevel@tonic-gate (void) tdb_writelock(pppdb); 5677c478bd9Sstevel@tonic-gate #endif 5687c478bd9Sstevel@tonic-gate set_ifunit(1); 5697c478bd9Sstevel@tonic-gate fd_loop = open_ppp_loopback(); 5707c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 5717c478bd9Sstevel@tonic-gate (void) tdb_writeunlock(pppdb); 5727c478bd9Sstevel@tonic-gate #endif 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate /* 5757c478bd9Sstevel@tonic-gate * Configure the interface and mark it up, etc. 5767c478bd9Sstevel@tonic-gate */ 5777c478bd9Sstevel@tonic-gate demand_conf(); 5787c478bd9Sstevel@tonic-gate } 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate new_phase(PHASE_INITIALIZED); 5817c478bd9Sstevel@tonic-gate do_callback = 0; 5827c478bd9Sstevel@tonic-gate for (;;) { 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate need_holdoff = 1; 5857c478bd9Sstevel@tonic-gate ttyfd = -1; 5867c478bd9Sstevel@tonic-gate real_ttyfd = -1; 5877c478bd9Sstevel@tonic-gate status = EXIT_OK; 5887c478bd9Sstevel@tonic-gate ++unsuccess; 5897c478bd9Sstevel@tonic-gate doing_callback = do_callback; 5907c478bd9Sstevel@tonic-gate do_callback = 0; 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate if (demand && !doing_callback) { 5937c478bd9Sstevel@tonic-gate /* 5947c478bd9Sstevel@tonic-gate * Don't do anything until we see some activity. 5957c478bd9Sstevel@tonic-gate */ 5967c478bd9Sstevel@tonic-gate kill_link = 0; 5977c478bd9Sstevel@tonic-gate new_phase(PHASE_DORMANT); 5987c478bd9Sstevel@tonic-gate demand_unblock(); 5997c478bd9Sstevel@tonic-gate add_fd(fd_loop); 6007c478bd9Sstevel@tonic-gate for (;;) { 6017c478bd9Sstevel@tonic-gate if (sigsetjmp(sigjmp, 1) == 0) { 6027c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL); 6037c478bd9Sstevel@tonic-gate if (kill_link || got_sigchld) { 6047c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 6057c478bd9Sstevel@tonic-gate } else { 6067c478bd9Sstevel@tonic-gate waiting = 1; 6077c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 6087c478bd9Sstevel@tonic-gate wait_input(timeleft(&timo)); 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate waiting = 0; 6127c478bd9Sstevel@tonic-gate calltimeout(); 6137c478bd9Sstevel@tonic-gate if (kill_link) { 6147c478bd9Sstevel@tonic-gate if (!persist) 6157c478bd9Sstevel@tonic-gate break; 6167c478bd9Sstevel@tonic-gate kill_link = 0; 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate if (get_loop_output()) 6197c478bd9Sstevel@tonic-gate break; 6207c478bd9Sstevel@tonic-gate if (got_sigchld) 6217c478bd9Sstevel@tonic-gate (void) reap_kids(0); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate remove_fd(fd_loop); 6247c478bd9Sstevel@tonic-gate if (kill_link && !persist) 6257c478bd9Sstevel@tonic-gate break; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate /* 6287c478bd9Sstevel@tonic-gate * Now we want to bring up the link. 6297c478bd9Sstevel@tonic-gate */ 6307c478bd9Sstevel@tonic-gate demand_block(); 6317c478bd9Sstevel@tonic-gate info("Starting link"); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate new_phase(doing_callback ? PHASE_CALLINGBACK : PHASE_SERIALCONN); 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate /* 6377c478bd9Sstevel@tonic-gate * Get a pty master/slave pair if the pty, notty, socket, 6387c478bd9Sstevel@tonic-gate * or record options were specified. 6397c478bd9Sstevel@tonic-gate */ 6407c478bd9Sstevel@tonic-gate (void) strlcpy(ppp_devnam, devnam, sizeof(ppp_devnam)); 6417c478bd9Sstevel@tonic-gate pty_master = -1; 6427c478bd9Sstevel@tonic-gate pty_slave = -1; 6437c478bd9Sstevel@tonic-gate if (using_pty || record_file != NULL) { 6447c478bd9Sstevel@tonic-gate if (!get_pty(&pty_master, &pty_slave, ppp_devnam, uid)) { 6457c478bd9Sstevel@tonic-gate error("Couldn't allocate pseudo-tty"); 6467c478bd9Sstevel@tonic-gate status = EXIT_FATAL_ERROR; 6477c478bd9Sstevel@tonic-gate goto fail; 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate set_up_tty(pty_slave, 1); 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate /* 6537c478bd9Sstevel@tonic-gate * Lock the device if we've been asked to. 6547c478bd9Sstevel@tonic-gate */ 6557c478bd9Sstevel@tonic-gate status = EXIT_LOCK_FAILED; 6567c478bd9Sstevel@tonic-gate if (lockflag && !privopen && !direct_tty) { 6577c478bd9Sstevel@tonic-gate if (lock(devnam) < 0) 6587c478bd9Sstevel@tonic-gate goto fail; 6597c478bd9Sstevel@tonic-gate locked = 1; 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate /* 6637c478bd9Sstevel@tonic-gate * Open the serial device and set it up to be the ppp interface. 6647c478bd9Sstevel@tonic-gate * First we open it in non-blocking mode so we can set the 6657c478bd9Sstevel@tonic-gate * various termios flags appropriately. If we aren't dialling 6667c478bd9Sstevel@tonic-gate * out and we want to use the modem lines, we reopen it later 6677c478bd9Sstevel@tonic-gate * in order to wait for the carrier detect signal from the modem. 6687c478bd9Sstevel@tonic-gate */ 6697c478bd9Sstevel@tonic-gate hungup = 0; 6707c478bd9Sstevel@tonic-gate kill_link = 0; 6717c478bd9Sstevel@tonic-gate connector = doing_callback? callback_script: connect_script; 6727c478bd9Sstevel@tonic-gate if (direct_tty) { 6737c478bd9Sstevel@tonic-gate ttyfd = 0; 6747c478bd9Sstevel@tonic-gate } else if (devnam[0] != '\0') { 6757c478bd9Sstevel@tonic-gate for (;;) { 6767c478bd9Sstevel@tonic-gate /* If the user specified the device name, become the 6777c478bd9Sstevel@tonic-gate user before opening it. */ 6787c478bd9Sstevel@tonic-gate int err; 6797c478bd9Sstevel@tonic-gate if (!devnam_info.priv && !privopen) 6807c478bd9Sstevel@tonic-gate (void) seteuid(uid); 6817c478bd9Sstevel@tonic-gate if ((ttyfd = sys_extra_fd()) < 0) 6827c478bd9Sstevel@tonic-gate ttyfd = open(devnam, O_NONBLOCK | O_RDWR); 6837c478bd9Sstevel@tonic-gate err = errno; 6847c478bd9Sstevel@tonic-gate if (!devnam_info.priv && !privopen) 6857c478bd9Sstevel@tonic-gate (void) seteuid(0); 6867c478bd9Sstevel@tonic-gate if (ttyfd >= 0) 6877c478bd9Sstevel@tonic-gate break; 6887c478bd9Sstevel@tonic-gate errno = err; 6897c478bd9Sstevel@tonic-gate if (err != EINTR) { 6907c478bd9Sstevel@tonic-gate error("Failed to open %s: %m", devnam); 6917c478bd9Sstevel@tonic-gate status = EXIT_OPEN_FAILED; 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate if (!persist || err != EINTR) 6947c478bd9Sstevel@tonic-gate goto fail; 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 6977c478bd9Sstevel@tonic-gate || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) 6987c478bd9Sstevel@tonic-gate warn("Couldn't reset non-blocking mode on device: %m"); 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate /* 7017c478bd9Sstevel@tonic-gate * Do the equivalent of `mesg n' to stop broadcast messages. 7027c478bd9Sstevel@tonic-gate */ 7037c478bd9Sstevel@tonic-gate if (fstat(ttyfd, &statbuf) < 0 7047c478bd9Sstevel@tonic-gate || fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) { 7057c478bd9Sstevel@tonic-gate warn("Couldn't restrict write permissions to %s: %m", devnam); 7067c478bd9Sstevel@tonic-gate } else 7077c478bd9Sstevel@tonic-gate tty_mode = statbuf.st_mode; 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* 7107c478bd9Sstevel@tonic-gate * Set line speed, flow control, etc. 7117c478bd9Sstevel@tonic-gate * If we have a non-null connection or initializer script, 7127c478bd9Sstevel@tonic-gate * on most systems we set CLOCAL for now so that we can talk 7137c478bd9Sstevel@tonic-gate * to the modem before carrier comes up. But this has the 7147c478bd9Sstevel@tonic-gate * side effect that we might miss it if CD drops before we 7157c478bd9Sstevel@tonic-gate * get to clear CLOCAL below. On systems where we can talk 7167c478bd9Sstevel@tonic-gate * successfully to the modem with CLOCAL clear and CD down, 7177c478bd9Sstevel@tonic-gate * we could clear CLOCAL at this point. 7187c478bd9Sstevel@tonic-gate */ 7197c478bd9Sstevel@tonic-gate set_up_tty(ttyfd, ((connector != NULL && connector[0] != '\0') 7207c478bd9Sstevel@tonic-gate || initializer != NULL)); 7217c478bd9Sstevel@tonic-gate real_ttyfd = ttyfd; 7227c478bd9Sstevel@tonic-gate } 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate /* 7257c478bd9Sstevel@tonic-gate * If the pty, socket, notty and/or record option was specified, 7267c478bd9Sstevel@tonic-gate * start up the character shunt now. 7277c478bd9Sstevel@tonic-gate */ 7287c478bd9Sstevel@tonic-gate status = EXIT_PTYCMD_FAILED; 7297c478bd9Sstevel@tonic-gate if (ptycommand != NULL) { 7307c478bd9Sstevel@tonic-gate if (record_file != NULL) { 7317c478bd9Sstevel@tonic-gate int ipipe[2], opipe[2], ok; 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate if (pipe(ipipe) < 0 || pipe(opipe) < 0) 7347c478bd9Sstevel@tonic-gate fatal("Couldn't create pipes for record option: %m"); 7357c478bd9Sstevel@tonic-gate dbglog("starting charshunt for pty option"); 7367c478bd9Sstevel@tonic-gate ok = device_script(ptycommand, opipe[0], ipipe[1], 1, 7377c478bd9Sstevel@tonic-gate "record") == 0 && start_charshunt(ipipe[0], opipe[1]); 7387c478bd9Sstevel@tonic-gate (void) close(ipipe[0]); 7397c478bd9Sstevel@tonic-gate (void) close(ipipe[1]); 7407c478bd9Sstevel@tonic-gate (void) close(opipe[0]); 7417c478bd9Sstevel@tonic-gate (void) close(opipe[1]); 7427c478bd9Sstevel@tonic-gate if (!ok) 7437c478bd9Sstevel@tonic-gate goto fail; 7447c478bd9Sstevel@tonic-gate } else { 7457c478bd9Sstevel@tonic-gate if (device_script(ptycommand, pty_master, pty_master, 1, 7467c478bd9Sstevel@tonic-gate "pty") < 0) 7477c478bd9Sstevel@tonic-gate goto fail; 7487c478bd9Sstevel@tonic-gate ttyfd = pty_slave; 7497c478bd9Sstevel@tonic-gate (void) close(pty_master); 7507c478bd9Sstevel@tonic-gate pty_master = -1; 7517c478bd9Sstevel@tonic-gate } 7527c478bd9Sstevel@tonic-gate } else if (pty_socket != NULL) { 7537c478bd9Sstevel@tonic-gate int fd = open_socket(pty_socket); 7547c478bd9Sstevel@tonic-gate if (fd < 0) 7557c478bd9Sstevel@tonic-gate goto fail; 7567c478bd9Sstevel@tonic-gate dbglog("starting charshunt for socket option"); 7577c478bd9Sstevel@tonic-gate if (!start_charshunt(fd, fd)) 7587c478bd9Sstevel@tonic-gate goto fail; 7597c478bd9Sstevel@tonic-gate } else if (notty) { 7607c478bd9Sstevel@tonic-gate dbglog("starting charshunt for notty option"); 7617c478bd9Sstevel@tonic-gate if (!start_charshunt(0, 1)) 7627c478bd9Sstevel@tonic-gate goto fail; 7637c478bd9Sstevel@tonic-gate } else if (record_file != NULL) { 7647c478bd9Sstevel@tonic-gate dbglog("starting charshunt for record option"); 7657c478bd9Sstevel@tonic-gate if (!start_charshunt(ttyfd, ttyfd)) 7667c478bd9Sstevel@tonic-gate goto fail; 7677c478bd9Sstevel@tonic-gate } 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate /* run connection script */ 7707c478bd9Sstevel@tonic-gate if (((connector != NULL) && (connector[0] != '\0')) || initializer) { 7717c478bd9Sstevel@tonic-gate if (real_ttyfd != -1) { 7727c478bd9Sstevel@tonic-gate /* XXX do this if doing_callback == CALLBACK_DIALIN? */ 7737c478bd9Sstevel@tonic-gate if (!default_device && modem && !direct_tty) { 7747c478bd9Sstevel@tonic-gate setdtr(real_ttyfd, 0); /* in case modem is off hook */ 7757c478bd9Sstevel@tonic-gate (void) sleep(1); 7767c478bd9Sstevel@tonic-gate setdtr(real_ttyfd, 1); 7777c478bd9Sstevel@tonic-gate } 7787c478bd9Sstevel@tonic-gate } 7797c478bd9Sstevel@tonic-gate 7807c478bd9Sstevel@tonic-gate if ((initializer != NULL) && (initializer[0] != '\0')) { 7817c478bd9Sstevel@tonic-gate if (device_script(initializer, ttyfd, ttyfd, 0, "init") < 0) { 7827c478bd9Sstevel@tonic-gate error("Initializer script failed"); 7837c478bd9Sstevel@tonic-gate status = EXIT_INIT_FAILED; 7847c478bd9Sstevel@tonic-gate goto fail; 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate if (kill_link) 7877c478bd9Sstevel@tonic-gate goto disconnect; 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate info("Serial port initialized."); 7907c478bd9Sstevel@tonic-gate } 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate if ((connector != NULL) && (connector[0] != '\0')) { 7937c478bd9Sstevel@tonic-gate if (device_script(connector, ttyfd, ttyfd, 0, "connect") < 0) { 7947c478bd9Sstevel@tonic-gate error("Connect script failed"); 7957c478bd9Sstevel@tonic-gate status = EXIT_CONNECT_FAILED; 7967c478bd9Sstevel@tonic-gate goto fail; 7977c478bd9Sstevel@tonic-gate } 7987c478bd9Sstevel@tonic-gate if (kill_link) 7997c478bd9Sstevel@tonic-gate goto disconnect; 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate info("Serial connection established."); 8027c478bd9Sstevel@tonic-gate } 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate /* 8057c478bd9Sstevel@tonic-gate * Clear CLOCAL if modem option -- we now have carrier 8067c478bd9Sstevel@tonic-gate * established, and we should respect loss of carrier. 8077c478bd9Sstevel@tonic-gate */ 8087c478bd9Sstevel@tonic-gate if (real_ttyfd != -1) 8097c478bd9Sstevel@tonic-gate set_up_tty(real_ttyfd, 0); 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate if (doing_callback == CALLBACK_DIALIN) 8127c478bd9Sstevel@tonic-gate connector = NULL; 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate /* reopen tty if necessary to wait for carrier */ 8167c478bd9Sstevel@tonic-gate if (connector == NULL && modem && devnam[0] != '\0' && !direct_tty) { 8177c478bd9Sstevel@tonic-gate for (;;) { 8187c478bd9Sstevel@tonic-gate if ((i = open(devnam, O_RDWR)) >= 0) 8197c478bd9Sstevel@tonic-gate break; 8207c478bd9Sstevel@tonic-gate if (errno != EINTR) { 8217c478bd9Sstevel@tonic-gate error("Failed to reopen %s: %m", devnam); 8227c478bd9Sstevel@tonic-gate status = EXIT_OPEN_FAILED; 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate if (!persist || errno != EINTR || hungup || kill_link) 8257c478bd9Sstevel@tonic-gate goto fail; 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate (void) close(i); 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", baud_rate); 8317c478bd9Sstevel@tonic-gate script_setenv("SPEED", numbuf, 0); 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate /* run welcome script, if any */ 8347c478bd9Sstevel@tonic-gate if ((welcomer != NULL) && (welcomer[0] != '\0')) { 8357c478bd9Sstevel@tonic-gate if (device_script(welcomer, ttyfd, ttyfd, 0, "welcome") < 0) 8367c478bd9Sstevel@tonic-gate warn("Welcome script failed"); 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate /* set up the serial device as a ppp interface */ 8407c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 8417c478bd9Sstevel@tonic-gate (void) tdb_writelock(pppdb); 8427c478bd9Sstevel@tonic-gate #endif 8437c478bd9Sstevel@tonic-gate fd_ppp = establish_ppp(ttyfd); 8447c478bd9Sstevel@tonic-gate if (fd_ppp < 0) { 8457c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 8467c478bd9Sstevel@tonic-gate (void) tdb_writeunlock(pppdb); 8477c478bd9Sstevel@tonic-gate #endif 8487c478bd9Sstevel@tonic-gate status = EXIT_FATAL_ERROR; 8497c478bd9Sstevel@tonic-gate goto disconnect; 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate 8527c478bd9Sstevel@tonic-gate if (!demand && ifunit >= 0) 8537c478bd9Sstevel@tonic-gate set_ifunit(1); 8547c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 8557c478bd9Sstevel@tonic-gate (void) tdb_writeunlock(pppdb); 8567c478bd9Sstevel@tonic-gate #endif 8577c478bd9Sstevel@tonic-gate 8587c478bd9Sstevel@tonic-gate /* 8597c478bd9Sstevel@tonic-gate * Start opening the connection and wait for 8607c478bd9Sstevel@tonic-gate * incoming events (reply, timeout, etc.). 8617c478bd9Sstevel@tonic-gate */ 8627c478bd9Sstevel@tonic-gate notice("Connect: %s <--> %s", ifname, ppp_devnam); 8637c478bd9Sstevel@tonic-gate (void) gettimeofday(&start_time, NULL); 8647c478bd9Sstevel@tonic-gate link_stats_valid = 0; 8657c478bd9Sstevel@tonic-gate script_unsetenv("CONNECT_TIME"); 8667c478bd9Sstevel@tonic-gate script_unsetenv("BYTES_SENT"); 8677c478bd9Sstevel@tonic-gate script_unsetenv("BYTES_RCVD"); 8687c478bd9Sstevel@tonic-gate lcp_lowerup(0); 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* Mostly for accounting purposes */ 8717c478bd9Sstevel@tonic-gate new_phase(PHASE_CONNECTED); 8727c478bd9Sstevel@tonic-gate 8737c478bd9Sstevel@tonic-gate /* 8747c478bd9Sstevel@tonic-gate * If we are initiating this connection, wait for a short 8757c478bd9Sstevel@tonic-gate * time for something from the peer. This can avoid bouncing 876*48bbca81SDaniel Hoffman * our packets off its tty before it has set up the tty. 8777c478bd9Sstevel@tonic-gate */ 8787c478bd9Sstevel@tonic-gate add_fd(fd_ppp); 8797c478bd9Sstevel@tonic-gate if (connect_delay != 0 && (connector != NULL || ptycommand != NULL)) { 8807c478bd9Sstevel@tonic-gate struct timeval t; 8817c478bd9Sstevel@tonic-gate t.tv_sec = connect_delay / 1000; 8827c478bd9Sstevel@tonic-gate t.tv_usec = connect_delay % 1000; 8837c478bd9Sstevel@tonic-gate wait_input(&t); 8847c478bd9Sstevel@tonic-gate } 8857c478bd9Sstevel@tonic-gate 8867c478bd9Sstevel@tonic-gate lcp_open(0); /* Start protocol */ 8877c478bd9Sstevel@tonic-gate open_ccp_flag = 0; 8887c478bd9Sstevel@tonic-gate status = EXIT_NEGOTIATION_FAILED; 8897c478bd9Sstevel@tonic-gate new_phase(PHASE_ESTABLISH); 8907c478bd9Sstevel@tonic-gate while (phase != PHASE_DEAD) { 8917c478bd9Sstevel@tonic-gate if (sigsetjmp(sigjmp, 1) == 0) { 8927c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL); 8937c478bd9Sstevel@tonic-gate if (kill_link || open_ccp_flag || got_sigchld) { 8947c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 8957c478bd9Sstevel@tonic-gate } else { 8967c478bd9Sstevel@tonic-gate waiting = 1; 8977c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 8987c478bd9Sstevel@tonic-gate wait_input(timeleft(&timo)); 8997c478bd9Sstevel@tonic-gate } 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate waiting = 0; 9027c478bd9Sstevel@tonic-gate calltimeout(); 9037c478bd9Sstevel@tonic-gate get_input(); 9047c478bd9Sstevel@tonic-gate if (kill_link) { 9057c478bd9Sstevel@tonic-gate lcp_close(0, "User request"); 9067c478bd9Sstevel@tonic-gate kill_link = 0; 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate if (open_ccp_flag) { 9097c478bd9Sstevel@tonic-gate if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) { 9107c478bd9Sstevel@tonic-gate /* Uncloak ourselves. */ 9117c478bd9Sstevel@tonic-gate ccp_fsm[0].flags &= ~OPT_SILENT; 9127c478bd9Sstevel@tonic-gate (*ccp_protent.open)(0); 9137c478bd9Sstevel@tonic-gate } 9147c478bd9Sstevel@tonic-gate open_ccp_flag = 0; 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate if (got_sigchld) 9177c478bd9Sstevel@tonic-gate (void) reap_kids(0); /* Don't leave dead kids lying around */ 9187c478bd9Sstevel@tonic-gate } 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate /* 9217c478bd9Sstevel@tonic-gate * Print connect time and statistics. 9227c478bd9Sstevel@tonic-gate */ 9237c478bd9Sstevel@tonic-gate if (link_stats_valid) { 9247c478bd9Sstevel@tonic-gate int t = (link_connect_time + 5) / 6; /* 1/10ths of minutes */ 9257c478bd9Sstevel@tonic-gate info("Connect time %d.%d minutes.", t/10, t%10); 9267c478bd9Sstevel@tonic-gate info("Sent %" PPP_COUNTER_F " bytes (%" PPP_COUNTER_F 9277c478bd9Sstevel@tonic-gate " packets), received %" PPP_COUNTER_F " bytes (%" PPP_COUNTER_F 9287c478bd9Sstevel@tonic-gate " packets).", 9297c478bd9Sstevel@tonic-gate link_stats.bytes_out, link_stats.pkts_out, 9307c478bd9Sstevel@tonic-gate link_stats.bytes_in, link_stats.pkts_in); 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate /* 9347c478bd9Sstevel@tonic-gate * Delete pid file before disestablishing ppp. Otherwise it 9357c478bd9Sstevel@tonic-gate * can happen that another pppd gets the same unit and then 9367c478bd9Sstevel@tonic-gate * we delete its pid file. 9377c478bd9Sstevel@tonic-gate */ 9387c478bd9Sstevel@tonic-gate if (!demand) { 9397c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0' 9407c478bd9Sstevel@tonic-gate && unlink(pidfilename) < 0 && errno != ENOENT) 9417c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", pidfilename); 9427c478bd9Sstevel@tonic-gate pidfilename[0] = '\0'; 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate /* 9467c478bd9Sstevel@tonic-gate * If we may want to bring the link up again, transfer 9477c478bd9Sstevel@tonic-gate * the ppp unit back to the loopback. Set the 9487c478bd9Sstevel@tonic-gate * real serial device back to its normal mode of operation. 9497c478bd9Sstevel@tonic-gate */ 9507c478bd9Sstevel@tonic-gate remove_fd(fd_ppp); 9517c478bd9Sstevel@tonic-gate clean_check(); 9527c478bd9Sstevel@tonic-gate if (demand) 9537c478bd9Sstevel@tonic-gate restore_loop(); 9547c478bd9Sstevel@tonic-gate disestablish_ppp(ttyfd); 9557c478bd9Sstevel@tonic-gate fd_ppp = -1; 9567c478bd9Sstevel@tonic-gate if (!hungup) 9577c478bd9Sstevel@tonic-gate lcp_lowerdown(0); 9587c478bd9Sstevel@tonic-gate if (!demand) 9597c478bd9Sstevel@tonic-gate script_unsetenv("IFNAME"); 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate /* 9627c478bd9Sstevel@tonic-gate * Run disconnector script, if requested. 9637c478bd9Sstevel@tonic-gate * XXX we may not be able to do this if the line has hung up! 9647c478bd9Sstevel@tonic-gate */ 9657c478bd9Sstevel@tonic-gate disconnect: 9667c478bd9Sstevel@tonic-gate if ((disconnect_script != NULL) && (disconnect_script[0] != '\0') && 9677c478bd9Sstevel@tonic-gate !hungup) { 9687c478bd9Sstevel@tonic-gate new_phase(PHASE_DISCONNECT); 9697c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0) 9707c478bd9Sstevel@tonic-gate set_up_tty(real_ttyfd, 1); 9717c478bd9Sstevel@tonic-gate if (device_script(disconnect_script, ttyfd, ttyfd, 0, 9727c478bd9Sstevel@tonic-gate "disconnect") < 0) { 9737c478bd9Sstevel@tonic-gate warn("disconnect script failed"); 9747c478bd9Sstevel@tonic-gate } else { 9757c478bd9Sstevel@tonic-gate info("Serial link disconnected."); 9767c478bd9Sstevel@tonic-gate } 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate fail: 9807c478bd9Sstevel@tonic-gate if (pty_master >= 0) 9817c478bd9Sstevel@tonic-gate (void) close(pty_master); 9827c478bd9Sstevel@tonic-gate if (pty_slave >= 0) { 9837c478bd9Sstevel@tonic-gate (void) close(pty_slave); 9847c478bd9Sstevel@tonic-gate pty_slave = -1; 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0) 9877c478bd9Sstevel@tonic-gate close_tty(); 9887c478bd9Sstevel@tonic-gate if (locked) { 9897c478bd9Sstevel@tonic-gate locked = 0; 9907c478bd9Sstevel@tonic-gate unlock(); 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate if (!demand) { 9947c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0' 9957c478bd9Sstevel@tonic-gate && unlink(pidfilename) < 0 && errno != ENOENT) 9967c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", pidfilename); 9977c478bd9Sstevel@tonic-gate pidfilename[0] = '\0'; 9987c478bd9Sstevel@tonic-gate } 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate if (!persist || (maxfail > 0 && unsuccess >= maxfail)) 10017c478bd9Sstevel@tonic-gate break; 10027c478bd9Sstevel@tonic-gate 10037c478bd9Sstevel@tonic-gate kill_link = 0; 10047c478bd9Sstevel@tonic-gate if (demand) 10057c478bd9Sstevel@tonic-gate demand_discard(); 10067c478bd9Sstevel@tonic-gate t = need_holdoff? holdoff: 0; 10077c478bd9Sstevel@tonic-gate if (holdoff_hook != NULL) 10087c478bd9Sstevel@tonic-gate t = (*holdoff_hook)(); 10097c478bd9Sstevel@tonic-gate if (t > 0) { 10107c478bd9Sstevel@tonic-gate new_phase(PHASE_HOLDOFF); 10117c478bd9Sstevel@tonic-gate TIMEOUT(holdoff_end, NULL, t); 10127c478bd9Sstevel@tonic-gate do { 10137c478bd9Sstevel@tonic-gate if (sigsetjmp(sigjmp, 1) == 0) { 10147c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &main_sigmask, NULL); 10157c478bd9Sstevel@tonic-gate if (kill_link || got_sigchld) { 10167c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 10177c478bd9Sstevel@tonic-gate } else { 10187c478bd9Sstevel@tonic-gate waiting = 1; 10197c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &main_sigmask, NULL); 10207c478bd9Sstevel@tonic-gate wait_input(timeleft(&timo)); 10217c478bd9Sstevel@tonic-gate } 10227c478bd9Sstevel@tonic-gate } 10237c478bd9Sstevel@tonic-gate waiting = 0; 10247c478bd9Sstevel@tonic-gate calltimeout(); 10257c478bd9Sstevel@tonic-gate if (kill_link) { 10267c478bd9Sstevel@tonic-gate kill_link = 0; 10277c478bd9Sstevel@tonic-gate new_phase(PHASE_DORMANT); /* allow signal to end holdoff */ 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate if (got_sigchld) 10307c478bd9Sstevel@tonic-gate (void) reap_kids(0); 10317c478bd9Sstevel@tonic-gate } while (phase == PHASE_HOLDOFF); 10327c478bd9Sstevel@tonic-gate if (!persist) 10337c478bd9Sstevel@tonic-gate break; 10347c478bd9Sstevel@tonic-gate } 10357c478bd9Sstevel@tonic-gate } 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate /* Wait for scripts to finish */ 10387c478bd9Sstevel@tonic-gate final_reap(); 10397c478bd9Sstevel@tonic-gate 10407c478bd9Sstevel@tonic-gate die(status); 10417c478bd9Sstevel@tonic-gate return (0); 10427c478bd9Sstevel@tonic-gate } 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate /* 10457c478bd9Sstevel@tonic-gate * setup_signals - initialize signal handling. 10467c478bd9Sstevel@tonic-gate */ 10477c478bd9Sstevel@tonic-gate static void 10487c478bd9Sstevel@tonic-gate setup_signals() 10497c478bd9Sstevel@tonic-gate { 10507c478bd9Sstevel@tonic-gate struct sigaction sa; 10517c478bd9Sstevel@tonic-gate 10527c478bd9Sstevel@tonic-gate /* 10537c478bd9Sstevel@tonic-gate * Compute mask of all interesting signals and install signal handlers 10547c478bd9Sstevel@tonic-gate * for each. Only one signal handler may be active at a time. Therefore, 10557c478bd9Sstevel@tonic-gate * all other signals should be masked when any handler is executing. 10567c478bd9Sstevel@tonic-gate */ 10577c478bd9Sstevel@tonic-gate (void) sigemptyset(&main_sigmask); 10587c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGHUP); 10597c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGINT); 10607c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGTERM); 10617c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGCHLD); 10627c478bd9Sstevel@tonic-gate (void) sigaddset(&main_sigmask, SIGUSR2); 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate #define SIGNAL(s, handler) if (1) { \ 10657c478bd9Sstevel@tonic-gate sa.sa_handler = handler; \ 10667c478bd9Sstevel@tonic-gate if (sigaction(s, &sa, NULL) < 0) \ 10677c478bd9Sstevel@tonic-gate fatal("Couldn't establish signal handler (%d): %m", s); \ 10687c478bd9Sstevel@tonic-gate } else ((void)0) 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate sa.sa_mask = main_sigmask; 10717c478bd9Sstevel@tonic-gate sa.sa_flags = 0; 10727c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGHUP, hup); /* Hangup */ 10737c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGINT, term); /* Interrupt */ 10747c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGTERM, term); /* Terminate */ 10757c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGCHLD, chld); 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGUSR1, toggle_debug); /* Toggle debug flag */ 10787c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGUSR2, open_ccp); /* Reopen CCP */ 10797c478bd9Sstevel@tonic-gate 10807c478bd9Sstevel@tonic-gate /* 10817c478bd9Sstevel@tonic-gate * Install a handler for other signals which would otherwise 10827c478bd9Sstevel@tonic-gate * cause pppd to exit without cleaning up. 10837c478bd9Sstevel@tonic-gate */ 10847c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGALRM, bad_signal); 1085f53eecf5SJames Carlson /*CONSTANTCONDITION*/ SIGNAL(SIGQUIT, bad_signal); 1086f53eecf5SJames Carlson 1087f53eecf5SJames Carlson /* Do not hook any of these signals on Solaris; allow core dump instead */ 1088f53eecf5SJames Carlson #ifndef SOL2 1089f53eecf5SJames Carlson /*CONSTANTCONDITION*/ SIGNAL(SIGABRT, bad_signal); 10907c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGFPE, bad_signal); 10917c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGILL, bad_signal); 10927c478bd9Sstevel@tonic-gate #ifndef DEBUG 10937c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGSEGV, bad_signal); 10947c478bd9Sstevel@tonic-gate #endif 10957c478bd9Sstevel@tonic-gate #ifdef SIGBUS 10967c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGBUS, bad_signal); 10977c478bd9Sstevel@tonic-gate #endif 10987c478bd9Sstevel@tonic-gate #ifdef SIGEMT 10997c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGEMT, bad_signal); 11007c478bd9Sstevel@tonic-gate #endif 11017c478bd9Sstevel@tonic-gate #ifdef SIGPOLL 11027c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGPOLL, bad_signal); 11037c478bd9Sstevel@tonic-gate #endif 11047c478bd9Sstevel@tonic-gate #ifdef SIGPROF 11057c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGPROF, bad_signal); 11067c478bd9Sstevel@tonic-gate #endif 11077c478bd9Sstevel@tonic-gate #ifdef SIGSYS 11087c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGSYS, bad_signal); 11097c478bd9Sstevel@tonic-gate #endif 11107c478bd9Sstevel@tonic-gate #ifdef SIGTRAP 11117c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGTRAP, bad_signal); 11127c478bd9Sstevel@tonic-gate #endif 11137c478bd9Sstevel@tonic-gate #ifdef SIGVTALRM 11147c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGVTALRM, bad_signal); 11157c478bd9Sstevel@tonic-gate #endif 11167c478bd9Sstevel@tonic-gate #ifdef SIGXCPU 11177c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGXCPU, bad_signal); 11187c478bd9Sstevel@tonic-gate #endif 11197c478bd9Sstevel@tonic-gate #ifdef SIGXFSZ 11207c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGXFSZ, bad_signal); 1121f53eecf5SJames Carlson #endif 11227c478bd9Sstevel@tonic-gate #endif 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate /* 11257c478bd9Sstevel@tonic-gate * Apparently we can get a SIGPIPE when we call syslog, if 11267c478bd9Sstevel@tonic-gate * syslogd has died and been restarted. Ignoring it seems 11277c478bd9Sstevel@tonic-gate * be sufficient. 11287c478bd9Sstevel@tonic-gate */ 11297c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_IGN); 11307c478bd9Sstevel@tonic-gate } 11317c478bd9Sstevel@tonic-gate 11327c478bd9Sstevel@tonic-gate /* 11337c478bd9Sstevel@tonic-gate * set_ifunit - do things we need to do once we know which ppp 11347c478bd9Sstevel@tonic-gate * unit we are using. 11357c478bd9Sstevel@tonic-gate */ 11367c478bd9Sstevel@tonic-gate void 11377c478bd9Sstevel@tonic-gate set_ifunit(iskey) 11387c478bd9Sstevel@tonic-gate int iskey; 11397c478bd9Sstevel@tonic-gate { 11407c478bd9Sstevel@tonic-gate sys_ifname(); 11417c478bd9Sstevel@tonic-gate info("Using interface %s", ifname); 11427c478bd9Sstevel@tonic-gate script_setenv("IFNAME", ifname, iskey); 11437c478bd9Sstevel@tonic-gate if (iskey) { 11447c478bd9Sstevel@tonic-gate create_pidfile(); /* write pid to file */ 11457c478bd9Sstevel@tonic-gate create_linkpidfile(); 11467c478bd9Sstevel@tonic-gate } 11477c478bd9Sstevel@tonic-gate } 11487c478bd9Sstevel@tonic-gate 11497c478bd9Sstevel@tonic-gate /* 11507c478bd9Sstevel@tonic-gate * detach - detach us from the controlling terminal. 11517c478bd9Sstevel@tonic-gate */ 11527c478bd9Sstevel@tonic-gate void 11537c478bd9Sstevel@tonic-gate detach() 11547c478bd9Sstevel@tonic-gate { 11557c478bd9Sstevel@tonic-gate pid_t pid; 11567c478bd9Sstevel@tonic-gate char numbuf[16]; 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate if (detached) 11597c478bd9Sstevel@tonic-gate return; 11607c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)-1) { 11617c478bd9Sstevel@tonic-gate error("Couldn't detach (fork failed: %m)"); 11627c478bd9Sstevel@tonic-gate die(1); /* or just return? */ 11637c478bd9Sstevel@tonic-gate } 11647c478bd9Sstevel@tonic-gate if (pid != (pid_t)0) { 11657c478bd9Sstevel@tonic-gate /* parent */ 11667c478bd9Sstevel@tonic-gate if (locked) 11677c478bd9Sstevel@tonic-gate (void) relock(pid); 11687c478bd9Sstevel@tonic-gate exit(0); /* parent dies */ 11697c478bd9Sstevel@tonic-gate } 11707c478bd9Sstevel@tonic-gate (void) setsid(); 11717c478bd9Sstevel@tonic-gate /* 11727c478bd9Sstevel@tonic-gate * Fork again to relinquish session leadership. This is needed 11737c478bd9Sstevel@tonic-gate * to prevent the daemon from acquiring controlling terminal. 11747c478bd9Sstevel@tonic-gate */ 11757c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)-1) { 11767c478bd9Sstevel@tonic-gate error("Couldn't detach (second fork failed: %m)"); 11777c478bd9Sstevel@tonic-gate die(1); /* or just return? */ 11787c478bd9Sstevel@tonic-gate } 11797c478bd9Sstevel@tonic-gate if (pid != (pid_t)0) { 11807c478bd9Sstevel@tonic-gate /* parent */ 11817c478bd9Sstevel@tonic-gate if (locked) 11827c478bd9Sstevel@tonic-gate (void) relock(pid); 11837c478bd9Sstevel@tonic-gate exit(0); /* parent dies */ 11847c478bd9Sstevel@tonic-gate } 11857c478bd9Sstevel@tonic-gate (void) chdir("/"); 11867c478bd9Sstevel@tonic-gate (void) close(0); 11877c478bd9Sstevel@tonic-gate (void) close(1); 11887c478bd9Sstevel@tonic-gate (void) close(2); 11897c478bd9Sstevel@tonic-gate detached = 1; 11907c478bd9Sstevel@tonic-gate if (!log_to_file && !log_to_specific_fd) 11917c478bd9Sstevel@tonic-gate log_to_fd = -1; 11927c478bd9Sstevel@tonic-gate /* update pid files if they have been written already */ 11937c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0') 11947c478bd9Sstevel@tonic-gate create_pidfile(); 11957c478bd9Sstevel@tonic-gate if (linkpidfile[0] != '\0') 11967c478bd9Sstevel@tonic-gate create_linkpidfile(); 11977c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", getpid()); 11987c478bd9Sstevel@tonic-gate script_setenv("PPPD_PID", numbuf, 1); 11997c478bd9Sstevel@tonic-gate } 12007c478bd9Sstevel@tonic-gate 12017c478bd9Sstevel@tonic-gate /* 12027c478bd9Sstevel@tonic-gate * reopen_log - (re)open our connection to syslog. 12037c478bd9Sstevel@tonic-gate */ 12047c478bd9Sstevel@tonic-gate void 12057c478bd9Sstevel@tonic-gate reopen_log() 12067c478bd9Sstevel@tonic-gate { 12077c478bd9Sstevel@tonic-gate #ifdef ULTRIX 12087c478bd9Sstevel@tonic-gate openlog("pppd", LOG_PID); 12097c478bd9Sstevel@tonic-gate #else 12107c478bd9Sstevel@tonic-gate openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP); 12117c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_INFO)); 12127c478bd9Sstevel@tonic-gate #endif 12137c478bd9Sstevel@tonic-gate } 12147c478bd9Sstevel@tonic-gate 12157c478bd9Sstevel@tonic-gate /* 12167c478bd9Sstevel@tonic-gate * Create a file containing our process ID. 12177c478bd9Sstevel@tonic-gate */ 12187c478bd9Sstevel@tonic-gate static void 12197c478bd9Sstevel@tonic-gate create_pidfile() 12207c478bd9Sstevel@tonic-gate { 12217c478bd9Sstevel@tonic-gate FILE *pidfile; 12227c478bd9Sstevel@tonic-gate 12237c478bd9Sstevel@tonic-gate (void) slprintf(pidfilename, sizeof(pidfilename), "%s%s.pid", 12247c478bd9Sstevel@tonic-gate _PATH_VARRUN, ifname); 12257c478bd9Sstevel@tonic-gate if ((pidfile = fopen(pidfilename, "w")) != NULL) { 12267c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%u\n", (unsigned)getpid()); 12277c478bd9Sstevel@tonic-gate (void) fclose(pidfile); 12287c478bd9Sstevel@tonic-gate } else { 12297c478bd9Sstevel@tonic-gate error("Failed to create pid file %s: %m", pidfilename); 12307c478bd9Sstevel@tonic-gate pidfilename[0] = '\0'; 12317c478bd9Sstevel@tonic-gate } 12327c478bd9Sstevel@tonic-gate } 12337c478bd9Sstevel@tonic-gate 12347c478bd9Sstevel@tonic-gate static void 12357c478bd9Sstevel@tonic-gate create_linkpidfile() 12367c478bd9Sstevel@tonic-gate { 12377c478bd9Sstevel@tonic-gate FILE *pidfile; 12387c478bd9Sstevel@tonic-gate 12397c478bd9Sstevel@tonic-gate if (linkname[0] == '\0') 12407c478bd9Sstevel@tonic-gate return; 12417c478bd9Sstevel@tonic-gate script_setenv("LINKNAME", linkname, 1); 12427c478bd9Sstevel@tonic-gate (void) slprintf(linkpidfile, sizeof(linkpidfile), "%sppp-%s.pid", 12437c478bd9Sstevel@tonic-gate _PATH_VARRUN, linkname); 12447c478bd9Sstevel@tonic-gate if ((pidfile = fopen(linkpidfile, "w")) != NULL) { 12457c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%u\n", (unsigned)getpid()); 12467c478bd9Sstevel@tonic-gate if (ifname[0] != '\0') 12477c478bd9Sstevel@tonic-gate (void) fprintf(pidfile, "%s\n", ifname); 12487c478bd9Sstevel@tonic-gate (void) fclose(pidfile); 12497c478bd9Sstevel@tonic-gate } else { 12507c478bd9Sstevel@tonic-gate error("Failed to create pid file %s: %m", linkpidfile); 12517c478bd9Sstevel@tonic-gate linkpidfile[0] = '\0'; 12527c478bd9Sstevel@tonic-gate } 12537c478bd9Sstevel@tonic-gate } 12547c478bd9Sstevel@tonic-gate 12557c478bd9Sstevel@tonic-gate /* 12567c478bd9Sstevel@tonic-gate * holdoff_end - called via a timeout when the holdoff period ends. 12577c478bd9Sstevel@tonic-gate */ 12587c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 12597c478bd9Sstevel@tonic-gate static void 12607c478bd9Sstevel@tonic-gate holdoff_end(arg) 12617c478bd9Sstevel@tonic-gate void *arg; 12627c478bd9Sstevel@tonic-gate { 12637c478bd9Sstevel@tonic-gate new_phase(PHASE_DORMANT); 12647c478bd9Sstevel@tonic-gate } 12657c478bd9Sstevel@tonic-gate 12667c478bd9Sstevel@tonic-gate /* List of protocol names, to make our messages a little more informative. */ 12677c478bd9Sstevel@tonic-gate struct protocol_list { 12687c478bd9Sstevel@tonic-gate u_short proto; 12697c478bd9Sstevel@tonic-gate const char *name; 12707c478bd9Sstevel@tonic-gate } protocol_list[] = { 12717c478bd9Sstevel@tonic-gate { 0x21, "IP" }, 12727c478bd9Sstevel@tonic-gate { 0x23, "OSI Network Layer" }, 12737c478bd9Sstevel@tonic-gate { 0x25, "Xerox NS IDP" }, 12747c478bd9Sstevel@tonic-gate { 0x27, "DECnet Phase IV" }, 12757c478bd9Sstevel@tonic-gate { 0x29, "Appletalk" }, 12767c478bd9Sstevel@tonic-gate { 0x2b, "Novell IPX" }, 12777c478bd9Sstevel@tonic-gate { 0x2d, "VJ compressed TCP/IP" }, 12787c478bd9Sstevel@tonic-gate { 0x2f, "VJ uncompressed TCP/IP" }, 12797c478bd9Sstevel@tonic-gate { 0x31, "Bridging PDU" }, 12807c478bd9Sstevel@tonic-gate { 0x33, "Stream Protocol ST-II" }, 12817c478bd9Sstevel@tonic-gate { 0x35, "Banyan Vines" }, 12827c478bd9Sstevel@tonic-gate { 0x37, "Old VJ compressed TCP/IP" }, 12837c478bd9Sstevel@tonic-gate { 0x39, "AppleTalk EDDP" }, 12847c478bd9Sstevel@tonic-gate { 0x3b, "AppleTalk SmartBuffered" }, 12857c478bd9Sstevel@tonic-gate { 0x3d, "Multilink" }, 12867c478bd9Sstevel@tonic-gate { 0x3f, "NetBIOS Frame" }, 12877c478bd9Sstevel@tonic-gate { 0x41, "Cisco LAN Extension" }, 12887c478bd9Sstevel@tonic-gate { 0x43, "Ascom Timeplex" }, 12897c478bd9Sstevel@tonic-gate { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" }, 12907c478bd9Sstevel@tonic-gate { 0x47, "DCA Remote Lan" }, 12917c478bd9Sstevel@tonic-gate { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" }, 12927c478bd9Sstevel@tonic-gate { 0x4b, "SNA over 802.2" }, 12937c478bd9Sstevel@tonic-gate { 0x4d, "SNA" }, 12947c478bd9Sstevel@tonic-gate { 0x4f, "IP6 Header Compression" }, 12957c478bd9Sstevel@tonic-gate { 0x51, "KNX Bridging" }, 12967c478bd9Sstevel@tonic-gate { 0x53, "Encrypted" }, 12977c478bd9Sstevel@tonic-gate { 0x55, "per-link encrypted" }, 12987c478bd9Sstevel@tonic-gate { 0x57, "IPv6" }, 12997c478bd9Sstevel@tonic-gate { 0x59, "PPP Muxing" }, 13007c478bd9Sstevel@tonic-gate { 0x6f, "Stampede Bridging" }, 13017c478bd9Sstevel@tonic-gate { 0x73, "MP+" }, 13027c478bd9Sstevel@tonic-gate { 0xc1, "STMF" }, 13037c478bd9Sstevel@tonic-gate { 0xfb, "per-link compressed" }, 13047c478bd9Sstevel@tonic-gate { 0xfd, "compressed datagram" }, 13057c478bd9Sstevel@tonic-gate { 0x0201, "802.1d Hello Packets" }, 13067c478bd9Sstevel@tonic-gate { 0x0203, "IBM Source Routing BPDU" }, 13077c478bd9Sstevel@tonic-gate { 0x0205, "DEC LANBridge100 Spanning Tree" }, 13087c478bd9Sstevel@tonic-gate { 0x0207, "Cisco Discovery Protocol" }, 13097c478bd9Sstevel@tonic-gate { 0x0231, "Luxcom" }, 13107c478bd9Sstevel@tonic-gate { 0x0233, "Sigma Network Systems" }, 13117c478bd9Sstevel@tonic-gate { 0x0235, "Apple Client Server Protocol" }, 13127c478bd9Sstevel@tonic-gate { 0x0281, "MPLS Unicast" }, 13137c478bd9Sstevel@tonic-gate { 0x0283, "MPLS Multicast" }, 13147c478bd9Sstevel@tonic-gate { 0x0285, "IEEE p1284.4" }, 13157c478bd9Sstevel@tonic-gate { 0x0287, "ETSI TETRA TNP1" }, 13167c478bd9Sstevel@tonic-gate { 0x4021, "Stacker LZS" }, 13177c478bd9Sstevel@tonic-gate { 0x8021, "Internet Protocol Control Protocol" }, 13187c478bd9Sstevel@tonic-gate { 0x8023, "OSI Network Layer Control Protocol" }, 13197c478bd9Sstevel@tonic-gate { 0x8025, "Xerox NS IDP Control Protocol" }, 13207c478bd9Sstevel@tonic-gate { 0x8027, "DECnet Phase IV Control Protocol" }, 13217c478bd9Sstevel@tonic-gate { 0x8029, "Appletalk Control Protocol" }, 13227c478bd9Sstevel@tonic-gate { 0x802b, "Novell IPX Control Protocol" }, 13237c478bd9Sstevel@tonic-gate { 0x8031, "Bridging Control Protocol" }, 13247c478bd9Sstevel@tonic-gate { 0x8033, "Stream Protocol Control Protocol" }, 13257c478bd9Sstevel@tonic-gate { 0x8035, "Banyan Vines Control Protocol" }, 13267c478bd9Sstevel@tonic-gate { 0x803f, "NetBIOS Frames Control Protocol" }, 13277c478bd9Sstevel@tonic-gate { 0x8041, "Cisco LAN Extension Control Protocol" }, 13287c478bd9Sstevel@tonic-gate { 0x8043, "Ascom Timeplex Control Protocol" }, 13297c478bd9Sstevel@tonic-gate { 0x8045, "Fujitsu LBLB Control Protocol" }, 13307c478bd9Sstevel@tonic-gate { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, 13317c478bd9Sstevel@tonic-gate { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, 13327c478bd9Sstevel@tonic-gate { 0x804b, "SNA over 802.2 Control Protocol" }, 13337c478bd9Sstevel@tonic-gate { 0x804d, "SNA Control Protocol" }, 13347c478bd9Sstevel@tonic-gate { 0x8051, "KNX Bridging Control Protocol" }, 13357c478bd9Sstevel@tonic-gate { 0x8053, "Encryption Control Protocol" }, 13367c478bd9Sstevel@tonic-gate { 0x8055, "Per-link Encryption Control Protocol" }, 13377c478bd9Sstevel@tonic-gate { 0x8057, "IPv6 Control Protocol" }, 13387c478bd9Sstevel@tonic-gate { 0x806f, "Stampede Bridging Control Protocol" }, 13397c478bd9Sstevel@tonic-gate { 0x80c1, "STMF Control Protocol" }, 13407c478bd9Sstevel@tonic-gate { 0x80fb, "Per-link Compression Control Protocol" }, 13417c478bd9Sstevel@tonic-gate { 0x80fd, "Compression Control Protocol" }, 13427c478bd9Sstevel@tonic-gate { 0x8207, "Cisco Discovery Control Protocol" }, 13437c478bd9Sstevel@tonic-gate { 0x8235, "Apple Client Server Control Protocol" }, 13447c478bd9Sstevel@tonic-gate { 0x8281, "MPLS Control Protocol" }, 13457c478bd9Sstevel@tonic-gate { 0x8287, "ETSI TETRA TNP1 Control Protocol" }, 13467c478bd9Sstevel@tonic-gate { 0xc021, "Link Control Protocol" }, 13477c478bd9Sstevel@tonic-gate { 0xc023, "Password Authentication Protocol" }, 13487c478bd9Sstevel@tonic-gate { 0xc025, "Link Quality Report" }, 13497c478bd9Sstevel@tonic-gate { 0xc027, "Shiva Password Authentication Protocol" }, 13507c478bd9Sstevel@tonic-gate { 0xc029, "CallBack Control Protocol (CBCP)" }, 13517c478bd9Sstevel@tonic-gate { 0xc02b, "Bandwidth Allocation Control Protocol" }, 13527c478bd9Sstevel@tonic-gate { 0xc02d, "BAP" }, 13537c478bd9Sstevel@tonic-gate { 0xc081, "Container Control Protocol" }, 13547c478bd9Sstevel@tonic-gate { 0xc223, "Challenge Handshake Authentication Protocol" }, 13557c478bd9Sstevel@tonic-gate { 0xc227, "Extensible Authentication Protocol" }, 13567c478bd9Sstevel@tonic-gate { 0xc281, "Funk Proprietary Authentication Protocol" }, 13577c478bd9Sstevel@tonic-gate { 0, NULL }, 13587c478bd9Sstevel@tonic-gate }; 13597c478bd9Sstevel@tonic-gate 13607c478bd9Sstevel@tonic-gate /* 13617c478bd9Sstevel@tonic-gate * protocol_name - find a name for a PPP protocol. 13627c478bd9Sstevel@tonic-gate */ 13637c478bd9Sstevel@tonic-gate const char * 13647c478bd9Sstevel@tonic-gate protocol_name(proto) 13657c478bd9Sstevel@tonic-gate int proto; 13667c478bd9Sstevel@tonic-gate { 13677c478bd9Sstevel@tonic-gate struct protocol_list *lp; 13687c478bd9Sstevel@tonic-gate 13697c478bd9Sstevel@tonic-gate for (lp = protocol_list; lp->proto != 0; ++lp) 13707c478bd9Sstevel@tonic-gate if (proto == lp->proto) 13717c478bd9Sstevel@tonic-gate return (lp->name); 13727c478bd9Sstevel@tonic-gate return (NULL); 13737c478bd9Sstevel@tonic-gate } 13747c478bd9Sstevel@tonic-gate 13757c478bd9Sstevel@tonic-gate static const char *phase_names[] = { PHASE__NAMES }; 13767c478bd9Sstevel@tonic-gate 13777c478bd9Sstevel@tonic-gate const char * 13787c478bd9Sstevel@tonic-gate phase_name(pval) 13797c478bd9Sstevel@tonic-gate int pval; 13807c478bd9Sstevel@tonic-gate { 13817c478bd9Sstevel@tonic-gate static char buf[32]; 13827c478bd9Sstevel@tonic-gate 13837c478bd9Sstevel@tonic-gate if (pval < 0 || pval >= Dim(phase_names)) { 13847c478bd9Sstevel@tonic-gate (void) slprintf(buf, sizeof (buf), "unknown %d", pval); 13857c478bd9Sstevel@tonic-gate return ((const char *)buf); 13867c478bd9Sstevel@tonic-gate } 13877c478bd9Sstevel@tonic-gate return (phase_names[pval]); 13887c478bd9Sstevel@tonic-gate } 13897c478bd9Sstevel@tonic-gate 13907c478bd9Sstevel@tonic-gate /* 13917c478bd9Sstevel@tonic-gate * get_input - called when incoming data is available. 13927c478bd9Sstevel@tonic-gate */ 13937c478bd9Sstevel@tonic-gate static void 13947c478bd9Sstevel@tonic-gate get_input() 13957c478bd9Sstevel@tonic-gate { 13967c478bd9Sstevel@tonic-gate int len, i; 13977c478bd9Sstevel@tonic-gate u_char *p; 13987c478bd9Sstevel@tonic-gate u_short protocol; 13997c478bd9Sstevel@tonic-gate struct protent *protp; 14007c478bd9Sstevel@tonic-gate const char *pname; 14017c478bd9Sstevel@tonic-gate 14027c478bd9Sstevel@tonic-gate p = inpacket_buf; /* point to beginning of packet buffer */ 14037c478bd9Sstevel@tonic-gate 14047c478bd9Sstevel@tonic-gate len = read_packet(inpacket_buf); 14057c478bd9Sstevel@tonic-gate if (len < 0) 14067c478bd9Sstevel@tonic-gate return; 14077c478bd9Sstevel@tonic-gate 14087c478bd9Sstevel@tonic-gate if (len == 0) { 14097c478bd9Sstevel@tonic-gate notice("Modem hangup"); 14107c478bd9Sstevel@tonic-gate hungup = 1; 14117c478bd9Sstevel@tonic-gate status = EXIT_HANGUP; 14127c478bd9Sstevel@tonic-gate lcp_lowerdown(0); /* serial link is no longer available */ 14137c478bd9Sstevel@tonic-gate link_terminated(0); 14147c478bd9Sstevel@tonic-gate return; 14157c478bd9Sstevel@tonic-gate } 14167c478bd9Sstevel@tonic-gate 14177c478bd9Sstevel@tonic-gate if (debug /*&& (debugflags & DBG_INPACKET)*/) 14187c478bd9Sstevel@tonic-gate dbglog("rcvd %P", p, len); 14197c478bd9Sstevel@tonic-gate 14207c478bd9Sstevel@tonic-gate if (len < PPP_HDRLEN) { 14217c478bd9Sstevel@tonic-gate dbglog("Discarded short packet (%d < %d)", len, PPP_HDRLEN); 14227c478bd9Sstevel@tonic-gate return; 14237c478bd9Sstevel@tonic-gate } 14247c478bd9Sstevel@tonic-gate 14257c478bd9Sstevel@tonic-gate p += 2; /* Skip address and control */ 14267c478bd9Sstevel@tonic-gate GETSHORT(protocol, p); 14277c478bd9Sstevel@tonic-gate len -= PPP_HDRLEN; 14287c478bd9Sstevel@tonic-gate 14297c478bd9Sstevel@tonic-gate pname = debug ? NULL : protocol_name(protocol); 14307c478bd9Sstevel@tonic-gate 14317c478bd9Sstevel@tonic-gate /* 14327c478bd9Sstevel@tonic-gate * Toss all non-LCP packets unless LCP is in Opened state and 14337c478bd9Sstevel@tonic-gate * discard non-authentication protocols if we're not yet 14347c478bd9Sstevel@tonic-gate * authenticated. 14357c478bd9Sstevel@tonic-gate */ 14367c478bd9Sstevel@tonic-gate if ((protocol != PPP_LCP && 14377c478bd9Sstevel@tonic-gate (phase < PHASE_AUTHENTICATE || phase > PHASE_RUNNING)) || 14387c478bd9Sstevel@tonic-gate (phase <= PHASE_AUTHENTICATE && 14397c478bd9Sstevel@tonic-gate !(protocol == PPP_LCP || protocol == PPP_LQR || 14407c478bd9Sstevel@tonic-gate protocol == PPP_PAP || protocol == PPP_CHAP))) { 14417c478bd9Sstevel@tonic-gate if (pname == NULL) 14427c478bd9Sstevel@tonic-gate dbglog("Discarded proto 0x%x in %s phase", 14437c478bd9Sstevel@tonic-gate protocol, phase_name(phase)); 14447c478bd9Sstevel@tonic-gate else 14457c478bd9Sstevel@tonic-gate dbglog("Discarded %s (0x%x) in %s phase", 14467c478bd9Sstevel@tonic-gate pname, protocol, phase_name(phase)); 14477c478bd9Sstevel@tonic-gate return; 14487c478bd9Sstevel@tonic-gate } 14497c478bd9Sstevel@tonic-gate 14507c478bd9Sstevel@tonic-gate /* 14517c478bd9Sstevel@tonic-gate * Upcall the proper protocol input routine. 14527c478bd9Sstevel@tonic-gate */ 14537c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) { 14547c478bd9Sstevel@tonic-gate if (protp->protocol == protocol && protp->enabled_flag) { 14557c478bd9Sstevel@tonic-gate (*protp->input)(0, p, len); 14567c478bd9Sstevel@tonic-gate return; 14577c478bd9Sstevel@tonic-gate } 14587c478bd9Sstevel@tonic-gate if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag 14597c478bd9Sstevel@tonic-gate && protp->datainput != NULL) { 14607c478bd9Sstevel@tonic-gate (*protp->datainput)(0, p, len); 14617c478bd9Sstevel@tonic-gate return; 14627c478bd9Sstevel@tonic-gate } 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate if (debug) { 14667c478bd9Sstevel@tonic-gate if (pname != NULL) 14677c478bd9Sstevel@tonic-gate warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); 14687c478bd9Sstevel@tonic-gate else 14697c478bd9Sstevel@tonic-gate warn("Unsupported protocol 0x%x received", protocol); 14707c478bd9Sstevel@tonic-gate } 14717c478bd9Sstevel@tonic-gate lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN); 14727c478bd9Sstevel@tonic-gate } 14737c478bd9Sstevel@tonic-gate 14747c478bd9Sstevel@tonic-gate /* 14757c478bd9Sstevel@tonic-gate * new_phase - signal the start of a new phase of pppd's operation. 14767c478bd9Sstevel@tonic-gate */ 14777c478bd9Sstevel@tonic-gate void 14787c478bd9Sstevel@tonic-gate new_phase(p) 14797c478bd9Sstevel@tonic-gate int p; 14807c478bd9Sstevel@tonic-gate { 14817c478bd9Sstevel@tonic-gate if (new_phase_hook != NULL) 14827c478bd9Sstevel@tonic-gate (*new_phase_hook)(p, phase); 14837c478bd9Sstevel@tonic-gate phase = p; 14847c478bd9Sstevel@tonic-gate } 14857c478bd9Sstevel@tonic-gate 14867c478bd9Sstevel@tonic-gate /* 14877c478bd9Sstevel@tonic-gate * die - clean up state and exit with the specified status. 14887c478bd9Sstevel@tonic-gate */ 14897c478bd9Sstevel@tonic-gate void 14907c478bd9Sstevel@tonic-gate die(status) 14917c478bd9Sstevel@tonic-gate int status; 14927c478bd9Sstevel@tonic-gate { 14937c478bd9Sstevel@tonic-gate cleanup(); 14947c478bd9Sstevel@tonic-gate if (phase != PHASE_EXIT) { 14957c478bd9Sstevel@tonic-gate syslog(LOG_INFO, "Exit."); 14967c478bd9Sstevel@tonic-gate new_phase(PHASE_EXIT); 14977c478bd9Sstevel@tonic-gate } 14987c478bd9Sstevel@tonic-gate exit(status); 14997c478bd9Sstevel@tonic-gate } 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate /* 15027c478bd9Sstevel@tonic-gate * cleanup - restore anything which needs to be restored before we exit 15037c478bd9Sstevel@tonic-gate */ 15047c478bd9Sstevel@tonic-gate static void 15057c478bd9Sstevel@tonic-gate cleanup() 15067c478bd9Sstevel@tonic-gate { 15077c478bd9Sstevel@tonic-gate sys_cleanup(); /* XXX: Need to check if this is okay after close_tty */ 15087c478bd9Sstevel@tonic-gate 15097c478bd9Sstevel@tonic-gate if (fd_ppp >= 0) { 15107c478bd9Sstevel@tonic-gate fd_ppp = -1; 15117c478bd9Sstevel@tonic-gate disestablish_ppp(ttyfd); 15127c478bd9Sstevel@tonic-gate } 15137c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0) 15147c478bd9Sstevel@tonic-gate close_tty(); 15157c478bd9Sstevel@tonic-gate 15167c478bd9Sstevel@tonic-gate if (pidfilename[0] != '\0' && unlink(pidfilename) < 0 && errno != ENOENT) 15177c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", pidfilename); 15187c478bd9Sstevel@tonic-gate pidfilename[0] = '\0'; 15197c478bd9Sstevel@tonic-gate if (linkpidfile[0] != '\0' && unlink(linkpidfile) < 0 && errno != ENOENT) 15207c478bd9Sstevel@tonic-gate warn("unable to delete pid file %s: %m", linkpidfile); 15217c478bd9Sstevel@tonic-gate linkpidfile[0] = '\0'; 15227c478bd9Sstevel@tonic-gate 15237c478bd9Sstevel@tonic-gate if (locked) { 15247c478bd9Sstevel@tonic-gate locked = 0; 15257c478bd9Sstevel@tonic-gate unlock(); 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate 15287c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 15297c478bd9Sstevel@tonic-gate if (pppdb != NULL) { 15307c478bd9Sstevel@tonic-gate cleanup_db(); 15317c478bd9Sstevel@tonic-gate pppdb = NULL; 15327c478bd9Sstevel@tonic-gate } 15337c478bd9Sstevel@tonic-gate #endif 15347c478bd9Sstevel@tonic-gate } 15357c478bd9Sstevel@tonic-gate 15367c478bd9Sstevel@tonic-gate /* 15377c478bd9Sstevel@tonic-gate * close_tty - restore the terminal device and close it. 15387c478bd9Sstevel@tonic-gate */ 15397c478bd9Sstevel@tonic-gate static void 15407c478bd9Sstevel@tonic-gate close_tty() 15417c478bd9Sstevel@tonic-gate { 15427c478bd9Sstevel@tonic-gate int fd = real_ttyfd; 15437c478bd9Sstevel@tonic-gate 15447c478bd9Sstevel@tonic-gate real_ttyfd = -1; 15457c478bd9Sstevel@tonic-gate 15467c478bd9Sstevel@tonic-gate /* drop dtr to hang up */ 15477c478bd9Sstevel@tonic-gate if (!default_device && modem) { 15487c478bd9Sstevel@tonic-gate setdtr(fd, 0); 15497c478bd9Sstevel@tonic-gate /* 15507c478bd9Sstevel@tonic-gate * This sleep is in case the serial port has CLOCAL set by default, 15517c478bd9Sstevel@tonic-gate * and consequently will reassert DTR when we close the device. 15527c478bd9Sstevel@tonic-gate */ 15537c478bd9Sstevel@tonic-gate (void) sleep(1); 15547c478bd9Sstevel@tonic-gate } 15557c478bd9Sstevel@tonic-gate 15567c478bd9Sstevel@tonic-gate restore_tty(fd); 15577c478bd9Sstevel@tonic-gate 15587c478bd9Sstevel@tonic-gate if (tty_mode != (mode_t) -1) { 15597c478bd9Sstevel@tonic-gate if (fchmod(fd, tty_mode) != 0) { 15607c478bd9Sstevel@tonic-gate /* XXX if devnam is a symlink, this will change the link */ 15617c478bd9Sstevel@tonic-gate if (chmod(devnam, tty_mode) != 0) { 15627c478bd9Sstevel@tonic-gate error("Unable to chmod file %s: %m", devnam); 15637c478bd9Sstevel@tonic-gate } 15647c478bd9Sstevel@tonic-gate } 15657c478bd9Sstevel@tonic-gate } 15667c478bd9Sstevel@tonic-gate 15677c478bd9Sstevel@tonic-gate (void) close(fd); 15687c478bd9Sstevel@tonic-gate } 15697c478bd9Sstevel@tonic-gate 15707c478bd9Sstevel@tonic-gate /* 15717c478bd9Sstevel@tonic-gate * update_link_stats - get stats at link termination. 15727c478bd9Sstevel@tonic-gate */ 15737c478bd9Sstevel@tonic-gate void 15747c478bd9Sstevel@tonic-gate update_link_stats(u) 15757c478bd9Sstevel@tonic-gate int u; 15767c478bd9Sstevel@tonic-gate { 15777c478bd9Sstevel@tonic-gate struct timeval now; 15787c478bd9Sstevel@tonic-gate char numbuf[32]; 15797c478bd9Sstevel@tonic-gate 15807c478bd9Sstevel@tonic-gate if (gettimeofday(&now, NULL) >= 0) { 15817c478bd9Sstevel@tonic-gate link_connect_time = now.tv_sec - start_time.tv_sec; 15827c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); 15837c478bd9Sstevel@tonic-gate script_setenv("CONNECT_TIME", numbuf, 0); 15847c478bd9Sstevel@tonic-gate } else { 15857c478bd9Sstevel@tonic-gate link_connect_time = 0; 15867c478bd9Sstevel@tonic-gate } 15877c478bd9Sstevel@tonic-gate 15887c478bd9Sstevel@tonic-gate if (get_ppp_stats(u, &link_stats)) { 15897c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F, 15907c478bd9Sstevel@tonic-gate link_stats.bytes_out); 15917c478bd9Sstevel@tonic-gate script_setenv("BYTES_SENT", numbuf, 0); 15927c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F, 15937c478bd9Sstevel@tonic-gate link_stats.bytes_in); 15947c478bd9Sstevel@tonic-gate script_setenv("BYTES_RCVD", numbuf, 0); 15957c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F, 15967c478bd9Sstevel@tonic-gate link_stats.pkts_in); 15977c478bd9Sstevel@tonic-gate script_setenv("PKTS_RCVD", numbuf, 0); 15987c478bd9Sstevel@tonic-gate (void) slprintf(numbuf, sizeof(numbuf), "%" PPP_COUNTER_F, 15997c478bd9Sstevel@tonic-gate link_stats.pkts_out); 16007c478bd9Sstevel@tonic-gate script_setenv("PKTS_SENT", numbuf, 0); 16017c478bd9Sstevel@tonic-gate link_stats_valid = 1; 16027c478bd9Sstevel@tonic-gate } 16037c478bd9Sstevel@tonic-gate } 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate 16067c478bd9Sstevel@tonic-gate struct callout { 16077c478bd9Sstevel@tonic-gate struct timeval c_time; /* time at which to call routine */ 16087c478bd9Sstevel@tonic-gate void *c_arg; /* argument to routine */ 16097c478bd9Sstevel@tonic-gate void (*c_func) __P((void *)); /* routine */ 16107c478bd9Sstevel@tonic-gate struct callout *c_next; 16117c478bd9Sstevel@tonic-gate }; 16127c478bd9Sstevel@tonic-gate 16137c478bd9Sstevel@tonic-gate static struct callout *callout = NULL; /* Callout list */ 16147c478bd9Sstevel@tonic-gate static struct timeval timenow; /* Current time */ 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate /* 16177c478bd9Sstevel@tonic-gate * timeout - Schedule a timeout. 16187c478bd9Sstevel@tonic-gate * 16197c478bd9Sstevel@tonic-gate * Note that this timeout takes the number of seconds, NOT hz (as in 16207c478bd9Sstevel@tonic-gate * the kernel). 16217c478bd9Sstevel@tonic-gate */ 16227c478bd9Sstevel@tonic-gate void 16237c478bd9Sstevel@tonic-gate timeout(func, arg, time) 16247c478bd9Sstevel@tonic-gate void (*func) __P((void *)); 16257c478bd9Sstevel@tonic-gate void *arg; 16267c478bd9Sstevel@tonic-gate int time; 16277c478bd9Sstevel@tonic-gate { 16287c478bd9Sstevel@tonic-gate struct callout *newp, *p, **pp; 16297c478bd9Sstevel@tonic-gate 16307c478bd9Sstevel@tonic-gate MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); 16317c478bd9Sstevel@tonic-gate 16327c478bd9Sstevel@tonic-gate /* 16337c478bd9Sstevel@tonic-gate * Allocate timeout. 16347c478bd9Sstevel@tonic-gate */ 16357c478bd9Sstevel@tonic-gate if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) 16367c478bd9Sstevel@tonic-gate novm("callout structure for timeout."); 16377c478bd9Sstevel@tonic-gate newp->c_arg = arg; 16387c478bd9Sstevel@tonic-gate newp->c_func = func; 16397c478bd9Sstevel@tonic-gate (void) gettimeofday(&timenow, NULL); 16407c478bd9Sstevel@tonic-gate newp->c_time.tv_sec = timenow.tv_sec + time; 16417c478bd9Sstevel@tonic-gate newp->c_time.tv_usec = timenow.tv_usec; 16427c478bd9Sstevel@tonic-gate 16437c478bd9Sstevel@tonic-gate /* 16447c478bd9Sstevel@tonic-gate * Find correct place and link it in. 16457c478bd9Sstevel@tonic-gate */ 16467c478bd9Sstevel@tonic-gate for (pp = &callout; (p = *pp) != NULL; pp = &p->c_next) 16477c478bd9Sstevel@tonic-gate if (newp->c_time.tv_sec < p->c_time.tv_sec 16487c478bd9Sstevel@tonic-gate || (newp->c_time.tv_sec == p->c_time.tv_sec 16497c478bd9Sstevel@tonic-gate && newp->c_time.tv_usec < p->c_time.tv_usec)) 16507c478bd9Sstevel@tonic-gate break; 16517c478bd9Sstevel@tonic-gate newp->c_next = p; 16527c478bd9Sstevel@tonic-gate *pp = newp; 16537c478bd9Sstevel@tonic-gate } 16547c478bd9Sstevel@tonic-gate 16557c478bd9Sstevel@tonic-gate 16567c478bd9Sstevel@tonic-gate /* 16577c478bd9Sstevel@tonic-gate * untimeout - Unschedule a timeout. 16587c478bd9Sstevel@tonic-gate */ 16597c478bd9Sstevel@tonic-gate void 16607c478bd9Sstevel@tonic-gate untimeout(func, arg) 16617c478bd9Sstevel@tonic-gate void (*func) __P((void *)); 16627c478bd9Sstevel@tonic-gate void *arg; 16637c478bd9Sstevel@tonic-gate { 16647c478bd9Sstevel@tonic-gate struct callout **copp, *freep; 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate MAINDEBUG(("Untimeout %p:%p.", func, arg)); 16677c478bd9Sstevel@tonic-gate 16687c478bd9Sstevel@tonic-gate /* 16697c478bd9Sstevel@tonic-gate * Find first matching timeout and remove it from the list. 16707c478bd9Sstevel@tonic-gate */ 16717c478bd9Sstevel@tonic-gate for (copp = &callout; (freep = *copp) != NULL; copp = &freep->c_next) 16727c478bd9Sstevel@tonic-gate if (freep->c_func == func && freep->c_arg == arg) { 16737c478bd9Sstevel@tonic-gate *copp = freep->c_next; 16747c478bd9Sstevel@tonic-gate free((char *) freep); 16757c478bd9Sstevel@tonic-gate break; 16767c478bd9Sstevel@tonic-gate } 16777c478bd9Sstevel@tonic-gate } 16787c478bd9Sstevel@tonic-gate 16797c478bd9Sstevel@tonic-gate 16807c478bd9Sstevel@tonic-gate /* 16817c478bd9Sstevel@tonic-gate * calltimeout - Call any timeout routines which are now due. 16827c478bd9Sstevel@tonic-gate */ 16837c478bd9Sstevel@tonic-gate static void 16847c478bd9Sstevel@tonic-gate calltimeout() 16857c478bd9Sstevel@tonic-gate { 16867c478bd9Sstevel@tonic-gate struct callout *p; 16877c478bd9Sstevel@tonic-gate 16887c478bd9Sstevel@tonic-gate while (callout != NULL) { 16897c478bd9Sstevel@tonic-gate p = callout; 16907c478bd9Sstevel@tonic-gate 16917c478bd9Sstevel@tonic-gate if (gettimeofday(&timenow, NULL) < 0) 16927c478bd9Sstevel@tonic-gate fatal("Failed to get time of day: %m"); 16937c478bd9Sstevel@tonic-gate if (!(p->c_time.tv_sec < timenow.tv_sec 16947c478bd9Sstevel@tonic-gate || (p->c_time.tv_sec == timenow.tv_sec 16957c478bd9Sstevel@tonic-gate && p->c_time.tv_usec <= timenow.tv_usec))) 16967c478bd9Sstevel@tonic-gate break; /* no, it's not time yet */ 16977c478bd9Sstevel@tonic-gate 16987c478bd9Sstevel@tonic-gate callout = p->c_next; 16997c478bd9Sstevel@tonic-gate (*p->c_func)(p->c_arg); 17007c478bd9Sstevel@tonic-gate 17017c478bd9Sstevel@tonic-gate free((char *) p); 17027c478bd9Sstevel@tonic-gate } 17037c478bd9Sstevel@tonic-gate } 17047c478bd9Sstevel@tonic-gate 17057c478bd9Sstevel@tonic-gate 17067c478bd9Sstevel@tonic-gate /* 17077c478bd9Sstevel@tonic-gate * timeleft - return the length of time until the next timeout is due. 17087c478bd9Sstevel@tonic-gate */ 17097c478bd9Sstevel@tonic-gate static struct timeval * 17107c478bd9Sstevel@tonic-gate timeleft(tvp) 17117c478bd9Sstevel@tonic-gate struct timeval *tvp; 17127c478bd9Sstevel@tonic-gate { 17137c478bd9Sstevel@tonic-gate if (callout == NULL) 17147c478bd9Sstevel@tonic-gate return (NULL); 17157c478bd9Sstevel@tonic-gate 17167c478bd9Sstevel@tonic-gate (void) gettimeofday(&timenow, NULL); 17177c478bd9Sstevel@tonic-gate tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; 17187c478bd9Sstevel@tonic-gate tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; 17197c478bd9Sstevel@tonic-gate if (tvp->tv_usec < 0) { 17207c478bd9Sstevel@tonic-gate tvp->tv_usec += 1000000; 17217c478bd9Sstevel@tonic-gate tvp->tv_sec -= 1; 17227c478bd9Sstevel@tonic-gate } 17237c478bd9Sstevel@tonic-gate if (tvp->tv_sec < 0) 17247c478bd9Sstevel@tonic-gate tvp->tv_sec = tvp->tv_usec = 0; 17257c478bd9Sstevel@tonic-gate 17267c478bd9Sstevel@tonic-gate return (tvp); 17277c478bd9Sstevel@tonic-gate } 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate 17307c478bd9Sstevel@tonic-gate /* 17317c478bd9Sstevel@tonic-gate * kill_my_pg - send a signal to our process group, and ignore it ourselves. 17327c478bd9Sstevel@tonic-gate */ 17337c478bd9Sstevel@tonic-gate static void 17347c478bd9Sstevel@tonic-gate kill_my_pg(sig) 17357c478bd9Sstevel@tonic-gate int sig; 17367c478bd9Sstevel@tonic-gate { 17377c478bd9Sstevel@tonic-gate struct sigaction act, oldact; 17387c478bd9Sstevel@tonic-gate sigset_t mask; 17397c478bd9Sstevel@tonic-gate 17407c478bd9Sstevel@tonic-gate BZERO(&act, sizeof (act)); 17417c478bd9Sstevel@tonic-gate act.sa_handler = SIG_IGN; 17427c478bd9Sstevel@tonic-gate (void) sigemptyset(&mask); 17437c478bd9Sstevel@tonic-gate (void) sigaddset(&mask, sig); 17447c478bd9Sstevel@tonic-gate /* 17457c478bd9Sstevel@tonic-gate * Ignore signal 'sig' temporarily, before finally re-activating the 17467c478bd9Sstevel@tonic-gate * original handler. We need to do it in the following sequence, since 17477c478bd9Sstevel@tonic-gate * otherwise the signal handler for 'sig' will be called forever. 17487c478bd9Sstevel@tonic-gate */ 17497c478bd9Sstevel@tonic-gate if (sigaction(sig, &act, &oldact) < 0) { 17507c478bd9Sstevel@tonic-gate fatal("kill_my_pg: couldn't establish signal handler (%d): %m", sig); 17517c478bd9Sstevel@tonic-gate } 17527c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &mask, NULL); 17537c478bd9Sstevel@tonic-gate /* 17547c478bd9Sstevel@tonic-gate * Send signal 'sig' to all processes whose process group ID is equal 17557c478bd9Sstevel@tonic-gate * to the process group ID of the sender. 17567c478bd9Sstevel@tonic-gate */ 17577c478bd9Sstevel@tonic-gate (void) kill(0, sig); 17587c478bd9Sstevel@tonic-gate if (sigaction(sig, &oldact, NULL) < 0) { 17597c478bd9Sstevel@tonic-gate fatal("kill_my_pg: couldn't establish signal handler (%d): %m", sig); 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate } 17627c478bd9Sstevel@tonic-gate 17637c478bd9Sstevel@tonic-gate 17647c478bd9Sstevel@tonic-gate /* 17657c478bd9Sstevel@tonic-gate * hup - Catch SIGHUP signal. 17667c478bd9Sstevel@tonic-gate * 17677c478bd9Sstevel@tonic-gate * Indicates that the physical layer has been disconnected. 17687c478bd9Sstevel@tonic-gate * We don't rely on this indication; if the user has sent this 17697c478bd9Sstevel@tonic-gate * signal, we just take the link down. 17707c478bd9Sstevel@tonic-gate */ 17717c478bd9Sstevel@tonic-gate static void 17727c478bd9Sstevel@tonic-gate hup(sig) 17737c478bd9Sstevel@tonic-gate int sig; 17747c478bd9Sstevel@tonic-gate { 17757c478bd9Sstevel@tonic-gate info("Hangup (SIGHUP)"); 17767c478bd9Sstevel@tonic-gate kill_link = 1; 17777c478bd9Sstevel@tonic-gate if (status != EXIT_HANGUP) 17787c478bd9Sstevel@tonic-gate status = EXIT_USER_REQUEST; 17797c478bd9Sstevel@tonic-gate if (conn_running > 0) 17807c478bd9Sstevel@tonic-gate /* Send the signal to the [dis]connector process(es) also */ 17817c478bd9Sstevel@tonic-gate kill_my_pg(sig); 17827c478bd9Sstevel@tonic-gate if (charshunt_pid) 17837c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, sig); 17847c478bd9Sstevel@tonic-gate if (waiting) 17857c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1); 17867c478bd9Sstevel@tonic-gate } 17877c478bd9Sstevel@tonic-gate 17887c478bd9Sstevel@tonic-gate 17897c478bd9Sstevel@tonic-gate /* 17907c478bd9Sstevel@tonic-gate * term - Catch SIGTERM signal and SIGINT signal (^C/del). 17917c478bd9Sstevel@tonic-gate * 17927c478bd9Sstevel@tonic-gate * Indicates that we should initiate a graceful disconnect and exit. 17937c478bd9Sstevel@tonic-gate */ 17947c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 17957c478bd9Sstevel@tonic-gate static void 17967c478bd9Sstevel@tonic-gate term(sig) 17977c478bd9Sstevel@tonic-gate int sig; 17987c478bd9Sstevel@tonic-gate { 17997c478bd9Sstevel@tonic-gate info("Terminating on signal %d.", sig); 18007c478bd9Sstevel@tonic-gate persist = 0; /* don't try to restart */ 18017c478bd9Sstevel@tonic-gate kill_link = 1; 18027c478bd9Sstevel@tonic-gate status = EXIT_USER_REQUEST; 18037c478bd9Sstevel@tonic-gate if (conn_running > 0) 18047c478bd9Sstevel@tonic-gate /* Send the signal to the [dis]connector process(es) also */ 18057c478bd9Sstevel@tonic-gate kill_my_pg(sig); 18067c478bd9Sstevel@tonic-gate if (charshunt_pid) 18077c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, sig); 18087c478bd9Sstevel@tonic-gate if (waiting) 18097c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1); 18107c478bd9Sstevel@tonic-gate } 18117c478bd9Sstevel@tonic-gate 18127c478bd9Sstevel@tonic-gate 18137c478bd9Sstevel@tonic-gate /* 18147c478bd9Sstevel@tonic-gate * chld - Catch SIGCHLD signal. 18157c478bd9Sstevel@tonic-gate * Sets a flag so we will call reap_kids in the mainline. 18167c478bd9Sstevel@tonic-gate */ 18177c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18187c478bd9Sstevel@tonic-gate static void 18197c478bd9Sstevel@tonic-gate chld(sig) 18207c478bd9Sstevel@tonic-gate int sig; 18217c478bd9Sstevel@tonic-gate { 18227c478bd9Sstevel@tonic-gate got_sigchld = 1; 18237c478bd9Sstevel@tonic-gate if (waiting) 18247c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1); 18257c478bd9Sstevel@tonic-gate } 18267c478bd9Sstevel@tonic-gate 18277c478bd9Sstevel@tonic-gate /* 18287c478bd9Sstevel@tonic-gate * toggle_debug - Catch SIGUSR1 signal. 18297c478bd9Sstevel@tonic-gate * 18307c478bd9Sstevel@tonic-gate * Toggle debug flag. 18317c478bd9Sstevel@tonic-gate */ 18327c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18337c478bd9Sstevel@tonic-gate static void 18347c478bd9Sstevel@tonic-gate toggle_debug(sig) 18357c478bd9Sstevel@tonic-gate int sig; 18367c478bd9Sstevel@tonic-gate { 18377c478bd9Sstevel@tonic-gate if (debug) { 18387c478bd9Sstevel@tonic-gate print_ncpstate(0, NULL); 18397c478bd9Sstevel@tonic-gate dbglog("debug logging disabled"); 18407c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_WARNING)); 18417c478bd9Sstevel@tonic-gate debug = 0; 18427c478bd9Sstevel@tonic-gate } else { 18437c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_DEBUG)); 18447c478bd9Sstevel@tonic-gate dbglog("debug logging enabled"); 18457c478bd9Sstevel@tonic-gate print_ncpstate(0, NULL); 18467c478bd9Sstevel@tonic-gate debug = 1; 18477c478bd9Sstevel@tonic-gate } 18487c478bd9Sstevel@tonic-gate } 18497c478bd9Sstevel@tonic-gate 18507c478bd9Sstevel@tonic-gate 18517c478bd9Sstevel@tonic-gate /* 18527c478bd9Sstevel@tonic-gate * open_ccp - Catch SIGUSR2 signal. 18537c478bd9Sstevel@tonic-gate * 18547c478bd9Sstevel@tonic-gate * Try to (re)negotiate compression. 18557c478bd9Sstevel@tonic-gate */ 18567c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18577c478bd9Sstevel@tonic-gate static void 18587c478bd9Sstevel@tonic-gate open_ccp(sig) 18597c478bd9Sstevel@tonic-gate int sig; 18607c478bd9Sstevel@tonic-gate { 18617c478bd9Sstevel@tonic-gate open_ccp_flag = 1; 18627c478bd9Sstevel@tonic-gate if (waiting) 18637c478bd9Sstevel@tonic-gate siglongjmp(sigjmp, 1); 18647c478bd9Sstevel@tonic-gate } 18657c478bd9Sstevel@tonic-gate 18667c478bd9Sstevel@tonic-gate 18677c478bd9Sstevel@tonic-gate /* 18687c478bd9Sstevel@tonic-gate * bad_signal - We've caught a fatal signal. Clean up state and exit. 18697c478bd9Sstevel@tonic-gate */ 18707c478bd9Sstevel@tonic-gate static void 18717c478bd9Sstevel@tonic-gate bad_signal(sig) 18727c478bd9Sstevel@tonic-gate int sig; 18737c478bd9Sstevel@tonic-gate { 18747c478bd9Sstevel@tonic-gate static int crashed = 0; 18757c478bd9Sstevel@tonic-gate 18767c478bd9Sstevel@tonic-gate if (crashed) 18777c478bd9Sstevel@tonic-gate _exit(127); 18787c478bd9Sstevel@tonic-gate crashed = 1; 18797c478bd9Sstevel@tonic-gate error("Fatal signal %d", sig); 18807c478bd9Sstevel@tonic-gate if (conn_running > 0) 18817c478bd9Sstevel@tonic-gate kill_my_pg(SIGTERM); 18827c478bd9Sstevel@tonic-gate if (charshunt_pid) 18837c478bd9Sstevel@tonic-gate (void) kill(charshunt_pid, SIGTERM); 18847c478bd9Sstevel@tonic-gate die(127); 18857c478bd9Sstevel@tonic-gate } 18867c478bd9Sstevel@tonic-gate 18877c478bd9Sstevel@tonic-gate 18887c478bd9Sstevel@tonic-gate /* 18897c478bd9Sstevel@tonic-gate * device_script - run a program to talk to the serial device 18907c478bd9Sstevel@tonic-gate * (e.g. to run the connector or disconnector script). 18917c478bd9Sstevel@tonic-gate */ 18927c478bd9Sstevel@tonic-gate static int 18937c478bd9Sstevel@tonic-gate device_script(program, in, out, dont_wait, optname) 18947c478bd9Sstevel@tonic-gate char *program; 18957c478bd9Sstevel@tonic-gate int in, out; 18967c478bd9Sstevel@tonic-gate int dont_wait; 18977c478bd9Sstevel@tonic-gate char *optname; 18987c478bd9Sstevel@tonic-gate { 18997c478bd9Sstevel@tonic-gate pid_t pid; 19007c478bd9Sstevel@tonic-gate int status = -1; 19017c478bd9Sstevel@tonic-gate int errfd; 19027c478bd9Sstevel@tonic-gate int envpipe[2]; 19037c478bd9Sstevel@tonic-gate 19047c478bd9Sstevel@tonic-gate envpipe[0] = envpipe[1] = -1; 19057c478bd9Sstevel@tonic-gate if (!dont_wait && device_pipe_hook != NULL && pipe(envpipe) == -1) { 19067c478bd9Sstevel@tonic-gate error("Cannot create pipe for child: %m"); 19077c478bd9Sstevel@tonic-gate return (-1); 19087c478bd9Sstevel@tonic-gate } 19097c478bd9Sstevel@tonic-gate 19107c478bd9Sstevel@tonic-gate ++conn_running; 19117c478bd9Sstevel@tonic-gate pid = fork(); 19127c478bd9Sstevel@tonic-gate 19137c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) { 19147c478bd9Sstevel@tonic-gate --conn_running; 19157c478bd9Sstevel@tonic-gate error("Failed to create child process: %m"); 19167c478bd9Sstevel@tonic-gate return (-1); 19177c478bd9Sstevel@tonic-gate } 19187c478bd9Sstevel@tonic-gate 19197c478bd9Sstevel@tonic-gate if (pid == (pid_t)0) { 19207c478bd9Sstevel@tonic-gate sys_close(); 19217c478bd9Sstevel@tonic-gate closelog(); 19227c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0) { 19237c478bd9Sstevel@tonic-gate if (envpipe[1] <= 2) 19247c478bd9Sstevel@tonic-gate envpipe[1] = dup(envpipe[1]); 19257c478bd9Sstevel@tonic-gate (void) close(envpipe[0]); 19267c478bd9Sstevel@tonic-gate } 19277c478bd9Sstevel@tonic-gate if (in == 2) { 19287c478bd9Sstevel@tonic-gate /* aargh!!! */ 19297c478bd9Sstevel@tonic-gate int newin = dup(in); 19307c478bd9Sstevel@tonic-gate if (in == out) 19317c478bd9Sstevel@tonic-gate out = newin; 19327c478bd9Sstevel@tonic-gate in = newin; 19337c478bd9Sstevel@tonic-gate } else if (out == 2) { 19347c478bd9Sstevel@tonic-gate out = dup(out); 19357c478bd9Sstevel@tonic-gate } 19367c478bd9Sstevel@tonic-gate if (log_to_fd >= 0) { 19377c478bd9Sstevel@tonic-gate if (log_to_fd != 2) { 19387c478bd9Sstevel@tonic-gate if (dup2(log_to_fd, 2) < 0) 19397c478bd9Sstevel@tonic-gate error("dup2(log_to_fd, STDERR) failed: %m"); 19407c478bd9Sstevel@tonic-gate } 19417c478bd9Sstevel@tonic-gate } else { 19427c478bd9Sstevel@tonic-gate (void) close(2); 19437c478bd9Sstevel@tonic-gate errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600); 19447c478bd9Sstevel@tonic-gate if (errfd >= 0 && errfd != 2) { 19457c478bd9Sstevel@tonic-gate if (dup2(errfd, 2) < 0) 19467c478bd9Sstevel@tonic-gate error("dup2(errfd, STDERR) failed: %m"); 19477c478bd9Sstevel@tonic-gate (void) close(errfd); 19487c478bd9Sstevel@tonic-gate } 19497c478bd9Sstevel@tonic-gate } 19507c478bd9Sstevel@tonic-gate if (in != 0) { 19517c478bd9Sstevel@tonic-gate if (out == 0) 19527c478bd9Sstevel@tonic-gate out = dup(out); 19537c478bd9Sstevel@tonic-gate if (dup2(in, 0) < 0) 19547c478bd9Sstevel@tonic-gate error("dup2(in, STDIN) failed: %m"); 19557c478bd9Sstevel@tonic-gate } 19567c478bd9Sstevel@tonic-gate if (out != 1) { 19577c478bd9Sstevel@tonic-gate if (dup2(out, 1) < 0) 19587c478bd9Sstevel@tonic-gate error("dup2(out, STDOUT) failed: %m"); 19597c478bd9Sstevel@tonic-gate } 19607c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0 && dup2(envpipe[1], 3) < 0) 19617c478bd9Sstevel@tonic-gate error("dup2(pipe, pipeout) failed: %m"); 19627c478bd9Sstevel@tonic-gate if (real_ttyfd > 2) 19637c478bd9Sstevel@tonic-gate (void) close(real_ttyfd); 19647c478bd9Sstevel@tonic-gate if (pty_master > 2) 19657c478bd9Sstevel@tonic-gate (void) close(pty_master); 19667c478bd9Sstevel@tonic-gate if (pty_slave > 2) { 19677c478bd9Sstevel@tonic-gate (void) close(pty_slave); 19687c478bd9Sstevel@tonic-gate pty_slave = -1; 19697c478bd9Sstevel@tonic-gate } 19707c478bd9Sstevel@tonic-gate (void) setuid(uid); 19717c478bd9Sstevel@tonic-gate if (getuid() != uid) { 19727c478bd9Sstevel@tonic-gate error("setuid failed"); 19737c478bd9Sstevel@tonic-gate exit(1); 19747c478bd9Sstevel@tonic-gate } 19757c478bd9Sstevel@tonic-gate (void) setgid(getgid()); 19767c478bd9Sstevel@tonic-gate if (script_env != NULL) { 19777c478bd9Sstevel@tonic-gate while (*script_env != NULL) { 19787c478bd9Sstevel@tonic-gate if (putenv(*script_env) == -1) 19797c478bd9Sstevel@tonic-gate warn("unable to set %s for %s: %m", *script_env, program); 19807c478bd9Sstevel@tonic-gate script_env++; 19817c478bd9Sstevel@tonic-gate } 19827c478bd9Sstevel@tonic-gate } 19837c478bd9Sstevel@tonic-gate (void) execl("/bin/sh", "sh", "-c", program, (char *)0); 19847c478bd9Sstevel@tonic-gate error("could not exec /bin/sh: %m"); 19857c478bd9Sstevel@tonic-gate exit(99); 19867c478bd9Sstevel@tonic-gate /* NOTREACHED */ 19877c478bd9Sstevel@tonic-gate } 19887c478bd9Sstevel@tonic-gate 19897c478bd9Sstevel@tonic-gate if (debug) 19907c478bd9Sstevel@tonic-gate dbglog("%s option: '%s' started (pid %d)", optname, program, pid); 19917c478bd9Sstevel@tonic-gate if (dont_wait) { 19927c478bd9Sstevel@tonic-gate record_child(pid, program, NULL, NULL); 19937c478bd9Sstevel@tonic-gate status = 0; 19947c478bd9Sstevel@tonic-gate } else { 19957c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0) { 19967c478bd9Sstevel@tonic-gate (void) close(envpipe[1]); 19977c478bd9Sstevel@tonic-gate (*device_pipe_hook)(envpipe[0]); 19987c478bd9Sstevel@tonic-gate } 19997c478bd9Sstevel@tonic-gate while (waitpid(pid, &status, 0) < 0) { 20007c478bd9Sstevel@tonic-gate if (errno == EINTR) 20017c478bd9Sstevel@tonic-gate continue; 20027c478bd9Sstevel@tonic-gate fatal("error waiting for (dis)connection process: %m"); 20037c478bd9Sstevel@tonic-gate } 20047c478bd9Sstevel@tonic-gate if (envpipe[0] >= 0) 20057c478bd9Sstevel@tonic-gate (void) close(envpipe[0]); 20067c478bd9Sstevel@tonic-gate --conn_running; 20077c478bd9Sstevel@tonic-gate } 20087c478bd9Sstevel@tonic-gate 20097c478bd9Sstevel@tonic-gate return (status == 0 ? 0 : -1); 20107c478bd9Sstevel@tonic-gate } 20117c478bd9Sstevel@tonic-gate 20127c478bd9Sstevel@tonic-gate 20137c478bd9Sstevel@tonic-gate /* 20147c478bd9Sstevel@tonic-gate * run-program - execute a program with given arguments, 20157c478bd9Sstevel@tonic-gate * but don't wait for it. 20167c478bd9Sstevel@tonic-gate * If the program can't be executed, logs an error unless 20177c478bd9Sstevel@tonic-gate * must_exist is 0 and the program file doesn't exist. 20187c478bd9Sstevel@tonic-gate * Returns -1 if it couldn't fork, 0 if the file doesn't exist 20197c478bd9Sstevel@tonic-gate * or isn't an executable plain file, or the process ID of the child. 20207c478bd9Sstevel@tonic-gate * If done != NULL, (*done)(arg, int) will be called later (within 20217c478bd9Sstevel@tonic-gate * reap_kids) if this routine returns value > 0. 20227c478bd9Sstevel@tonic-gate */ 20237c478bd9Sstevel@tonic-gate pid_t 20247c478bd9Sstevel@tonic-gate run_program(prog, args, must_exist, done, arg) 20257c478bd9Sstevel@tonic-gate char *prog; 20267c478bd9Sstevel@tonic-gate char **args; 20277c478bd9Sstevel@tonic-gate int must_exist; 20287c478bd9Sstevel@tonic-gate void (*done) __P((void *arg, int status)); 20297c478bd9Sstevel@tonic-gate void *arg; 20307c478bd9Sstevel@tonic-gate { 20317c478bd9Sstevel@tonic-gate pid_t pid; 20327c478bd9Sstevel@tonic-gate struct stat sbuf; 20337c478bd9Sstevel@tonic-gate int retv; 20347c478bd9Sstevel@tonic-gate 20357c478bd9Sstevel@tonic-gate /* 20367c478bd9Sstevel@tonic-gate * First check if the file exists and is executable. 20377c478bd9Sstevel@tonic-gate * We don't use access() because that would use the 20387c478bd9Sstevel@tonic-gate * real user-id, which might not be root, and the script 20397c478bd9Sstevel@tonic-gate * might be accessible only to root. 20407c478bd9Sstevel@tonic-gate */ 20417c478bd9Sstevel@tonic-gate errno = EINVAL; 20427c478bd9Sstevel@tonic-gate if (stat(prog, &sbuf) < 0 || !S_ISREG(sbuf.st_mode) 20437c478bd9Sstevel@tonic-gate || (sbuf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) { 20447c478bd9Sstevel@tonic-gate if (must_exist || errno != ENOENT) 20457c478bd9Sstevel@tonic-gate warn("Can't execute %s: %m", prog); 20467c478bd9Sstevel@tonic-gate return (0); 20477c478bd9Sstevel@tonic-gate } 20487c478bd9Sstevel@tonic-gate 20497c478bd9Sstevel@tonic-gate if (updown_script_hook != NULL) { 20507c478bd9Sstevel@tonic-gate retv = (*updown_script_hook)((const char ***)&args); 20517c478bd9Sstevel@tonic-gate if (retv == -1) { 20527c478bd9Sstevel@tonic-gate return (-1); 20537c478bd9Sstevel@tonic-gate } 20547c478bd9Sstevel@tonic-gate } 20557c478bd9Sstevel@tonic-gate 20567c478bd9Sstevel@tonic-gate pid = fork(); 20577c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) { 20587c478bd9Sstevel@tonic-gate error("Failed to create child process for %s: %m", prog); 20597c478bd9Sstevel@tonic-gate return (-1); 20607c478bd9Sstevel@tonic-gate } 20617c478bd9Sstevel@tonic-gate if (pid == (pid_t)0) { 20627c478bd9Sstevel@tonic-gate int new_fd; 20637c478bd9Sstevel@tonic-gate 20647c478bd9Sstevel@tonic-gate /* Leave the current location */ 20657c478bd9Sstevel@tonic-gate (void) setsid(); /* No controlling tty. */ 20667c478bd9Sstevel@tonic-gate (void) umask (S_IRWXG|S_IRWXO); 20677c478bd9Sstevel@tonic-gate (void) chdir ("/"); /* no current directory. */ 20687c478bd9Sstevel@tonic-gate (void) setuid(0); /* set real UID = root */ 20697c478bd9Sstevel@tonic-gate (void) setgid(getegid()); 20707c478bd9Sstevel@tonic-gate 20717c478bd9Sstevel@tonic-gate /* Ensure that nothing of our device environment is inherited. */ 20727c478bd9Sstevel@tonic-gate sys_close(); 20737c478bd9Sstevel@tonic-gate closelog(); 20747c478bd9Sstevel@tonic-gate (void) close(0); 20757c478bd9Sstevel@tonic-gate (void) close(1); 20767c478bd9Sstevel@tonic-gate (void) close(2); 20777c478bd9Sstevel@tonic-gate (void) close(ttyfd); /* tty interface to the ppp device */ 20787c478bd9Sstevel@tonic-gate if (real_ttyfd >= 0) 20797c478bd9Sstevel@tonic-gate (void) close(real_ttyfd); 20807c478bd9Sstevel@tonic-gate 20817c478bd9Sstevel@tonic-gate /* Don't pass handles to the PPP device, even by accident. */ 20827c478bd9Sstevel@tonic-gate new_fd = open (_PATH_DEVNULL, O_RDWR); 20837c478bd9Sstevel@tonic-gate if (new_fd >= 0) { 20847c478bd9Sstevel@tonic-gate if (new_fd != 0) { 20857c478bd9Sstevel@tonic-gate if (dup2(new_fd, 0) < 0) /* stdin <- /dev/null */ 20867c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDIN) failed: %m"); 20877c478bd9Sstevel@tonic-gate (void) close(new_fd); 20887c478bd9Sstevel@tonic-gate } 20897c478bd9Sstevel@tonic-gate if (dup2(0, 1) < 0) /* stdout -> /dev/null */ 20907c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDOUT) failed: %m"); 20917c478bd9Sstevel@tonic-gate if (dup2(0, 2) < 0) /* stderr -> /dev/null */ 20927c478bd9Sstevel@tonic-gate error("dup2(/dev/null, STDERR) failed: %m"); 20937c478bd9Sstevel@tonic-gate } 20947c478bd9Sstevel@tonic-gate 20957c478bd9Sstevel@tonic-gate #ifdef BSD 20967c478bd9Sstevel@tonic-gate /* Force the priority back to zero if pppd is running higher. */ 20977c478bd9Sstevel@tonic-gate if (setpriority (PRIO_PROCESS, 0, 0) < 0) 2098*48bbca81SDaniel Hoffman warn("can't reset priority to 0: %m"); 20997c478bd9Sstevel@tonic-gate #endif 21007c478bd9Sstevel@tonic-gate 21017c478bd9Sstevel@tonic-gate /* SysV recommends a second fork at this point. */ 21027c478bd9Sstevel@tonic-gate 21037c478bd9Sstevel@tonic-gate /* run the program */ 21047c478bd9Sstevel@tonic-gate (void) execve(prog, args, script_env); 21057c478bd9Sstevel@tonic-gate if (must_exist || errno != ENOENT) { 21067c478bd9Sstevel@tonic-gate /* have to reopen the log, there's nowhere else 21077c478bd9Sstevel@tonic-gate for the message to go. */ 21087c478bd9Sstevel@tonic-gate reopen_log(); 21097c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Can't execute %s: %m", prog); 21107c478bd9Sstevel@tonic-gate closelog(); 21117c478bd9Sstevel@tonic-gate } 21127c478bd9Sstevel@tonic-gate _exit(-1); 21137c478bd9Sstevel@tonic-gate } 21147c478bd9Sstevel@tonic-gate 21157c478bd9Sstevel@tonic-gate if (debug) 21167c478bd9Sstevel@tonic-gate dbglog("Script %s started (pid %d)", prog, pid); 21177c478bd9Sstevel@tonic-gate record_child(pid, prog, done, arg); 21187c478bd9Sstevel@tonic-gate 21197c478bd9Sstevel@tonic-gate return (pid); 21207c478bd9Sstevel@tonic-gate } 21217c478bd9Sstevel@tonic-gate 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate /* 21247c478bd9Sstevel@tonic-gate * record_child - add a child process to the list for reap_kids 21257c478bd9Sstevel@tonic-gate * to use. 21267c478bd9Sstevel@tonic-gate */ 21277c478bd9Sstevel@tonic-gate static void 21287c478bd9Sstevel@tonic-gate record_child(pid, prog, done, arg) 21297c478bd9Sstevel@tonic-gate pid_t pid; 21307c478bd9Sstevel@tonic-gate char *prog; 21317c478bd9Sstevel@tonic-gate void (*done) __P((void *, int)); 21327c478bd9Sstevel@tonic-gate void *arg; 21337c478bd9Sstevel@tonic-gate { 21347c478bd9Sstevel@tonic-gate struct subprocess *chp; 21357c478bd9Sstevel@tonic-gate 21367c478bd9Sstevel@tonic-gate ++n_children; 21377c478bd9Sstevel@tonic-gate 21387c478bd9Sstevel@tonic-gate chp = (struct subprocess *) malloc(sizeof(struct subprocess)); 21397c478bd9Sstevel@tonic-gate if (chp == NULL) { 21407c478bd9Sstevel@tonic-gate warn("losing track of %s process", prog); 21417c478bd9Sstevel@tonic-gate } else { 21427c478bd9Sstevel@tonic-gate chp->pid = pid; 21437c478bd9Sstevel@tonic-gate chp->prog = prog; 21447c478bd9Sstevel@tonic-gate chp->done = done; 21457c478bd9Sstevel@tonic-gate chp->arg = arg; 21467c478bd9Sstevel@tonic-gate chp->next = children; 21477c478bd9Sstevel@tonic-gate children = chp; 21487c478bd9Sstevel@tonic-gate } 21497c478bd9Sstevel@tonic-gate } 21507c478bd9Sstevel@tonic-gate 21517c478bd9Sstevel@tonic-gate 21527c478bd9Sstevel@tonic-gate /* 21537c478bd9Sstevel@tonic-gate * reap_kids - get status from any dead child processes, 21547c478bd9Sstevel@tonic-gate * and log a message for abnormal terminations. 21557c478bd9Sstevel@tonic-gate */ 21567c478bd9Sstevel@tonic-gate static int 21577c478bd9Sstevel@tonic-gate reap_kids(waitfor) 21587c478bd9Sstevel@tonic-gate int waitfor; 21597c478bd9Sstevel@tonic-gate { 21607c478bd9Sstevel@tonic-gate pid_t pid; 21617c478bd9Sstevel@tonic-gate int status, i; 21627c478bd9Sstevel@tonic-gate struct subprocess *chp, **prevp; 21637c478bd9Sstevel@tonic-gate 21647c478bd9Sstevel@tonic-gate got_sigchld = 0; 21657c478bd9Sstevel@tonic-gate if (n_children == 0) 21667c478bd9Sstevel@tonic-gate return (0); 21677c478bd9Sstevel@tonic-gate 21687c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ 21697c478bd9Sstevel@tonic-gate while (1) { 21707c478bd9Sstevel@tonic-gate pid = waitpid(-1, &status, (waitfor ? 0 : WNOHANG)); 21717c478bd9Sstevel@tonic-gate if (pid == 0) { 21727c478bd9Sstevel@tonic-gate break; /* return 0 */ 21737c478bd9Sstevel@tonic-gate } else if (pid == -1) { 21747c478bd9Sstevel@tonic-gate if (errno == EINTR) 21757c478bd9Sstevel@tonic-gate continue; 21767c478bd9Sstevel@tonic-gate if (errno != ECHILD) 21777c478bd9Sstevel@tonic-gate error("Error waiting for child process: %m"); 21787c478bd9Sstevel@tonic-gate return (-1); 21797c478bd9Sstevel@tonic-gate } else { 21807c478bd9Sstevel@tonic-gate for (prevp = &children; (chp = *prevp) != NULL; 21817c478bd9Sstevel@tonic-gate prevp = &chp->next) { 21827c478bd9Sstevel@tonic-gate if (chp->pid == pid) { 21837c478bd9Sstevel@tonic-gate --n_children; 21847c478bd9Sstevel@tonic-gate *prevp = chp->next; 21857c478bd9Sstevel@tonic-gate break; 21867c478bd9Sstevel@tonic-gate } 21877c478bd9Sstevel@tonic-gate } 21887c478bd9Sstevel@tonic-gate if (WIFSIGNALED(status) || WIFSTOPPED(status)) { 21897c478bd9Sstevel@tonic-gate i = WIFSIGNALED(status) ? WTERMSIG(status) : WSTOPSIG(status); 21907c478bd9Sstevel@tonic-gate warn("Child process %s (pid %d) %s with signal %d (%s)", 21917c478bd9Sstevel@tonic-gate (chp != NULL ? chp->prog : "??"), pid, 21927c478bd9Sstevel@tonic-gate (WIFSIGNALED(status) ? "terminated" : "stopped"), 21937c478bd9Sstevel@tonic-gate i, signal_name(i)); 21947c478bd9Sstevel@tonic-gate } else if (debug) { 21957c478bd9Sstevel@tonic-gate dbglog("Child process %s finished (pid %d), status = %d", 21967c478bd9Sstevel@tonic-gate (chp != NULL ? chp->prog: "??"), pid, 21977c478bd9Sstevel@tonic-gate WEXITSTATUS(status)); 21987c478bd9Sstevel@tonic-gate } 21997c478bd9Sstevel@tonic-gate if ((chp != NULL) && (chp->done != NULL)) 22007c478bd9Sstevel@tonic-gate (*chp->done)(chp->arg, status); 22017c478bd9Sstevel@tonic-gate if (chp != NULL) 22027c478bd9Sstevel@tonic-gate free(chp); 22037c478bd9Sstevel@tonic-gate } 22047c478bd9Sstevel@tonic-gate } 22057c478bd9Sstevel@tonic-gate return (0); 22067c478bd9Sstevel@tonic-gate } 22077c478bd9Sstevel@tonic-gate 22087c478bd9Sstevel@tonic-gate /* 22097c478bd9Sstevel@tonic-gate * infanticide - timeout while waiting for child process. 22107c478bd9Sstevel@tonic-gate */ 22117c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 22127c478bd9Sstevel@tonic-gate static void 22137c478bd9Sstevel@tonic-gate infanticide(sig) 22147c478bd9Sstevel@tonic-gate int sig; 22157c478bd9Sstevel@tonic-gate { 22167c478bd9Sstevel@tonic-gate struct subprocess *chp; 22177c478bd9Sstevel@tonic-gate static int runcount = 0; 22187c478bd9Sstevel@tonic-gate 22197c478bd9Sstevel@tonic-gate if (runcount < 2) { 22207c478bd9Sstevel@tonic-gate for (chp = children; chp != NULL; chp = chp->next) 22217c478bd9Sstevel@tonic-gate (void) kill(chp->pid, runcount == 0 ? SIGTERM : SIGKILL); 22227c478bd9Sstevel@tonic-gate } else { 22237c478bd9Sstevel@tonic-gate kill_my_pg(SIGTERM); 22247c478bd9Sstevel@tonic-gate /* Quit and hope for the best. */ 22257c478bd9Sstevel@tonic-gate n_children = 0; 22267c478bd9Sstevel@tonic-gate } 22277c478bd9Sstevel@tonic-gate runcount++; 22287c478bd9Sstevel@tonic-gate } 22297c478bd9Sstevel@tonic-gate 22307c478bd9Sstevel@tonic-gate /* 22317c478bd9Sstevel@tonic-gate * Perform final wait before exiting. 22327c478bd9Sstevel@tonic-gate */ 22337c478bd9Sstevel@tonic-gate static void 22347c478bd9Sstevel@tonic-gate final_reap() 22357c478bd9Sstevel@tonic-gate { 22367c478bd9Sstevel@tonic-gate struct sigaction sa; 22377c478bd9Sstevel@tonic-gate struct subprocess *chp; 22387c478bd9Sstevel@tonic-gate 22397c478bd9Sstevel@tonic-gate if (n_children > 0 && debug) { 22407c478bd9Sstevel@tonic-gate dbglog("Waiting for %d child processes...", n_children); 22417c478bd9Sstevel@tonic-gate for (chp = children; chp != NULL; chp = chp->next) 22427c478bd9Sstevel@tonic-gate dbglog(" pid %d: %s", chp->pid, chp->prog); 22437c478bd9Sstevel@tonic-gate } 22447c478bd9Sstevel@tonic-gate BZERO(&sa, sizeof (sa)); 22457c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ SIGNAL(SIGALRM, infanticide); 22467c478bd9Sstevel@tonic-gate while (n_children > 0) { 22477c478bd9Sstevel@tonic-gate (void) alarm(7); 22487c478bd9Sstevel@tonic-gate if (reap_kids(1) < 0) 22497c478bd9Sstevel@tonic-gate break; 22507c478bd9Sstevel@tonic-gate } 22517c478bd9Sstevel@tonic-gate (void) alarm(0); 22527c478bd9Sstevel@tonic-gate } 22537c478bd9Sstevel@tonic-gate 22547c478bd9Sstevel@tonic-gate /* 22557c478bd9Sstevel@tonic-gate * novm - log an error message saying we ran out of memory, and die. 22567c478bd9Sstevel@tonic-gate */ 22577c478bd9Sstevel@tonic-gate void 22587c478bd9Sstevel@tonic-gate novm(msg) 22597c478bd9Sstevel@tonic-gate char *msg; 22607c478bd9Sstevel@tonic-gate { 22617c478bd9Sstevel@tonic-gate fatal("Virtual memory exhausted allocating %s\n", msg); 22627c478bd9Sstevel@tonic-gate } 22637c478bd9Sstevel@tonic-gate 22647c478bd9Sstevel@tonic-gate /* 22657c478bd9Sstevel@tonic-gate * script_setenv - set an environment variable value to be used 22667c478bd9Sstevel@tonic-gate * for scripts that we run (e.g. ip-up, auth-up, etc.) 22677c478bd9Sstevel@tonic-gate */ 22687c478bd9Sstevel@tonic-gate void 22697c478bd9Sstevel@tonic-gate script_setenv(var, value, iskey) 22707c478bd9Sstevel@tonic-gate const char *var; 22717c478bd9Sstevel@tonic-gate const char *value; 22727c478bd9Sstevel@tonic-gate int iskey; 22737c478bd9Sstevel@tonic-gate { 22747c478bd9Sstevel@tonic-gate size_t varl = strlen(var); 22757c478bd9Sstevel@tonic-gate size_t vl = varl + strlen(value) + 2; 22767c478bd9Sstevel@tonic-gate int i; 22777c478bd9Sstevel@tonic-gate char *p, *newstring; 22787c478bd9Sstevel@tonic-gate 22797c478bd9Sstevel@tonic-gate /* 22807c478bd9Sstevel@tonic-gate * XXX: Can we assert that a tdb write lock is held here ? It appears that 22817c478bd9Sstevel@tonic-gate * Linux's use of tdb is not safe. 22827c478bd9Sstevel@tonic-gate */ 22837c478bd9Sstevel@tonic-gate newstring = (char *) malloc(vl+1); 22847c478bd9Sstevel@tonic-gate if (newstring == NULL) { 22857c478bd9Sstevel@tonic-gate novm("script environment string"); 22867c478bd9Sstevel@tonic-gate return; 22877c478bd9Sstevel@tonic-gate } 22887c478bd9Sstevel@tonic-gate *newstring++ = iskey; 22897c478bd9Sstevel@tonic-gate (void) slprintf(newstring, vl, "%s=%s", var, value); 22907c478bd9Sstevel@tonic-gate 22917c478bd9Sstevel@tonic-gate /* check if this variable is already set */ 22927c478bd9Sstevel@tonic-gate if (script_env != NULL) { 22937c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) { 22947c478bd9Sstevel@tonic-gate if (strncmp(p, var, varl) == 0 && p[varl] == '=') { 22957c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 22967c478bd9Sstevel@tonic-gate if (p[-1] != '\0' && pppdb != NULL) 22977c478bd9Sstevel@tonic-gate delete_db_key(p); 22987c478bd9Sstevel@tonic-gate #endif 22997c478bd9Sstevel@tonic-gate free(p-1); 23007c478bd9Sstevel@tonic-gate script_env[i] = newstring; 23017c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 23027c478bd9Sstevel@tonic-gate if (iskey && pppdb != NULL) 23037c478bd9Sstevel@tonic-gate add_db_key(newstring); 23047c478bd9Sstevel@tonic-gate update_db_entry(); 23057c478bd9Sstevel@tonic-gate #endif 23067c478bd9Sstevel@tonic-gate return; 23077c478bd9Sstevel@tonic-gate } 23087c478bd9Sstevel@tonic-gate } 23097c478bd9Sstevel@tonic-gate } else { 23107c478bd9Sstevel@tonic-gate /* no space allocated for script env. ptrs. yet */ 23117c478bd9Sstevel@tonic-gate i = 0; 23127c478bd9Sstevel@tonic-gate script_env = (char **) malloc(16 * sizeof(char *)); 23137c478bd9Sstevel@tonic-gate if (script_env == NULL) { 23147c478bd9Sstevel@tonic-gate novm("script environment variable."); 23157c478bd9Sstevel@tonic-gate return; 23167c478bd9Sstevel@tonic-gate } 23177c478bd9Sstevel@tonic-gate s_env_nalloc = 16; 23187c478bd9Sstevel@tonic-gate } 23197c478bd9Sstevel@tonic-gate 23207c478bd9Sstevel@tonic-gate /* reallocate script_env with more space if needed */ 23217c478bd9Sstevel@tonic-gate if (i + 1 >= s_env_nalloc) { 23227c478bd9Sstevel@tonic-gate int new_n = i + 17; 23237c478bd9Sstevel@tonic-gate char **newenv = (char **) realloc((void *)script_env, 23247c478bd9Sstevel@tonic-gate new_n * sizeof(char *)); 23257c478bd9Sstevel@tonic-gate if (newenv == NULL) { 23267c478bd9Sstevel@tonic-gate novm("expanded script environment variable."); 23277c478bd9Sstevel@tonic-gate return; 23287c478bd9Sstevel@tonic-gate } 23297c478bd9Sstevel@tonic-gate script_env = newenv; 23307c478bd9Sstevel@tonic-gate s_env_nalloc = new_n; 23317c478bd9Sstevel@tonic-gate } 23327c478bd9Sstevel@tonic-gate 23337c478bd9Sstevel@tonic-gate script_env[i] = newstring; 23347c478bd9Sstevel@tonic-gate script_env[i+1] = NULL; 23357c478bd9Sstevel@tonic-gate 23367c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 23377c478bd9Sstevel@tonic-gate if (pppdb != NULL) { 23387c478bd9Sstevel@tonic-gate if (iskey) 23397c478bd9Sstevel@tonic-gate add_db_key(newstring); 23407c478bd9Sstevel@tonic-gate update_db_entry(); 23417c478bd9Sstevel@tonic-gate } 23427c478bd9Sstevel@tonic-gate #endif 23437c478bd9Sstevel@tonic-gate } 23447c478bd9Sstevel@tonic-gate 23457c478bd9Sstevel@tonic-gate /* 23467c478bd9Sstevel@tonic-gate * script_unsetenv - remove a variable from the environment 23477c478bd9Sstevel@tonic-gate * for scripts. 23487c478bd9Sstevel@tonic-gate */ 23497c478bd9Sstevel@tonic-gate void 23507c478bd9Sstevel@tonic-gate script_unsetenv(var) 23517c478bd9Sstevel@tonic-gate const char *var; 23527c478bd9Sstevel@tonic-gate { 23537c478bd9Sstevel@tonic-gate int vl = strlen(var); 23547c478bd9Sstevel@tonic-gate int i; 23557c478bd9Sstevel@tonic-gate char *p; 23567c478bd9Sstevel@tonic-gate 23577c478bd9Sstevel@tonic-gate /* 23587c478bd9Sstevel@tonic-gate * XXX: Can we assert that a tdb write lock is held here ? It appears that 23597c478bd9Sstevel@tonic-gate * Linux's use of tdb is not safe. 23607c478bd9Sstevel@tonic-gate */ 23617c478bd9Sstevel@tonic-gate if (script_env == NULL) 23627c478bd9Sstevel@tonic-gate return; 23637c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) { 23647c478bd9Sstevel@tonic-gate if (strncmp(p, var, vl) == 0 && p[vl] == '=') { 23657c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 23667c478bd9Sstevel@tonic-gate if (p[-1] != '\0' && pppdb != NULL) 23677c478bd9Sstevel@tonic-gate delete_db_key(p); 23687c478bd9Sstevel@tonic-gate #endif 23697c478bd9Sstevel@tonic-gate free(p-1); 23707c478bd9Sstevel@tonic-gate while ((script_env[i] = script_env[i+1]) != NULL) 23717c478bd9Sstevel@tonic-gate ++i; 23727c478bd9Sstevel@tonic-gate break; 23737c478bd9Sstevel@tonic-gate } 23747c478bd9Sstevel@tonic-gate } 23757c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 23767c478bd9Sstevel@tonic-gate if ((pppdb != NULL) && (p != NULL)) 23777c478bd9Sstevel@tonic-gate update_db_entry(); 23787c478bd9Sstevel@tonic-gate #endif 23797c478bd9Sstevel@tonic-gate } 23807c478bd9Sstevel@tonic-gate 23817c478bd9Sstevel@tonic-gate /* 23827c478bd9Sstevel@tonic-gate * script_getenv - find a variable in the script environment. 23837c478bd9Sstevel@tonic-gate */ 23847c478bd9Sstevel@tonic-gate const char * 23857c478bd9Sstevel@tonic-gate script_getenv(var) 23867c478bd9Sstevel@tonic-gate const char *var; 23877c478bd9Sstevel@tonic-gate { 23887c478bd9Sstevel@tonic-gate int vl = strlen(var); 23897c478bd9Sstevel@tonic-gate int i; 23907c478bd9Sstevel@tonic-gate char *p; 23917c478bd9Sstevel@tonic-gate 23927c478bd9Sstevel@tonic-gate if (script_env == NULL) 23937c478bd9Sstevel@tonic-gate return (NULL); 23947c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) { 23957c478bd9Sstevel@tonic-gate if (strncmp(p, var, vl) == 0 && p[vl] == '=') 23967c478bd9Sstevel@tonic-gate return ((const char *)p+vl+1); 23977c478bd9Sstevel@tonic-gate } 23987c478bd9Sstevel@tonic-gate return (NULL); 23997c478bd9Sstevel@tonic-gate } 24007c478bd9Sstevel@tonic-gate 24017c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK 24027c478bd9Sstevel@tonic-gate /* 24037c478bd9Sstevel@tonic-gate * update_db_entry - update our entry in the database. 24047c478bd9Sstevel@tonic-gate */ 24057c478bd9Sstevel@tonic-gate static void 24067c478bd9Sstevel@tonic-gate update_db_entry() 24077c478bd9Sstevel@tonic-gate { 24087c478bd9Sstevel@tonic-gate TDB_DATA key, dbuf; 24097c478bd9Sstevel@tonic-gate int vlen, i; 24107c478bd9Sstevel@tonic-gate char *p, *q, *vbuf; 24117c478bd9Sstevel@tonic-gate 24127c478bd9Sstevel@tonic-gate if (script_env == NULL) 24137c478bd9Sstevel@tonic-gate return; 24147c478bd9Sstevel@tonic-gate /* 24157c478bd9Sstevel@tonic-gate * vlen needs to be initialized as 1, or otherwise, the last string 24167c478bd9Sstevel@tonic-gate * is truncated by slprintf. 24177c478bd9Sstevel@tonic-gate */ 24187c478bd9Sstevel@tonic-gate vlen = 1; 24197c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) 24207c478bd9Sstevel@tonic-gate vlen += strlen(p) + 1; 24217c478bd9Sstevel@tonic-gate vbuf = malloc(vlen); 24227c478bd9Sstevel@tonic-gate if (vbuf == NULL) 24237c478bd9Sstevel@tonic-gate novm("database entry"); 24247c478bd9Sstevel@tonic-gate q = vbuf; 24257c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) 24267c478bd9Sstevel@tonic-gate q += slprintf(q, vbuf + vlen - q, "%s;", p); 24277c478bd9Sstevel@tonic-gate 24287c478bd9Sstevel@tonic-gate key.dptr = db_key; 24297c478bd9Sstevel@tonic-gate key.dsize = strlen(db_key); 24307c478bd9Sstevel@tonic-gate dbuf.dptr = vbuf; 24317c478bd9Sstevel@tonic-gate dbuf.dsize = vlen; 24327c478bd9Sstevel@tonic-gate if (tdb_store(pppdb, key, dbuf, TDB_REPLACE)) 24337c478bd9Sstevel@tonic-gate error("tdb_store failed: %s", tdb_error(pppdb)); 24347c478bd9Sstevel@tonic-gate } 24357c478bd9Sstevel@tonic-gate 24367c478bd9Sstevel@tonic-gate /* 24377c478bd9Sstevel@tonic-gate * add_db_key - add a key that we can use to look up our database entry. 24387c478bd9Sstevel@tonic-gate */ 24397c478bd9Sstevel@tonic-gate static void 24407c478bd9Sstevel@tonic-gate add_db_key(str) 24417c478bd9Sstevel@tonic-gate const char *str; 24427c478bd9Sstevel@tonic-gate { 24437c478bd9Sstevel@tonic-gate TDB_DATA key, dbuf; 24447c478bd9Sstevel@tonic-gate 24457c478bd9Sstevel@tonic-gate key.dptr = (char *) str; 24467c478bd9Sstevel@tonic-gate key.dsize = strlen(str); 24477c478bd9Sstevel@tonic-gate dbuf.dptr = db_key; 24487c478bd9Sstevel@tonic-gate dbuf.dsize = strlen(db_key); 24497c478bd9Sstevel@tonic-gate if (tdb_store(pppdb, key, dbuf, TDB_REPLACE)) 24507c478bd9Sstevel@tonic-gate error("tdb_store key failed: %s", tdb_error(pppdb)); 24517c478bd9Sstevel@tonic-gate } 24527c478bd9Sstevel@tonic-gate 24537c478bd9Sstevel@tonic-gate /* 24547c478bd9Sstevel@tonic-gate * delete_db_key - delete a key for looking up our database entry. 24557c478bd9Sstevel@tonic-gate */ 24567c478bd9Sstevel@tonic-gate static void 24577c478bd9Sstevel@tonic-gate delete_db_key(str) 24587c478bd9Sstevel@tonic-gate const char *str; 24597c478bd9Sstevel@tonic-gate { 24607c478bd9Sstevel@tonic-gate TDB_DATA key; 24617c478bd9Sstevel@tonic-gate 24627c478bd9Sstevel@tonic-gate key.dptr = (char *) str; 24637c478bd9Sstevel@tonic-gate key.dsize = strlen(str); 24647c478bd9Sstevel@tonic-gate (void) tdb_delete(pppdb, key); 24657c478bd9Sstevel@tonic-gate } 24667c478bd9Sstevel@tonic-gate 24677c478bd9Sstevel@tonic-gate /* 24687c478bd9Sstevel@tonic-gate * cleanup_db - delete all the entries we put in the database. 24697c478bd9Sstevel@tonic-gate */ 24707c478bd9Sstevel@tonic-gate static void 24717c478bd9Sstevel@tonic-gate cleanup_db() 24727c478bd9Sstevel@tonic-gate { 24737c478bd9Sstevel@tonic-gate TDB_DATA key; 24747c478bd9Sstevel@tonic-gate int i; 24757c478bd9Sstevel@tonic-gate char *p; 24767c478bd9Sstevel@tonic-gate 24777c478bd9Sstevel@tonic-gate key.dptr = db_key; 24787c478bd9Sstevel@tonic-gate key.dsize = strlen(db_key); 24797c478bd9Sstevel@tonic-gate (void) tdb_delete(pppdb, key); 24807c478bd9Sstevel@tonic-gate for (i = 0; (p = script_env[i]) != NULL; ++i) 24817c478bd9Sstevel@tonic-gate if (p[-1] != '\0') 24827c478bd9Sstevel@tonic-gate delete_db_key(p); 24837c478bd9Sstevel@tonic-gate } 24847c478bd9Sstevel@tonic-gate #endif /* HAVE_MULTILINK */ 24857c478bd9Sstevel@tonic-gate 24867c478bd9Sstevel@tonic-gate /* 24877c478bd9Sstevel@tonic-gate * open_socket - establish a stream socket connection to the nominated 24887c478bd9Sstevel@tonic-gate * host and port. 24897c478bd9Sstevel@tonic-gate * XXX: Need IPv6 support for those systems that support it (use getaddrinfo), 24907c478bd9Sstevel@tonic-gate * but requires portability changes. 24917c478bd9Sstevel@tonic-gate */ 24927c478bd9Sstevel@tonic-gate static int 24937c478bd9Sstevel@tonic-gate open_socket(dest) 24947c478bd9Sstevel@tonic-gate char *dest; 24957c478bd9Sstevel@tonic-gate { 24967c478bd9Sstevel@tonic-gate char *sep, *endp = NULL; 24977c478bd9Sstevel@tonic-gate int sock; 24987c478bd9Sstevel@tonic-gate int port = -1; 24997c478bd9Sstevel@tonic-gate u_int32_t host; 25007c478bd9Sstevel@tonic-gate struct hostent *hent = NULL; 25017c478bd9Sstevel@tonic-gate struct sockaddr_in sad; 25027c478bd9Sstevel@tonic-gate struct servent *se; 25037c478bd9Sstevel@tonic-gate 25047c478bd9Sstevel@tonic-gate /* parse host:port and resolve host to an IP address */ 25057c478bd9Sstevel@tonic-gate sep = strchr(dest, ':'); 25067c478bd9Sstevel@tonic-gate if (sep != NULL) { 25077c478bd9Sstevel@tonic-gate se = getservbyname((const char *)sep+1, "tcp"); 25087c478bd9Sstevel@tonic-gate if (se != NULL) { 25097c478bd9Sstevel@tonic-gate port = ntohs(se->s_port); 25107c478bd9Sstevel@tonic-gate } else { 25117c478bd9Sstevel@tonic-gate port = strtol(sep+1, &endp, 10); 25127c478bd9Sstevel@tonic-gate if (endp == sep+1 || *endp != '\0') { 25137c478bd9Sstevel@tonic-gate error("Can't parse host:port for socket destination"); 25147c478bd9Sstevel@tonic-gate return (-1); 25157c478bd9Sstevel@tonic-gate } 25167c478bd9Sstevel@tonic-gate } 25177c478bd9Sstevel@tonic-gate } 25187c478bd9Sstevel@tonic-gate if (port < 0 || port > 65535 || sep == dest) { 25197c478bd9Sstevel@tonic-gate error("Can't parse host:port for socket destination"); 25207c478bd9Sstevel@tonic-gate return (-1); 25217c478bd9Sstevel@tonic-gate } 25227c478bd9Sstevel@tonic-gate *sep = '\0'; 25237c478bd9Sstevel@tonic-gate host = inet_addr(dest); 25247c478bd9Sstevel@tonic-gate if (host == (u_int32_t) -1) { 25257c478bd9Sstevel@tonic-gate hent = gethostbyname(dest); 25267c478bd9Sstevel@tonic-gate if (hent == NULL) { 25277c478bd9Sstevel@tonic-gate error("%s: unknown host in socket option", dest); 25287c478bd9Sstevel@tonic-gate *sep = ':'; 25297c478bd9Sstevel@tonic-gate return (-1); 25307c478bd9Sstevel@tonic-gate } 25317c478bd9Sstevel@tonic-gate BCOPY(hent->h_addr_list[0], &host, sizeof(host)); 25327c478bd9Sstevel@tonic-gate hent->h_addr_list++; 25337c478bd9Sstevel@tonic-gate } 25347c478bd9Sstevel@tonic-gate *sep = ':'; 25357c478bd9Sstevel@tonic-gate 25367c478bd9Sstevel@tonic-gate for (;;) { 25377c478bd9Sstevel@tonic-gate /* get a socket and connect it to the other end */ 25387c478bd9Sstevel@tonic-gate sock = socket(PF_INET, SOCK_STREAM, 0); 25397c478bd9Sstevel@tonic-gate if (sock < 0) { 25407c478bd9Sstevel@tonic-gate error("Can't create socket: %m"); 25417c478bd9Sstevel@tonic-gate return (-1); 25427c478bd9Sstevel@tonic-gate } 25437c478bd9Sstevel@tonic-gate BZERO(&sad, sizeof(sad)); 25447c478bd9Sstevel@tonic-gate sad.sin_family = AF_INET; 25457c478bd9Sstevel@tonic-gate sad.sin_port = htons(port); 25467c478bd9Sstevel@tonic-gate sad.sin_addr.s_addr = host; 25477c478bd9Sstevel@tonic-gate if (connect(sock, (struct sockaddr *)&sad, sizeof(sad)) >= 0) { 25487c478bd9Sstevel@tonic-gate break; /* return sock file descriptor */ 25497c478bd9Sstevel@tonic-gate } 25507c478bd9Sstevel@tonic-gate if ((hent != NULL) && (hent->h_addr_list != NULL)) { 25517c478bd9Sstevel@tonic-gate BCOPY(hent->h_addr_list[0], &host, sizeof(host)); 25527c478bd9Sstevel@tonic-gate hent->h_addr_list++; 25537c478bd9Sstevel@tonic-gate (void) close(sock); 25547c478bd9Sstevel@tonic-gate continue; 25557c478bd9Sstevel@tonic-gate } 25567c478bd9Sstevel@tonic-gate error("Can't connect to %s: %m", dest); 25577c478bd9Sstevel@tonic-gate (void) close(sock); 25587c478bd9Sstevel@tonic-gate return (-1); 25597c478bd9Sstevel@tonic-gate } 25607c478bd9Sstevel@tonic-gate return (sock); 25617c478bd9Sstevel@tonic-gate } 25627c478bd9Sstevel@tonic-gate 25637c478bd9Sstevel@tonic-gate /* 25647c478bd9Sstevel@tonic-gate * print_ncpstate - prints out current NCP state. 25657c478bd9Sstevel@tonic-gate * 25667c478bd9Sstevel@tonic-gate * We're normally called from SIGUSR1 here, but this is safe because 25677c478bd9Sstevel@tonic-gate * these signals are blocked unless we're idle waiting for events. 25687c478bd9Sstevel@tonic-gate * There's no need to otherwise lock the data structures referenced. 25697c478bd9Sstevel@tonic-gate */ 25707c478bd9Sstevel@tonic-gate void 25717c478bd9Sstevel@tonic-gate print_ncpstate(unit, strptr) 25727c478bd9Sstevel@tonic-gate int unit; 25737c478bd9Sstevel@tonic-gate FILE *strptr; 25747c478bd9Sstevel@tonic-gate { 25757c478bd9Sstevel@tonic-gate struct protent *protp; 25767c478bd9Sstevel@tonic-gate int i; 25777c478bd9Sstevel@tonic-gate 25787c478bd9Sstevel@tonic-gate (void) flprintf(strptr, "In %s phase\n", phase_name(phase)); 25797c478bd9Sstevel@tonic-gate for (i = 0; (protp = protocols[i]) != NULL; ++i) { 25807c478bd9Sstevel@tonic-gate if (protp->print_stat != NULL) 25817c478bd9Sstevel@tonic-gate (*protp->print_stat)(unit, strptr); 25827c478bd9Sstevel@tonic-gate } 25837c478bd9Sstevel@tonic-gate sys_print_state(strptr); 25847c478bd9Sstevel@tonic-gate } 25857c478bd9Sstevel@tonic-gate 25867c478bd9Sstevel@tonic-gate /* 25877c478bd9Sstevel@tonic-gate * start_charshunt - create a child process to run the character shunt. 25887c478bd9Sstevel@tonic-gate */ 25897c478bd9Sstevel@tonic-gate static int 25907c478bd9Sstevel@tonic-gate start_charshunt(ifd, ofd) 25917c478bd9Sstevel@tonic-gate int ifd, ofd; 25927c478bd9Sstevel@tonic-gate { 25937c478bd9Sstevel@tonic-gate pid_t cpid; 25947c478bd9Sstevel@tonic-gate 25957c478bd9Sstevel@tonic-gate cpid = fork(); 25967c478bd9Sstevel@tonic-gate if (cpid == (pid_t)-1) { 25977c478bd9Sstevel@tonic-gate error("Can't fork process for character shunt: %m"); 25987c478bd9Sstevel@tonic-gate return (0); 25997c478bd9Sstevel@tonic-gate } 26007c478bd9Sstevel@tonic-gate if (cpid == (pid_t)0) { 26017c478bd9Sstevel@tonic-gate /* child */ 26027c478bd9Sstevel@tonic-gate (void) close(pty_slave); 26037c478bd9Sstevel@tonic-gate pty_slave = -1; 26047c478bd9Sstevel@tonic-gate (void) setgid(getgid()); 26057c478bd9Sstevel@tonic-gate (void) setuid(uid); 26067c478bd9Sstevel@tonic-gate if (getuid() != uid) 26077c478bd9Sstevel@tonic-gate fatal("setuid failed"); 26087c478bd9Sstevel@tonic-gate if (!nodetach) 26097c478bd9Sstevel@tonic-gate log_to_fd = -1; 26107c478bd9Sstevel@tonic-gate charshunt(ifd, ofd, record_file); 26117c478bd9Sstevel@tonic-gate exit(0); 26127c478bd9Sstevel@tonic-gate } 26137c478bd9Sstevel@tonic-gate charshunt_pid = cpid; 26147c478bd9Sstevel@tonic-gate (void) close(pty_master); 26157c478bd9Sstevel@tonic-gate pty_master = -1; 26167c478bd9Sstevel@tonic-gate ttyfd = pty_slave; 26177c478bd9Sstevel@tonic-gate record_child(cpid, "pppd (charshunt)", charshunt_done, NULL); 26187c478bd9Sstevel@tonic-gate return (1); 26197c478bd9Sstevel@tonic-gate } 26207c478bd9Sstevel@tonic-gate 26217c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 26227c478bd9Sstevel@tonic-gate static void 26237c478bd9Sstevel@tonic-gate charshunt_done(arg, status) 26247c478bd9Sstevel@tonic-gate void *arg; 26257c478bd9Sstevel@tonic-gate int status; 26267c478bd9Sstevel@tonic-gate { 26277c478bd9Sstevel@tonic-gate charshunt_pid = (pid_t)0; 26287c478bd9Sstevel@tonic-gate } 26297c478bd9Sstevel@tonic-gate 26307c478bd9Sstevel@tonic-gate static void 26317c478bd9Sstevel@tonic-gate reportme(int signo) 26327c478bd9Sstevel@tonic-gate { 26337c478bd9Sstevel@tonic-gate dbglog("charshunt taking signal %d", signo); 26347c478bd9Sstevel@tonic-gate exit(1); 26357c478bd9Sstevel@tonic-gate } 26367c478bd9Sstevel@tonic-gate 26377c478bd9Sstevel@tonic-gate /* 26387c478bd9Sstevel@tonic-gate * charshunt - the character shunt, which passes characters between 26397c478bd9Sstevel@tonic-gate * the pty master side and the serial port (or stdin/stdout). 26407c478bd9Sstevel@tonic-gate * This runs as the user (not as root). 26417c478bd9Sstevel@tonic-gate * (We assume ofd >= ifd which is true the way this gets called. :-). 26427c478bd9Sstevel@tonic-gate */ 26437c478bd9Sstevel@tonic-gate static void 26447c478bd9Sstevel@tonic-gate charshunt(ifd, ofd, record_file) 26457c478bd9Sstevel@tonic-gate int ifd, ofd; 26467c478bd9Sstevel@tonic-gate char *record_file; 26477c478bd9Sstevel@tonic-gate { 26487c478bd9Sstevel@tonic-gate int n, nfds; 26497c478bd9Sstevel@tonic-gate fd_set ready, writey; 26507c478bd9Sstevel@tonic-gate u_char *ibufp, *obufp; 26517c478bd9Sstevel@tonic-gate int nibuf, nobuf; 26527c478bd9Sstevel@tonic-gate int flags; 26537c478bd9Sstevel@tonic-gate struct timeval lasttime; 26547c478bd9Sstevel@tonic-gate FILE *recordf = NULL; 26557c478bd9Sstevel@tonic-gate int ilevel, olevel, max_level; 26567c478bd9Sstevel@tonic-gate struct timeval levelt, tout, *top; 26577c478bd9Sstevel@tonic-gate 26587c478bd9Sstevel@tonic-gate /* 26597c478bd9Sstevel@tonic-gate * Reset signal handlers. 26607c478bd9Sstevel@tonic-gate */ 26617c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); /* Hangup */ 26627c478bd9Sstevel@tonic-gate (void) signal(SIGINT, reportme); /* Interrupt */ 26637c478bd9Sstevel@tonic-gate (void) signal(SIGTERM, reportme); /* Terminate */ 26647c478bd9Sstevel@tonic-gate (void) signal(SIGCHLD, reportme); 26657c478bd9Sstevel@tonic-gate (void) signal(SIGUSR1, reportme); 26667c478bd9Sstevel@tonic-gate (void) signal(SIGUSR2, reportme); 26677c478bd9Sstevel@tonic-gate (void) signal(SIGABRT, reportme); 26687c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, reportme); 26697c478bd9Sstevel@tonic-gate (void) signal(SIGFPE, reportme); 26707c478bd9Sstevel@tonic-gate (void) signal(SIGILL, reportme); 26717c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, reportme); 26727c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, reportme); 26737c478bd9Sstevel@tonic-gate #ifndef DEBUG 26747c478bd9Sstevel@tonic-gate (void) signal(SIGSEGV, reportme); 26757c478bd9Sstevel@tonic-gate #endif 26767c478bd9Sstevel@tonic-gate #ifdef SIGBUS 26777c478bd9Sstevel@tonic-gate (void) signal(SIGBUS, reportme); 26787c478bd9Sstevel@tonic-gate #endif 26797c478bd9Sstevel@tonic-gate #ifdef SIGEMT 26807c478bd9Sstevel@tonic-gate (void) signal(SIGEMT, reportme); 26817c478bd9Sstevel@tonic-gate #endif 26827c478bd9Sstevel@tonic-gate #ifdef SIGPOLL 26837c478bd9Sstevel@tonic-gate (void) signal(SIGPOLL, reportme); 26847c478bd9Sstevel@tonic-gate #endif 26857c478bd9Sstevel@tonic-gate #ifdef SIGPROF 26867c478bd9Sstevel@tonic-gate (void) signal(SIGPROF, reportme); 26877c478bd9Sstevel@tonic-gate #endif 26887c478bd9Sstevel@tonic-gate #ifdef SIGSYS 26897c478bd9Sstevel@tonic-gate (void) signal(SIGSYS, reportme); 26907c478bd9Sstevel@tonic-gate #endif 26917c478bd9Sstevel@tonic-gate #ifdef SIGTRAP 26927c478bd9Sstevel@tonic-gate (void) signal(SIGTRAP, reportme); 26937c478bd9Sstevel@tonic-gate #endif 26947c478bd9Sstevel@tonic-gate #ifdef SIGVTALRM 26957c478bd9Sstevel@tonic-gate (void) signal(SIGVTALRM, reportme); 26967c478bd9Sstevel@tonic-gate #endif 26977c478bd9Sstevel@tonic-gate #ifdef SIGXCPU 26987c478bd9Sstevel@tonic-gate (void) signal(SIGXCPU, reportme); 26997c478bd9Sstevel@tonic-gate #endif 27007c478bd9Sstevel@tonic-gate #ifdef SIGXFSZ 27017c478bd9Sstevel@tonic-gate (void) signal(SIGXFSZ, reportme); 27027c478bd9Sstevel@tonic-gate #endif 27037c478bd9Sstevel@tonic-gate 27047c478bd9Sstevel@tonic-gate /* 27057c478bd9Sstevel@tonic-gate * Open the record file if required. 27067c478bd9Sstevel@tonic-gate */ 27077c478bd9Sstevel@tonic-gate if (record_file != NULL) { 27087c478bd9Sstevel@tonic-gate recordf = fopen(record_file, "a"); 27097c478bd9Sstevel@tonic-gate if (recordf == NULL) 27107c478bd9Sstevel@tonic-gate error("Couldn't create record file %s: %m", record_file); 27117c478bd9Sstevel@tonic-gate } 27127c478bd9Sstevel@tonic-gate 27137c478bd9Sstevel@tonic-gate /* set all the fds to non-blocking mode */ 27147c478bd9Sstevel@tonic-gate flags = fcntl(pty_master, F_GETFL); 27157c478bd9Sstevel@tonic-gate if (flags == -1 27167c478bd9Sstevel@tonic-gate || fcntl(pty_master, F_SETFL, flags | O_NONBLOCK) == -1) 27177c478bd9Sstevel@tonic-gate warn("couldn't set pty master to nonblock: %m"); 27187c478bd9Sstevel@tonic-gate flags = fcntl(ifd, F_GETFL); 27197c478bd9Sstevel@tonic-gate if (flags == -1 27207c478bd9Sstevel@tonic-gate || fcntl(ifd, F_SETFL, flags | O_NONBLOCK) == -1) 27217c478bd9Sstevel@tonic-gate warn("couldn't set %s to nonblock: %m", (ifd==0? "stdin": "tty")); 27227c478bd9Sstevel@tonic-gate if (ofd != ifd) { 27237c478bd9Sstevel@tonic-gate flags = fcntl(ofd, F_GETFL); 27247c478bd9Sstevel@tonic-gate if (flags == -1 27257c478bd9Sstevel@tonic-gate || fcntl(ofd, F_SETFL, flags | O_NONBLOCK) == -1) 27267c478bd9Sstevel@tonic-gate warn("couldn't set stdout to nonblock: %m"); 27277c478bd9Sstevel@tonic-gate } 27287c478bd9Sstevel@tonic-gate 27297c478bd9Sstevel@tonic-gate nibuf = nobuf = 0; 27307c478bd9Sstevel@tonic-gate ibufp = obufp = NULL; 27317c478bd9Sstevel@tonic-gate 27327c478bd9Sstevel@tonic-gate ilevel = olevel = 0; 27337c478bd9Sstevel@tonic-gate (void) gettimeofday(&levelt, NULL); 27347c478bd9Sstevel@tonic-gate if (max_data_rate) { 27357c478bd9Sstevel@tonic-gate max_level = max_data_rate / 10; 27367c478bd9Sstevel@tonic-gate if (max_level < MAXLEVELMINSIZE) 27377c478bd9Sstevel@tonic-gate max_level = MAXLEVELMINSIZE; 27387c478bd9Sstevel@tonic-gate } else 27397c478bd9Sstevel@tonic-gate max_level = sizeof(inpacket_buf) + 1; 27407c478bd9Sstevel@tonic-gate 27417c478bd9Sstevel@tonic-gate nfds = (ofd > pty_master? ofd: pty_master) + 1; 27427c478bd9Sstevel@tonic-gate if (recordf != NULL) { 27437c478bd9Sstevel@tonic-gate (void) gettimeofday(&lasttime, NULL); 27447c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMESTART, recordf); /* put start marker */ 27457c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 24, recordf); 27467c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 16, recordf); 27477c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec >> 8, recordf); 27487c478bd9Sstevel@tonic-gate (void) putc(lasttime.tv_sec, recordf); 27497c478bd9Sstevel@tonic-gate lasttime.tv_usec = 0; 27507c478bd9Sstevel@tonic-gate } 27517c478bd9Sstevel@tonic-gate 27527c478bd9Sstevel@tonic-gate while (nibuf != 0 || nobuf != 0 || ofd >= 0 || pty_master >= 0) { 27537c478bd9Sstevel@tonic-gate top = 0; 27547c478bd9Sstevel@tonic-gate tout.tv_sec = 0; 27557c478bd9Sstevel@tonic-gate tout.tv_usec = 10000; 27567c478bd9Sstevel@tonic-gate FD_ZERO(&ready); 27577c478bd9Sstevel@tonic-gate FD_ZERO(&writey); 27587c478bd9Sstevel@tonic-gate if (nibuf != 0) { 27597c478bd9Sstevel@tonic-gate if (ilevel >= max_level) 27607c478bd9Sstevel@tonic-gate top = &tout; 27617c478bd9Sstevel@tonic-gate else if (pty_master >= 0) 27627c478bd9Sstevel@tonic-gate FD_SET(pty_master, &writey); 27637c478bd9Sstevel@tonic-gate } else if (ifd >= 0) 27647c478bd9Sstevel@tonic-gate FD_SET(ifd, &ready); 27657c478bd9Sstevel@tonic-gate if (nobuf != 0) { 27667c478bd9Sstevel@tonic-gate if (olevel >= max_level) 27677c478bd9Sstevel@tonic-gate top = &tout; 27687c478bd9Sstevel@tonic-gate else if (ofd >= 0) 27697c478bd9Sstevel@tonic-gate FD_SET(ofd, &writey); 27707c478bd9Sstevel@tonic-gate } else { 27717c478bd9Sstevel@tonic-gate /* Don't read from pty if it's gone or it has closed. */ 27727c478bd9Sstevel@tonic-gate if (pty_master >= 0 && ofd >= 0) 27737c478bd9Sstevel@tonic-gate FD_SET(pty_master, &ready); 27747c478bd9Sstevel@tonic-gate } 27757c478bd9Sstevel@tonic-gate if (select(nfds, &ready, &writey, NULL, top) < 0) { 27767c478bd9Sstevel@tonic-gate if (errno != EINTR) 27777c478bd9Sstevel@tonic-gate fatal("select"); 27787c478bd9Sstevel@tonic-gate continue; 27797c478bd9Sstevel@tonic-gate } 27807c478bd9Sstevel@tonic-gate if (max_data_rate) { 27817c478bd9Sstevel@tonic-gate double dt; 27827c478bd9Sstevel@tonic-gate int nbt; 27837c478bd9Sstevel@tonic-gate struct timeval now; 27847c478bd9Sstevel@tonic-gate 27857c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL); 27867c478bd9Sstevel@tonic-gate dt = (now.tv_sec - levelt.tv_sec 27877c478bd9Sstevel@tonic-gate + (now.tv_usec - levelt.tv_usec) / 1e6); 27887c478bd9Sstevel@tonic-gate nbt = (int)(dt * max_data_rate); 27897c478bd9Sstevel@tonic-gate ilevel = (nbt < 0 || nbt > ilevel)? 0: ilevel - nbt; 27907c478bd9Sstevel@tonic-gate olevel = (nbt < 0 || nbt > olevel)? 0: olevel - nbt; 27917c478bd9Sstevel@tonic-gate levelt = now; 27927c478bd9Sstevel@tonic-gate } else 27937c478bd9Sstevel@tonic-gate ilevel = olevel = 0; 27947c478bd9Sstevel@tonic-gate if (FD_ISSET(ifd, &ready)) { 27957c478bd9Sstevel@tonic-gate ibufp = inpacket_buf; 27967c478bd9Sstevel@tonic-gate nibuf = read(ifd, ibufp, sizeof(inpacket_buf)); 27977c478bd9Sstevel@tonic-gate if (nibuf < 0 && errno == EIO) 27987c478bd9Sstevel@tonic-gate nibuf = 0; 27997c478bd9Sstevel@tonic-gate if (nibuf < 0 || pty_master == -1) { 28007c478bd9Sstevel@tonic-gate if (errno != EINTR && errno != EAGAIN) { 28017c478bd9Sstevel@tonic-gate error("Error reading standard input: %m"); 28027c478bd9Sstevel@tonic-gate break; 28037c478bd9Sstevel@tonic-gate } 28047c478bd9Sstevel@tonic-gate nibuf = 0; 28057c478bd9Sstevel@tonic-gate } else if (nibuf == 0) { 28067c478bd9Sstevel@tonic-gate /* end of file from stdin */ 28077c478bd9Sstevel@tonic-gate (void) close(pty_master); 28087c478bd9Sstevel@tonic-gate pty_master = -1; 28097c478bd9Sstevel@tonic-gate (void) close(ifd); 28107c478bd9Sstevel@tonic-gate ifd = -1; 28117c478bd9Sstevel@tonic-gate if (recordf) 28127c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_ENDRECV, NULL, 0, 28137c478bd9Sstevel@tonic-gate &lasttime)) 28147c478bd9Sstevel@tonic-gate recordf = NULL; 28157c478bd9Sstevel@tonic-gate } else { 28167c478bd9Sstevel@tonic-gate FD_SET(pty_master, &writey); 28177c478bd9Sstevel@tonic-gate if (recordf) 28187c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_STARTRECV, ibufp, nibuf, 28197c478bd9Sstevel@tonic-gate &lasttime)) 28207c478bd9Sstevel@tonic-gate recordf = NULL; 28217c478bd9Sstevel@tonic-gate } 28227c478bd9Sstevel@tonic-gate } 28237c478bd9Sstevel@tonic-gate if (ofd >= 0 && pty_master >= 0 && FD_ISSET(pty_master, &ready)) { 28247c478bd9Sstevel@tonic-gate obufp = outpacket_buf; 28257c478bd9Sstevel@tonic-gate nobuf = read(pty_master, obufp, sizeof(outpacket_buf)); 28267c478bd9Sstevel@tonic-gate if (nobuf < 0 && errno == EIO) 28277c478bd9Sstevel@tonic-gate nobuf = 0; 28287c478bd9Sstevel@tonic-gate if (nobuf < 0 || ofd == -1) { 28297c478bd9Sstevel@tonic-gate if (!(errno == EINTR || errno == EAGAIN)) { 28307c478bd9Sstevel@tonic-gate error("Error reading pseudo-tty master: %m"); 28317c478bd9Sstevel@tonic-gate break; 28327c478bd9Sstevel@tonic-gate } 28337c478bd9Sstevel@tonic-gate nobuf = 0; 28347c478bd9Sstevel@tonic-gate } else if (nobuf == 0) { 28357c478bd9Sstevel@tonic-gate /* end of file from the pty - slave side has closed */ 28367c478bd9Sstevel@tonic-gate nibuf = 0; 28377c478bd9Sstevel@tonic-gate (void) close(ofd); 28387c478bd9Sstevel@tonic-gate ofd = -1; 28397c478bd9Sstevel@tonic-gate if (recordf) 28407c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_ENDSEND, NULL, 0, 28417c478bd9Sstevel@tonic-gate &lasttime)) 28427c478bd9Sstevel@tonic-gate recordf = NULL; 28437c478bd9Sstevel@tonic-gate } else { 28447c478bd9Sstevel@tonic-gate FD_SET(ofd, &writey); 28457c478bd9Sstevel@tonic-gate if (recordf) 28467c478bd9Sstevel@tonic-gate if (!record_write(recordf, RECMARK_STARTSEND, obufp, nobuf, 28477c478bd9Sstevel@tonic-gate &lasttime)) 28487c478bd9Sstevel@tonic-gate recordf = NULL; 28497c478bd9Sstevel@tonic-gate } 28507c478bd9Sstevel@tonic-gate } 28517c478bd9Sstevel@tonic-gate if (ofd == -1) 28527c478bd9Sstevel@tonic-gate nobuf = 0; 28537c478bd9Sstevel@tonic-gate else if (FD_ISSET(ofd, &writey)) { 28547c478bd9Sstevel@tonic-gate n = nobuf; 28557c478bd9Sstevel@tonic-gate if (olevel + n > max_level) 28567c478bd9Sstevel@tonic-gate n = max_level - olevel; 28577c478bd9Sstevel@tonic-gate n = write(ofd, obufp, n); 28587c478bd9Sstevel@tonic-gate if (n < 0) { 28597c478bd9Sstevel@tonic-gate if (errno == EIO) { 28607c478bd9Sstevel@tonic-gate (void) close(ofd); 28617c478bd9Sstevel@tonic-gate ofd = -1; 28627c478bd9Sstevel@tonic-gate nobuf = 0; 28637c478bd9Sstevel@tonic-gate } else if (errno != EAGAIN && errno != EINTR) { 28647c478bd9Sstevel@tonic-gate error("Error writing standard output: %m"); 28657c478bd9Sstevel@tonic-gate break; 28667c478bd9Sstevel@tonic-gate } 28677c478bd9Sstevel@tonic-gate } else { 28687c478bd9Sstevel@tonic-gate obufp += n; 28697c478bd9Sstevel@tonic-gate nobuf -= n; 28707c478bd9Sstevel@tonic-gate olevel += n; 28717c478bd9Sstevel@tonic-gate } 28727c478bd9Sstevel@tonic-gate } 28737c478bd9Sstevel@tonic-gate if (pty_master == -1) 28747c478bd9Sstevel@tonic-gate nibuf = 0; 28757c478bd9Sstevel@tonic-gate else if (FD_ISSET(pty_master, &writey)) { 28767c478bd9Sstevel@tonic-gate n = nibuf; 28777c478bd9Sstevel@tonic-gate if (ilevel + n > max_level) 28787c478bd9Sstevel@tonic-gate n = max_level - ilevel; 28797c478bd9Sstevel@tonic-gate n = write(pty_master, ibufp, n); 28807c478bd9Sstevel@tonic-gate if (n < 0) { 28817c478bd9Sstevel@tonic-gate if (errno == EAGAIN || errno == EINTR) 28827c478bd9Sstevel@tonic-gate continue; 28837c478bd9Sstevel@tonic-gate if (errno != EIO) { 28847c478bd9Sstevel@tonic-gate error("Error writing pseudo-tty master: %m"); 28857c478bd9Sstevel@tonic-gate break; 28867c478bd9Sstevel@tonic-gate } 28877c478bd9Sstevel@tonic-gate (void) close(pty_master); 28887c478bd9Sstevel@tonic-gate pty_master = -1; 28897c478bd9Sstevel@tonic-gate nibuf = 0; 28907c478bd9Sstevel@tonic-gate } else { 28917c478bd9Sstevel@tonic-gate ibufp += n; 28927c478bd9Sstevel@tonic-gate nibuf -= n; 28937c478bd9Sstevel@tonic-gate ilevel += n; 28947c478bd9Sstevel@tonic-gate } 28957c478bd9Sstevel@tonic-gate } 28967c478bd9Sstevel@tonic-gate } 28977c478bd9Sstevel@tonic-gate exit(0); 28987c478bd9Sstevel@tonic-gate } 28997c478bd9Sstevel@tonic-gate 29007c478bd9Sstevel@tonic-gate static int 29017c478bd9Sstevel@tonic-gate record_write(f, code, buf, nb, tp) 29027c478bd9Sstevel@tonic-gate FILE *f; 29037c478bd9Sstevel@tonic-gate int code; 29047c478bd9Sstevel@tonic-gate u_char *buf; 29057c478bd9Sstevel@tonic-gate int nb; 29067c478bd9Sstevel@tonic-gate struct timeval *tp; 29077c478bd9Sstevel@tonic-gate { 29087c478bd9Sstevel@tonic-gate struct timeval now; 29097c478bd9Sstevel@tonic-gate int diff; 29107c478bd9Sstevel@tonic-gate 29117c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL); 29127c478bd9Sstevel@tonic-gate now.tv_usec /= 100000; /* actually 1/10 s, not usec now */ 29137c478bd9Sstevel@tonic-gate diff = (now.tv_sec - tp->tv_sec) * 10 + (now.tv_usec - tp->tv_usec); 29147c478bd9Sstevel@tonic-gate if (diff > 0) { 29157c478bd9Sstevel@tonic-gate if (diff > 255) { 29167c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMEDELTA32, f); 29177c478bd9Sstevel@tonic-gate (void) putc(diff >> 24, f); 29187c478bd9Sstevel@tonic-gate (void) putc(diff >> 16, f); 29197c478bd9Sstevel@tonic-gate (void) putc(diff >> 8, f); 29207c478bd9Sstevel@tonic-gate (void) putc(diff, f); 29217c478bd9Sstevel@tonic-gate } else { 29227c478bd9Sstevel@tonic-gate (void) putc(RECMARK_TIMEDELTA8, f); 29237c478bd9Sstevel@tonic-gate (void) putc(diff, f); 29247c478bd9Sstevel@tonic-gate } 29257c478bd9Sstevel@tonic-gate *tp = now; 29267c478bd9Sstevel@tonic-gate } 29277c478bd9Sstevel@tonic-gate (void) putc(code, f); 29287c478bd9Sstevel@tonic-gate if (buf != NULL) { 29297c478bd9Sstevel@tonic-gate (void) putc(nb >> 8, f); 29307c478bd9Sstevel@tonic-gate (void) putc(nb, f); 29317c478bd9Sstevel@tonic-gate (void) fwrite(buf, nb, 1, f); 29327c478bd9Sstevel@tonic-gate } 29337c478bd9Sstevel@tonic-gate (void) fflush(f); 29347c478bd9Sstevel@tonic-gate if (ferror(f)) { 29357c478bd9Sstevel@tonic-gate error("Error writing record file: %m"); 29367c478bd9Sstevel@tonic-gate return (0); 29377c478bd9Sstevel@tonic-gate } 29387c478bd9Sstevel@tonic-gate return (1); 29397c478bd9Sstevel@tonic-gate } 2940