main.c revision 36452
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.126 1998/05/27 22:43:31 brian Exp $ 21 * 22 * TODO: 23 */ 24 25#include <sys/types.h> 26#include <sys/socket.h> 27#include <netinet/in.h> 28#include <netinet/in_systm.h> 29#include <netinet/ip.h> 30#include <sys/un.h> 31#include <net/if_tun.h> 32 33#include <errno.h> 34#include <fcntl.h> 35#include <paths.h> 36#include <signal.h> 37#include <stdio.h> 38#include <string.h> 39#include <sys/time.h> 40#include <termios.h> 41#include <unistd.h> 42 43#include "mbuf.h" 44#include "log.h" 45#include "defs.h" 46#include "id.h" 47#include "timer.h" 48#include "fsm.h" 49#include "lqr.h" 50#include "hdlc.h" 51#include "lcp.h" 52#include "ccp.h" 53#include "iplist.h" 54#include "throughput.h" 55#include "slcompress.h" 56#include "ipcp.h" 57#include "filter.h" 58#include "descriptor.h" 59#include "link.h" 60#include "mp.h" 61#include "bundle.h" 62#include "loadalias.h" 63#include "auth.h" 64#include "systems.h" 65#include "ip.h" 66#include "sig.h" 67#include "main.h" 68#include "tun.h" 69#include "server.h" 70#include "prompt.h" 71#include "chat.h" 72#include "chap.h" 73#include "datalink.h" 74 75#ifndef O_NONBLOCK 76#ifdef O_NDELAY 77#define O_NONBLOCK O_NDELAY 78#endif 79#endif 80 81static void DoLoop(struct bundle *); 82static void TerminalStop(int); 83static const char *ex_desc(int); 84 85static struct bundle *SignalBundle; 86static struct prompt *SignalPrompt; 87 88void 89Cleanup(int excode) 90{ 91 SignalBundle->CleaningUp = 1; 92 if (bundle_Phase(SignalBundle) != PHASE_DEAD) 93 bundle_Close(SignalBundle, NULL, 1); 94} 95 96void 97AbortProgram(int excode) 98{ 99 server_Close(SignalBundle); 100 log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode)); 101 bundle_Close(SignalBundle, NULL, 1); 102 bundle_Destroy(SignalBundle); 103 log_Close(); 104 exit(excode); 105} 106 107static void 108CloseConnection(int signo) 109{ 110 /* NOTE, these are manual, we've done a setsid() */ 111 sig_signal(SIGINT, SIG_IGN); 112 log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo); 113 bundle_Down(SignalBundle); 114 sig_signal(SIGINT, CloseConnection); 115} 116 117static void 118CloseSession(int signo) 119{ 120 log_Printf(LogPHASE, "Signal %d, terminate.\n", signo); 121 Cleanup(EX_TERM); 122} 123 124static pid_t BGPid = 0; 125 126static void 127KillChild(int signo) 128{ 129 log_Printf(LogPHASE, "Parent: Signal %d\n", signo); 130 kill(BGPid, SIGINT); 131} 132 133static void 134TerminalCont(int signo) 135{ 136 signal(SIGCONT, SIG_DFL); 137 prompt_Continue(SignalPrompt); 138} 139 140static void 141TerminalStop(int signo) 142{ 143 prompt_Suspend(SignalPrompt); 144 signal(SIGCONT, TerminalCont); 145 raise(SIGSTOP); 146} 147 148static void 149BringDownServer(int signo) 150{ 151 /* Drops all child prompts too ! */ 152 server_Close(SignalBundle); 153} 154 155static const char * 156ex_desc(int ex) 157{ 158 static char num[12]; 159 static const char *desc[] = { 160 "normal", "start", "sock", "modem", "dial", "dead", "done", 161 "reboot", "errdead", "hangup", "term", "nodial", "nologin" 162 }; 163 164 if (ex >= 0 && ex < sizeof desc / sizeof *desc) 165 return desc[ex]; 166 snprintf(num, sizeof num, "%d", ex); 167 return num; 168} 169 170static void 171Usage(void) 172{ 173 fprintf(stderr, 174 "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]" 175#ifndef NOALIAS 176 " [ -alias ]" 177#endif 178 " [system]\n"); 179 exit(EX_START); 180} 181 182static char * 183ProcessArgs(int argc, char **argv, int *mode) 184{ 185 int optc, labelrequired; 186 char *cp; 187 188 optc = labelrequired = 0; 189 *mode = PHYS_MANUAL; 190 while (argc > 0 && **argv == '-') { 191 cp = *argv + 1; 192 if (strcmp(cp, "auto") == 0) { 193 *mode = PHYS_DEMAND; 194 labelrequired = 1; 195 } else if (strcmp(cp, "background") == 0) { 196 *mode = PHYS_1OFF; 197 labelrequired = 1; 198 } else if (strcmp(cp, "direct") == 0) 199 *mode = PHYS_DIRECT; 200 else if (strcmp(cp, "dedicated") == 0) 201 *mode = PHYS_DEDICATED; 202 else if (strcmp(cp, "ddial") == 0) { 203 *mode = PHYS_PERM; 204 labelrequired = 1; 205 } else if (strcmp(cp, "alias") == 0) { 206#ifndef NOALIAS 207 if (alias_Load() != 0) 208#endif 209 log_Printf(LogWARN, "Cannot load alias library\n"); 210 optc--; /* this option isn't exclusive */ 211 } else 212 Usage(); 213 optc++; 214 argv++; 215 argc--; 216 } 217 if (argc > 1) { 218 fprintf(stderr, "You may specify only one system label.\n"); 219 exit(EX_START); 220 } 221 222 if (optc > 1) { 223 fprintf(stderr, "You may specify only one mode.\n"); 224 exit(EX_START); 225 } 226 227 if (labelrequired && argc != 1) { 228 fprintf(stderr, "Destination system must be specified in" 229 " auto, background or ddial mode.\n"); 230 exit(EX_START); 231 } 232 233 return argc == 1 ? *argv : NULL; /* Don't SetLabel yet ! */ 234} 235 236int 237main(int argc, char **argv) 238{ 239 char *name, *label; 240 int nfds, mode; 241 struct bundle *bundle; 242 struct prompt *prompt; 243 244 nfds = getdtablesize(); 245 if (nfds >= FD_SETSIZE) 246 /* 247 * If we've got loads of file descriptors, make sure they're all 248 * closed. If they aren't, we may end up with a seg fault when our 249 * `fd_set's get too big when select()ing ! 250 */ 251 while (--nfds > 2) 252 close(nfds); 253 254 name = strrchr(argv[0], '/'); 255 log_Open(name ? name + 1 : argv[0]); 256 257 argc--; 258 argv++; 259 label = ProcessArgs(argc, argv, &mode); 260 261#ifdef __FreeBSD__ 262 /* 263 * A FreeBSD hack to dodge a bug in the tty driver that drops output 264 * occasionally.... I must find the real reason some time. To display 265 * the dodgy behaviour, comment out this bit, make yourself a large 266 * routing table and then run ppp in interactive mode. The `show route' 267 * command will drop chunks of data !!! 268 */ 269 if (mode == PHYS_MANUAL) { 270 close(STDIN_FILENO); 271 if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) { 272 fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY); 273 return 2; 274 } 275 } 276#endif 277 278 /* Allow output for the moment (except in direct mode) */ 279 if (mode == PHYS_DIRECT) 280 prompt = NULL; 281 else { 282 const char *m; 283 284 SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD); 285 if (mode == PHYS_PERM) 286 m = "direct dial"; 287 else if (mode & PHYS_1OFF) 288 m = "background"; 289 else if (mode & PHYS_DEMAND) 290 m = "auto"; 291 else if (mode & PHYS_DEDICATED) 292 m = "dedicated"; 293 else if (mode & PHYS_MANUAL) 294 m = "interactive"; 295 else 296 m = NULL; 297 298 if (m) 299 prompt_Printf(prompt, "Working in %s mode\n", m); 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", 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 (!system_IsValid(label, prompt, mode)) { 319 fprintf(stderr, "You may not use ppp in this mode with this label\n"); 320 if (mode == PHYS_DIRECT) { 321 const char *l; 322 l = label ? label : "default"; 323 log_Printf(LogWARN, "Label %s rejected -direct connection\n", l); 324 } 325 log_Close(); 326 return 1; 327 } 328 329 if ((bundle = bundle_Create(TUN_PREFIX, mode)) == NULL) { 330 log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno)); 331 return EX_START; 332 } 333 if (prompt) { 334 prompt->bundle = bundle; /* couldn't do it earlier */ 335 prompt_Printf(prompt, "Using interface: %s\n", bundle->ifp.Name); 336 } 337 SignalBundle = bundle; 338 339 if (system_Select(bundle, "default", CONFFILE, prompt) < 0) 340 prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); 341 342 sig_signal(SIGHUP, CloseSession); 343 sig_signal(SIGTERM, CloseSession); 344 sig_signal(SIGINT, CloseConnection); 345 sig_signal(SIGQUIT, CloseSession); 346 sig_signal(SIGALRM, SIG_IGN); 347 signal(SIGPIPE, SIG_IGN); 348 349 if (mode == PHYS_MANUAL) 350 sig_signal(SIGTSTP, TerminalStop); 351 352 sig_signal(SIGUSR2, BringDownServer); 353 354 if (label) { 355 /* 356 * Set label both before and after system_Select ! 357 * This way, "set enddisc label" works during system_Select, and we 358 * also end up with the correct label if we have embedded load 359 * commands. 360 */ 361 bundle_SetLabel(bundle, label); 362 if (system_Select(bundle, label, CONFFILE, prompt) < 0) { 363 prompt_Printf(prompt, "Destination system (%s) not found.\n", label); 364 AbortProgram(EX_START); 365 } 366 bundle_SetLabel(bundle, label); 367 if (mode == PHYS_DEMAND && 368 bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) { 369 prompt_Printf(prompt, "You must \"set ifaddr\" with a peer address " 370 "in label %s for auto mode.\n", label); 371 AbortProgram(EX_START); 372 } 373 } 374 375 if (mode != PHYS_MANUAL) { 376 if (mode != PHYS_DIRECT) { 377 int bgpipe[2]; 378 pid_t bgpid; 379 380 if (mode == PHYS_1OFF && 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_1OFF) { 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_1OFF) { 419 close(bgpipe[0]); 420 bundle->notify.fd = bgpipe[1]; 421 } 422 423 /* -auto, -dedicated, -ddial & -background */ 424 prompt_Destroy(prompt, 0); 425 close(STDOUT_FILENO); 426 close(STDERR_FILENO); 427 close(STDIN_FILENO); 428 setsid(); 429 } else { 430 /* -direct: STDIN_FILENO gets used by modem_Open */ 431 prompt_TtyInit(NULL); 432 close(STDOUT_FILENO); 433 close(STDERR_FILENO); 434 } 435 } else { 436 /* Interactive mode */ 437 close(STDERR_FILENO); 438 prompt_TtyInit(prompt); 439 prompt_TtyCommandMode(prompt); 440 prompt_Required(prompt); 441 } 442 443 log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode)); 444 DoLoop(bundle); 445 AbortProgram(EX_NORMAL); 446 447 return EX_NORMAL; 448} 449 450static void 451DoLoop(struct bundle *bundle) 452{ 453 fd_set rfds, wfds, efds; 454 int i, nfds; 455 456 do { 457 nfds = 0; 458 FD_ZERO(&rfds); 459 FD_ZERO(&wfds); 460 FD_ZERO(&efds); 461 462 /* All our datalinks, the tun device and the MP socket */ 463 descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds); 464 465 /* All our prompts and the diagnostic socket */ 466 descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds); 467 468 if (bundle_IsDead(bundle)) 469 /* Don't select - we'll be here forever */ 470 break; 471 472 i = select(nfds, &rfds, &wfds, &efds, NULL); 473 474 if (i < 0 && errno != EINTR) { 475 log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno)); 476 if (log_IsKept(LogTIMER)) { 477 struct timeval t; 478 479 for (i = 0; i <= nfds; i++) { 480 if (FD_ISSET(i, &rfds)) { 481 log_Printf(LogTIMER, "Read set contains %d\n", i); 482 FD_CLR(i, &rfds); 483 t.tv_sec = t.tv_usec = 0; 484 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 485 log_Printf(LogTIMER, "The culprit !\n"); 486 break; 487 } 488 } 489 if (FD_ISSET(i, &wfds)) { 490 log_Printf(LogTIMER, "Write set contains %d\n", i); 491 FD_CLR(i, &wfds); 492 t.tv_sec = t.tv_usec = 0; 493 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 494 log_Printf(LogTIMER, "The culprit !\n"); 495 break; 496 } 497 } 498 if (FD_ISSET(i, &efds)) { 499 log_Printf(LogTIMER, "Error set contains %d\n", i); 500 FD_CLR(i, &efds); 501 t.tv_sec = t.tv_usec = 0; 502 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 503 log_Printf(LogTIMER, "The culprit !\n"); 504 break; 505 } 506 } 507 } 508 } 509 break; 510 } 511 512 sig_Handle(); 513 514 if (i <= 0) 515 continue; 516 517 for (i = 0; i <= nfds; i++) 518 if (FD_ISSET(i, &efds)) { 519 log_Printf(LogALERT, "Exception detected on descriptor %d\n", i); 520 break; 521 } 522 523 if (i <= nfds) 524 break; 525 526 if (descriptor_IsSet(&server.desc, &rfds)) 527 descriptor_Read(&server.desc, bundle, &rfds); 528 529 if (descriptor_IsSet(&bundle->desc, &wfds)) 530 descriptor_Write(&bundle->desc, bundle, &wfds); 531 532 if (descriptor_IsSet(&bundle->desc, &rfds)) 533 descriptor_Read(&bundle->desc, bundle, &rfds); 534 535 } while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle)); 536 537 log_Printf(LogDEBUG, "DoLoop done.\n"); 538} 539