main.c revision 1.4
1/*
2 * main.c - Point-to-Point Protocol main module
3 *
4 * Copyright (c) 1989 Carnegie Mellon University.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University.  The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20#ifndef lint
21static char rcsid[] = "$Id: main.c,v 1.4 1996/02/04 19:39:39 hannken Exp $";
22#endif
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <signal.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <syslog.h>
31#include <netdb.h>
32#include <utmp.h>
33#include <pwd.h>
34#include <sys/param.h>
35#include <sys/types.h>
36#include <sys/wait.h>
37#include <sys/time.h>
38#include <sys/resource.h>
39#include <sys/stat.h>
40#include <sys/socket.h>
41#include <net/if.h>
42
43#include "pppd.h"
44#include "magic.h"
45#include "fsm.h"
46#include "lcp.h"
47#include "ipcp.h"
48#include "upap.h"
49#include "chap.h"
50#include "ccp.h"
51#include "pathnames.h"
52#include "patchlevel.h"
53
54/*
55 * If REQ_SYSOPTIONS is defined to 1, pppd will not run unless
56 * /etc/ppp/options exists.
57 */
58#ifndef	REQ_SYSOPTIONS
59#define REQ_SYSOPTIONS	1
60#endif
61
62/* interface vars */
63char ifname[IFNAMSIZ];		/* Interface name */
64int ifunit;			/* Interface unit number */
65
66char *progname;			/* Name of this program */
67char hostname[MAXNAMELEN];	/* Our hostname */
68static char pidfilename[MAXPATHLEN];	/* name of pid file */
69static char default_devnam[MAXPATHLEN];	/* name of default device */
70static pid_t	pid;		/* Our pid */
71static pid_t	pgrpid;		/* Process Group ID */
72static uid_t uid;		/* Our real user-id */
73
74int fd = -1;			/* Device file descriptor */
75
76int phase;			/* where the link is at */
77int kill_link;
78int open_ccp_flag;
79
80static int initfdflags = -1;	/* Initial file descriptor flags */
81
82u_char outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for outgoing packet */
83static u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */
84
85int hungup;			/* terminal has been hung up */
86static int n_children;		/* # child processes still running */
87
88int baud_rate;
89
90char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n";
91
92/* prototypes */
93static void hup __P((int));
94static void term __P((int));
95static void chld __P((int));
96static void toggle_debug __P((int));
97static void open_ccp __P((int));
98static void bad_signal __P((int));
99
100static void get_input __P((void));
101void establish_ppp __P((void));
102void calltimeout __P((void));
103struct timeval *timeleft __P((struct timeval *));
104void reap_kids __P((void));
105void cleanup __P((int, caddr_t));
106void close_fd __P((void));
107void die __P((int));
108void novm __P((char *));
109
110void log_packet __P((u_char *, int, char *));
111void format_packet __P((u_char *, int,
112			   void (*) (void *, char *, ...), void *));
113void pr_log __P((void *, char *, ...));
114
115extern	char	*ttyname __P((int));
116extern	char	*getlogin __P((void));
117
118#ifdef ultrix
119#undef	O_NONBLOCK
120#define	O_NONBLOCK	O_NDELAY
121#endif
122
123/*
124 * PPP Data Link Layer "protocol" table.
125 * One entry per supported protocol.
126 */
127static struct protent {
128    u_short protocol;
129    void (*init)();
130    void (*input)();
131    void (*protrej)();
132    int  (*printpkt)();
133    void (*datainput)();
134    char *name;
135} prottbl[] = {
136    { PPP_LCP, lcp_init, lcp_input, lcp_protrej,
137	  lcp_printpkt, NULL, "LCP" },
138    { PPP_IPCP, ipcp_init, ipcp_input, ipcp_protrej,
139	  ipcp_printpkt, NULL, "IPCP" },
140    { PPP_PAP, upap_init, upap_input, upap_protrej,
141	  upap_printpkt, NULL, "PAP" },
142    { PPP_CHAP, ChapInit, ChapInput, ChapProtocolReject,
143	  ChapPrintPkt, NULL, "CHAP" },
144    { PPP_CCP, ccp_init, ccp_input, ccp_protrej,
145	  ccp_printpkt, ccp_datainput, "CCP" },
146};
147
148#define N_PROTO		(sizeof(prottbl) / sizeof(prottbl[0]))
149
150main(argc, argv)
151    int argc;
152    char *argv[];
153{
154    int i, nonblock;
155    struct sigaction sa;
156    struct cmd *cmdp;
157    FILE *pidfile;
158    char *p;
159    struct passwd *pw;
160    struct timeval timo;
161    sigset_t mask;
162
163    p = ttyname(0);
164    if (p)
165	strcpy(devnam, p);
166    strcpy(default_devnam, devnam);
167
168    if (gethostname(hostname, MAXNAMELEN) < 0 ) {
169	perror("couldn't get hostname");
170	die(1);
171    }
172    hostname[MAXNAMELEN-1] = 0;
173
174    uid = getuid();
175
176    if (!ppp_available()) {
177	fprintf(stderr, no_ppp_msg);
178	exit(1);
179    }
180
181    /*
182     * Initialize to the standard option set, then parse, in order,
183     * the system options file, the user's options file, and the command
184     * line arguments.
185     */
186    for (i = 0; i < N_PROTO; i++)
187	(*prottbl[i].init)(0);
188
189    progname = *argv;
190
191    if (!options_from_file(_PATH_SYSOPTIONS, REQ_SYSOPTIONS, 0) ||
192	!options_from_user() ||
193	!parse_args(argc-1, argv+1) ||
194	!options_for_tty())
195	die(1);
196    check_auth_options();
197    setipdefault();
198
199    /*
200     * If the user has specified the default device name explicitly,
201     * pretend they hadn't.
202     */
203    if (!default_device && strcmp(devnam, default_devnam) == 0)
204	default_device = 1;
205
206    /*
207     * Initialize system-dependent stuff and magic number package.
208     */
209    sys_init();
210    magic_init();
211
212    /*
213     * Detach ourselves from the terminal, if required,
214     * and identify who is running us.
215     */
216    if (!default_device && !nodetach && daemon(0, 0) < 0) {
217	perror("Couldn't detach from controlling terminal");
218	exit(1);
219    }
220    pid = getpid();
221    p = getlogin();
222    if (p == NULL) {
223	pw = getpwuid(uid);
224	if (pw != NULL && pw->pw_name != NULL)
225	    p = pw->pw_name;
226	else
227	    p = "(unknown)";
228    }
229    syslog(LOG_NOTICE, "pppd %s.%d started by %s, uid %d",
230	   VERSION, PATCHLEVEL, p, uid);
231
232    /*
233     * Compute mask of all interesting signals and install signal handlers
234     * for each.  Only one signal handler may be active at a time.  Therefore,
235     * all other signals should be masked when any handler is executing.
236     */
237    sigemptyset(&mask);
238    sigaddset(&mask, SIGHUP);
239    sigaddset(&mask, SIGINT);
240    sigaddset(&mask, SIGTERM);
241    sigaddset(&mask, SIGCHLD);
242
243#define SIGNAL(s, handler)	{ \
244	sa.sa_handler = handler; \
245	if (sigaction(s, &sa, NULL) < 0) { \
246	    syslog(LOG_ERR, "Couldn't establish signal handler (%d): %m", s); \
247	    die(1); \
248	} \
249    }
250
251    sa.sa_mask = mask;
252    sa.sa_flags = 0;
253    SIGNAL(SIGHUP, hup);		/* Hangup */
254    SIGNAL(SIGINT, term);		/* Interrupt */
255    SIGNAL(SIGTERM, term);		/* Terminate */
256    SIGNAL(SIGCHLD, chld);
257
258    SIGNAL(SIGUSR1, toggle_debug);	/* Toggle debug flag */
259    SIGNAL(SIGUSR2, open_ccp);		/* Reopen CCP */
260
261    /*
262     * Install a handler for other signals which would otherwise
263     * cause pppd to exit without cleaning up.
264     */
265    SIGNAL(SIGABRT, bad_signal);
266    SIGNAL(SIGALRM, bad_signal);
267    SIGNAL(SIGFPE, bad_signal);
268    SIGNAL(SIGILL, bad_signal);
269    SIGNAL(SIGPIPE, bad_signal);
270    SIGNAL(SIGQUIT, bad_signal);
271    SIGNAL(SIGSEGV, bad_signal);
272#ifdef SIGBUS
273    SIGNAL(SIGBUS, bad_signal);
274#endif
275#ifdef SIGEMT
276    SIGNAL(SIGEMT, bad_signal);
277#endif
278#ifdef SIGPOLL
279    SIGNAL(SIGPOLL, bad_signal);
280#endif
281#ifdef SIGPROF
282    SIGNAL(SIGPROF, bad_signal);
283#endif
284#ifdef SIGSYS
285    SIGNAL(SIGSYS, bad_signal);
286#endif
287#ifdef SIGTRAP
288    SIGNAL(SIGTRAP, bad_signal);
289#endif
290#ifdef SIGVTALRM
291    SIGNAL(SIGVTALRM, bad_signal);
292#endif
293#ifdef SIGXCPU
294    SIGNAL(SIGXCPU, bad_signal);
295#endif
296#ifdef SIGXFSZ
297    SIGNAL(SIGXFSZ, bad_signal);
298#endif
299
300    /*
301     * Lock the device if we've been asked to.
302     */
303    if (lockflag && !default_device)
304	if (lock(devnam) < 0)
305	    die(1);
306
307    do {
308
309	/*
310	 * Open the serial device and set it up to be the ppp interface.
311	 * If we're dialling out, or we don't want to use the modem lines,
312	 * we open it in non-blocking mode, but then we need to clear
313	 * the non-blocking I/O bit.
314	 */
315	nonblock = (connector || !modem)? O_NONBLOCK: 0;
316	if ((fd = open(devnam, nonblock | O_RDWR, 0)) < 0) {
317	    syslog(LOG_ERR, "Failed to open %s: %m", devnam);
318	    die(1);
319	}
320	if ((initfdflags = fcntl(fd, F_GETFL)) == -1) {
321	    syslog(LOG_ERR, "Couldn't get device fd flags: %m");
322	    die(1);
323	}
324	if (nonblock) {
325	    initfdflags &= ~O_NONBLOCK;
326	    fcntl(fd, F_SETFL, initfdflags);
327	}
328	hungup = 0;
329	kill_link = 0;
330
331	/* run connection script */
332	if (connector && connector[0]) {
333	    MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector));
334
335	    /* set line speed, flow control, etc.; set CLOCAL for now */
336	    set_up_tty(fd, 1);
337
338	    /* drop dtr to hang up in case modem is off hook */
339	    if (!default_device && modem) {
340		setdtr(fd, FALSE);
341		sleep(1);
342		setdtr(fd, TRUE);
343	    }
344
345	    if (device_script(connector, fd, fd) < 0) {
346		syslog(LOG_ERR, "Connect script failed");
347		setdtr(fd, FALSE);
348		die(1);
349	    }
350
351	    syslog(LOG_INFO, "Serial connection established.");
352	    sleep(1);		/* give it time to set up its terminal */
353	}
354
355	/* set line speed, flow control, etc.; clear CLOCAL if modem option */
356	set_up_tty(fd, 0);
357
358	/* set up the serial device as a ppp interface */
359	establish_ppp();
360
361	syslog(LOG_INFO, "Using interface ppp%d", ifunit);
362	(void) sprintf(ifname, "ppp%d", ifunit);
363
364	/* write pid to file */
365	(void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
366	if ((pidfile = fopen(pidfilename, "w")) != NULL) {
367	    fprintf(pidfile, "%d\n", pid);
368	    (void) fclose(pidfile);
369	} else {
370	    syslog(LOG_ERR, "Failed to create pid file %s: %m", pidfilename);
371	    pidfilename[0] = 0;
372	}
373
374	/*
375	 * Set device for non-blocking reads.
376	 */
377	if (fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
378	    syslog(LOG_ERR, "Couldn't set device to non-blocking mode: %m");
379	    die(1);
380	}
381
382	/*
383	 * Block all signals, start opening the connection, and wait for
384	 * incoming events (reply, timeout, etc.).
385	 */
386	syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devnam);
387	lcp_lowerup(0);
388	lcp_open(0);		/* Start protocol */
389	for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; ) {
390	    wait_input(timeleft(&timo));
391	    calltimeout();
392	    get_input();
393	    if (kill_link) {
394		lcp_close(0);
395		kill_link = 0;
396	    }
397	    if (open_ccp_flag) {
398		if (phase == PHASE_NETWORK) {
399		    ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
400		    ccp_open(0);
401		}
402		open_ccp_flag = 0;
403	    }
404	    reap_kids();	/* Don't leave dead kids lying around */
405	}
406
407	/*
408	 * Run disconnector script, if requested.
409	 * First we need to reset non-blocking mode.
410	 * XXX we may not be able to do this if the line has hung up!
411	 */
412	if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) >= 0)
413	    initfdflags = -1;
414	disestablish_ppp();
415	if (disconnector) {
416	    set_up_tty(fd, 1);
417	    if (device_script(disconnector, fd, fd) < 0) {
418		syslog(LOG_WARNING, "disconnect script failed");
419	    } else {
420		syslog(LOG_INFO, "Serial link disconnected.");
421	    }
422	}
423
424	close_fd();
425	if (unlink(pidfilename) < 0 && errno != ENOENT)
426	    syslog(LOG_WARNING, "unable to delete pid file: %m");
427	pidfilename[0] = 0;
428
429    } while (persist);
430
431    die(0);
432}
433
434
435/*
436 * get_input - called when incoming data is available.
437 */
438static void
439get_input()
440{
441    int len, i;
442    u_char *p;
443    u_short protocol;
444
445    p = inpacket_buf;	/* point to beginning of packet buffer */
446
447    len = read_packet(inpacket_buf);
448    if (len < 0)
449	return;
450
451    if (len == 0) {
452	syslog(LOG_NOTICE, "Modem hangup");
453	hungup = 1;
454	lcp_lowerdown(0);	/* serial link is no longer available */
455	phase = PHASE_DEAD;
456	return;
457    }
458
459    if (debug /*&& (debugflags & DBG_INPACKET)*/)
460	log_packet(p, len, "rcvd ");
461
462    if (len < PPP_HDRLEN) {
463	MAINDEBUG((LOG_INFO, "io(): Received short packet."));
464	return;
465    }
466
467    p += 2;				/* Skip address and control */
468    GETSHORT(protocol, p);
469    len -= PPP_HDRLEN;
470
471    /*
472     * Toss all non-LCP packets unless LCP is OPEN.
473     */
474    if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) {
475	MAINDEBUG((LOG_INFO,
476		   "io(): Received non-LCP packet when LCP not open."));
477	return;
478    }
479
480    /*
481     * Upcall the proper protocol input routine.
482     */
483    for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++) {
484	if (prottbl[i].protocol == protocol) {
485	    (*prottbl[i].input)(0, p, len);
486	    return;
487	}
488        if (protocol == (prottbl[i].protocol & ~0x8000)
489	    && prottbl[i].datainput != NULL) {
490	    (*prottbl[i].datainput)(0, p, len);
491	    return;
492	}
493    }
494
495    if (debug)
496    	syslog(LOG_WARNING, "Unknown protocol (0x%x) received", protocol);
497    lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN);
498}
499
500
501/*
502 * demuxprotrej - Demultiplex a Protocol-Reject.
503 */
504void
505demuxprotrej(unit, protocol)
506    int unit;
507    u_short protocol;
508{
509    int i;
510
511    /*
512     * Upcall the proper Protocol-Reject routine.
513     */
514    for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
515	if (prottbl[i].protocol == protocol) {
516	    (*prottbl[i].protrej)(unit);
517	    return;
518	}
519
520    syslog(LOG_WARNING,
521	   "demuxprotrej: Unrecognized Protocol-Reject for protocol 0x%x",
522	   protocol);
523}
524
525
526/*
527 * bad_signal - We've caught a fatal signal.  Clean up state and exit.
528 */
529static void
530bad_signal(sig)
531    int sig;
532{
533    syslog(LOG_ERR, "Fatal signal %d", sig);
534    die(1);
535}
536
537/*
538 * quit - Clean up state and exit (with an error indication).
539 */
540void
541quit()
542{
543    die(1);
544}
545
546/*
547 * die - like quit, except we can specify an exit status.
548 */
549void
550die(status)
551    int status;
552{
553    cleanup(0, NULL);
554    syslog(LOG_INFO, "Exit.");
555    exit(status);
556}
557
558/*
559 * cleanup - restore anything which needs to be restored before we exit
560 */
561/* ARGSUSED */
562void
563cleanup(status, arg)
564    int status;
565    caddr_t arg;
566{
567    if (fd >= 0)
568	close_fd();
569
570    if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT)
571	syslog(LOG_WARNING, "unable to delete pid file: %m");
572    pidfilename[0] = 0;
573
574    if (lockflag && !default_device)
575	unlock();
576}
577
578/*
579 * close_fd - restore the terminal device and close it.
580 */
581void
582close_fd()
583{
584    disestablish_ppp();
585
586    /* drop dtr to hang up */
587    if (modem) {
588	setdtr(fd, FALSE);
589	sleep(1);
590    }
591
592    if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
593	syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
594    initfdflags = -1;
595
596    restore_tty();
597
598    close(fd);
599    fd = -1;
600}
601
602
603struct	callout {
604    struct timeval	c_time;		/* time at which to call routine */
605    caddr_t		c_arg;		/* argument to routine */
606    void		(*c_func)();	/* routine */
607    struct		callout *c_next;
608};
609
610static struct callout *callout = NULL;	/* Callout list */
611static struct timeval timenow;		/* Current time */
612
613/*
614 * timeout - Schedule a timeout.
615 *
616 * Note that this timeout takes the number of seconds, NOT hz (as in
617 * the kernel).
618 */
619void
620timeout(func, arg, time)
621    void (*func)();
622    caddr_t arg;
623    int time;
624{
625    struct callout *newp, *p, **pp;
626
627    MAINDEBUG((LOG_DEBUG, "Timeout %lx:%lx in %d seconds.",
628	       (long) func, (long) arg, time));
629
630    /*
631     * Allocate timeout.
632     */
633    if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) {
634	syslog(LOG_ERR, "Out of memory in timeout()!");
635	die(1);
636    }
637    newp->c_arg = arg;
638    newp->c_func = func;
639    gettimeofday(&timenow, NULL);
640    newp->c_time.tv_sec = timenow.tv_sec + time;
641    newp->c_time.tv_usec = timenow.tv_usec;
642
643    /*
644     * Find correct place and link it in.
645     */
646    for (pp = &callout; (p = *pp); pp = &p->c_next)
647	if (newp->c_time.tv_sec < p->c_time.tv_sec
648	    || (newp->c_time.tv_sec == p->c_time.tv_sec
649		&& newp->c_time.tv_usec < p->c_time.tv_sec))
650	    break;
651    newp->c_next = p;
652    *pp = newp;
653}
654
655
656/*
657 * untimeout - Unschedule a timeout.
658 */
659void
660untimeout(func, arg)
661    void (*func)();
662    caddr_t arg;
663{
664    struct itimerval itv;
665    struct callout **copp, *freep;
666    int reschedule = 0;
667
668    MAINDEBUG((LOG_DEBUG, "Untimeout %lx:%lx.", (long) func, (long) arg));
669
670    /*
671     * Find first matching timeout and remove it from the list.
672     */
673    for (copp = &callout; (freep = *copp); copp = &freep->c_next)
674	if (freep->c_func == func && freep->c_arg == arg) {
675	    *copp = freep->c_next;
676	    (void) free((char *) freep);
677	    break;
678	}
679}
680
681
682/*
683 * calltimeout - Call any timeout routines which are now due.
684 */
685void
686calltimeout()
687{
688    struct callout *p;
689
690    while (callout != NULL) {
691	p = callout;
692
693	if (gettimeofday(&timenow, NULL) < 0) {
694	    syslog(LOG_ERR, "Failed to get time of day: %m");
695	    die(1);
696	}
697	if (!(p->c_time.tv_sec < timenow.tv_sec
698	      || (p->c_time.tv_sec == timenow.tv_sec
699		  && p->c_time.tv_usec <= timenow.tv_usec)))
700	    break;		/* no, it's not time yet */
701
702	callout = p->c_next;
703	(*p->c_func)(p->c_arg);
704
705	free((char *) p);
706    }
707}
708
709
710/*
711 * timeleft - return the length of time until the next timeout is due.
712 */
713struct timeval *
714timeleft(tvp)
715    struct timeval *tvp;
716{
717    if (callout == NULL)
718	return NULL;
719
720    gettimeofday(&timenow, NULL);
721    tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
722    tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec;
723    if (tvp->tv_usec < 0) {
724	tvp->tv_usec += 1000000;
725	tvp->tv_sec -= 1;
726    }
727    if (tvp->tv_sec < 0)
728	tvp->tv_sec = tvp->tv_usec = 0;
729
730    return tvp;
731}
732
733
734/*
735 * hup - Catch SIGHUP signal.
736 *
737 * Indicates that the physical layer has been disconnected.
738 * We don't rely on this indication; if the user has sent this
739 * signal, we just take the link down.
740 */
741static void
742hup(sig)
743    int sig;
744{
745    syslog(LOG_INFO, "Hangup (SIGHUP)");
746    kill_link = 1;
747}
748
749
750/*
751 * term - Catch SIGTERM signal and SIGINT signal (^C/del).
752 *
753 * Indicates that we should initiate a graceful disconnect and exit.
754 */
755/*ARGSUSED*/
756static void
757term(sig)
758    int sig;
759{
760    syslog(LOG_INFO, "Terminating on signal %d.", sig);
761    persist = 0;		/* don't try to restart */
762    kill_link = 1;
763}
764
765
766/*
767 * chld - Catch SIGCHLD signal.
768 * Calls reap_kids to get status for any dead kids.
769 */
770static void
771chld(sig)
772    int sig;
773{
774    reap_kids();
775}
776
777
778/*
779 * toggle_debug - Catch SIGUSR1 signal.
780 *
781 * Toggle debug flag.
782 */
783/*ARGSUSED*/
784static void
785toggle_debug(sig)
786    int sig;
787{
788    debug = !debug;
789    note_debug_level();
790}
791
792
793/*
794 * open_ccp - Catch SIGUSR2 signal.
795 *
796 * Try to (re)negotiate compression.
797 */
798/*ARGSUSED*/
799static void
800open_ccp(sig)
801    int sig;
802{
803    open_ccp_flag = 1;
804}
805
806
807/*
808 * device_script - run a program to connect or disconnect the
809 * serial device.
810 */
811int
812device_script(program, in, out)
813    char *program;
814    int in, out;
815{
816    int pid;
817    int status;
818    int errfd;
819
820    pid = fork();
821
822    if (pid < 0) {
823	syslog(LOG_ERR, "Failed to create child process: %m");
824	die(1);
825    }
826
827    if (pid == 0) {
828	dup2(in, 0);
829	dup2(out, 1);
830	errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0644);
831	if (errfd >= 0)
832	    dup2(errfd, 2);
833	setuid(getuid());
834	setgid(getgid());
835	execl("/bin/sh", "sh", "-c", program, (char *)0);
836	syslog(LOG_ERR, "could not exec /bin/sh: %m");
837	_exit(99);
838	/* NOTREACHED */
839    }
840
841    while (waitpid(pid, &status, 0) < 0) {
842	if (errno == EINTR)
843	    continue;
844	syslog(LOG_ERR, "error waiting for (dis)connection process: %m");
845	die(1);
846    }
847
848    return (status == 0 ? 0 : -1);
849}
850
851
852/*
853 * run-program - execute a program with given arguments,
854 * but don't wait for it.
855 * If the program can't be executed, logs an error unless
856 * must_exist is 0 and the program file doesn't exist.
857 */
858int
859run_program(prog, args, must_exist)
860    char *prog;
861    char **args;
862    int must_exist;
863{
864    int pid;
865
866    pid = fork();
867    if (pid == -1) {
868	syslog(LOG_ERR, "Failed to create child process for %s: %m", prog);
869	return -1;
870    }
871    if (pid == 0) {
872	int new_fd;
873
874	/* Leave the current location */
875	(void) setsid();    /* No controlling tty. */
876	(void) umask (S_IRWXG|S_IRWXO);
877	(void) chdir ("/"); /* no current directory. */
878	setuid(geteuid());
879	setgid(getegid());
880
881	/* Ensure that nothing of our device environment is inherited. */
882	close (0);
883	close (1);
884	close (2);
885	close (fd);  /* tty interface to the ppp device */
886	/* XXX should call sysdep cleanup procedure here */
887
888        /* Don't pass handles to the PPP device, even by accident. */
889	new_fd = open (_PATH_DEVNULL, O_RDWR);
890	if (new_fd >= 0) {
891	    if (new_fd != 0) {
892	        dup2  (new_fd, 0); /* stdin <- /dev/null */
893		close (new_fd);
894	    }
895	    dup2 (0, 1); /* stdout -> /dev/null */
896	    dup2 (0, 2); /* stderr -> /dev/null */
897	}
898
899#ifdef BSD
900	/* Force the priority back to zero if pppd is running higher. */
901	if (setpriority (PRIO_PROCESS, 0, 0) < 0)
902	    syslog (LOG_WARNING, "can't reset priority to 0: %m");
903#endif
904
905	/* SysV recommends a second fork at this point. */
906
907	execve(prog, args);
908	if (must_exist || errno != ENOENT)
909	    syslog(LOG_WARNING, "Can't execute %s: %m", prog);
910	_exit(-1);
911    }
912    MAINDEBUG((LOG_DEBUG, "Script %s started; pid = %d", prog, pid));
913    ++n_children;
914    return 0;
915}
916
917
918/*
919 * reap_kids - get status from any dead child processes,
920 * and log a message for abnormal terminations.
921 */
922void
923reap_kids()
924{
925    int pid, status;
926
927    if (n_children == 0)
928	return;
929    if ((pid = waitpid(-1, &status, WNOHANG)) == -1) {
930	if (errno != ECHILD)
931	    syslog(LOG_ERR, "Error waiting for child process: %m");
932	return;
933    }
934    if (pid > 0) {
935	--n_children;
936	if (WIFSIGNALED(status)) {
937	    syslog(LOG_WARNING, "Child process %d terminated with signal %d",
938		   pid, WTERMSIG(status));
939	}
940    }
941}
942
943
944/*
945 * log_packet - format a packet and log it.
946 */
947
948char line[256];			/* line to be logged accumulated here */
949char *linep;
950
951void
952log_packet(p, len, prefix)
953    u_char *p;
954    int len;
955    char *prefix;
956{
957    strcpy(line, prefix);
958    linep = line + strlen(line);
959    format_packet(p, len, pr_log, NULL);
960    if (linep != line)
961	syslog(LOG_DEBUG, "%s", line);
962}
963
964/*
965 * format_packet - make a readable representation of a packet,
966 * calling `printer(arg, format, ...)' to output it.
967 */
968void
969format_packet(p, len, printer, arg)
970    u_char *p;
971    int len;
972    void (*printer) __P((void *, char *, ...));
973    void *arg;
974{
975    int i, n;
976    u_short proto;
977    u_char x;
978
979    if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
980	p += 2;
981	GETSHORT(proto, p);
982	len -= PPP_HDRLEN;
983	for (i = 0; i < N_PROTO; ++i)
984	    if (proto == prottbl[i].protocol)
985		break;
986	if (i < N_PROTO) {
987	    printer(arg, "[%s", prottbl[i].name);
988	    n = (*prottbl[i].printpkt)(p, len, printer, arg);
989	    printer(arg, "]");
990	    p += n;
991	    len -= n;
992	} else {
993	    printer(arg, "[proto=0x%x]", proto);
994	}
995    }
996
997    for (; len > 0; --len) {
998	GETCHAR(x, p);
999	printer(arg, " %.2x", x);
1000    }
1001}
1002
1003#ifdef __STDC__
1004#include <stdarg.h>
1005
1006void
1007pr_log(void *arg, char *fmt, ...)
1008{
1009    int n;
1010    va_list pvar;
1011    char buf[256];
1012
1013    va_start(pvar, fmt);
1014    vsprintf(buf, fmt, pvar);
1015    va_end(pvar);
1016
1017    n = strlen(buf);
1018    if (linep + n + 1 > line + sizeof(line)) {
1019	syslog(LOG_DEBUG, "%s", line);
1020	linep = line;
1021    }
1022    strcpy(linep, buf);
1023    linep += n;
1024}
1025
1026#else /* __STDC__ */
1027#include <varargs.h>
1028
1029void
1030pr_log(arg, fmt, va_alist)
1031void *arg;
1032char *fmt;
1033va_dcl
1034{
1035    int n;
1036    va_list pvar;
1037    char buf[256];
1038
1039    va_start(pvar);
1040    vsprintf(buf, fmt, pvar);
1041    va_end(pvar);
1042
1043    n = strlen(buf);
1044    if (linep + n + 1 > line + sizeof(line)) {
1045	syslog(LOG_DEBUG, "%s", line);
1046	linep = line;
1047    }
1048    strcpy(linep, buf);
1049    linep += n;
1050}
1051#endif
1052
1053/*
1054 * print_string - print a readable representation of a string using
1055 * printer.
1056 */
1057void
1058print_string(p, len, printer, arg)
1059    char *p;
1060    int len;
1061    void (*printer) __P((void *, char *, ...));
1062    void *arg;
1063{
1064    int c;
1065
1066    printer(arg, "\"");
1067    for (; len > 0; --len) {
1068	c = *p++;
1069	if (' ' <= c && c <= '~')
1070	    printer(arg, "%c", c);
1071	else
1072	    printer(arg, "\\%.3o", c);
1073    }
1074    printer(arg, "\"");
1075}
1076
1077/*
1078 * novm - log an error message saying we ran out of memory, and die.
1079 */
1080void
1081novm(msg)
1082    char *msg;
1083{
1084    syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg);
1085    die(1);
1086}
1087