1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
|
/*
* diald.c - Demand dialing daemon for ppp.
*
* Copyright (c) 1994, 1995, 1996 Eric Schenk.
* All rights reserved. Please see the file LICENSE which should be
* distributed with this software for terms of use.
*
* Portions of this code were derived from the code for pppd copyright
* (c) 1989 Carnegie Mellon University. The copyright notice on this code
* is reproduced below.
*
* Copyright (c) 1989 Carnegie Mellon University.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by Carnegie Mellon University. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "diald.h"
#include "version.h"
/* intialized variables. */
int modem_fd = -1; /* modem device fp (for proxy reads) */
MONITORS *monitors = 0; /* Monitor pipes */
int modem_hup = 0; /* have we seen a modem HUP? */
int sockfd = -1; /* controling socket */
int delayed_quit = 0; /* has the user requested a delayed quit? */
int request_down = 0; /* has the user requested link down? */
int request_up = 0; /* has the user requested link down? */
int forced = 0; /* has the user requested the link forced up? */
int link_pid = 0; /* current protocol control command pid */
int dial_pid = 0; /* current dial command pid */
int running_pid = 0; /* current system command pid */
int running_status = 0; /* status of last system command */
int dial_status = 0; /* status from last dial command */
int state_timeout = -1; /* state machine timeout counter */
int proxy_iface = 0; /* Interface for the proxy */
int link_iface = -1; /* Interface for the link */
int force_dynamic = 0; /* true if connect passed back an addr */
int redial_rtimeout = -1; /* initialized value */
int dial_failures = 0; /* count of dialing failures */
int ppp_half_dead = 0; /* is the ppp link half dead? */
int terminate = 0;
char *pidfile = 0;
static PIPE fifo_pipe;
int argc_save;
char **argv_save;
#ifdef DEBIAN_REDIRECT_PPPD_STDERR
int pppd_stderr_fd=-1;
#endif /* ndef DEBIAN_REDIRECT_PPPD_STDERR */
void do_config(void)
{
init_vars();
flush_prules();
flush_vars();
flush_filters();
/* Get the default defs and config files first */
parse_options_file(DIALD_DEFS_FILE);
parse_options_file(DIALD_CONFIG_FILE);
/* Get the command line modifications */
parse_args(argc_save-1,argv_save+1);
/* Do validity checks on the setup */
check_setup();
/* FIXME: minor memory leak here */
orig_local_ip = strdup(local_ip);
orig_remote_ip = strdup(remote_ip);
}
void main(int argc, char *argv[])
{
int sel;
struct timeval timeout;
fd_set readfds;
long tstamp = ticks();
long ts;
argc_save = argc;
argv_save = argv;
/* initialize system log interface */
openlog("diald", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_LOCAL2);
/* initialize a firewall unit so we can store our options */
/* If I get things into a device this should be an "open" */
fwunit = ctl_firewall(IP_FW_OPEN,0);
parse_init();
do_config();
become_daemon();
/* Get an internet socket for doing socket ioctls. */
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
syslog(LOG_ERR, "Couldn't create IP socket: %m");
die(1);
}
open_fifo();
if (debug&DEBUG_VERBOSE)
syslog(LOG_INFO,"Starting diald version %s",VERSION);
signal_setup();
filter_setup();
/* get a pty and open up a proxy link on it */
get_pty(&proxy_mfd,&proxy_sfd);
proxy_mfp = fdopen(proxy_mfd,"r+");
proxy_up();
idle_filter_proxy();
if (debug&DEBUG_VERBOSE)
syslog(LOG_INFO,"Diald initial setup completed.");
/* main loop */
while (!terminate) {
/* wait up to a second for an event */
FD_ZERO(&readfds);
if (fifo_fd != -1)
FD_SET(fifo_fd,&readfds);
#ifdef DEBIAN_REDIRECT_PPPD_STDERR
if (pppd_stderr_fd != -1)
FD_SET(pppd_stderr_fd, &readfds);
#endif /* ndef DEBIAN_REDIRECT_PPPD_STDERR */
FD_SET(proxy_mfd,&readfds);
FD_SET(snoopfd,&readfds);
/* Compute the likely timeout for the next second boundary */
ts = tstamp + PAUSETIME*CLK_TCK - ticks();
if (ts < 0) ts = 0;
timeout.tv_sec = ts/CLK_TCK;
timeout.tv_usec = 1000*(ts%CLK_TCK)/CLK_TCK;
sel = select(100,&readfds,0,0,&timeout);
if (sel > 0) {
/* read user commands off the fifo */
if (fifo_fd != -1 && FD_ISSET(fifo_fd,&readfds)) fifo_read();
/* update the connection filters */
if (FD_ISSET(snoopfd,&readfds)) filter_read();
/* deal with packets coming into the pty proxy link */
if (FD_ISSET(proxy_mfd,&readfds)) proxy_read();
#ifdef DEBIAN_REDIRECT_PPPD_STDERR
/* read pppd's stderr */
if (pppd_stderr_fd != -1 && FD_ISSET(pppd_stderr_fd, &readfds)) {
char buf[512];
int readchars;
readchars=read(pppd_stderr_fd, buf, 511);
if (readchars==-1) {
syslog(LOG_ERR, "cannot read on pppd's stderr: %m");
close(pppd_stderr_fd);
pppd_stderr_fd=-1;
} else if (readchars==0) {
/* EOF */
close(pppd_stderr_fd);
pppd_stderr_fd=-1;
} else {
/* Error message from pppd */
buf[readchars]=0;
syslog(LOG_ERR, "pppd error: \"%s\"", buf);
}
}
#endif /* ndef DEBIAN_REDIRECT_PPPD_STDERR */
}
/* check if ticks() has advanced a second since last check.
* This is immune to wall clock skew because we use the ticks count.
*/
ts = tstamp + PAUSETIME*CLK_TCK - ticks();
if (ts <= 0) {
tstamp = ticks();
fire_timers();
/* Advance a second on state machine timeouts.
* Under high load this can get stretched out.
* Even under low load it is likely to have 1% error.
* This doesn't bother me enough to bother changing things.
*/
if (state_timeout > 0) state_timeout--;
if (debug&DEBUG_TICK)
syslog(LOG_DEBUG,"--- tick --- state %d block %d state_timeout %d",state,blocked,state_timeout);
monitor_queue();
}
change_state();
}
die(0);
}
/*
* Change into a daemon.
* Get rid of the stdio streams, and disassociate from the original
* controling terminal, and become a group leader.
*/
void become_daemon()
{
int pid;
FILE *fp;
if (daemon_flag) {
close(0);
close(1);
close(2);
/* go into the background */
if ((pid = fork()) < 0) {
syslog(LOG_ERR,"Could not fork into background: %m");
die(1);
}
/* parent process is finished */
if (pid != 0) exit(0);
}
pidfile = malloc(strlen(run_prefix) + strlen(pidlog) + 2);
sprintf(pidfile,"%s/%s",run_prefix,pidlog);
if ((fp = fopen(pidfile,"w")) != NULL) {
fprintf(fp,"%d\n",getpid());
fclose(fp);
} else {
syslog(LOG_ERR,"Unable to create run file %s: %m",pidfile);
}
}
/* Open the command fifo, if any */
void open_fifo()
{
struct stat sbuf;
if (fifoname) {
if (stat(fifoname,&sbuf) < 0 || !(sbuf.st_mode&S_IFIFO)) {
syslog(LOG_INFO,"Creating FIFO");
/* Create the fifo. */
mknod(fifoname, S_IFIFO|0277, 0);
chmod(fifoname, 0600);
}
/* We need to open this RDWR to make select() work the
* way we want in kernels after 1.3.81. In particular
* we don't want select() to return 1 whenever there
* are no writers on the remote side of the command fifo.
* This guarantees that there is always at least one writer...
*/
if ((fifo_fd = open(fifoname, O_RDWR)) >= 0) {
if (debug&DEBUG_VERBOSE)
syslog(LOG_INFO,"Using fifo %s",fifoname);
pipe_init(fifo_fd,&fifo_pipe);
} else {
syslog(LOG_ERR,"Could not open fifo file %s",fifoname);
fifo_fd = -1;
}
} else {
/* make sure to invalidate the fifo_fd if we don't open one. */
fifo_fd = -1;
}
}
/*
* Set up the signal handlers.
*/
static sigset_t sig_mask;
void stray_signal(int sig)
{
syslog(LOG_ERR, "Stray signal %d ignored", sig);
}
void signal_setup()
{
struct sigaction sa;
/* set up signal handlers */
sigemptyset(&sig_mask);
sigaddset(&sig_mask, SIGHUP);
sigaddset(&sig_mask, SIGINT);
sigaddset(&sig_mask, SIGTERM);
sigaddset(&sig_mask, SIGUSR1);
sigaddset(&sig_mask, SIGUSR2);
sigaddset(&sig_mask, SIGCHLD);
sigaddset(&sig_mask, SIGPIPE);
#define SIGNAL(s, handler) { \
sa.sa_handler = handler; \
if (sigaction(s, &sa, NULL) < 0) { \
syslog(LOG_ERR, "sigaction(%d): %m", s); \
die(1); \
} \
}
sa.sa_mask = sig_mask;
sa.sa_flags = 0;
SIGNAL(SIGHUP, sig_hup); /* Hangup: modem went down. */
SIGNAL(SIGINT, sig_intr); /* Interrupt: take demand dialer down */
SIGNAL(SIGQUIT, stray_signal);
SIGNAL(SIGILL, stray_signal);
SIGNAL(SIGABRT, stray_signal);
SIGNAL(SIGIOT, stray_signal);
SIGNAL(SIGBUS, stray_signal);
SIGNAL(SIGFPE, stray_signal);
SIGNAL(SIGUSR1, linkup); /* User requests the link to go up */
SIGNAL(SIGSEGV, stray_signal);
SIGNAL(SIGUSR2, print_filter_queue); /* dump the packet queue to the log */
SIGNAL(SIGPIPE, SIG_IGN);
SIGNAL(SIGTERM, sig_term); /* Terminate: user take link down */
SIGNAL(SIGSTKFLT, stray_signal);
SIGNAL(SIGCHLD, sig_chld); /* reap dead kids */
SIGNAL(SIGURG, stray_signal);
SIGNAL(SIGXCPU, stray_signal);
SIGNAL(SIGXFSZ, stray_signal);
SIGNAL(SIGVTALRM, stray_signal);
SIGNAL(SIGPROF, stray_signal);
SIGNAL(SIGWINCH, stray_signal);
SIGNAL(SIGIO, stray_signal);
SIGNAL(SIGPOLL, stray_signal);
SIGNAL(SIGPWR, stray_signal);
}
void block_signals()
{
sigprocmask(SIG_BLOCK, &sig_mask, NULL);
}
void default_sigacts()
{
struct sigaction sa;
sa.sa_mask = sig_mask;
sa.sa_flags = 0;
SIGNAL(SIGHUP, SIG_DFL);
SIGNAL(SIGINT, SIG_DFL);
SIGNAL(SIGTERM, SIG_DFL);
SIGNAL(SIGUSR1, SIG_DFL);
SIGNAL(SIGUSR2, SIG_DFL);
SIGNAL(SIGCHLD, SIG_DFL);
SIGNAL(SIGALRM, SIG_DFL);
SIGNAL(SIGPIPE, SIG_DFL);
}
void unblock_signals()
{
sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);
}
/*
* Get a pty and open both the slave and master sides.
*/
void get_pty(int *mfd, int *sfd)
{
char *ptys = "0123456789abcdef";
int i,c;
static char buf[128];
/* FIXME: this is a crudy way to find a pty.
*/
for (c = 'p'; c <= 's'; c++)
for (i = 0; i < 16; i++) {
sprintf(buf,"/dev/pty%c%c",c,ptys[i]);
if ((*mfd = open(buf,O_RDWR)) >= 0) {
sprintf(buf,"/dev/tty%c%c",c,ptys[i]);
if ((*sfd = open(buf,O_RDWR|O_NOCTTY|O_NDELAY)) < 0) {
syslog(LOG_ERR,"Can't open slave side of pty: %m");
die(1);
}
return;
}
}
syslog(LOG_ERR,"No pty found in range pty[p-s][0-9a-f]\n");
die(1);
}
/* Read a request off the fifo.
* Valid requests are:
* block - block diald from calling out.
* unblock - unblock diald from calling out.
* down - bring the link down.
* up - bring the link up.
* delayed-quit - quit next time diald is idle.
* quit - stop diald in its tracks.
* queue - dump the filter queue.
* debug level - set the debug level.
* force - force diald to put the connection up and keep it up.
* unforce - remove the forced up requirement.
* connect pid dev - go up on a connection to the named port.
* We assume the connection negotiations are
* already finished and any lock files are in place.
* When the connection should be killed we send a
* SIGTERM to the given pid.
* dynamic <lip> <rip> - pass back dynamic IP config info to diald.
* message <txt> - set the message text from the connect script.
* monitor file - start a monitoring program.
* reset - reread the configuration information.
*/
void fifo_read()
{
int i;
int pid, dev, j,k,l = 0;
char *buf, *tail;
i = pipe_read(&fifo_pipe);
buf = tail = fifo_pipe.buf;
if (i < 0) {
fifo_fd = -1;
return;
}
if (i == 0) return;
while (i--) {
if (*tail == '\n') {
*tail = '\0';
/* Ok, we've got a line, now we need to "parse" it. */
if (strcmp(buf,"block") == 0) {
syslog(LOG_INFO, "FIFO: Block request received.");
blocked = 1;
} else if (strcmp(buf,"unblock") == 0) {
syslog(LOG_INFO, "FIFO: Unblock request received.");
blocked = 0;
} else if (strcmp(buf,"force") == 0) {
syslog(LOG_INFO, "FIFO: Force request received.");
forced = 1;
} else if (strcmp(buf,"unforce") == 0) {
syslog(LOG_INFO, "FIFO: Unforce request received.");
forced = 0;
} else if (strcmp(buf,"down") == 0) {
syslog(LOG_INFO, "FIFO: Link down request received.");
request_down = 1;
request_up = 0;
} else if (strcmp(buf,"up") == 0) {
syslog(LOG_INFO, "FIFO: Link up request received.");
request_down = 0;
request_up = 1;
} else if (strcmp(buf,"delayed-quit") == 0) {
syslog(LOG_INFO, "FIFO. Delayed termination request received.");
delayed_quit = 1;
} else if (strcmp(buf,"quit") == 0) {
syslog(LOG_INFO, "FIFO. Termination request received.");
terminate = 1;
} else if (strcmp(buf,"reset") == 0) {
syslog(LOG_INFO, "FIFO. Reset request received. Re-reading configuration.");
do_config();
} else if (strcmp(buf,"queue") == 0) {
struct firewall_req req;
syslog(LOG_INFO,"FIFO. User requested dump of firewall queue.");
syslog(LOG_INFO,"--------------------------------------");
req.unit = fwunit;
ctl_firewall(IP_FW_PCONN,&req);
syslog(LOG_INFO,"--------------------------------------");
} else if (sscanf(buf,"debug %d", &pid) == 1) {
syslog(LOG_INFO,"FIFO. Changing debug flags to %d.",pid);
debug = pid;
} else if ((sscanf(buf,"dynamic %n%*s%n %n",&j,&k,&l) >= 0) && l) {
buf[k] = 0;
if (inet_addr(buf+j) == (unsigned long)0xffffffff
|| inet_addr(buf+l) == (unsigned long)0xffffffff) {
syslog(LOG_INFO,"FIFO: bad parameters '%s' and '%s' to dynamic command ignored", buf+j,buf+l);
} else {
local_ip = strdup(buf+j);
remote_ip = strdup(buf+l);
force_dynamic = 1;
}
} else if (strncmp(buf,"monitor", 7) == 0) {
struct stat sbuf;
int fd;
MONITORS *new;
k = 0;
if (sscanf(buf,"monitor %d %n",&j,&k) == 1) {
syslog(LOG_INFO,"FIFO: monitor connection at info level %d to %s requested",
j,buf+k);
} else if (buf[7] != 0 && buf[7] == ' ') {
syslog(LOG_INFO,"FIFO: full monitor connection to %s requested",
buf+k);
j = 255; /* Heavy weight connection requested */
k = 8;
}
if (k >= 8) {
/* Check list to see if this is just a status change */
block_signals(); /* don't let anything mess up the data */
new = monitors;
while (new) {
if (strcmp(new->name,buf+k) == 0) {
new->level = j;
output_state();
break;
}
new = new->next;
}
if (!new) {
if (stat(fifoname,&sbuf) < 0 || !sbuf.st_mode&S_IFIFO) {
syslog(LOG_INFO,"FIFO: %s not a pipe.",
buf+k);
} else if ((fd = open(buf+k,O_WRONLY|O_NDELAY))<0) {
syslog(LOG_INFO,"FIFO: could not open pipe %s: %m",
buf+k);
} else {
new = (MONITORS *)malloc(sizeof(MONITORS));
new->name = strdup(buf+k);
new->next = monitors;
new->fd = fd;
new->level = j;
monitors = new;
output_state();
}
}
unblock_signals();
} else {
syslog(LOG_INFO,"FIFO: empty monitor request ignored");
}
} else if (strncmp(buf,"message ",8) == 0) {
/* pass a message from the connector on to the monitor */
if (monitors) {
mon_write(MONITOR_MESSAGE,"MESSAGE\n",8);
mon_write(MONITOR_MESSAGE,buf+8,strlen(buf+8));
mon_write(MONITOR_MESSAGE,"\n",1);
}
} else if (sscanf(buf,"connect %d %n", &pid, &dev) == 1) {
if (pid > 1) {
if ((state != STATE_DOWN && state != STATE_CLOSE
&& !give_way)
|| state==STATE_UP || req_pid) {
/* somebody else already has this diald, tell 'em */
kill(pid, SIGTERM);
syslog(LOG_INFO,"FIFO: link up requested but denied");
} else {
req_pid = pid;
req_dev = (char *)malloc(tail-(buf+dev)+1);
if (req_dev == 0) {
req_pid = 0;
syslog(LOG_ERR,"FIFO: no memory to store requested devce!");
} else {
strcpy(req_dev, buf+dev);
request_down = 0;
request_up = 1;
syslog(LOG_INFO,"FIFO: link up requested on device %s", req_dev);
}
}
}
} else {
syslog(LOG_ERR,"Unknown request '%s' made.", buf);
}
buf = tail+1;
}
tail++;
}
pipe_flush(&fifo_pipe,buf-fifo_pipe.buf);
}
/*
* Deal with master side packet on the SLIP link.
*/
void proxy_read()
{
char buffer[4096];
int len;
struct SOCKADDR to;
/* read the SLIP packet */
len = recv_packet(buffer,4096);
if (!do_reroute) {
/* if we are doing unsafe routing, all counting is in the filter.
* otherwise we can see transmitted bytes directly at this spot.
*/
txtotal += len;
itxtotal += len;
rxtotal -= len; /* since it will double count on the snoop */
irxtotal -= len;
}
/* If we get here with the link up and fwdfd not -1,
* and we are rerouting, then it must be
* that the external interface has gone down without
* taking the link with it, and as a result our route
* to the external interface got lost. (This CAN legally
* happen with PPP). In this case we buffer the packet so
* we can retransmit it when the link comes back up.
* OR
* the kernel is retransmitting something through sl0, despite
* the existance of a route through another device...
*/
/* if the external iface is up then probably we can send it on */
if (link_iface != -1 && fwdfd != -1) {
/* Make sure we try to restore the link to working condition now... */
if (do_reroute && mode == MODE_PPP) {
/* Check if a route exists at this point through the ppp device. */
/* If not then we must be half dead. */
if (!ppp_route_exists()) {
/* The external iface is down, buffer the packet so we can
* forward it when the iface comes up.
*/
ppp_half_dead = 1;
if (buffer_packets)
buffer_packet(len,buffer);
return;
}
}
/* Ok, the interface is there, and the route is up,
* so just send it on. This can happen when routing is switched
* in the middle of a retransmission sequence. (There is a race
* between the route switching and the forwarding I think.)
*/
#ifdef HAS_SOCKADDR_PKT
to.spkt_family = AF_INET;
strcpy(to.spkt_device,snoop_dev);
to.spkt_protocol = htons(ETH_P_IP);
#else
to.sa_family = AF_INET;
strcpy(to.sa_data,snoop_dev);
#endif
if (debug&DEBUG_VERBOSE)
syslog(LOG_DEBUG,"Forwarding packet of length %d",len);
if (sendto(fwdfd,buffer,len,0,(struct sockaddr *)&to,sizeof(struct SOCKADDR)) < 0) {
syslog(LOG_ERR,
"Error forwarding data packet to physical device: %m");
}
} else {
/* If the link isn't up, then we better buffer the packets */
if (buffer_packets)
buffer_packet(len,buffer);
}
}
/*
* Terminate diald gracefully.
*/
static int in_die = 0;
void die(int i)
{
int count;
char tmp[128];
if (!in_die) {
sprintf(tmp,"Diald is dying with code %d",i);
syslog(LOG_INFO, tmp);
in_die = 1;
/* We're killing without a care here. Uhggg. */
if (link_pid) kill(link_pid,SIGINT);
if (dial_pid) kill(dial_pid,SIGINT);
if (running_pid) kill(running_pid,SIGINT);
/* Wait up to 30 seconds for them to die */
for (count = 0; (link_pid || dial_pid) && count < 30; count++)
sleep(1);
/* If they aren't dead yet, kill them for sure */
if (link_pid) kill(link_pid,SIGKILL);
if (dial_pid) kill(dial_pid,SIGKILL);
if (running_pid) kill(running_pid,SIGKILL);
/* Give the system a second to send the signals */
if (link_pid || dial_pid || running_pid) sleep(1);
close_modem();
interface_down();
proxy_down();
unlink(pidfile);
exit(i);
}
}
/*
* Signal handlers.
*/
/*
* Modem link went down.
*/
void sig_hup(int sig)
{
syslog(LOG_INFO, "SIGHUP: modem got hung up on.");
modem_hup = 1;
}
/*
* User wants the link to go down.
* (Perhaps there should be a 10 second delay? Configurable????)
*/
void sig_intr(int sig)
{
syslog(LOG_INFO, "SIGINT: Link down request received.");
request_down = 1;
request_up = 0;
}
/*
* The user has requested that the link be put up.
*/
void linkup(int sig)
{
syslog(LOG_INFO, "SIGUSR1. External link up request received.");
request_down = 0;
request_up = 1;
}
/*
* A child process died. Find out which one.
*/
void sig_chld(int sig)
{
int pid, status;
static int seq = 0;
++seq;
while ((pid = waitpid(-1,&status,WNOHANG)) > 0) {
if (debug&DEBUG_VERBOSE)
syslog( LOG_DEBUG, "SIGCHLD[%d]: pid %d %s, status %d", seq, pid,
pid == link_pid ? "link"
: pid == dial_pid ? "dial"
: pid == running_pid ? "system"
: "other",
status);
if (pid == link_pid) link_pid = 0;
else if (pid == dial_pid) { dial_status = status; dial_pid = 0; }
else if (pid == running_pid) { running_status = status; running_pid = 0; }
else if (!WIFEXITED(status))
syslog(LOG_ERR,"Abnormal exit (status %d) on pid %d",status,pid);
else if (WEXITSTATUS(status) != 0)
syslog(LOG_ERR,"Nonzero exit status (%d) on pid %d",
WEXITSTATUS(status),pid);
if (pid > 0) {
if (WIFSIGNALED(status)) {
syslog(LOG_WARNING, "child process %d terminated with signal %d",
pid, WTERMSIG(status));
}
}
}
if (pid && errno != ECHILD)
syslog(LOG_ERR, "waitpid: %m");
return;
}
/*
* User wants diald to be terminated.
*/
void sig_term(int sig)
{
syslog(LOG_INFO, "SIGTERM. Termination request received.");
terminate = 1;
}
int report_system_result(int res,char *buf)
{
if (res == -1)
syslog(LOG_ERR,"System call failure on command '%s'",buf);
else if (!WIFEXITED(res))
syslog(LOG_ERR,"Abnormal exit (status %d) on command '%s'",res,buf);
else if (WEXITSTATUS(res) != 0)
syslog(LOG_ERR,"Nonzero exit status (%d) on command '%s'",WEXITSTATUS(res),buf);
else
return 0;
return 1;
}
int system(const char *buf)
{
int fd;
block_signals();
if (debug&DEBUG_VERBOSE)
syslog(LOG_DEBUG,"running '%s'",buf);
running_pid = fork();
if (running_pid < 0) {
unblock_signals();
syslog(LOG_ERR, "failed to fork and run '%s': %m",buf);
return -1;
}
if (running_pid == 0) {
/* change the signal actions back to the defaults, then unblock them. */
default_sigacts();
unblock_signals();
/* Leave the current location */
(void) setsid(); /* No controlling tty. */
(void) umask (S_IRWXG|S_IRWXO);
(void) chdir ("/"); /* no current directory. */
/* close all fd's the child should not see */
close(0);
close(1);
close(2);
if (modem_fd >= 0) close(modem_fd);
close(proxy_mfd); /* close the master pty endpoint */
close(proxy_sfd); /* close the slave pty endpoint */
if (fifo_fd != -1) close(fifo_fd);
if (monitors) {
MONITORS *c = monitors;
while (c) {
close(c->fd);
c = c->next;
}
}
/* make sure the stdin, stdout and stderr get directed to /dev/null */
fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
if (fd != 0) {
dup2(fd, 0);
close(fd);
}
dup2(0, 1);
dup2(0, 2);
}
execl("/bin/sh", "sh", "-c", buf, (char *)0);
syslog(LOG_ERR, "could not exec /bin/sh: %m");
_exit(127);
/* NOTREACHED */
}
unblock_signals();
while (running_pid) {
pause();
}
return running_status;
}
void background_system(const char *buf)
{
int fd, pid;
block_signals();
if (debug&DEBUG_VERBOSE)
syslog(LOG_DEBUG,"running '%s'",buf);
pid = fork();
if (pid < 0) {
unblock_signals();
syslog(LOG_ERR, "failed to fork and run '%s': %m",buf);
return;
}
if (pid == 0) {
/* change the signal actions back to the defaults, then unblock them. */
default_sigacts();
unblock_signals();
/* Leave the current location */
(void) setsid(); /* No controlling tty. */
(void) umask (S_IRWXG|S_IRWXO);
(void) chdir ("/"); /* no current directory. */
/* close all fd's the child should not see */
close(0);
close(1);
close(2);
if (modem_fd >= 0) close(modem_fd);
close(proxy_mfd); /* close the master pty endpoint */
close(proxy_sfd); /* close the slave pty endpoint */
if (fifo_fd != -1) close(fifo_fd);
if (monitors) {
MONITORS *c = monitors;
while (c) {
close(c->fd);
c = c->next;
}
}
/* make sure the stdin, stdout and stderr get directed to /dev/null */
fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
if (fd != 0) {
dup2(fd, 0);
close(fd);
}
dup2(0, 1);
dup2(0, 2);
}
execl("/bin/sh", "sh", "-c", buf, (char *)0);
syslog(LOG_ERR, "could not exec /bin/sh: %m");
_exit(127);
/* NOTREACHED */
}
unblock_signals();
}
/*
* UGH. Pulling stuff out of the monitors list is full of races.
*/
void mon_write(int level, char *message,int len)
{
static int alreadyWarned=0;
MONITORS *c = monitors, *p = 0, *cn;
block_signals(); /* don't let anything mess up the data */
while (c) {
cn = c->next;
if (c->level&level) {
if (write(c->fd,message,len) < 0) {
if (errno == EPIPE) {
syslog(LOG_INFO,"Monitor pipe %s closed.",c->name);
} else {
if (!alreadyWarned) {
syslog(LOG_INFO,"Writing error on pipe %s: %m.",c->name);
alreadyWarned=1;
}
/* Write error. The reader probably got swapped out
* or something and the pipe flooded. We'll just "loose"
* the data.
*/
p = c;
c = cn;
continue;
}
close(c->fd);
if (p) p->next = c->next;
else monitors = c->next;
free(c->name);
free(c);
} else {
p = c;
}
}
c = cn;
}
unblock_signals(); /* don't let anything mess up the data */
}
|