main.c revision 1.18
1/*	$OpenBSD: main.c,v 1.18 1997/09/05 04:32:42 millert Exp $	*/
2
3/*
4 * main.c - Point-to-Point Protocol main module
5 *
6 * Copyright (c) 1989 Carnegie Mellon University.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by Carnegie Mellon University.  The name of the
15 * University may not be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22#ifndef lint
23static char rcsid[] = "$OpenBSD: main.c,v 1.18 1997/09/05 04:32:42 millert Exp $";
24#endif
25
26#include <stdio.h>
27#include <ctype.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31#include <signal.h>
32#include <errno.h>
33#include <fcntl.h>
34#include <syslog.h>
35#include <netdb.h>
36#include <utmp.h>
37#include <pwd.h>
38#include <sys/param.h>
39#include <sys/types.h>
40#include <sys/wait.h>
41#include <sys/time.h>
42#include <sys/resource.h>
43#include <sys/stat.h>
44#include <sys/socket.h>
45#include <net/if.h>
46
47#include "pppd.h"
48#include "magic.h"
49#include "fsm.h"
50#include "lcp.h"
51#include "ipcp.h"
52#include "upap.h"
53#include "chap.h"
54#include "ccp.h"
55#include "pathnames.h"
56#include "patchlevel.h"
57
58#ifdef CBCP_SUPPORT
59#include "cbcp.h"
60#endif
61
62#if defined(SUNOS4)
63extern char *strerror();
64#endif
65
66#ifdef IPX_CHANGE
67#include "ipxcp.h"
68#endif /* IPX_CHANGE */
69#ifdef AT_CHANGE
70#include "atcp.h"
71#endif
72
73/* interface vars */
74char ifname[IFNAMSIZ];		/* Interface name */
75int ifunit;			/* Interface unit number */
76
77char *progname;			/* Name of this program */
78char hostname[MAXNAMELEN];	/* Our hostname */
79static char pidfilename[MAXPATHLEN];	/* name of pid file */
80static char default_devnam[MAXPATHLEN];	/* name of default device */
81static pid_t pid;		/* Our pid */
82static uid_t uid;		/* Our real user-id */
83static int conn_running;	/* we have a [dis]connector running */
84
85int ttyfd = -1;			/* Serial port file descriptor */
86mode_t tty_mode = -1;		/* Original access permissions to tty */
87int baud_rate;			/* Actual bits/second for serial device */
88int hungup;			/* terminal has been hung up */
89int privileged;			/* we're running as real uid root */
90int need_holdoff;		/* need holdoff period before restarting */
91
92int phase;			/* where the link is at */
93int kill_link;
94int open_ccp_flag;
95int redirect_stderr;		/* Connector's stderr should go to file */
96
97u_char outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for outgoing packet */
98u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */
99
100static int n_children;		/* # child processes still running */
101
102static int locked;		/* lock() has succeeded */
103
104char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n";
105
106/* Prototypes for procedures local to this file. */
107
108static void cleanup __P((void));
109static void close_tty __P((void));
110static void get_input __P((void));
111static void calltimeout __P((void));
112static struct timeval *timeleft __P((struct timeval *));
113static void kill_my_pg __P((int));
114static void hup __P((int));
115static void term __P((int));
116static void chld __P((int));
117static void toggle_debug __P((int));
118static void open_ccp __P((int));
119static void bad_signal __P((int));
120static void holdoff_end __P((caddr_t));
121static int device_script __P((char *, int, int));
122static void reap_kids __P((void));
123static void pr_log __P((void *, char *, ...));
124
125extern	char	*ttyname __P((int));
126extern	char	*getlogin __P((void));
127int main __P((int, char *[]));
128
129#ifdef ultrix
130#undef	O_NONBLOCK
131#define	O_NONBLOCK	O_NDELAY
132#endif
133
134#ifdef ULTRIX
135#define setlogmask(x)
136#endif
137
138/*
139 * PPP Data Link Layer "protocol" table.
140 * One entry per supported protocol.
141 * The last entry must be NULL.
142 */
143struct protent *protocols[] = {
144    &lcp_protent,
145    &pap_protent,
146    &chap_protent,
147#ifdef CBCP_SUPPORT
148    &cbcp_protent,
149#endif
150    &ipcp_protent,
151    &ccp_protent,
152#ifdef IPX_CHANGE
153    &ipxcp_protent,
154#endif
155#ifdef AT_CHANGE
156    &atcp_protent,
157#endif
158    NULL
159};
160
161int
162main(argc, argv)
163    int argc;
164    char *argv[];
165{
166    int i, nonblock, fdflags;
167    struct sigaction sa;
168    FILE *pidfile;
169    char *p;
170    struct passwd *pw;
171    struct timeval timo;
172    sigset_t mask;
173    struct protent *protp;
174    struct stat statbuf;
175
176    phase = PHASE_INITIALIZE;
177    p = ttyname(0);
178    if (p)
179	strcpy(devnam, p);
180    strcpy(default_devnam, devnam);
181
182    /* Initialize syslog facilities */
183#ifdef ULTRIX
184    openlog("pppd", LOG_PID);
185#else
186    openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
187    setlogmask(LOG_UPTO(LOG_INFO));
188#endif
189
190    if (gethostname(hostname, MAXNAMELEN) < 0 ) {
191	option_error("Couldn't get hostname: %m");
192	die(1);
193    }
194    hostname[MAXNAMELEN-1] = 0;
195
196    uid = getuid();
197    privileged = uid == 0;
198
199    /*
200     * Initialize to the standard option set, then parse, in order,
201     * the system options file, the user's options file,
202     * the tty's options file, and the command line arguments.
203     */
204    for (i = 0; (protp = protocols[i]) != NULL; ++i)
205	(*protp->init)(0);
206
207    progname = *argv;
208
209    if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1)
210	|| !options_from_user())
211	exit(1);
212    scan_args(argc-1, argv+1);	/* look for tty name on command line */
213    if (!options_for_tty()
214	|| !parse_args(argc-1, argv+1))
215	exit(1);
216
217    /*
218     * Check that we are running as root.
219     */
220    if (geteuid() != 0) {
221	option_error("must be root to run %s, since it is not setuid-root",
222		     argv[0]);
223	die(1);
224    }
225
226    if (!ppp_available()) {
227	option_error(no_ppp_msg);
228	exit(1);
229    }
230
231    /*
232     * Check that the options given are valid and consistent.
233     */
234    sys_check_options();
235    auth_check_options();
236    for (i = 0; (protp = protocols[i]) != NULL; ++i)
237	if (protp->check_options != NULL)
238	    (*protp->check_options)();
239    if (demand && connector == 0) {
240	option_error("connect script required for demand-dialling\n");
241	exit(1);
242    }
243
244    /*
245     * If the user has specified the default device name explicitly,
246     * pretend they hadn't.
247     */
248    if (!default_device && strcmp(devnam, default_devnam) == 0)
249	default_device = 1;
250    redirect_stderr = !nodetach || default_device;
251
252    /*
253     * Initialize system-dependent stuff and magic number package.
254     */
255    sys_init();
256    magic_init();
257    if (debug)
258	setlogmask(LOG_UPTO(LOG_DEBUG));
259
260    /*
261     * Detach ourselves from the terminal, if required,
262     * and identify who is running us.
263     */
264    if (!default_device && !nodetach && daemon(0, 0) < 0) {
265	perror("Couldn't detach from controlling terminal");
266	exit(1);
267    }
268    pid = getpid();
269    p = getlogin();
270    if (p == NULL) {
271	pw = getpwuid(uid);
272	if (pw != NULL && pw->pw_name != NULL)
273	    p = pw->pw_name;
274	else
275	    p = "(unknown)";
276    }
277    syslog(LOG_NOTICE, "pppd %s.%d%s started by %s, uid %d",
278	   VERSION, PATCHLEVEL, IMPLEMENTATION, p, uid);
279
280    /*
281     * Compute mask of all interesting signals and install signal handlers
282     * for each.  Only one signal handler may be active at a time.  Therefore,
283     * all other signals should be masked when any handler is executing.
284     */
285    sigemptyset(&mask);
286    sigaddset(&mask, SIGHUP);
287    sigaddset(&mask, SIGINT);
288    sigaddset(&mask, SIGTERM);
289    sigaddset(&mask, SIGCHLD);
290
291#define SIGNAL(s, handler)	{ \
292	sa.sa_handler = handler; \
293	if (sigaction(s, &sa, NULL) < 0) { \
294	    syslog(LOG_ERR, "Couldn't establish signal handler (%d): %m", s); \
295	    die(1); \
296	} \
297    }
298
299    sa.sa_mask = mask;
300    sa.sa_flags = 0;
301    SIGNAL(SIGHUP, hup);		/* Hangup */
302    SIGNAL(SIGINT, term);		/* Interrupt */
303    SIGNAL(SIGTERM, term);		/* Terminate */
304    SIGNAL(SIGCHLD, chld);
305
306    SIGNAL(SIGUSR1, toggle_debug);	/* Toggle debug flag */
307    SIGNAL(SIGUSR2, open_ccp);		/* Reopen CCP */
308
309    /*
310     * Install a handler for other signals which would otherwise
311     * cause pppd to exit without cleaning up.
312     */
313    SIGNAL(SIGABRT, bad_signal);
314    SIGNAL(SIGALRM, bad_signal);
315    SIGNAL(SIGFPE, bad_signal);
316    SIGNAL(SIGILL, bad_signal);
317    SIGNAL(SIGPIPE, bad_signal);
318    SIGNAL(SIGQUIT, bad_signal);
319    SIGNAL(SIGSEGV, bad_signal);
320#ifdef SIGBUS
321    SIGNAL(SIGBUS, bad_signal);
322#endif
323#ifdef SIGEMT
324    SIGNAL(SIGEMT, bad_signal);
325#endif
326#ifdef SIGPOLL
327    SIGNAL(SIGPOLL, bad_signal);
328#endif
329#ifdef SIGPROF
330    SIGNAL(SIGPROF, bad_signal);
331#endif
332#ifdef SIGSYS
333    SIGNAL(SIGSYS, bad_signal);
334#endif
335#ifdef SIGTRAP
336    SIGNAL(SIGTRAP, bad_signal);
337#endif
338#ifdef SIGVTALRM
339    SIGNAL(SIGVTALRM, bad_signal);
340#endif
341#ifdef SIGXCPU
342    SIGNAL(SIGXCPU, bad_signal);
343#endif
344#ifdef SIGXFSZ
345    SIGNAL(SIGXFSZ, bad_signal);
346#endif
347
348    /*
349     * Apparently we can get a SIGPIPE when we call syslog, if
350     * syslogd has died and been restarted.  Ignoring it seems
351     * be sufficient.
352     */
353    signal(SIGPIPE, SIG_IGN);
354
355    /*
356     * If we're doing dial-on-demand, set up the interface now.
357     */
358    if (demand) {
359	/*
360	 * Open the loopback channel and set it up to be the ppp interface.
361	 */
362	open_ppp_loopback();
363
364	syslog(LOG_INFO, "Using interface ppp%d", ifunit);
365	(void) sprintf(ifname, "ppp%d", ifunit);
366
367	/* write pid to file */
368	(void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
369	if ((pidfile = fopen(pidfilename, "w")) != NULL) {
370	    fprintf(pidfile, "%d\n", pid);
371	    (void) fclose(pidfile);
372	} else {
373	    syslog(LOG_ERR, "Failed to create pid file %s: %m", pidfilename);
374	    pidfilename[0] = 0;
375	}
376
377	/*
378	 * Configure the interface and mark it up, etc.
379	 */
380	demand_conf();
381    }
382
383    for (;;) {
384
385	need_holdoff = 1;
386
387	if (demand) {
388	    /*
389	     * Don't do anything until we see some activity.
390	     */
391	    phase = PHASE_DORMANT;
392	    kill_link = 0;
393	    demand_unblock();
394	    for (;;) {
395		wait_loop_output(timeleft(&timo));
396		calltimeout();
397		if (kill_link) {
398		    if (!persist)
399			die(0);
400		    kill_link = 0;
401		}
402		if (get_loop_output())
403		    break;
404		reap_kids();
405	    }
406
407	    /*
408	     * Now we want to bring up the link.
409	     */
410	    demand_block();
411	    syslog(LOG_INFO, "Starting link");
412	}
413
414	/*
415	 * Lock the device if we've been asked to.
416	 */
417	if (lockflag && !default_device) {
418	    if (lock(devnam) < 0)
419		goto fail;
420	    locked = 1;
421	}
422
423	/*
424	 * Open the serial device and set it up to be the ppp interface.
425	 * First we open it in non-blocking mode so we can set the
426	 * various termios flags appropriately.  If we aren't dialling
427	 * out and we want to use the modem lines, we reopen it later
428	 * in order to wait for the carrier detect signal from the modem.
429	 */
430	while ((ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0)) < 0) {
431	    if (errno != EINTR)
432		syslog(LOG_ERR, "Failed to open %s: %m", devnam);
433	    if (!persist || errno != EINTR)
434		goto fail;
435	}
436	if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1
437	    || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
438	    syslog(LOG_WARNING,
439		   "Couldn't reset non-blocking mode on device: %m");
440	hungup = 0;
441	kill_link = 0;
442
443	/*
444	 * Do the equivalent of `mesg n' to stop broadcast messages.
445	 */
446	if (fstat(ttyfd, &statbuf) < 0
447	    || fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) {
448	    syslog(LOG_WARNING,
449		   "Couldn't restrict write permissions to %s: %m", devnam);
450	} else
451	    tty_mode = statbuf.st_mode;
452
453	/*
454	 * Set line speed, flow control, etc.
455	 * Previously, if we had a connection script, we would set CLOCAL
456	 * while the script was running.  But then, if CD was negated
457	 * before the script finished, we would miss it.
458	 */
459	set_up_tty(ttyfd, 0);
460
461	/* run connection script */
462	if (connector && connector[0]) {
463	    MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector));
464
465	    /* drop dtr to hang up in case modem is off hook */
466	    if (!default_device && modem) {
467		setdtr(ttyfd, FALSE);
468		sleep(1);
469		setdtr(ttyfd, TRUE);
470	    }
471
472	    if (device_script(connector, ttyfd, ttyfd) < 0) {
473		syslog(LOG_ERR, "Connect script failed");
474		setdtr(ttyfd, FALSE);
475		goto fail;
476	    }
477
478	    syslog(LOG_INFO, "Serial connection established.");
479	    sleep(1);		/* give it time to set up its terminal */
480	}
481
482	/* reopen tty if necessary to wait for carrier */
483	if (connector == NULL && modem) {
484	    while ((i = open(devnam, O_RDWR)) < 0) {
485		if (errno != EINTR)
486		    syslog(LOG_ERR, "Failed to reopen %s: %m", devnam);
487		if (!persist || errno != EINTR || hungup || kill_link)
488		    goto fail;
489	    }
490	    close(i);
491	}
492
493	/* run welcome script, if any */
494	if (welcomer && welcomer[0]) {
495	    if (device_script(welcomer, ttyfd, ttyfd) < 0)
496		syslog(LOG_WARNING, "Welcome script failed");
497	}
498
499	/* set up the serial device as a ppp interface */
500	establish_ppp(ttyfd);
501
502	if (!demand) {
503
504	    syslog(LOG_INFO, "Using interface ppp%d", ifunit);
505	    (void) sprintf(ifname, "ppp%d", ifunit);
506
507	    /* write pid to file */
508	    (void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
509	    if ((pidfile = fopen(pidfilename, "w")) != NULL) {
510		fprintf(pidfile, "%d\n", pid);
511		(void) fclose(pidfile);
512	    } else {
513		syslog(LOG_ERR, "Failed to create pid file %s: %m",
514		       pidfilename);
515		pidfilename[0] = 0;
516	    }
517	}
518
519	/*
520	 * Start opening the connection and wait for
521	 * incoming events (reply, timeout, etc.).
522	 */
523	syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devnam);
524	lcp_lowerup(0);
525	lcp_open(0);		/* Start protocol */
526	for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; ) {
527	    wait_input(timeleft(&timo));
528	    calltimeout();
529	    get_input();
530	    if (kill_link) {
531		lcp_close(0, "User request");
532		kill_link = 0;
533	    }
534	    if (open_ccp_flag) {
535		if (phase == PHASE_NETWORK) {
536		    ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
537		    (*ccp_protent.open)(0);
538		}
539		open_ccp_flag = 0;
540	    }
541	    reap_kids();	/* Don't leave dead kids lying around */
542	}
543
544	/*
545	 * If we may want to bring the link up again, transfer
546	 * the ppp unit back to the loopback.  Set the
547	 * real serial device back to its normal mode of operation.
548	 */
549	clean_check();
550	if (demand)
551	    restore_loop();
552	disestablish_ppp(ttyfd);
553
554	/*
555	 * Run disconnector script, if requested.
556	 * XXX we may not be able to do this if the line has hung up!
557	 */
558	if (disconnector && !hungup) {
559	    set_up_tty(ttyfd, 1);
560	    if (device_script(disconnector, ttyfd, ttyfd) < 0) {
561		syslog(LOG_WARNING, "disconnect script failed");
562	    } else {
563		syslog(LOG_INFO, "Serial link disconnected.");
564	    }
565	}
566
567    fail:
568	if (ttyfd >= 0)
569	    close_tty();
570	if (locked) {
571	    unlock();
572	    locked = 0;
573	}
574
575	if (!demand) {
576	    if (pidfilename[0] != 0
577		&& unlink(pidfilename) < 0 && errno != ENOENT)
578		syslog(LOG_WARNING, "unable to delete pid file: %m");
579	    pidfilename[0] = 0;
580	}
581
582	if (!persist)
583	    break;
584
585	if (demand)
586	    demand_discard();
587	if (holdoff > 0 && need_holdoff) {
588	    phase = PHASE_HOLDOFF;
589	    TIMEOUT(holdoff_end, NULL, holdoff);
590	    do {
591		wait_time(timeleft(&timo));
592		calltimeout();
593		if (kill_link) {
594		    if (!persist)
595			die(0);
596		    kill_link = 0;
597		    phase = PHASE_DORMANT; /* allow signal to end holdoff */
598		}
599		reap_kids();
600	    } while (phase == PHASE_HOLDOFF);
601	}
602    }
603
604    die(0);
605    return 0;
606}
607
608/*
609 * holdoff_end - called via a timeout when the holdoff period ends.
610 */
611static void
612holdoff_end(arg)
613    caddr_t arg;
614{
615    phase = PHASE_DORMANT;
616}
617
618/*
619 * get_input - called when incoming data is available.
620 */
621static void
622get_input()
623{
624    int len, i;
625    u_char *p;
626    u_short protocol;
627    struct protent *protp;
628
629    p = inpacket_buf;	/* point to beginning of packet buffer */
630
631    len = read_packet(inpacket_buf);
632    if (len < 0)
633	return;
634
635    if (len == 0) {
636	syslog(LOG_NOTICE, "Modem hangup");
637	hungup = 1;
638	lcp_lowerdown(0);	/* serial link is no longer available */
639	link_terminated(0);
640	return;
641    }
642
643    if (debug /*&& (debugflags & DBG_INPACKET)*/)
644	log_packet(p, len, "rcvd ", LOG_DEBUG);
645
646    if (len < PPP_HDRLEN) {
647	MAINDEBUG((LOG_INFO, "io(): Received short packet."));
648	return;
649    }
650
651    p += 2;				/* Skip address and control */
652    GETSHORT(protocol, p);
653    len -= PPP_HDRLEN;
654
655    /*
656     * Toss all non-LCP packets unless LCP is OPEN.
657     */
658    if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) {
659	MAINDEBUG((LOG_INFO,
660		   "get_input: Received non-LCP packet when LCP not open."));
661	return;
662    }
663
664    /*
665     * Until we get past the authentication phase, toss all packets
666     * except LCP, LQR and authentication packets.
667     */
668    if (phase <= PHASE_AUTHENTICATE
669	&& !(protocol == PPP_LCP || protocol == PPP_LQR
670	|| protocol == PPP_PAP || protocol == PPP_CHAP)) {
671	MAINDEBUG((LOG_INFO, "get_input: discarding proto 0x%x in phase %d",
672		   protocol, phase));
673	return;
674    }
675
676    /*
677     * Upcall the proper protocol input routine.
678     */
679    for (i = 0; (protp = protocols[i]) != NULL; ++i) {
680	if (protp->protocol == protocol && protp->enabled_flag) {
681	    (*protp->input)(0, p, len);
682	    return;
683	}
684	if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag
685	    && protp->datainput != NULL) {
686	    (*protp->datainput)(0, p, len);
687	    return;
688	}
689    }
690
691    if (debug)
692    	syslog(LOG_WARNING, "Unsupported protocol (0x%x) received", protocol);
693    lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN);
694}
695
696
697/*
698 * quit - Clean up state and exit (with an error indication).
699 */
700void
701quit()
702{
703    die(1);
704}
705
706/*
707 * die - like quit, except we can specify an exit status.
708 */
709void
710die(status)
711    int status;
712{
713    cleanup();
714    syslog(LOG_INFO, "Exit.");
715    exit(status);
716}
717
718/*
719 * cleanup - restore anything which needs to be restored before we exit
720 */
721/* ARGSUSED */
722static void
723cleanup()
724{
725    sys_cleanup();
726
727    if (ttyfd >= 0)
728	close_tty();
729
730    if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT)
731	syslog(LOG_WARNING, "unable to delete pid file: %m");
732    pidfilename[0] = 0;
733
734    if (locked)
735	unlock();
736}
737
738/*
739 * close_tty - restore the terminal device and close it.
740 */
741static void
742close_tty()
743{
744    disestablish_ppp(ttyfd);
745
746    /* drop dtr to hang up */
747    if (modem) {
748	setdtr(ttyfd, FALSE);
749	/*
750	 * This sleep is in case the serial port has CLOCAL set by default,
751	 * and consequently will reassert DTR when we close the device.
752	 */
753	sleep(1);
754    }
755
756    restore_tty(ttyfd);
757
758    if (tty_mode != (mode_t) -1)
759	chmod(devnam, tty_mode);
760
761    close(ttyfd);
762    ttyfd = -1;
763}
764
765
766struct	callout {
767    struct timeval	c_time;		/* time at which to call routine */
768    caddr_t		c_arg;		/* argument to routine */
769    void		(*c_func) __P((caddr_t)); /* routine */
770    struct		callout *c_next;
771};
772
773static struct callout *callout = NULL;	/* Callout list */
774static struct timeval timenow;		/* Current time */
775
776/*
777 * timeout - Schedule a timeout.
778 *
779 * Note that this timeout takes the number of seconds, NOT hz (as in
780 * the kernel).
781 */
782void
783timeout(func, arg, time)
784    void (*func) __P((caddr_t));
785    caddr_t arg;
786    int time;
787{
788    struct callout *newp, *p, **pp;
789
790    MAINDEBUG((LOG_DEBUG, "Timeout %lx:%lx in %d seconds.",
791	       (long) func, (long) arg, time));
792
793    /*
794     * Allocate timeout.
795     */
796    if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) {
797	syslog(LOG_ERR, "Out of memory in timeout()!");
798	die(1);
799    }
800    newp->c_arg = arg;
801    newp->c_func = func;
802    gettimeofday(&timenow, NULL);
803    newp->c_time.tv_sec = timenow.tv_sec + time;
804    newp->c_time.tv_usec = timenow.tv_usec;
805
806    /*
807     * Find correct place and link it in.
808     */
809    for (pp = &callout; (p = *pp); pp = &p->c_next)
810	if (newp->c_time.tv_sec < p->c_time.tv_sec
811	    || (newp->c_time.tv_sec == p->c_time.tv_sec
812		&& newp->c_time.tv_usec < p->c_time.tv_sec))
813	    break;
814    newp->c_next = p;
815    *pp = newp;
816}
817
818
819/*
820 * untimeout - Unschedule a timeout.
821 */
822void
823untimeout(func, arg)
824    void (*func) __P((caddr_t));
825    caddr_t arg;
826{
827    struct callout **copp, *freep;
828
829    MAINDEBUG((LOG_DEBUG, "Untimeout %lx:%lx.", (long) func, (long) arg));
830
831    /*
832     * Find first matching timeout and remove it from the list.
833     */
834    for (copp = &callout; (freep = *copp); copp = &freep->c_next)
835	if (freep->c_func == func && freep->c_arg == arg) {
836	    *copp = freep->c_next;
837	    (void) free((char *) freep);
838	    break;
839	}
840}
841
842
843/*
844 * calltimeout - Call any timeout routines which are now due.
845 */
846static void
847calltimeout()
848{
849    struct callout *p;
850
851    while (callout != NULL) {
852	p = callout;
853
854	if (gettimeofday(&timenow, NULL) < 0) {
855	    syslog(LOG_ERR, "Failed to get time of day: %m");
856	    die(1);
857	}
858	if (!(p->c_time.tv_sec < timenow.tv_sec
859	      || (p->c_time.tv_sec == timenow.tv_sec
860		  && p->c_time.tv_usec <= timenow.tv_usec)))
861	    break;		/* no, it's not time yet */
862
863	callout = p->c_next;
864	(*p->c_func)(p->c_arg);
865
866	free((char *) p);
867    }
868}
869
870
871/*
872 * timeleft - return the length of time until the next timeout is due.
873 */
874static struct timeval *
875timeleft(tvp)
876    struct timeval *tvp;
877{
878    if (callout == NULL)
879	return NULL;
880
881    gettimeofday(&timenow, NULL);
882    tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
883    tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec;
884    if (tvp->tv_usec < 0) {
885	tvp->tv_usec += 1000000;
886	tvp->tv_sec -= 1;
887    }
888    if (tvp->tv_sec < 0)
889	tvp->tv_sec = tvp->tv_usec = 0;
890
891    return tvp;
892}
893
894
895/*
896 * kill_my_pg - send a signal to our process group, and ignore it ourselves.
897 */
898static void
899kill_my_pg(sig)
900    int sig;
901{
902    struct sigaction act, oldact;
903
904    act.sa_handler = SIG_IGN;
905    act.sa_flags = 0;
906    kill(0, sig);
907    sigaction(sig, &act, &oldact);
908    sigaction(sig, &oldact, NULL);
909}
910
911
912/*
913 * hup - Catch SIGHUP signal.
914 *
915 * Indicates that the physical layer has been disconnected.
916 * We don't rely on this indication; if the user has sent this
917 * signal, we just take the link down.
918 */
919static void
920hup(sig)
921    int sig;
922{
923    syslog(LOG_INFO, "Hangup (SIGHUP)");
924    kill_link = 1;
925    if (conn_running)
926	/* Send the signal to the [dis]connector process(es) also */
927	kill_my_pg(sig);
928}
929
930
931/*
932 * term - Catch SIGTERM signal and SIGINT signal (^C/del).
933 *
934 * Indicates that we should initiate a graceful disconnect and exit.
935 */
936/*ARGSUSED*/
937static void
938term(sig)
939    int sig;
940{
941    syslog(LOG_INFO, "Terminating on signal %d.", sig);
942    persist = 0;		/* don't try to restart */
943    kill_link = 1;
944    if (conn_running)
945	/* Send the signal to the [dis]connector process(es) also */
946	kill_my_pg(sig);
947}
948
949
950/*
951 * chld - Catch SIGCHLD signal.
952 * Calls reap_kids to get status for any dead kids.
953 */
954static void
955chld(sig)
956    int sig;
957{
958    int save_errno = errno;
959
960    reap_kids();
961    errno = save_errno;
962}
963
964
965/*
966 * toggle_debug - Catch SIGUSR1 signal.
967 *
968 * Toggle debug flag.
969 */
970/*ARGSUSED*/
971static void
972toggle_debug(sig)
973    int sig;
974{
975    debug = !debug;
976    if (debug) {
977	setlogmask(LOG_UPTO(LOG_DEBUG));
978    } else {
979	setlogmask(LOG_UPTO(LOG_WARNING));
980    }
981}
982
983
984/*
985 * open_ccp - Catch SIGUSR2 signal.
986 *
987 * Try to (re)negotiate compression.
988 */
989/*ARGSUSED*/
990static void
991open_ccp(sig)
992    int sig;
993{
994    open_ccp_flag = 1;
995}
996
997
998/*
999 * bad_signal - We've caught a fatal signal.  Clean up state and exit.
1000 */
1001static void
1002bad_signal(sig)
1003    int sig;
1004{
1005    syslog(LOG_ERR, "Fatal signal %d", sig);
1006    if (conn_running)
1007	kill_my_pg(SIGTERM);
1008    die(1);
1009}
1010
1011
1012/*
1013 * device_script - run a program to connect or disconnect the
1014 * serial device.
1015 */
1016static int
1017device_script(program, in, out)
1018    char *program;
1019    int in, out;
1020{
1021    int pid;
1022    int status;
1023    int errfd;
1024
1025    conn_running = 1;
1026    pid = fork();
1027
1028    if (pid < 0) {
1029	conn_running = 0;
1030	syslog(LOG_ERR, "Failed to create child process: %m");
1031	die(1);
1032    }
1033
1034    if (pid == 0) {
1035	sys_close();
1036	closelog();
1037	if (in == out) {
1038	    if (in != 0) {
1039		dup2(in, 0);
1040		close(in);
1041	    }
1042	    dup2(0, 1);
1043	} else {
1044	    if (out == 0)
1045		out = dup(out);
1046	    if (in != 0) {
1047		dup2(in, 0);
1048		close(in);
1049	    }
1050	    if (out != 1) {
1051		dup2(out, 1);
1052		close(out);
1053	    }
1054	}
1055	if (redirect_stderr) {
1056	    close(2);
1057	    errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0644);
1058	    if (errfd >= 0 && errfd != 2) {
1059		dup2(errfd, 2);
1060		close(errfd);
1061	    }
1062	}
1063	/* revoke privs */
1064	seteuid(getuid());
1065	setuid(getuid());
1066	setegid(getgid());
1067	setgid(getgid());
1068	execl("/bin/sh", "sh", "-c", program, (char *)0);
1069	syslog(LOG_ERR, "could not exec /bin/sh: %m");
1070	_exit(99);
1071	/* NOTREACHED */
1072    }
1073
1074    while (waitpid(pid, &status, 0) < 0) {
1075	if (errno == EINTR)
1076	    continue;
1077	syslog(LOG_ERR, "error waiting for (dis)connection process: %m");
1078	die(1);
1079    }
1080    conn_running = 0;
1081
1082    return (status == 0 ? 0 : -1);
1083}
1084
1085
1086/*
1087 * run-program - execute a program with given arguments,
1088 * but don't wait for it.
1089 * If the program can't be executed, logs an error unless
1090 * must_exist is 0 and the program file doesn't exist.
1091 */
1092int
1093run_program(prog, args, must_exist)
1094    char *prog;
1095    char **args;
1096    int must_exist;
1097{
1098    int pid;
1099    char *nullenv[1];
1100
1101    pid = fork();
1102    if (pid == -1) {
1103	syslog(LOG_ERR, "Failed to create child process for %s: %m", prog);
1104	return -1;
1105    }
1106    if (pid == 0) {
1107	int new_fd;
1108
1109	/* Leave the current location */
1110	(void) setsid();    /* No controlling tty. */
1111	(void) umask (S_IRWXG|S_IRWXO);
1112	(void) chdir ("/"); /* no current directory. */
1113	setuid(geteuid());
1114	setgid(getegid());
1115
1116	/* Ensure that nothing of our device environment is inherited. */
1117	sys_close();
1118	closelog();
1119	close (0);
1120	close (1);
1121	close (2);
1122	close (ttyfd);  /* tty interface to the ppp device */
1123
1124	/* Don't pass handles to the PPP device, even by accident. */
1125	new_fd = open (_PATH_DEVNULL, O_RDWR);
1126	if (new_fd >= 0) {
1127	    if (new_fd != 0) {
1128		dup2  (new_fd, 0); /* stdin <- /dev/null */
1129		close (new_fd);
1130	    }
1131	    dup2 (0, 1); /* stdout -> /dev/null */
1132	    dup2 (0, 2); /* stderr -> /dev/null */
1133	}
1134
1135#ifdef BSD
1136	/* Force the priority back to zero if pppd is running higher. */
1137	if (setpriority (PRIO_PROCESS, 0, 0) < 0)
1138	    syslog (LOG_WARNING, "can't reset priority to 0: %m");
1139#endif
1140
1141	/* SysV recommends a second fork at this point. */
1142
1143	/* run the program; give it a null environment */
1144	nullenv[0] = NULL;
1145	execve(prog, args, nullenv);
1146	if (must_exist || errno != ENOENT)
1147	    syslog(LOG_WARNING, "Can't execute %s: %m", prog);
1148	_exit(-1);
1149    }
1150    MAINDEBUG((LOG_DEBUG, "Script %s started; pid = %d", prog, pid));
1151    ++n_children;
1152    return 0;
1153}
1154
1155
1156/*
1157 * reap_kids - get status from any dead child processes,
1158 * and log a message for abnormal terminations.
1159 */
1160static void
1161reap_kids()
1162{
1163    int pid, status;
1164
1165    if (n_children == 0)
1166	return;
1167    if ((pid = waitpid(-1, &status, WNOHANG)) == -1) {
1168	if (errno != ECHILD)
1169	    syslog(LOG_ERR, "Error waiting for child process: %m");
1170	return;
1171    }
1172    if (pid > 0) {
1173	--n_children;
1174	if (WIFSIGNALED(status)) {
1175	    syslog(LOG_WARNING, "Child process %d terminated with signal %d",
1176		   pid, WTERMSIG(status));
1177	}
1178    }
1179}
1180
1181
1182/*
1183 * log_packet - format a packet and log it.
1184 */
1185
1186char line[256];			/* line to be logged accumulated here */
1187char *linep;
1188
1189void
1190log_packet(p, len, prefix, level)
1191    u_char *p;
1192    int len;
1193    char *prefix;
1194    int level;
1195{
1196    strcpy(line, prefix);
1197    linep = line + strlen(line);
1198    format_packet(p, len, pr_log, NULL);
1199    if (linep != line)
1200	syslog(level, "%s", line);
1201}
1202
1203/*
1204 * format_packet - make a readable representation of a packet,
1205 * calling `printer(arg, format, ...)' to output it.
1206 */
1207void
1208format_packet(p, len, printer, arg)
1209    u_char *p;
1210    int len;
1211    void (*printer) __P((void *, char *, ...));
1212    void *arg;
1213{
1214    int i, n;
1215    u_short proto;
1216    u_char x;
1217    struct protent *protp;
1218
1219    if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
1220	p += 2;
1221	GETSHORT(proto, p);
1222	len -= PPP_HDRLEN;
1223	for (i = 0; (protp = protocols[i]) != NULL; ++i)
1224	    if (proto == protp->protocol)
1225		break;
1226	if (protp != NULL) {
1227	    printer(arg, "[%s", protp->name);
1228	    n = (*protp->printpkt)(p, len, printer, arg);
1229	    printer(arg, "]");
1230	    p += n;
1231	    len -= n;
1232	} else {
1233	    printer(arg, "[proto=0x%x]", proto);
1234	}
1235    }
1236
1237    for (; len > 0; --len) {
1238	GETCHAR(x, p);
1239	printer(arg, " %.2x", x);
1240    }
1241}
1242
1243static void
1244pr_log __V((void *arg, char *fmt, ...))
1245{
1246    int n;
1247    va_list pvar;
1248    char buf[256];
1249
1250#ifdef __STDC__
1251    va_start(pvar, fmt);
1252#else
1253    void *arg;
1254    char *fmt;
1255    va_start(pvar);
1256    arg = va_arg(pvar, void *);
1257    fmt = va_arg(pvar, char *);
1258#endif
1259
1260    vsprintf(buf, fmt, pvar);
1261    va_end(pvar);
1262
1263    n = strlen(buf);
1264    if (linep + n + 1 > line + sizeof(line)) {
1265	syslog(LOG_DEBUG, "%s", line);
1266	linep = line;
1267    }
1268    strcpy(linep, buf);
1269    linep += n;
1270}
1271
1272/*
1273 * print_string - print a readable representation of a string using
1274 * printer.
1275 */
1276void
1277print_string(p, len, printer, arg)
1278    char *p;
1279    int len;
1280    void (*printer) __P((void *, char *, ...));
1281    void *arg;
1282{
1283    int c;
1284
1285    printer(arg, "\"");
1286    for (; len > 0; --len) {
1287	c = *p++;
1288	if (' ' <= c && c <= '~') {
1289	    if (c == '\\' || c == '"')
1290		printer(arg, "\\");
1291	    printer(arg, "%c", c);
1292	} else {
1293	    switch (c) {
1294	    case '\n':
1295		printer(arg, "\\n");
1296		break;
1297	    case '\r':
1298		printer(arg, "\\r");
1299		break;
1300	    case '\t':
1301		printer(arg, "\\t");
1302		break;
1303	    default:
1304		printer(arg, "\\%.3o", c);
1305	    }
1306	}
1307    }
1308    printer(arg, "\"");
1309}
1310
1311/*
1312 * novm - log an error message saying we ran out of memory, and die.
1313 */
1314void
1315novm(msg)
1316    char *msg;
1317{
1318    syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg);
1319    die(1);
1320}
1321
1322/*
1323 * fmtmsg - format a message into a buffer.  Like sprintf except we
1324 * also specify the length of the output buffer, and we handle
1325 * %r (recursive format), %m (error message) and %I (IP address) formats.
1326 * Doesn't do floating-point formats.
1327 * Returns the number of chars put into buf.
1328 */
1329int
1330fmtmsg __V((char *buf, int buflen, char *fmt, ...))
1331{
1332    va_list args;
1333    int n;
1334
1335#ifdef __STDC__
1336    va_start(args, fmt);
1337#else
1338    char *buf;
1339    int buflen;
1340    char *fmt;
1341    va_start(args);
1342    buf = va_arg(args, char *);
1343    buflen = va_arg(args, int);
1344    fmt = va_arg(args, char *);
1345#endif
1346    n = vfmtmsg(buf, buflen, fmt, args);
1347    va_end(args);
1348    return n;
1349}
1350
1351/*
1352 * vfmtmsg - like fmtmsg, takes a va_list instead of a list of args.
1353 */
1354#define OUTCHAR(c)	(buflen > 0? (--buflen, *buf++ = (c)): 0)
1355
1356int
1357vfmtmsg(buf, buflen, fmt, args)
1358    char *buf;
1359    int buflen;
1360    char *fmt;
1361    va_list args;
1362{
1363    int c, i, n;
1364    int width, prec, fillch;
1365    int base, len, neg, quoted;
1366    unsigned long val = 0;
1367    char *str, *f, *buf0;
1368    unsigned char *p;
1369    char num[32];
1370    time_t t;
1371    static char hexchars[] = "0123456789abcdef";
1372
1373    buf0 = buf;
1374    --buflen;
1375    while (buflen > 0) {
1376	for (f = fmt; *f != '%' && *f != 0; ++f)
1377	    ;
1378	if (f > fmt) {
1379	    len = f - fmt;
1380	    if (len > buflen)
1381		len = buflen;
1382	    memcpy(buf, fmt, len);
1383	    buf += len;
1384	    buflen -= len;
1385	    fmt = f;
1386	}
1387	if (*fmt == 0)
1388	    break;
1389	c = *++fmt;
1390	width = prec = 0;
1391	fillch = ' ';
1392	if (c == '0') {
1393	    fillch = '0';
1394	    c = *++fmt;
1395	}
1396	if (c == '*') {
1397	    width = va_arg(args, int);
1398	    c = *++fmt;
1399	} else {
1400	    while (isdigit(c)) {
1401		width = width * 10 + c - '0';
1402		c = *++fmt;
1403	    }
1404	}
1405	if (c == '.') {
1406	    c = *++fmt;
1407	    if (c == '*') {
1408		prec = va_arg(args, int);
1409		c = *++fmt;
1410	    } else {
1411		while (isdigit(c)) {
1412		    prec = prec * 10 + c - '0';
1413		    c = *++fmt;
1414		}
1415	    }
1416	}
1417	str = 0;
1418	base = 0;
1419	neg = 0;
1420	++fmt;
1421	switch (c) {
1422	case 'd':
1423	    i = va_arg(args, int);
1424	    if (i < 0) {
1425		neg = 1;
1426		val = -i;
1427	    } else
1428		val = i;
1429	    base = 10;
1430	    break;
1431	case 'o':
1432	    val = va_arg(args, unsigned int);
1433	    base = 8;
1434	    break;
1435	case 'x':
1436	    val = va_arg(args, unsigned int);
1437	    base = 16;
1438	    break;
1439	case 'p':
1440	    val = (unsigned long) va_arg(args, void *);
1441	    base = 16;
1442	    neg = 2;
1443	    break;
1444	case 's':
1445	    str = va_arg(args, char *);
1446	    break;
1447	case 'c':
1448	    num[0] = va_arg(args, int);
1449	    num[1] = 0;
1450	    str = num;
1451	    break;
1452	case 'm':
1453	    str = strerror(errno);
1454	    break;
1455	case 'I':
1456	    str = ip_ntoa(va_arg(args, u_int32_t));
1457	    break;
1458	case 'r':
1459	    f = va_arg(args, char *);
1460#ifndef __powerpc__
1461	    n = vfmtmsg(buf, buflen + 1, f, va_arg(args, va_list));
1462#else
1463	    /* On the powerpc, a va_list is an array of 1 structure */
1464	    n = vfmtmsg(buf, buflen + 1, f, va_arg(args, void *));
1465#endif
1466	    buf += n;
1467	    buflen -= n;
1468	    continue;
1469	case 't':
1470	    time(&t);
1471	    str = ctime(&t);
1472	    str += 4;		/* chop off the day name */
1473	    str[15] = 0;	/* chop off year and newline */
1474	    break;
1475	case 'v':		/* "visible" string */
1476	case 'q':		/* quoted string */
1477	    quoted = c == 'q';
1478	    p = va_arg(args, unsigned char *);
1479	    if (fillch == '0' && prec > 0) {
1480		n = prec;
1481	    } else {
1482		n = strlen((char *)p);
1483		if (prec > 0 && prec < n)
1484		    n = prec;
1485	    }
1486	    while (n > 0 && buflen > 0) {
1487		c = *p++;
1488		--n;
1489		if (!quoted && c >= 0x80) {
1490		    OUTCHAR('M');
1491		    OUTCHAR('-');
1492		    c -= 0x80;
1493		}
1494		if (quoted && (c == '"' || c == '\\'))
1495		    OUTCHAR('\\');
1496		if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1497		    if (quoted) {
1498			OUTCHAR('\\');
1499			switch (c) {
1500			case '\t':	OUTCHAR('t');	break;
1501			case '\n':	OUTCHAR('n');	break;
1502			case '\b':	OUTCHAR('b');	break;
1503			case '\f':	OUTCHAR('f');	break;
1504			default:
1505			    OUTCHAR('x');
1506			    OUTCHAR(hexchars[c >> 4]);
1507			    OUTCHAR(hexchars[c & 0xf]);
1508			}
1509		    } else {
1510			if (c == '\t')
1511			    OUTCHAR(c);
1512			else {
1513			    OUTCHAR('^');
1514			    OUTCHAR(c ^ 0x40);
1515			}
1516		    }
1517		} else
1518		    OUTCHAR(c);
1519	    }
1520	    continue;
1521	default:
1522	    *buf++ = '%';
1523	    if (c != '%')
1524		--fmt;		/* so %z outputs %z etc. */
1525	    --buflen;
1526	    continue;
1527	}
1528	if (base != 0) {
1529	    str = num + sizeof(num);
1530	    *--str = 0;
1531	    while (str > num + neg) {
1532		*--str = hexchars[val % base];
1533		val = val / base;
1534		if (--prec <= 0 && val == 0)
1535		    break;
1536	    }
1537	    switch (neg) {
1538	    case 1:
1539		*--str = '-';
1540		break;
1541	    case 2:
1542		*--str = 'x';
1543		*--str = '0';
1544		break;
1545	    }
1546	    len = num + sizeof(num) - 1 - str;
1547	} else {
1548	    len = strlen(str);
1549	    if (prec > 0 && len > prec)
1550		len = prec;
1551	}
1552	if (width > 0) {
1553	    if (width > buflen)
1554		width = buflen;
1555	    if ((n = width - len) > 0) {
1556		buflen -= n;
1557		for (; n > 0; --n)
1558		    *buf++ = fillch;
1559	    }
1560	}
1561	if (len > buflen)
1562	    len = buflen;
1563	memcpy(buf, str, len);
1564	buf += len;
1565	buflen -= len;
1566    }
1567    *buf = 0;
1568    return buf - buf0;
1569}
1570