main.c revision 43187
1/* 2 * User Process PPP 3 * 4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5 * 6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the Internet Initiative Japan, Inc. The name of the 14 * IIJ may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * $Id: main.c,v 1.146 1998/12/10 18:36:30 brian Exp $ 21 * 22 * TODO: 23 */ 24 25#include <sys/types.h> 26#include <netinet/in.h> 27#include <netinet/in_systm.h> 28#include <netinet/ip.h> 29#include <sys/un.h> 30 31#include <errno.h> 32#include <fcntl.h> 33#include <paths.h> 34#include <signal.h> 35#include <stdio.h> 36#include <string.h> 37#include <sys/time.h> 38#include <termios.h> 39#include <unistd.h> 40 41#ifndef NOALIAS 42#ifdef __OpenBSD__ 43#include "alias.h" 44#else 45#include <alias.h> 46#endif 47#endif 48#include "probe.h" 49#include "mbuf.h" 50#include "log.h" 51#include "defs.h" 52#include "id.h" 53#include "timer.h" 54#include "fsm.h" 55#include "lqr.h" 56#include "hdlc.h" 57#include "lcp.h" 58#include "ccp.h" 59#include "iplist.h" 60#include "throughput.h" 61#include "slcompress.h" 62#include "ipcp.h" 63#include "filter.h" 64#include "descriptor.h" 65#include "link.h" 66#include "mp.h" 67#include "bundle.h" 68#include "auth.h" 69#include "systems.h" 70#include "sig.h" 71#include "main.h" 72#include "server.h" 73#include "prompt.h" 74#include "chat.h" 75#include "chap.h" 76#include "cbcp.h" 77#include "datalink.h" 78#include "iface.h" 79 80#ifndef O_NONBLOCK 81#ifdef O_NDELAY 82#define O_NONBLOCK O_NDELAY 83#endif 84#endif 85 86static void DoLoop(struct bundle *); 87static void TerminalStop(int); 88static const char *ex_desc(int); 89 90static struct bundle *SignalBundle; 91static struct prompt *SignalPrompt; 92 93void 94Cleanup(int excode) 95{ 96 SignalBundle->CleaningUp = 1; 97 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 98} 99 100void 101AbortProgram(int excode) 102{ 103 server_Close(SignalBundle); 104 log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode)); 105 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 106 bundle_Destroy(SignalBundle); 107 log_Close(); 108 exit(excode); 109} 110 111static void 112CloseConnection(int signo) 113{ 114 /* NOTE, these are manual, we've done a setsid() */ 115 sig_signal(SIGINT, SIG_IGN); 116 log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo); 117 bundle_Down(SignalBundle, CLOSE_STAYDOWN); 118 sig_signal(SIGINT, CloseConnection); 119} 120 121static void 122CloseSession(int signo) 123{ 124 log_Printf(LogPHASE, "Signal %d, terminate.\n", signo); 125 Cleanup(EX_TERM); 126} 127 128static pid_t BGPid = 0; 129 130static void 131KillChild(int signo) 132{ 133 log_Printf(LogPHASE, "Parent: Signal %d\n", signo); 134 kill(BGPid, SIGINT); 135} 136 137static void 138TerminalCont(int signo) 139{ 140 signal(SIGCONT, SIG_DFL); 141 prompt_Continue(SignalPrompt); 142} 143 144static void 145TerminalStop(int signo) 146{ 147 prompt_Suspend(SignalPrompt); 148 signal(SIGCONT, TerminalCont); 149 raise(SIGSTOP); 150} 151 152static void 153BringDownServer(int signo) 154{ 155 /* Drops all child prompts too ! */ 156 server_Close(SignalBundle); 157} 158 159static const char * 160ex_desc(int ex) 161{ 162 static char num[12]; /* Used immediately if returned */ 163 static const char *desc[] = { 164 "normal", "start", "sock", "modem", "dial", "dead", "done", 165 "reboot", "errdead", "hangup", "term", "nodial", "nologin" 166 }; 167 168 if (ex >= 0 && ex < sizeof desc / sizeof *desc) 169 return desc[ex]; 170 snprintf(num, sizeof num, "%d", ex); 171 return num; 172} 173 174static void 175Usage(void) 176{ 177 fprintf(stderr, 178 "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]" 179#ifndef NOALIAS 180 " [ -alias ]" 181#endif 182 " [system ...]\n"); 183 exit(EX_START); 184} 185 186static int 187ProcessArgs(int argc, char **argv, int *mode, int *alias) 188{ 189 int optc, newmode, arg; 190 char *cp; 191 192 optc = 0; 193 *mode = PHYS_INTERACTIVE; 194 *alias = 0; 195 for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) { 196 cp = argv[arg] + 1; 197 newmode = Nam2mode(cp); 198 switch (newmode) { 199 case PHYS_NONE: 200 if (strcmp(cp, "alias") == 0) { 201#ifdef NOALIAS 202 log_Printf(LogWARN, "Cannot load alias library (compiled out)\n"); 203#else 204 *alias = 1; 205#endif 206 optc--; /* this option isn't exclusive */ 207 } else 208 Usage(); 209 break; 210 211 case PHYS_ALL: 212 Usage(); 213 break; 214 215 default: 216 *mode = newmode; 217 } 218 } 219 220 if (optc > 1) { 221 fprintf(stderr, "You may specify only one mode.\n"); 222 exit(EX_START); 223 } 224 225 if (*mode == PHYS_AUTO && arg == argc) { 226 fprintf(stderr, "A system must be specified in auto mode.\n"); 227 exit(EX_START); 228 } 229 230 return arg; /* Don't SetLabel yet ! */ 231} 232 233static void 234CheckLabel(const char *label, struct prompt *prompt, int mode) 235{ 236 const char *err; 237 238 if ((err = system_IsValid(label, prompt, mode)) != NULL) { 239 fprintf(stderr, "You may not use ppp in this mode with this label\n"); 240 fprintf(stderr, "%s: %s\n", label, err); 241 if (mode == PHYS_DIRECT) 242 log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n", 243 label, err); 244 log_Close(); 245 exit(1); 246 } 247} 248 249 250int 251main(int argc, char **argv) 252{ 253 char *name; 254 const char *lastlabel; 255 int nfds, mode, alias, label, arg; 256 struct bundle *bundle; 257 struct prompt *prompt; 258 259 nfds = getdtablesize(); 260 if (nfds >= FD_SETSIZE) 261 /* 262 * If we've got loads of file descriptors, make sure they're all 263 * closed. If they aren't, we may end up with a seg fault when our 264 * `fd_set's get too big when select()ing ! 265 */ 266 while (--nfds > 2) 267 close(nfds); 268 269 name = strrchr(argv[0], '/'); 270 log_Open(name ? name + 1 : argv[0]); 271 272#ifndef NOALIAS 273 PacketAliasInit(); 274#endif 275 label = ProcessArgs(argc, argv, &mode, &alias); 276 277#ifdef __FreeBSD__ 278 /* 279 * A FreeBSD hack to dodge a bug in the tty driver that drops output 280 * occasionally.... I must find the real reason some time. To display 281 * the dodgy behaviour, comment out this bit, make yourself a large 282 * routing table and then run ppp in interactive mode. The `show route' 283 * command will drop chunks of data !!! 284 */ 285 if (mode == PHYS_INTERACTIVE) { 286 close(STDIN_FILENO); 287 if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) { 288 fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY); 289 return 2; 290 } 291 } 292#endif 293 294 /* Allow output for the moment (except in direct mode) */ 295 if (mode == PHYS_DIRECT) 296 prompt = NULL; 297 else { 298 SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD); 299 prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(mode)); 300 } 301 302 ID0init(); 303 if (ID0realuid() != 0) { 304 char conf[200], *ptr; 305 306 snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE); 307 do { 308 if (!access(conf, W_OK)) { 309 log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n", 310 conf); 311 return -1; 312 } 313 ptr = conf + strlen(conf)-2; 314 while (ptr > conf && *ptr != '/') 315 *ptr-- = '\0'; 316 } while (ptr >= conf); 317 } 318 319 if (label < argc) 320 for (arg = label; arg < argc; arg++) 321 CheckLabel(argv[arg], prompt, mode); 322 else 323 CheckLabel("default", prompt, mode); 324 325 if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) { 326 log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno)); 327 return EX_START; 328 } 329 330 /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */ 331 332 if (prompt) { 333 prompt->bundle = bundle; /* couldn't do it earlier */ 334 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name); 335 } 336 SignalBundle = bundle; 337 bundle->AliasEnabled = alias; 338 if (alias) 339 bundle->cfg.opt |= OPT_IFACEALIAS; 340 341 if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0) 342 prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); 343 344 sig_signal(SIGHUP, CloseSession); 345 sig_signal(SIGTERM, CloseSession); 346 sig_signal(SIGINT, CloseConnection); 347 sig_signal(SIGQUIT, CloseSession); 348 sig_signal(SIGALRM, SIG_IGN); 349 signal(SIGPIPE, SIG_IGN); 350 351 if (mode == PHYS_INTERACTIVE) 352 sig_signal(SIGTSTP, TerminalStop); 353 354 sig_signal(SIGUSR2, BringDownServer); 355 356 lastlabel = argc == 2 ? bundle->argv1 : argv[argc - 1]; 357 for (arg = label; arg < argc; arg++) { 358 /* In case we use LABEL or ``set enddisc label'' */ 359 bundle_SetLabel(bundle, lastlabel); 360 system_Select(bundle, arg == 1 ? bundle->argv1 : argv[arg], 361 CONFFILE, prompt, NULL); 362 } 363 364 if (label < argc) 365 /* In case the last label did a ``load'' */ 366 bundle_SetLabel(bundle, lastlabel); 367 368 if (mode == PHYS_AUTO && 369 bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) { 370 prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address " 371 "in auto mode.\n"); 372 AbortProgram(EX_START); 373 } 374 375 if (mode != PHYS_INTERACTIVE) { 376 if (mode != PHYS_DIRECT) { 377 int bgpipe[2]; 378 pid_t bgpid; 379 380 if (mode == PHYS_BACKGROUND && pipe(bgpipe)) { 381 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 382 AbortProgram(EX_SOCK); 383 } 384 385 bgpid = fork(); 386 if (bgpid == -1) { 387 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 388 AbortProgram(EX_SOCK); 389 } 390 391 if (bgpid) { 392 char c = EX_NORMAL; 393 394 if (mode == PHYS_BACKGROUND) { 395 close(bgpipe[1]); 396 BGPid = bgpid; 397 /* If we get a signal, kill the child */ 398 signal(SIGHUP, KillChild); 399 signal(SIGTERM, KillChild); 400 signal(SIGINT, KillChild); 401 signal(SIGQUIT, KillChild); 402 403 /* Wait for our child to close its pipe before we exit */ 404 if (read(bgpipe[0], &c, 1) != 1) { 405 prompt_Printf(prompt, "Child exit, no status.\n"); 406 log_Printf(LogPHASE, "Parent: Child exit, no status.\n"); 407 } else if (c == EX_NORMAL) { 408 prompt_Printf(prompt, "PPP enabled.\n"); 409 log_Printf(LogPHASE, "Parent: PPP enabled.\n"); 410 } else { 411 prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c)); 412 log_Printf(LogPHASE, "Parent: Child failed (%s).\n", 413 ex_desc((int) c)); 414 } 415 close(bgpipe[0]); 416 } 417 return c; 418 } else if (mode == PHYS_BACKGROUND) { 419 close(bgpipe[0]); 420 bundle->notify.fd = bgpipe[1]; 421 } 422 423 bundle_LockTun(bundle); /* we have a new pid */ 424 425 /* -auto, -dedicated, -ddial & -background */ 426 prompt_Destroy(prompt, 0); 427 close(STDOUT_FILENO); 428 close(STDERR_FILENO); 429 close(STDIN_FILENO); 430 setsid(); 431 } else { 432 /* -direct: STDIN_FILENO gets used by modem_Open */ 433 prompt_TtyInit(NULL); 434 close(STDOUT_FILENO); 435 close(STDERR_FILENO); 436 } 437 } else { 438 /* Interactive mode */ 439 close(STDERR_FILENO); 440 prompt_TtyInit(prompt); 441 prompt_TtyCommandMode(prompt); 442 prompt_Required(prompt); 443 } 444 445 log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode)); 446 DoLoop(bundle); 447 AbortProgram(EX_NORMAL); 448 449 return EX_NORMAL; 450} 451 452static void 453DoLoop(struct bundle *bundle) 454{ 455 fd_set rfds, wfds, efds; 456 int i, nfds, nothing_done; 457 struct probe probe; 458 459 probe_Init(&probe); 460 461 do { 462 nfds = 0; 463 FD_ZERO(&rfds); 464 FD_ZERO(&wfds); 465 FD_ZERO(&efds); 466 467 /* All our datalinks, the tun device and the MP socket */ 468 descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds); 469 470 /* All our prompts and the diagnostic socket */ 471 descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds); 472 473 if (bundle_IsDead(bundle)) 474 /* Don't select - we'll be here forever */ 475 break; 476 477 i = select(nfds, &rfds, &wfds, &efds, NULL); 478 479 if (i < 0 && errno != EINTR) { 480 log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno)); 481 if (log_IsKept(LogTIMER)) { 482 struct timeval t; 483 484 for (i = 0; i <= nfds; i++) { 485 if (FD_ISSET(i, &rfds)) { 486 log_Printf(LogTIMER, "Read set contains %d\n", i); 487 FD_CLR(i, &rfds); 488 t.tv_sec = t.tv_usec = 0; 489 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 490 log_Printf(LogTIMER, "The culprit !\n"); 491 break; 492 } 493 } 494 if (FD_ISSET(i, &wfds)) { 495 log_Printf(LogTIMER, "Write set contains %d\n", i); 496 FD_CLR(i, &wfds); 497 t.tv_sec = t.tv_usec = 0; 498 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 499 log_Printf(LogTIMER, "The culprit !\n"); 500 break; 501 } 502 } 503 if (FD_ISSET(i, &efds)) { 504 log_Printf(LogTIMER, "Error set contains %d\n", i); 505 FD_CLR(i, &efds); 506 t.tv_sec = t.tv_usec = 0; 507 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 508 log_Printf(LogTIMER, "The culprit !\n"); 509 break; 510 } 511 } 512 } 513 } 514 break; 515 } 516 517 sig_Handle(); 518 519 if (i <= 0) 520 continue; 521 522 for (i = 0; i <= nfds; i++) 523 if (FD_ISSET(i, &efds)) { 524 log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i); 525 /* We deal gracefully with link descriptor exceptions */ 526 if (!bundle_Exception(bundle, i)) { 527 log_Printf(LogERROR, "Exception cannot be handled !\n"); 528 break; 529 } 530 } 531 532 if (i <= nfds) 533 break; 534 535 nothing_done = 1; 536 537 if (descriptor_IsSet(&server.desc, &rfds)) { 538 descriptor_Read(&server.desc, bundle, &rfds); 539 nothing_done = 0; 540 } 541 542 if (descriptor_IsSet(&bundle->desc, &rfds)) { 543 descriptor_Read(&bundle->desc, bundle, &rfds); 544 nothing_done = 0; 545 } 546 547 if (descriptor_IsSet(&bundle->desc, &wfds)) 548 if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) { 549 /* 550 * This is disasterous. The OS has told us that something is 551 * writable, and all our write()s have failed. Rather than 552 * going back immediately to do our UpdateSet()s and select(), 553 * we sleep for a bit to avoid gobbling up all cpu time. 554 */ 555 struct timeval t; 556 557 t.tv_sec = 0; 558 t.tv_usec = 100000; 559 select(0, NULL, NULL, NULL, &t); 560 } 561 562 } while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle)); 563 564 log_Printf(LogDEBUG, "DoLoop done.\n"); 565} 566