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