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