1994-05-26 06:35:07 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1990, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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-09-15 20:06:50 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static char copyright[] =
|
|
|
|
"@(#) Copyright (c) 1990, 1991, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)startslip.c 8.1 (Berkeley) 6/5/93";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-09-15 20:06:50 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/ioctl.h>
|
1994-12-04 12:26:01 +00:00
|
|
|
#include <sys/types.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/socket.h>
|
1995-09-14 19:03:20 +00:00
|
|
|
#include <syslog.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_slvar.h>
|
1994-12-04 12:26:01 +00:00
|
|
|
#include <net/slip.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
1995-09-15 20:06:50 +00:00
|
|
|
#include <stdlib.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <signal.h>
|
1994-12-04 12:26:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1995-09-15 20:06:50 +00:00
|
|
|
#include <paths.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
#define DEFAULT_BAUD B9600
|
|
|
|
int speed = DEFAULT_BAUD;
|
|
|
|
#define FC_NONE 0 /* flow control: none */
|
1995-09-14 19:03:20 +00:00
|
|
|
#define FC_HW 1 /* flow control: hardware (RTS/CTS) */
|
1994-05-26 06:35:07 +00:00
|
|
|
int flowcontrol = FC_NONE;
|
1995-09-15 20:06:50 +00:00
|
|
|
int modem_control = 1; /* !CLOCAL+HUPCL iff we watch carrier. */
|
1994-05-26 06:35:07 +00:00
|
|
|
char *annex;
|
|
|
|
int hup;
|
1995-09-14 19:03:20 +00:00
|
|
|
int terminate;
|
1995-09-14 22:01:06 +00:00
|
|
|
int locked;
|
1994-12-04 12:26:01 +00:00
|
|
|
int logged_in = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
int wait_time = 60; /* then back off */
|
1995-09-14 19:03:20 +00:00
|
|
|
int script_timeout = 90; /* connect script default timeout */
|
1995-09-15 20:06:50 +00:00
|
|
|
int MAXTRIES = 6; /* w/60 sec and doubling, takes an hour */
|
|
|
|
#define PIDFILE "%sstartslip.%s.pid"
|
1995-09-14 19:03:20 +00:00
|
|
|
|
|
|
|
#define MAXDIALS 20
|
|
|
|
char *dials[MAXDIALS];
|
|
|
|
int diali, dialc;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1995-09-15 17:30:03 +00:00
|
|
|
int fd = -1;
|
1995-09-14 22:01:06 +00:00
|
|
|
FILE *pfd;
|
1995-09-15 20:06:50 +00:00
|
|
|
char *dvname, *devicename;
|
1995-09-14 22:01:06 +00:00
|
|
|
char pidfile[80];
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
int debug = 1;
|
|
|
|
#undef LOG_ERR
|
|
|
|
#undef LOG_INFO
|
|
|
|
#define syslog fprintf
|
|
|
|
#define LOG_ERR stderr
|
|
|
|
#define LOG_INFO stderr
|
|
|
|
#else
|
|
|
|
int debug = 0;
|
|
|
|
#endif
|
|
|
|
#define printd if (debug) printf
|
|
|
|
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
char *cp, **ap;
|
|
|
|
int ch, disc;
|
1995-09-14 19:03:20 +00:00
|
|
|
void sighup(), sigterm();
|
1995-09-14 22:01:06 +00:00
|
|
|
FILE *wfd = NULL;
|
1994-05-26 06:35:07 +00:00
|
|
|
char *dialerstring = 0, buf[BUFSIZ];
|
1994-12-04 12:26:01 +00:00
|
|
|
int unitnum;
|
|
|
|
char unitname[32];
|
1995-09-15 17:30:03 +00:00
|
|
|
char *username, *password;
|
1994-12-04 12:26:01 +00:00
|
|
|
char *upscript = NULL, *downscript = NULL;
|
1994-05-26 06:35:07 +00:00
|
|
|
int first = 1, tries = 0;
|
1995-09-15 20:06:50 +00:00
|
|
|
time_t fintimeout;
|
1995-09-14 19:03:20 +00:00
|
|
|
pid_t pid;
|
1994-05-26 06:35:07 +00:00
|
|
|
struct termios t;
|
|
|
|
|
1995-09-15 20:06:50 +00:00
|
|
|
while ((ch = getopt(argc, argv, "dhlWb:s:t:w:A:U:D:")) != EOF)
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
|
|
|
debug = 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
speed = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
1995-09-14 19:03:20 +00:00
|
|
|
if (diali >= MAXDIALS) {
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"max dial strings number (%d) exceeded\n", MAXDIALS);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dials[diali++] = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
script_timeout = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
wait_time = atoi(optarg);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
1995-09-15 20:06:50 +00:00
|
|
|
case 'W':
|
|
|
|
MAXTRIES = atoi(optarg);
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case 'A':
|
1994-12-04 12:26:01 +00:00
|
|
|
annex = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
upscript = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
downscript = strdup(optarg);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
1995-09-15 20:06:50 +00:00
|
|
|
case 'l':
|
|
|
|
modem_control = 0;
|
|
|
|
break;
|
1995-09-14 19:03:20 +00:00
|
|
|
case 'h':
|
|
|
|
flowcontrol = FC_HW;
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 3)
|
|
|
|
usage();
|
|
|
|
|
1994-12-04 12:26:01 +00:00
|
|
|
/*
|
|
|
|
* Copy these so they exist after we clobber them.
|
|
|
|
*/
|
|
|
|
devicename = strdup(argv[0]);
|
|
|
|
username = strdup(argv[1]);
|
|
|
|
password = strdup(argv[2]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Security hack. Do not want private information such as the
|
|
|
|
* password and possible phone number to be left around.
|
|
|
|
* So we clobber the arguments.
|
|
|
|
*/
|
|
|
|
for (ap = argv - optind + 1; ap < argv + 3; ap++)
|
|
|
|
for (cp = *ap; *cp != 0; cp++)
|
|
|
|
*cp = '\0';
|
|
|
|
|
1995-09-14 19:03:20 +00:00
|
|
|
openlog("startslip", LOG_PID|LOG_PERROR, LOG_DAEMON);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
if (debug)
|
|
|
|
setbuf(stdout, NULL);
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1995-09-15 20:06:50 +00:00
|
|
|
if ((dvname = strrchr(devicename, '/')) == NULL)
|
|
|
|
dvname = devicename;
|
1995-09-14 19:03:20 +00:00
|
|
|
else
|
1995-09-15 20:06:50 +00:00
|
|
|
dvname++;
|
|
|
|
sprintf(pidfile, PIDFILE, _PATH_VARRUN, dvname);
|
1995-09-14 19:03:20 +00:00
|
|
|
if ((pfd = fopen(pidfile, "r")) != NULL) {
|
1994-05-26 06:35:07 +00:00
|
|
|
pid = 0;
|
1995-09-15 20:06:50 +00:00
|
|
|
fscanf(pfd, "%ld\n", &pid);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (pid > 0)
|
1995-09-14 19:03:20 +00:00
|
|
|
kill(pid, SIGTERM);
|
1994-05-26 06:35:07 +00:00
|
|
|
fclose(pfd);
|
1995-09-14 19:03:20 +00:00
|
|
|
pfd = NULL; /* not remove pidfile yet */
|
|
|
|
sleep(5); /* allow down script to be completed */
|
|
|
|
} else
|
1994-05-26 06:35:07 +00:00
|
|
|
restart:
|
1994-12-04 12:26:01 +00:00
|
|
|
if (logged_in) {
|
1995-09-15 16:41:10 +00:00
|
|
|
sprintf(buf, "LINE=%d %s %s down &",
|
|
|
|
diali ? (dialc - 1) % diali : 0,
|
|
|
|
downscript ? downscript : "/sbin/ifconfig" , unitname);
|
1994-12-04 12:26:01 +00:00
|
|
|
(void) system(buf);
|
1995-09-15 20:06:50 +00:00
|
|
|
logged_in = 0;
|
1994-12-04 12:26:01 +00:00
|
|
|
}
|
1995-09-14 22:01:06 +00:00
|
|
|
if (terminate)
|
|
|
|
down(0);
|
1995-09-15 20:06:50 +00:00
|
|
|
tries++;
|
|
|
|
if (MAXTRIES > 0 && tries > MAXTRIES) {
|
1995-09-14 19:03:20 +00:00
|
|
|
syslog(LOG_ERR, "exiting login after %d tries\n", tries);
|
1994-05-26 06:35:07 +00:00
|
|
|
/* ???
|
|
|
|
if (first)
|
|
|
|
*/
|
1995-09-14 22:01:06 +00:00
|
|
|
down(3);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1995-09-14 19:03:20 +00:00
|
|
|
if (diali > 0)
|
|
|
|
dialerstring = dials[dialc++ % diali];
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We may get a HUP below, when the parent (session leader/
|
|
|
|
* controlling process) exits; ignore HUP until into new session.
|
|
|
|
*/
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
hup = 0;
|
|
|
|
if (fork() > 0) {
|
|
|
|
if (first)
|
|
|
|
printd("parent exit\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
1995-09-15 20:06:50 +00:00
|
|
|
if (setsid() < 0) {
|
1995-09-14 19:03:20 +00:00
|
|
|
syslog(LOG_ERR, "setsid: %m");
|
1995-09-15 20:06:50 +00:00
|
|
|
down(2);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
pid = getpid();
|
1995-09-15 20:06:50 +00:00
|
|
|
printd("restart: pid %ld: ", pid);
|
|
|
|
if ((pfd = fopen(pidfile, "w")) != NULL) {
|
|
|
|
fprintf(pfd, "%ld\n", pid);
|
1994-05-26 06:35:07 +00:00
|
|
|
fclose(pfd);
|
|
|
|
}
|
|
|
|
if (wfd) {
|
|
|
|
printd("fclose, ");
|
|
|
|
fclose(wfd);
|
1995-09-15 20:06:50 +00:00
|
|
|
uu_unlock(dvname);
|
1995-09-14 22:01:06 +00:00
|
|
|
locked = 0;
|
|
|
|
wfd = NULL;
|
|
|
|
fd = -1;
|
|
|
|
sleep(5);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
if (fd >= 0) {
|
|
|
|
printd("close, ");
|
|
|
|
close(fd);
|
1995-09-15 20:06:50 +00:00
|
|
|
uu_unlock(dvname);
|
1995-09-14 22:01:06 +00:00
|
|
|
locked = 0;
|
|
|
|
fd = -1;
|
1994-05-26 06:35:07 +00:00
|
|
|
sleep(5);
|
|
|
|
}
|
1995-09-15 20:06:50 +00:00
|
|
|
if (tries > 1) {
|
|
|
|
syslog(LOG_INFO, "sleeping %d seconds (%d tries)",
|
|
|
|
wait_time * (tries - 1), tries - 1);
|
|
|
|
sleep(wait_time * (tries - 1));
|
|
|
|
if (hup || terminate)
|
|
|
|
goto restart;
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
printd("open");
|
1995-09-15 20:06:50 +00:00
|
|
|
if (uu_lock(dvname)) {
|
1995-09-14 22:01:06 +00:00
|
|
|
syslog(LOG_ERR, "can't lock %s", devicename);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
locked = 1;
|
1995-09-15 20:06:50 +00:00
|
|
|
if ((fd = open(devicename, O_RDWR | O_NONBLOCK)) < 0) {
|
1994-12-04 12:26:01 +00:00
|
|
|
syslog(LOG_ERR, "open %s: %m\n", devicename);
|
1995-09-14 22:01:06 +00:00
|
|
|
if (first)
|
|
|
|
down(1);
|
|
|
|
else {
|
1995-09-15 20:06:50 +00:00
|
|
|
uu_unlock(dvname);
|
1995-09-14 22:01:06 +00:00
|
|
|
locked = 0;
|
1994-05-26 06:35:07 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printd(" %d", fd);
|
|
|
|
signal(SIGHUP, sighup);
|
1995-09-14 19:03:20 +00:00
|
|
|
signal(SIGTERM, sigterm);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (debug) {
|
|
|
|
if (ioctl(fd, TIOCGETD, &disc) < 0)
|
1995-09-14 19:03:20 +00:00
|
|
|
syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
|
1995-09-14 22:01:06 +00:00
|
|
|
else
|
|
|
|
printd(" (disc was %d)", disc);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
disc = TTYDISC;
|
|
|
|
if (ioctl(fd, TIOCSETD, &disc) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: ioctl (TIOCSETD 0): %m\n",
|
1994-12-04 12:26:01 +00:00
|
|
|
devicename);
|
1995-09-14 22:01:06 +00:00
|
|
|
down(2);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1995-09-15 17:30:03 +00:00
|
|
|
if (ioctl(fd, TIOCSCTTY, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m");
|
|
|
|
down(2);
|
|
|
|
}
|
1995-09-15 20:06:50 +00:00
|
|
|
if (tcsetpgrp(fd, getpid()) < 0) {
|
|
|
|
syslog(LOG_ERR, "tcsetpgrp failed: %m");
|
|
|
|
down(2);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
printd(", ioctl");
|
|
|
|
if (tcgetattr(fd, &t) < 0) {
|
1994-12-04 12:26:01 +00:00
|
|
|
syslog(LOG_ERR, "%s: tcgetattr: %m\n", devicename);
|
1995-09-14 22:01:06 +00:00
|
|
|
down(2);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
cfmakeraw(&t);
|
|
|
|
switch (flowcontrol) {
|
|
|
|
case FC_HW:
|
|
|
|
t.c_cflag |= (CRTS_IFLOW|CCTS_OFLOW);
|
|
|
|
break;
|
|
|
|
case FC_NONE:
|
|
|
|
t.c_cflag &= ~(CRTS_IFLOW|CCTS_OFLOW);
|
|
|
|
break;
|
|
|
|
}
|
1995-09-15 20:06:50 +00:00
|
|
|
if (modem_control)
|
|
|
|
t.c_cflag |= HUPCL;
|
|
|
|
else
|
|
|
|
t.c_cflag &= ~(HUPCL);
|
|
|
|
t.c_cflag |= CLOCAL; /* until modem commands passes */
|
1994-05-26 06:35:07 +00:00
|
|
|
cfsetspeed(&t, speed);
|
|
|
|
if (tcsetattr(fd, TCSAFLUSH, &t) < 0) {
|
1994-12-04 12:26:01 +00:00
|
|
|
syslog(LOG_ERR, "%s: tcsetattr: %m\n", devicename);
|
1995-09-14 22:01:06 +00:00
|
|
|
down(2);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
sleep(2); /* wait for flakey line to settle */
|
1995-09-14 19:03:20 +00:00
|
|
|
if (hup || terminate)
|
1994-05-26 06:35:07 +00:00
|
|
|
goto restart;
|
|
|
|
|
|
|
|
wfd = fdopen(fd, "w+");
|
|
|
|
if (wfd == NULL) {
|
1995-09-14 22:01:06 +00:00
|
|
|
syslog(LOG_ERR, "can't fdopen %s: %m", devicename);
|
|
|
|
down(2);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
setbuf(wfd, (char *)0);
|
|
|
|
if (dialerstring) {
|
1995-09-14 19:03:20 +00:00
|
|
|
printd(", send dialstring: %s\\r", dialerstring);
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(wfd, "%s\r", dialerstring);
|
1995-09-14 19:03:20 +00:00
|
|
|
} else {
|
|
|
|
printd(", send \\r");
|
1994-05-26 06:35:07 +00:00
|
|
|
putc('\r', wfd);
|
1995-09-14 19:03:20 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
printd("\n");
|
|
|
|
|
1995-09-15 20:06:50 +00:00
|
|
|
fintimeout = time(NULL) + script_timeout;
|
|
|
|
if (modem_control) {
|
|
|
|
printd("waiting for carrier\n");
|
|
|
|
while (time(NULL) < fintimeout && !carrier()) {
|
|
|
|
sleep(1);
|
|
|
|
if (hup || terminate)
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
if (!carrier())
|
|
|
|
goto restart;
|
|
|
|
t.c_cflag &= ~(CLOCAL);
|
|
|
|
if (tcsetattr(fd, TCSANOW, &t) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: tcsetattr: %m", devicename);
|
|
|
|
down(2);
|
|
|
|
}
|
|
|
|
/* Only now we able to receive HUP on carier drop! */
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Log in
|
|
|
|
*/
|
|
|
|
printd("look for login: ");
|
|
|
|
for (;;) {
|
1995-09-15 20:06:50 +00:00
|
|
|
if (getline(buf, BUFSIZ, fd, fintimeout) == 0 || hup || terminate)
|
1994-05-26 06:35:07 +00:00
|
|
|
goto restart;
|
|
|
|
if (annex) {
|
|
|
|
if (bcmp(buf, annex, strlen(annex)) == 0) {
|
|
|
|
fprintf(wfd, "slip\r");
|
|
|
|
printd("Sent \"slip\"\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bcmp(&buf[1], "sername:", 8) == 0) {
|
1994-12-04 12:26:01 +00:00
|
|
|
fprintf(wfd, "%s\r", username);
|
|
|
|
printd("Sent login: %s\n", username);
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bcmp(&buf[1], "assword:", 8) == 0) {
|
1994-12-04 12:26:01 +00:00
|
|
|
fprintf(wfd, "%s\r", password);
|
|
|
|
printd("Sent password: %s\n", password);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
1995-09-14 19:03:20 +00:00
|
|
|
if (strstr(&buf[1], "ogin:") != NULL) {
|
1994-12-04 12:26:01 +00:00
|
|
|
fprintf(wfd, "%s\r", username);
|
|
|
|
printd("Sent login: %s\n", username);
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
1995-09-14 19:03:20 +00:00
|
|
|
if (strstr(&buf[1], "assword:") != NULL) {
|
1994-12-04 12:26:01 +00:00
|
|
|
fprintf(wfd, "%s\r", password);
|
|
|
|
printd("Sent password: %s\n", password);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1995-09-15 17:08:42 +00:00
|
|
|
sleep(5); /* Wait until login completed */
|
|
|
|
if (hup || terminate)
|
|
|
|
goto restart;
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Attach
|
|
|
|
*/
|
|
|
|
printd("setd");
|
|
|
|
disc = SLIPDISC;
|
|
|
|
if (ioctl(fd, TIOCSETD, &disc) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: ioctl (TIOCSETD SLIP): %m\n",
|
1994-12-04 12:26:01 +00:00
|
|
|
devicename);
|
1995-09-14 22:01:06 +00:00
|
|
|
down(2);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1994-12-04 12:26:01 +00:00
|
|
|
if (ioctl(fd, SLIOCGUNIT, (caddr_t)&unitnum) < 0) {
|
|
|
|
syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m");
|
1995-09-14 22:01:06 +00:00
|
|
|
down(2);
|
1994-12-04 12:26:01 +00:00
|
|
|
}
|
|
|
|
sprintf(unitname, "sl%d", unitnum);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (first && debug == 0) {
|
|
|
|
close(0);
|
|
|
|
close(1);
|
|
|
|
close(2);
|
|
|
|
(void) open("/dev/null", O_RDWR);
|
|
|
|
(void) dup2(0, 1);
|
|
|
|
(void) dup2(0, 2);
|
|
|
|
}
|
1994-12-04 12:26:01 +00:00
|
|
|
|
1995-09-15 16:41:10 +00:00
|
|
|
sprintf(buf, "LINE=%d %s %s up &",
|
|
|
|
diali ? (dialc - 1) % diali : 0,
|
|
|
|
upscript ? upscript : "/sbin/ifconfig" , unitname);
|
1994-12-04 12:26:01 +00:00
|
|
|
(void) system(buf);
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
printd(", ready\n");
|
|
|
|
if (!first)
|
1994-12-04 12:26:01 +00:00
|
|
|
syslog(LOG_INFO, "reconnected on %s (%d tries).\n", unitname, tries);
|
1994-05-26 06:35:07 +00:00
|
|
|
first = 0;
|
|
|
|
tries = 0;
|
|
|
|
logged_in = 1;
|
1995-09-14 19:03:20 +00:00
|
|
|
while (hup == 0 && terminate == 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
sigpause(0L);
|
|
|
|
printd("sigpause return\n");
|
|
|
|
}
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sighup()
|
|
|
|
{
|
|
|
|
|
|
|
|
printd("hup\n");
|
|
|
|
if (hup == 0 && logged_in)
|
|
|
|
syslog(LOG_INFO, "hangup signal\n");
|
|
|
|
hup = 1;
|
|
|
|
}
|
|
|
|
|
1995-09-14 19:03:20 +00:00
|
|
|
void
|
|
|
|
sigterm()
|
|
|
|
{
|
|
|
|
|
|
|
|
printd("terminate\n");
|
|
|
|
if (terminate == 0 && logged_in)
|
|
|
|
syslog(LOG_INFO, "terminate signal\n");
|
|
|
|
terminate = 1;
|
|
|
|
}
|
|
|
|
|
1995-09-15 20:06:50 +00:00
|
|
|
getline(buf, size, fd, fintimeout)
|
1994-05-26 06:35:07 +00:00
|
|
|
char *buf;
|
1995-09-15 20:06:50 +00:00
|
|
|
int size, fd;
|
|
|
|
time_t fintimeout;
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
int ret;
|
1994-12-04 12:26:01 +00:00
|
|
|
fd_set readfds;
|
|
|
|
struct timeval tv;
|
1995-09-15 20:06:50 +00:00
|
|
|
time_t timeout;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
size--;
|
|
|
|
for (i = 0; i < size; i++) {
|
1995-09-14 19:03:20 +00:00
|
|
|
if (hup || terminate)
|
1994-05-26 06:35:07 +00:00
|
|
|
return (0);
|
1995-09-15 20:06:50 +00:00
|
|
|
if ((timeout = fintimeout - time(NULL)) <= 0)
|
|
|
|
goto tout;
|
1994-12-04 12:26:01 +00:00
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(fd, &readfds);
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if ((ret = select(fd + 1, &readfds, NULL, NULL, &tv)) < 0) {
|
1995-05-30 06:12:45 +00:00
|
|
|
if (errno != EINTR)
|
1995-09-14 19:03:20 +00:00
|
|
|
syslog(LOG_ERR, "getline: select: %m");
|
1994-12-04 12:26:01 +00:00
|
|
|
} else {
|
|
|
|
if (! ret) {
|
1995-09-15 20:06:50 +00:00
|
|
|
tout:
|
1994-12-04 12:26:01 +00:00
|
|
|
printd("getline: timed out\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if ((ret = read(fd, &buf[i], 1)) == 1) {
|
|
|
|
buf[i] &= 0177;
|
1995-09-14 19:03:20 +00:00
|
|
|
if (buf[i] == '\r' || buf[i] == '\0') {
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
1994-12-04 12:26:01 +00:00
|
|
|
if (buf[i] != '\n' && buf[i] != ':')
|
|
|
|
continue;
|
|
|
|
buf[i + 1] = '\0';
|
1995-09-14 19:03:20 +00:00
|
|
|
printd("Got %d: %s", i + 1, buf);
|
1994-12-04 12:26:01 +00:00
|
|
|
return (i+1);
|
|
|
|
}
|
|
|
|
if (ret <= 0) {
|
|
|
|
if (ret < 0) {
|
1995-09-14 19:03:20 +00:00
|
|
|
syslog(LOG_ERR, "getline: read: %m");
|
1994-12-04 12:26:01 +00:00
|
|
|
} else
|
1995-09-14 19:03:20 +00:00
|
|
|
syslog(LOG_ERR, "read returned 0");
|
1994-12-04 12:26:01 +00:00
|
|
|
buf[i] = '\0';
|
1995-09-14 19:03:20 +00:00
|
|
|
printd("returning %d after %d: %s\n", ret, i, buf);
|
1994-12-04 12:26:01 +00:00
|
|
|
return (0);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1995-09-15 20:06:50 +00:00
|
|
|
carrier()
|
|
|
|
{
|
|
|
|
int comstate;
|
|
|
|
|
|
|
|
if (ioctl(fd, TIOCMGET, &comstate) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: ioctl (TIOCMGET): %m",
|
|
|
|
devicename);
|
|
|
|
down(2);
|
|
|
|
}
|
|
|
|
return !!(comstate & TIOCM_CD);
|
|
|
|
}
|
|
|
|
|
1995-09-14 22:01:06 +00:00
|
|
|
down(code)
|
|
|
|
{
|
1995-09-15 17:30:03 +00:00
|
|
|
int disc = TTYDISC;
|
|
|
|
|
|
|
|
if (fd > -1 && ioctl(fd, TIOCSETD, &disc) < 0)
|
|
|
|
syslog(LOG_ERR, "%s: ioctl (TIOCSETD 0): %m",
|
|
|
|
devicename);
|
1995-09-14 22:01:06 +00:00
|
|
|
if (pfd)
|
|
|
|
unlink(pidfile);
|
|
|
|
if (locked)
|
1995-09-15 20:06:50 +00:00
|
|
|
uu_unlock(dvname);
|
1995-09-14 22:01:06 +00:00
|
|
|
exit(code);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
usage()
|
|
|
|
{
|
1995-09-14 19:03:20 +00:00
|
|
|
(void)fprintf(stderr, "\
|
|
|
|
usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-A annexname]\n\
|
1995-09-15 20:06:50 +00:00
|
|
|
[-h] [-l] [-U upscript] [-D downscript] [-t script_timeout]\n\
|
|
|
|
[-w retry_pause] [-W maxtries] device user passwd\n");
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(1);
|
|
|
|
}
|