2001-06-13 21:52:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
|
|
|
|
* Internet Initiative Japan, Inc (IIJ)
|
|
|
|
* All rights reserved.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1998-04-07 00:54:26 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
#include <sys/param.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1998-04-10 13:19:23 +00:00
|
|
|
#include <sys/un.h>
|
2000-03-14 01:46:44 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/route.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <fcntl.h>
|
1995-10-08 14:57:32 +00:00
|
|
|
#include <paths.h>
|
1997-02-25 14:05:17 +00:00
|
|
|
#include <signal.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
1999-10-19 15:21:09 +00:00
|
|
|
#include <stdlib.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
1999-08-09 22:54:51 +00:00
|
|
|
#include <sys/stat.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1999-08-19 18:15:52 +00:00
|
|
|
#ifndef NONAT
|
2000-03-14 01:47:02 +00:00
|
|
|
#ifdef LOCALNAT
|
1999-04-26 08:54:34 +00:00
|
|
|
#include "alias.h"
|
2000-03-14 01:47:02 +00:00
|
|
|
#else
|
|
|
|
#include <alias.h>
|
1998-09-17 00:45:27 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2000-03-14 01:47:02 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "layer.h"
|
1998-06-24 19:33:36 +00:00
|
|
|
#include "probe.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#include "id.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "lcp.h"
|
1997-12-03 10:23:54 +00:00
|
|
|
#include "ccp.h"
|
1998-01-29 23:11:44 +00:00
|
|
|
#include "iplist.h"
|
|
|
|
#include "throughput.h"
|
1998-03-16 22:54:35 +00:00
|
|
|
#include "slcompress.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "ipcp.h"
|
1998-03-16 22:52:54 +00:00
|
|
|
#include "filter.h"
|
1998-03-20 19:48:28 +00:00
|
|
|
#include "descriptor.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "link.h"
|
|
|
|
#include "mp.h"
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "bundle.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
#include "auth.h"
|
1996-01-11 17:48:59 +00:00
|
|
|
#include "systems.h"
|
1997-03-13 12:45:35 +00:00
|
|
|
#include "sig.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "main.h"
|
1998-02-09 19:24:03 +00:00
|
|
|
#include "server.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "prompt.h"
|
1998-02-13 05:10:26 +00:00
|
|
|
#include "chat.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "chap.h"
|
1998-08-07 18:42:51 +00:00
|
|
|
#include "cbcp.h"
|
1998-02-16 00:01:12 +00:00
|
|
|
#include "datalink.h"
|
1998-10-22 02:32:50 +00:00
|
|
|
#include "iface.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
|
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#ifdef O_NDELAY
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#endif
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-05-27 22:43:37 +00:00
|
|
|
static void DoLoop(struct bundle *);
|
1997-10-26 01:04:02 +00:00
|
|
|
static void TerminalStop(int);
|
|
|
|
|
1998-02-06 02:24:29 +00:00
|
|
|
static struct bundle *SignalBundle;
|
1998-04-03 19:26:02 +00:00
|
|
|
static struct prompt *SignalPrompt;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
void
|
1998-01-29 00:44:16 +00:00
|
|
|
Cleanup(int excode)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-03-16 22:53:15 +00:00
|
|
|
SignalBundle->CleaningUp = 1;
|
1998-08-02 06:56:40 +00:00
|
|
|
bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
|
1995-09-02 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-02-08 11:07:32 +00:00
|
|
|
AbortProgram(int excode)
|
1995-09-02 17:20:54 +00:00
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
server_Close(SignalBundle);
|
|
|
|
log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
|
1998-06-15 19:05:27 +00:00
|
|
|
bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
|
1998-02-08 19:29:45 +00:00
|
|
|
bundle_Destroy(SignalBundle);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Close();
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(excode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
CloseConnection(int signo)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-06-23 23:10:13 +00:00
|
|
|
/* NOTE, these are manual, we've done a setsid() */
|
1998-05-01 19:26:12 +00:00
|
|
|
sig_signal(SIGINT, SIG_IGN);
|
|
|
|
log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
|
1998-06-16 19:40:28 +00:00
|
|
|
bundle_Down(SignalBundle, CLOSE_STAYDOWN);
|
1998-05-01 19:26:12 +00:00
|
|
|
sig_signal(SIGINT, CloseConnection);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
CloseSession(int signo)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
|
1997-08-25 00:29:32 +00:00
|
|
|
Cleanup(EX_TERM);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-03-25 18:38:24 +00:00
|
|
|
static pid_t BGPid = 0;
|
|
|
|
|
1995-09-02 17:20:54 +00:00
|
|
|
static void
|
1998-03-25 18:38:24 +00:00
|
|
|
KillChild(int signo)
|
1995-09-02 17:20:54 +00:00
|
|
|
{
|
1999-05-13 16:34:57 +00:00
|
|
|
signal(signo, SIG_IGN);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
|
1998-03-25 18:38:24 +00:00
|
|
|
kill(BGPid, SIGINT);
|
1995-09-02 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-01-29 00:44:16 +00:00
|
|
|
TerminalCont(int signo)
|
1995-09-02 17:20:54 +00:00
|
|
|
{
|
1998-04-05 18:25:34 +00:00
|
|
|
signal(SIGCONT, SIG_DFL);
|
|
|
|
prompt_Continue(SignalPrompt);
|
1995-09-02 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
static void
|
1998-01-29 00:44:16 +00:00
|
|
|
TerminalStop(int signo)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
1998-04-05 18:25:34 +00:00
|
|
|
prompt_Suspend(SignalPrompt);
|
|
|
|
signal(SIGCONT, TerminalCont);
|
|
|
|
raise(SIGSTOP);
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
1997-11-09 22:07:29 +00:00
|
|
|
static void
|
|
|
|
BringDownServer(int signo)
|
|
|
|
{
|
1998-04-03 19:26:02 +00:00
|
|
|
/* Drops all child prompts too ! */
|
2001-01-26 01:41:34 +00:00
|
|
|
if (server_Close(SignalBundle))
|
|
|
|
log_Printf(LogPHASE, "Closed server socket\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
RestartServer(int signo)
|
|
|
|
{
|
|
|
|
/* Drops all child prompts and re-opens the socket */
|
|
|
|
server_Reopen(SignalBundle);
|
1997-11-09 22:07:29 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
Usage(void)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2000-10-30 00:15:29 +00:00
|
|
|
fprintf(stderr, "Usage: ppp [-auto | -foreground | -background | -direct |"
|
|
|
|
" -dedicated | -ddial | -interactive]"
|
1997-11-22 03:37:54 +00:00
|
|
|
#ifndef NOALIAS
|
1999-08-19 18:15:52 +00:00
|
|
|
" [-nat]"
|
1997-11-22 03:37:54 +00:00
|
|
|
#endif
|
1999-10-19 15:21:09 +00:00
|
|
|
" [-quiet] [-unit N] [system ...]\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
struct switches {
|
|
|
|
unsigned nat : 1;
|
|
|
|
unsigned fg : 1;
|
|
|
|
unsigned quiet : 1;
|
|
|
|
int mode;
|
|
|
|
int unit;
|
|
|
|
};
|
|
|
|
|
1998-10-31 17:38:47 +00:00
|
|
|
static int
|
1999-10-19 15:21:09 +00:00
|
|
|
ProcessArgs(int argc, char **argv, struct switches *sw)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-10-31 17:38:47 +00:00
|
|
|
int optc, newmode, arg;
|
1995-01-31 06:29:58 +00:00
|
|
|
char *cp;
|
|
|
|
|
1998-10-31 17:38:47 +00:00
|
|
|
optc = 0;
|
1999-10-19 15:21:09 +00:00
|
|
|
memset(sw, '\0', sizeof *sw);
|
|
|
|
sw->mode = PHYS_INTERACTIVE;
|
|
|
|
sw->unit = -1;
|
|
|
|
|
1998-10-31 17:38:47 +00:00
|
|
|
for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
|
|
|
|
cp = argv[arg] + 1;
|
1998-05-29 18:32:11 +00:00
|
|
|
newmode = Nam2mode(cp);
|
|
|
|
switch (newmode) {
|
|
|
|
case PHYS_NONE:
|
1999-10-19 15:21:09 +00:00
|
|
|
if (strcmp(cp, "nat") == 0) {
|
1999-08-19 18:15:52 +00:00
|
|
|
#ifdef NONAT
|
1999-10-19 15:21:09 +00:00
|
|
|
log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
|
1998-06-27 14:17:28 +00:00
|
|
|
#else
|
1999-10-19 15:21:09 +00:00
|
|
|
sw->nat = 1;
|
1997-11-22 03:37:54 +00:00
|
|
|
#endif
|
1998-05-29 18:32:11 +00:00
|
|
|
optc--; /* this option isn't exclusive */
|
1999-10-19 15:21:09 +00:00
|
|
|
} else if (strcmp(cp, "alias") == 0) {
|
|
|
|
#ifdef NONAT
|
|
|
|
log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
|
|
|
|
fprintf(stderr, "%s ignored: NAT is compiled out\n", argv[arg]);
|
|
|
|
#else
|
1999-11-29 18:53:43 +00:00
|
|
|
log_Printf(LogWARN, "%s is deprecated\n", argv[arg]);
|
|
|
|
fprintf(stderr, "%s is deprecated\n", argv[arg]);
|
1999-10-19 15:21:09 +00:00
|
|
|
sw->nat = 1;
|
|
|
|
#endif
|
|
|
|
optc--; /* this option isn't exclusive */
|
|
|
|
} else if (strncmp(cp, "unit", 4) == 0) {
|
1999-11-09 21:59:09 +00:00
|
|
|
optc--; /* this option isn't exclusive */
|
1999-10-19 15:21:09 +00:00
|
|
|
if (cp[4] == '\0') {
|
1999-11-09 21:59:09 +00:00
|
|
|
optc--; /* nor is the argument */
|
1999-10-19 15:21:09 +00:00
|
|
|
if (++arg == argc) {
|
|
|
|
fprintf(stderr, "-unit: Expected unit number\n");
|
|
|
|
Usage();
|
|
|
|
} else
|
|
|
|
sw->unit = atoi(argv[arg]);
|
|
|
|
} else
|
|
|
|
sw->unit = atoi(cp + 4);
|
1999-08-19 18:15:52 +00:00
|
|
|
} else if (strcmp(cp, "quiet") == 0) {
|
1999-10-19 15:21:09 +00:00
|
|
|
sw->quiet = 1;
|
1999-08-19 18:15:52 +00:00
|
|
|
optc--; /* this option isn't exclusive */
|
1998-05-29 18:32:11 +00:00
|
|
|
} else
|
|
|
|
Usage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PHYS_ALL:
|
|
|
|
Usage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-10-19 15:21:09 +00:00
|
|
|
sw->mode = newmode;
|
1999-11-28 15:50:08 +00:00
|
|
|
if (newmode == PHYS_FOREGROUND)
|
|
|
|
sw->fg = 1;
|
1998-05-29 18:32:11 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (optc > 1) {
|
1998-04-07 23:46:09 +00:00
|
|
|
fprintf(stderr, "You may specify only one mode.\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
if (sw->mode == PHYS_AUTO && arg == argc) {
|
1998-10-31 17:38:47 +00:00
|
|
|
fprintf(stderr, "A system must be specified in auto mode.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
1997-11-17 01:13:41 +00:00
|
|
|
|
1998-10-31 17:38:47 +00:00
|
|
|
return arg; /* Don't SetLabel yet ! */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
CheckLabel(const char *label, struct prompt *prompt, int mode)
|
|
|
|
{
|
|
|
|
const char *err;
|
|
|
|
|
|
|
|
if ((err = system_IsValid(label, prompt, mode)) != NULL) {
|
|
|
|
fprintf(stderr, "%s: %s\n", label, err);
|
|
|
|
if (mode == PHYS_DIRECT)
|
|
|
|
log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
|
|
|
|
label, err);
|
|
|
|
log_Close();
|
|
|
|
exit(1);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-10-31 17:38:47 +00:00
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
int
|
1997-08-25 00:29:32 +00:00
|
|
|
main(int argc, char **argv)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-10-31 17:38:47 +00:00
|
|
|
char *name;
|
1999-01-25 10:19:03 +00:00
|
|
|
const char *lastlabel;
|
2000-10-09 21:18:23 +00:00
|
|
|
int label, arg;
|
1998-02-02 19:32:16 +00:00
|
|
|
struct bundle *bundle;
|
1998-04-03 19:26:02 +00:00
|
|
|
struct prompt *prompt;
|
1999-10-19 15:21:09 +00:00
|
|
|
struct switches sw;
|
1997-12-17 21:21:47 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
name = strrchr(argv[0], '/');
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Open(name ? name + 1 : argv[0]);
|
1997-12-30 23:22:31 +00:00
|
|
|
|
1999-08-19 18:15:52 +00:00
|
|
|
#ifndef NONAT
|
1998-08-09 09:13:54 +00:00
|
|
|
PacketAliasInit();
|
|
|
|
#endif
|
1999-10-19 15:21:09 +00:00
|
|
|
label = ProcessArgs(argc, argv, &sw);
|
1998-02-10 03:23:50 +00:00
|
|
|
|
|
|
|
/*
|
1999-03-07 01:02:39 +00:00
|
|
|
* A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops
|
|
|
|
* output occasionally.... I must find the real reason some time. To
|
|
|
|
* display the dodgy behaviour, comment out this bit, make yourself a large
|
1998-02-10 03:23:50 +00:00
|
|
|
* routing table and then run ppp in interactive mode. The `show route'
|
|
|
|
* command will drop chunks of data !!!
|
|
|
|
*/
|
1999-10-19 15:21:09 +00:00
|
|
|
if (sw.mode == PHYS_INTERACTIVE) {
|
1998-02-10 03:23:50 +00:00
|
|
|
close(STDIN_FILENO);
|
|
|
|
if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
|
|
|
|
fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
/* Allow output for the moment (except in direct mode) */
|
1999-10-19 15:21:09 +00:00
|
|
|
if (sw.mode == PHYS_DIRECT)
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt = NULL;
|
1999-02-02 09:35:30 +00:00
|
|
|
else
|
1998-04-03 19:26:02 +00:00
|
|
|
SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
|
1997-11-11 22:58:14 +00:00
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
ID0init();
|
1997-11-13 15:35:06 +00:00
|
|
|
if (ID0realuid() != 0) {
|
|
|
|
char conf[200], *ptr;
|
|
|
|
|
2001-03-23 11:43:22 +00:00
|
|
|
snprintf(conf, sizeof conf, "%s/%s", PPP_CONFDIR, CONFFILE);
|
1997-11-13 15:35:06 +00:00
|
|
|
do {
|
1999-08-09 22:54:51 +00:00
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) {
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n",
|
|
|
|
conf);
|
1997-11-13 15:35:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr = conf + strlen(conf)-2;
|
|
|
|
while (ptr > conf && *ptr != '/')
|
|
|
|
*ptr-- = '\0';
|
|
|
|
} while (ptr >= conf);
|
|
|
|
}
|
|
|
|
|
1998-10-31 17:38:47 +00:00
|
|
|
if (label < argc)
|
|
|
|
for (arg = label; arg < argc; arg++)
|
1999-10-19 15:21:09 +00:00
|
|
|
CheckLabel(argv[arg], prompt, sw.mode);
|
1998-10-31 17:38:47 +00:00
|
|
|
else
|
1999-10-19 15:21:09 +00:00
|
|
|
CheckLabel("default", prompt, sw.mode);
|
1997-11-11 22:58:14 +00:00
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
if (!sw.quiet)
|
|
|
|
prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode));
|
1999-02-02 09:35:30 +00:00
|
|
|
|
1999-11-17 21:12:35 +00:00
|
|
|
if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit)) == NULL)
|
1997-06-25 19:30:05 +00:00
|
|
|
return EX_START;
|
1999-01-25 10:19:03 +00:00
|
|
|
|
|
|
|
/* NOTE: We may now have changed argv[1] via a ``set proctitle'' */
|
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
if (prompt) {
|
|
|
|
prompt->bundle = bundle; /* couldn't do it earlier */
|
1999-10-19 15:21:09 +00:00
|
|
|
if (!sw.quiet)
|
1999-08-19 18:15:52 +00:00
|
|
|
prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
|
1998-05-23 22:24:50 +00:00
|
|
|
}
|
1998-02-06 02:24:29 +00:00
|
|
|
SignalBundle = bundle;
|
1999-10-19 15:21:09 +00:00
|
|
|
bundle->NatEnabled = sw.nat;
|
|
|
|
if (sw.nat)
|
1998-10-22 02:32:50 +00:00
|
|
|
bundle->cfg.opt |= OPT_IFACEALIAS;
|
1997-11-11 22:58:14 +00:00
|
|
|
|
1998-06-15 19:05:51 +00:00
|
|
|
if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0)
|
1998-04-10 13:19:23 +00:00
|
|
|
prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
sig_signal(SIGHUP, CloseSession);
|
|
|
|
sig_signal(SIGTERM, CloseSession);
|
|
|
|
sig_signal(SIGINT, CloseConnection);
|
|
|
|
sig_signal(SIGQUIT, CloseSession);
|
|
|
|
sig_signal(SIGALRM, SIG_IGN);
|
1997-04-09 17:35:54 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
1998-04-10 13:19:23 +00:00
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
if (sw.mode == PHYS_INTERACTIVE)
|
1998-05-01 19:26:12 +00:00
|
|
|
sig_signal(SIGTSTP, TerminalStop);
|
1998-04-05 18:25:34 +00:00
|
|
|
|
2001-01-26 01:41:34 +00:00
|
|
|
sig_signal(SIGUSR1, RestartServer);
|
1998-05-01 19:26:12 +00:00
|
|
|
sig_signal(SIGUSR2, BringDownServer);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-11-17 21:12:35 +00:00
|
|
|
lastlabel = argv[argc - 1];
|
1998-10-31 17:38:47 +00:00
|
|
|
for (arg = label; arg < argc; arg++) {
|
|
|
|
/* In case we use LABEL or ``set enddisc label'' */
|
1999-01-25 10:19:03 +00:00
|
|
|
bundle_SetLabel(bundle, lastlabel);
|
1999-11-17 21:12:35 +00:00
|
|
|
system_Select(bundle, argv[arg], CONFFILE, prompt, NULL);
|
1998-10-31 17:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (label < argc)
|
|
|
|
/* In case the last label did a ``load'' */
|
1999-01-25 10:19:03 +00:00
|
|
|
bundle_SetLabel(bundle, lastlabel);
|
1998-10-31 17:38:47 +00:00
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
if (sw.mode == PHYS_AUTO &&
|
1998-10-31 17:38:47 +00:00
|
|
|
bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
|
|
|
|
prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
|
|
|
|
"in auto mode.\n");
|
|
|
|
AbortProgram(EX_START);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
if (sw.mode != PHYS_INTERACTIVE) {
|
|
|
|
if (sw.mode != PHYS_DIRECT) {
|
|
|
|
if (!sw.fg) {
|
1999-08-19 18:15:52 +00:00
|
|
|
int bgpipe[2];
|
|
|
|
pid_t bgpid;
|
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) {
|
1999-08-19 18:15:52 +00:00
|
|
|
log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
|
|
|
|
AbortProgram(EX_SOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
bgpid = fork();
|
|
|
|
if (bgpid == -1) {
|
|
|
|
log_Printf(LogERROR, "fork: %s\n", strerror(errno));
|
|
|
|
AbortProgram(EX_SOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bgpid) {
|
|
|
|
char c = EX_NORMAL;
|
2000-04-07 23:46:14 +00:00
|
|
|
int ret;
|
1999-08-19 18:15:52 +00:00
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
if (sw.mode == PHYS_BACKGROUND) {
|
1999-08-19 18:15:52 +00:00
|
|
|
close(bgpipe[1]);
|
|
|
|
BGPid = bgpid;
|
|
|
|
/* If we get a signal, kill the child */
|
|
|
|
signal(SIGHUP, KillChild);
|
|
|
|
signal(SIGTERM, KillChild);
|
|
|
|
signal(SIGINT, KillChild);
|
|
|
|
signal(SIGQUIT, KillChild);
|
|
|
|
|
|
|
|
/* Wait for our child to close its pipe before we exit */
|
2000-04-07 23:46:14 +00:00
|
|
|
while ((ret = read(bgpipe[0], &c, 1)) == 1) {
|
|
|
|
switch (c) {
|
|
|
|
case EX_NORMAL:
|
2001-04-03 08:20:20 +00:00
|
|
|
if (!sw.quiet) {
|
|
|
|
prompt_Printf(prompt, "PPP enabled\n");
|
|
|
|
log_Printf(LogPHASE, "Parent: PPP enabled\n");
|
|
|
|
}
|
2000-04-09 18:27:08 +00:00
|
|
|
break;
|
2000-04-07 23:46:14 +00:00
|
|
|
case EX_REDIAL:
|
|
|
|
if (!sw.quiet)
|
|
|
|
prompt_Printf(prompt, "Attempting redial\n");
|
|
|
|
continue;
|
|
|
|
case EX_RECONNECT:
|
|
|
|
if (!sw.quiet)
|
|
|
|
prompt_Printf(prompt, "Attempting reconnect\n");
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
prompt_Printf(prompt, "Child failed (%s)\n",
|
|
|
|
ex_desc((int)c));
|
|
|
|
log_Printf(LogPHASE, "Parent: Child failed (%s)\n",
|
|
|
|
ex_desc((int) c));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret != 1) {
|
1999-08-19 18:15:52 +00:00
|
|
|
prompt_Printf(prompt, "Child exit, no status.\n");
|
|
|
|
log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
|
|
|
|
}
|
|
|
|
close(bgpipe[0]);
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1999-08-19 18:15:52 +00:00
|
|
|
return c;
|
1999-10-19 15:21:09 +00:00
|
|
|
} else if (sw.mode == PHYS_BACKGROUND) {
|
1998-03-25 18:38:59 +00:00
|
|
|
close(bgpipe[0]);
|
1999-08-19 18:15:52 +00:00
|
|
|
bundle->notify.fd = bgpipe[1];
|
|
|
|
}
|
1997-06-11 03:57:51 +00:00
|
|
|
|
2000-01-21 00:33:27 +00:00
|
|
|
bundle_ChangedPID(bundle);
|
1999-08-19 18:15:52 +00:00
|
|
|
bundle_LockTun(bundle); /* we have a new pid */
|
|
|
|
}
|
1998-06-06 20:50:57 +00:00
|
|
|
|
1999-08-19 18:15:52 +00:00
|
|
|
/* -auto, -dedicated, -ddial, -foreground & -background */
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Destroy(prompt, 0);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
1997-12-30 23:22:31 +00:00
|
|
|
close(STDIN_FILENO);
|
1999-10-19 15:21:09 +00:00
|
|
|
if (!sw.fg)
|
1999-08-19 18:15:52 +00:00
|
|
|
setsid();
|
1998-04-10 13:19:23 +00:00
|
|
|
} else {
|
1999-08-19 18:15:52 +00:00
|
|
|
/* -direct - STDIN_FILENO gets used by physical_Open */
|
1998-04-10 13:19:23 +00:00
|
|
|
prompt_TtyInit(NULL);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
1997-06-16 13:24:59 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1999-08-19 18:15:52 +00:00
|
|
|
/* -interactive */
|
1997-12-30 23:22:31 +00:00
|
|
|
close(STDERR_FILENO);
|
1998-04-10 13:19:23 +00:00
|
|
|
prompt_TtyInit(prompt);
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_TtyCommandMode(prompt);
|
|
|
|
prompt_Required(prompt);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-09-22 00:46:56 +00:00
|
|
|
|
1999-10-19 15:21:09 +00:00
|
|
|
log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode));
|
1998-05-27 22:43:37 +00:00
|
|
|
DoLoop(bundle);
|
1998-02-08 11:07:32 +00:00
|
|
|
AbortProgram(EX_NORMAL);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-02-08 11:07:32 +00:00
|
|
|
return EX_NORMAL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-05-27 22:43:37 +00:00
|
|
|
DoLoop(struct bundle *bundle)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2000-10-09 21:18:23 +00:00
|
|
|
fd_set *rfds, *wfds, *efds;
|
1998-06-24 19:33:36 +00:00
|
|
|
int i, nfds, nothing_done;
|
|
|
|
struct probe probe;
|
|
|
|
|
|
|
|
probe_Init(&probe);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2000-10-09 21:18:23 +00:00
|
|
|
if ((rfds = mkfdset()) == NULL) {
|
|
|
|
log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((wfds = mkfdset()) == NULL) {
|
|
|
|
log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
|
|
|
|
free(rfds);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((efds = mkfdset()) == NULL) {
|
|
|
|
log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
|
|
|
|
free(rfds);
|
|
|
|
free(wfds);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-11-09 23:21:47 +00:00
|
|
|
for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) {
|
1997-03-10 06:21:02 +00:00
|
|
|
nfds = 0;
|
2000-10-09 21:18:23 +00:00
|
|
|
zerofdset(rfds);
|
|
|
|
zerofdset(wfds);
|
|
|
|
zerofdset(efds);
|
1995-03-11 15:18:55 +00:00
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
/* All our datalinks, the tun device and the MP socket */
|
2000-10-09 21:18:23 +00:00
|
|
|
descriptor_UpdateSet(&bundle->desc, rfds, wfds, efds, &nfds);
|
1998-05-23 22:24:50 +00:00
|
|
|
|
|
|
|
/* All our prompts and the diagnostic socket */
|
2000-10-09 21:18:23 +00:00
|
|
|
descriptor_UpdateSet(&server.desc, rfds, NULL, NULL, &nfds);
|
1997-08-25 00:29:32 +00:00
|
|
|
|
2000-03-22 03:01:48 +00:00
|
|
|
bundle_CleanDatalinks(bundle);
|
1998-04-03 19:21:56 +00:00
|
|
|
if (bundle_IsDead(bundle))
|
1998-02-23 00:38:44 +00:00
|
|
|
/* Don't select - we'll be here forever */
|
|
|
|
break;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1999-03-30 00:44:57 +00:00
|
|
|
/*
|
|
|
|
* It's possible that we've had a signal since we last checked. If
|
|
|
|
* we don't check again before calling select(), we may end up stuck
|
|
|
|
* after having missed the event.... sig_Handle() tries to be as
|
|
|
|
* quick as possible if nothing is likely to have happened.
|
|
|
|
* This is only really likely if we block in open(... O_NONBLOCK)
|
|
|
|
* which will happen with a misconfigured device.
|
|
|
|
*/
|
|
|
|
if (sig_Handle())
|
|
|
|
continue;
|
|
|
|
|
2000-10-09 21:18:23 +00:00
|
|
|
i = select(nfds, rfds, wfds, efds, NULL);
|
1997-01-29 01:27:58 +00:00
|
|
|
|
1998-05-25 02:22:38 +00:00
|
|
|
if (i < 0 && errno != EINTR) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
|
1998-05-10 22:20:20 +00:00
|
|
|
if (log_IsKept(LogTIMER)) {
|
|
|
|
struct timeval t;
|
|
|
|
|
|
|
|
for (i = 0; i <= nfds; i++) {
|
2000-10-09 21:18:23 +00:00
|
|
|
if (FD_ISSET(i, rfds)) {
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "Read set contains %d\n", i);
|
2000-10-09 21:18:23 +00:00
|
|
|
FD_CLR(i, rfds);
|
1998-05-10 22:20:20 +00:00
|
|
|
t.tv_sec = t.tv_usec = 0;
|
2000-10-09 21:18:23 +00:00
|
|
|
if (select(nfds, rfds, wfds, efds, &t) != -1) {
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "The culprit !\n");
|
|
|
|
break;
|
|
|
|
}
|
1997-12-17 21:21:56 +00:00
|
|
|
}
|
2000-10-09 21:18:23 +00:00
|
|
|
if (FD_ISSET(i, wfds)) {
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "Write set contains %d\n", i);
|
2000-10-09 21:18:23 +00:00
|
|
|
FD_CLR(i, wfds);
|
1998-05-10 22:20:20 +00:00
|
|
|
t.tv_sec = t.tv_usec = 0;
|
2000-10-09 21:18:23 +00:00
|
|
|
if (select(nfds, rfds, wfds, efds, &t) != -1) {
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "The culprit !\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-10-09 21:18:23 +00:00
|
|
|
if (FD_ISSET(i, efds)) {
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "Error set contains %d\n", i);
|
2000-10-09 21:18:23 +00:00
|
|
|
FD_CLR(i, efds);
|
1998-05-10 22:20:20 +00:00
|
|
|
t.tv_sec = t.tv_usec = 0;
|
2000-10-09 21:18:23 +00:00
|
|
|
if (select(nfds, rfds, wfds, efds, &t) != -1) {
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "The culprit !\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1999-02-06 02:54:47 +00:00
|
|
|
log_Printf(LogTIMER, "Select returns %d\n", i);
|
|
|
|
|
1998-05-25 02:22:38 +00:00
|
|
|
sig_Handle();
|
|
|
|
|
|
|
|
if (i <= 0)
|
|
|
|
continue;
|
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
for (i = 0; i <= nfds; i++)
|
2000-10-09 21:18:23 +00:00
|
|
|
if (FD_ISSET(i, efds)) {
|
2000-03-14 01:47:07 +00:00
|
|
|
log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
|
|
|
|
/* We deal gracefully with link descriptor exceptions */
|
1998-12-10 18:36:30 +00:00
|
|
|
if (!bundle_Exception(bundle, i)) {
|
|
|
|
log_Printf(LogERROR, "Exception cannot be handled !\n");
|
|
|
|
break;
|
|
|
|
}
|
1997-08-21 16:21:39 +00:00
|
|
|
}
|
|
|
|
|
1998-05-08 01:15:19 +00:00
|
|
|
if (i <= nfds)
|
|
|
|
break;
|
1997-12-28 02:46:26 +00:00
|
|
|
|
1998-06-24 19:33:36 +00:00
|
|
|
nothing_done = 1;
|
|
|
|
|
2000-10-09 21:18:23 +00:00
|
|
|
if (descriptor_IsSet(&server.desc, rfds)) {
|
|
|
|
descriptor_Read(&server.desc, bundle, rfds);
|
1998-06-24 19:33:36 +00:00
|
|
|
nothing_done = 0;
|
|
|
|
}
|
|
|
|
|
2000-10-09 21:18:23 +00:00
|
|
|
if (descriptor_IsSet(&bundle->desc, rfds)) {
|
|
|
|
descriptor_Read(&bundle->desc, bundle, rfds);
|
1998-06-24 19:33:36 +00:00
|
|
|
nothing_done = 0;
|
|
|
|
}
|
1998-02-09 19:21:11 +00:00
|
|
|
|
2000-10-09 21:18:23 +00:00
|
|
|
if (descriptor_IsSet(&bundle->desc, wfds))
|
|
|
|
if (!descriptor_Write(&bundle->desc, bundle, wfds) && nothing_done) {
|
1998-06-24 19:33:36 +00:00
|
|
|
/*
|
|
|
|
* This is disasterous. The OS has told us that something is
|
|
|
|
* writable, and all our write()s have failed. Rather than
|
|
|
|
* going back immediately to do our UpdateSet()s and select(),
|
|
|
|
* we sleep for a bit to avoid gobbling up all cpu time.
|
|
|
|
*/
|
|
|
|
struct timeval t;
|
1998-03-20 19:48:28 +00:00
|
|
|
|
1998-06-24 19:33:36 +00:00
|
|
|
t.tv_sec = 0;
|
|
|
|
t.tv_usec = 100000;
|
|
|
|
select(0, NULL, NULL, NULL, &t);
|
|
|
|
}
|
1999-11-09 23:21:47 +00:00
|
|
|
}
|
1998-04-10 13:19:23 +00:00
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "DoLoop done.\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|