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