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