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.144 1998/10/22 02:32:49 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 86 static void DoLoop(struct bundle *); 87 static void TerminalStop(int); 88 static const char *ex_desc(int); 89 90 static struct bundle *SignalBundle; 91 static struct prompt *SignalPrompt; 92 93 void 94 Cleanup(int excode) 95 { 96 SignalBundle->CleaningUp = 1; 97 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 98 } 99 100 void 101 AbortProgram(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 111 static void 112 CloseConnection(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 121 static void 122 CloseSession(int signo) 123 { 124 log_Printf(LogPHASE, "Signal %d, terminate.\n", signo); 125 Cleanup(EX_TERM); 126 } 127 128 static pid_t BGPid = 0; 129 130 static void 131 KillChild(int signo) 132 { 133 log_Printf(LogPHASE, "Parent: Signal %d\n", signo); 134 kill(BGPid, SIGINT); 135 } 136 137 static void 138 TerminalCont(int signo) 139 { 140 signal(SIGCONT, SIG_DFL); 141 prompt_Continue(SignalPrompt); 142 } 143 144 static void 145 TerminalStop(int signo) 146 { 147 prompt_Suspend(SignalPrompt); 148 signal(SIGCONT, TerminalCont); 149 raise(SIGSTOP); 150 } 151 152 static void 153 BringDownServer(int signo) 154 { 155 /* Drops all child prompts too ! */ 156 server_Close(SignalBundle); 157 } 158 159 static const char * 160 ex_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 174 static void 175 Usage(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 186 static int 187 ProcessArgs(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 233 static void 234 CheckLabel(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 250 int 251 main(int argc, char **argv) 252 { 253 char *name; 254 int nfds, mode, alias, label, arg; 255 struct bundle *bundle; 256 struct prompt *prompt; 257 258 nfds = getdtablesize(); 259 if (nfds >= FD_SETSIZE) 260 /* 261 * If we've got loads of file descriptors, make sure they're all 262 * closed. If they aren't, we may end up with a seg fault when our 263 * `fd_set's get too big when select()ing ! 264 */ 265 while (--nfds > 2) 266 close(nfds); 267 268 name = strrchr(argv[0], '/'); 269 log_Open(name ? name + 1 : argv[0]); 270 271 #ifndef NOALIAS 272 PacketAliasInit(); 273 #endif 274 label = ProcessArgs(argc, argv, &mode, &alias); 275 276 #ifdef __FreeBSD__ 277 /* 278 * A FreeBSD hack to dodge a bug in the tty driver that drops output 279 * occasionally.... I must find the real reason some time. To display 280 * the dodgy behaviour, comment out this bit, make yourself a large 281 * routing table and then run ppp in interactive mode. The `show route' 282 * command will drop chunks of data !!! 283 */ 284 if (mode == PHYS_INTERACTIVE) { 285 close(STDIN_FILENO); 286 if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) { 287 fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY); 288 return 2; 289 } 290 } 291 #endif 292 293 /* Allow output for the moment (except in direct mode) */ 294 if (mode == PHYS_DIRECT) 295 prompt = NULL; 296 else { 297 SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD); 298 prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(mode)); 299 } 300 301 ID0init(); 302 if (ID0realuid() != 0) { 303 char conf[200], *ptr; 304 305 snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE); 306 do { 307 if (!access(conf, W_OK)) { 308 log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n", 309 conf); 310 return -1; 311 } 312 ptr = conf + strlen(conf)-2; 313 while (ptr > conf && *ptr != '/') 314 *ptr-- = '\0'; 315 } while (ptr >= conf); 316 } 317 318 if (label < argc) 319 for (arg = label; arg < argc; arg++) 320 CheckLabel(argv[arg], prompt, mode); 321 else 322 CheckLabel("default", prompt, mode); 323 324 if ((bundle = bundle_Create(TUN_PREFIX, mode, (const char **)argv)) == NULL) { 325 log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno)); 326 return EX_START; 327 } 328 if (prompt) { 329 prompt->bundle = bundle; /* couldn't do it earlier */ 330 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name); 331 } 332 SignalBundle = bundle; 333 bundle->AliasEnabled = alias; 334 if (alias) 335 bundle->cfg.opt |= OPT_IFACEALIAS; 336 337 if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0) 338 prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); 339 340 sig_signal(SIGHUP, CloseSession); 341 sig_signal(SIGTERM, CloseSession); 342 sig_signal(SIGINT, CloseConnection); 343 sig_signal(SIGQUIT, CloseSession); 344 sig_signal(SIGALRM, SIG_IGN); 345 signal(SIGPIPE, SIG_IGN); 346 347 if (mode == PHYS_INTERACTIVE) 348 sig_signal(SIGTSTP, TerminalStop); 349 350 sig_signal(SIGUSR2, BringDownServer); 351 352 for (arg = label; arg < argc; arg++) { 353 /* In case we use LABEL or ``set enddisc label'' */ 354 bundle_SetLabel(bundle, argv[argc - 1]); 355 system_Select(bundle, argv[arg], CONFFILE, prompt, NULL); 356 } 357 358 if (label < argc) 359 /* In case the last label did a ``load'' */ 360 bundle_SetLabel(bundle, argv[argc - 1]); 361 362 if (mode == PHYS_AUTO && 363 bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) { 364 prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address " 365 "in auto mode.\n"); 366 AbortProgram(EX_START); 367 } 368 369 if (mode != PHYS_INTERACTIVE) { 370 if (mode != PHYS_DIRECT) { 371 int bgpipe[2]; 372 pid_t bgpid; 373 374 if (mode == PHYS_BACKGROUND && pipe(bgpipe)) { 375 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 376 AbortProgram(EX_SOCK); 377 } 378 379 bgpid = fork(); 380 if (bgpid == -1) { 381 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 382 AbortProgram(EX_SOCK); 383 } 384 385 if (bgpid) { 386 char c = EX_NORMAL; 387 388 if (mode == PHYS_BACKGROUND) { 389 close(bgpipe[1]); 390 BGPid = bgpid; 391 /* If we get a signal, kill the child */ 392 signal(SIGHUP, KillChild); 393 signal(SIGTERM, KillChild); 394 signal(SIGINT, KillChild); 395 signal(SIGQUIT, KillChild); 396 397 /* Wait for our child to close its pipe before we exit */ 398 if (read(bgpipe[0], &c, 1) != 1) { 399 prompt_Printf(prompt, "Child exit, no status.\n"); 400 log_Printf(LogPHASE, "Parent: Child exit, no status.\n"); 401 } else if (c == EX_NORMAL) { 402 prompt_Printf(prompt, "PPP enabled.\n"); 403 log_Printf(LogPHASE, "Parent: PPP enabled.\n"); 404 } else { 405 prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c)); 406 log_Printf(LogPHASE, "Parent: Child failed (%s).\n", 407 ex_desc((int) c)); 408 } 409 close(bgpipe[0]); 410 } 411 return c; 412 } else if (mode == PHYS_BACKGROUND) { 413 close(bgpipe[0]); 414 bundle->notify.fd = bgpipe[1]; 415 } 416 417 bundle_LockTun(bundle); /* we have a new pid */ 418 419 /* -auto, -dedicated, -ddial & -background */ 420 prompt_Destroy(prompt, 0); 421 close(STDOUT_FILENO); 422 close(STDERR_FILENO); 423 close(STDIN_FILENO); 424 setsid(); 425 } else { 426 /* -direct: STDIN_FILENO gets used by modem_Open */ 427 prompt_TtyInit(NULL); 428 close(STDOUT_FILENO); 429 close(STDERR_FILENO); 430 } 431 } else { 432 /* Interactive mode */ 433 close(STDERR_FILENO); 434 prompt_TtyInit(prompt); 435 prompt_TtyCommandMode(prompt); 436 prompt_Required(prompt); 437 } 438 439 log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode)); 440 DoLoop(bundle); 441 AbortProgram(EX_NORMAL); 442 443 return EX_NORMAL; 444 } 445 446 static void 447 DoLoop(struct bundle *bundle) 448 { 449 fd_set rfds, wfds, efds; 450 int i, nfds, nothing_done; 451 struct probe probe; 452 453 probe_Init(&probe); 454 455 do { 456 nfds = 0; 457 FD_ZERO(&rfds); 458 FD_ZERO(&wfds); 459 FD_ZERO(&efds); 460 461 /* All our datalinks, the tun device and the MP socket */ 462 descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds); 463 464 /* All our prompts and the diagnostic socket */ 465 descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds); 466 467 if (bundle_IsDead(bundle)) 468 /* Don't select - we'll be here forever */ 469 break; 470 471 i = select(nfds, &rfds, &wfds, &efds, NULL); 472 473 if (i < 0 && errno != EINTR) { 474 log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno)); 475 if (log_IsKept(LogTIMER)) { 476 struct timeval t; 477 478 for (i = 0; i <= nfds; i++) { 479 if (FD_ISSET(i, &rfds)) { 480 log_Printf(LogTIMER, "Read set contains %d\n", i); 481 FD_CLR(i, &rfds); 482 t.tv_sec = t.tv_usec = 0; 483 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 484 log_Printf(LogTIMER, "The culprit !\n"); 485 break; 486 } 487 } 488 if (FD_ISSET(i, &wfds)) { 489 log_Printf(LogTIMER, "Write set contains %d\n", i); 490 FD_CLR(i, &wfds); 491 t.tv_sec = t.tv_usec = 0; 492 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 493 log_Printf(LogTIMER, "The culprit !\n"); 494 break; 495 } 496 } 497 if (FD_ISSET(i, &efds)) { 498 log_Printf(LogTIMER, "Error set contains %d\n", i); 499 FD_CLR(i, &efds); 500 t.tv_sec = t.tv_usec = 0; 501 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 502 log_Printf(LogTIMER, "The culprit !\n"); 503 break; 504 } 505 } 506 } 507 } 508 break; 509 } 510 511 sig_Handle(); 512 513 if (i <= 0) 514 continue; 515 516 for (i = 0; i <= nfds; i++) 517 if (FD_ISSET(i, &efds)) { 518 log_Printf(LogERROR, "Exception detected on descriptor %d\n", i); 519 break; 520 } 521 522 if (i <= nfds) 523 break; 524 525 nothing_done = 1; 526 527 if (descriptor_IsSet(&server.desc, &rfds)) { 528 descriptor_Read(&server.desc, bundle, &rfds); 529 nothing_done = 0; 530 } 531 532 if (descriptor_IsSet(&bundle->desc, &rfds)) { 533 descriptor_Read(&bundle->desc, bundle, &rfds); 534 nothing_done = 0; 535 } 536 537 if (descriptor_IsSet(&bundle->desc, &wfds)) 538 if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) { 539 /* 540 * This is disasterous. The OS has told us that something is 541 * writable, and all our write()s have failed. Rather than 542 * going back immediately to do our UpdateSet()s and select(), 543 * we sleep for a bit to avoid gobbling up all cpu time. 544 */ 545 struct timeval t; 546 547 t.tv_sec = 0; 548 t.tv_usec = 100000; 549 select(0, NULL, NULL, NULL, &t); 550 } 551 552 } while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle)); 553 554 log_Printf(LogDEBUG, "DoLoop done.\n"); 555 } 556