2 * Copyright (c) 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1990, 1991, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)startslip.c 8.1 (Berkeley) 6/5/93";
40 static const char rcsid[] =
44 #include <sys/types.h>
62 #define DEFAULT_BAUD B9600
63 int speed = DEFAULT_BAUD;
64 #define FC_NONE 0 /* flow control: none */
65 #define FC_HW 1 /* flow control: hardware (RTS/CTS) */
66 int flowcontrol = FC_NONE;
67 int modem_control = 1; /* !CLOCAL+HUPCL iff we watch carrier. */
69 int uucp_lock = 0; /* uucp locking */
74 int locked = 0; /* uucp lock active */
76 int wait_time = 60; /* then back off */
77 int script_timeout = 90; /* connect script default timeout */
78 time_t conn_time, start_time;
79 int MAXTRIES = 6; /* w/60 sec and doubling, takes an hour */
80 #define PIDFILE "%sstartslip.%s.pid"
83 char *dials[MAXDIALS];
88 char *dvname, *devicename;
95 #define syslog fprintf
96 #define LOG_ERR stderr
97 #define LOG_INFO stderr
101 #define printd if (debug) printf
105 int getline(char *, int, int, time_t);
109 static void usage(void);
112 main(int argc, char **argv)
117 char *dialerstring = 0, buf[BUFSIZ];
118 int unitnum, keepal = 0, outfill = 0;
121 char *upscript = NULL, *downscript = NULL;
122 int first = 1, tries = 0;
128 while ((ch = getopt(argc, argv, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
134 speed = atoi(optarg);
137 if (diali >= MAXDIALS)
138 errx(1, "max dial strings number (%d) exceeded", MAXDIALS);
139 dials[diali++] = strdup(optarg);
142 script_timeout = atoi(optarg);
145 wait_time = atoi(optarg);
148 MAXTRIES = atoi(optarg);
151 annex = strdup(optarg);
154 upscript = strdup(optarg);
157 downscript = strdup(optarg);
169 keepal = atoi(optarg);
172 outfill = atoi(optarg);
175 sl_unit = atoi(optarg);
188 * Copy these so they exist after we clobber them.
190 devicename = strdup(argv[0]);
191 username = strdup(argv[1]);
192 password = strdup(argv[2]);
195 * Security hack. Do not want private information such as the
196 * password and possible phone number to be left around.
197 * So we clobber the arguments.
199 for (ap = argv - optind + 1; ap < argv + 3; ap++)
200 for (cp = *ap; *cp != 0; cp++)
203 openlog("startslip", LOG_PID|LOG_PERROR, LOG_DAEMON);
206 setbuf(stdout, NULL);
208 signal(SIGTERM, sigterm);
209 if ((dvname = strrchr(devicename, '/')) == NULL)
213 if (snprintf(pidfile, sizeof(pidfile), PIDFILE, _PATH_VARRUN, dvname)
214 >= (int)sizeof(pidfile))
217 if ((pfd = fopen(pidfile, "r")) != NULL) {
218 if (fscanf(pfd, "%ld\n", &lpid) == 1) {
220 if (pid == lpid && pid > 0)
224 pfd = NULL; /* not remove pidfile yet */
225 sleep(5); /* allow down script to be completed */
228 signal(SIGHUP, SIG_IGN);
229 signal(SIGURG, SIG_IGN);
234 conn_time = time(NULL) - start_time;
241 } else if (fd >= 0) {
244 conn_time = time(NULL) - start_time;
252 syslog(LOG_INFO, "%s: connection time elapsed: %ld secs",
253 username, (long)conn_time);
254 sprintf(buf, "LINE=%d %s %s down",
255 diali ? (dialc - 1) % diali : 0,
256 downscript ? downscript : _PATH_IFCONFIG , unitname);
263 if (MAXTRIES > 0 && tries > MAXTRIES) {
264 syslog(LOG_ERR, "%s: exiting login after %d tries", username, tries);
271 syslog(LOG_INFO, "%s: sleeping %d seconds (%d tries)",
272 username, wait_time * (tries - 1), tries);
273 sleep(wait_time * (tries - 1));
278 if (daemon(1, debug) < 0) {
279 syslog(LOG_ERR, "%s: daemon: %m", username);
284 printd("restart: pid %ld: ", (long)pid);
285 if ((pfd = fopen(pidfile, "w")) != NULL) {
286 fprintf(pfd, "%ld\n", (long)pid);
292 if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
293 if (res != UU_LOCK_INUSE)
294 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
295 syslog(LOG_ERR, "%s: can't lock %s", username, devicename);
300 if ((fd = open(devicename, O_RDWR | O_NONBLOCK)) < 0) {
301 syslog(LOG_ERR, "%s: open %s: %m", username, devicename);
312 signal(SIGHUP, sighup);
313 if (ioctl(fd, TIOCSCTTY, 0) < 0) {
314 syslog(LOG_ERR, "%s: ioctl (TIOCSCTTY): %m", username);
317 if (tcsetpgrp(fd, getpid()) < 0) {
318 syslog(LOG_ERR, "%s: tcsetpgrp failed: %m", username);
322 if (tcgetattr(fd, &t) < 0) {
323 syslog(LOG_ERR, "%s: tcgetattr(%s): %m", username, devicename);
327 switch (flowcontrol) {
329 t.c_cflag |= (CRTS_IFLOW|CCTS_OFLOW);
332 t.c_cflag &= ~(CRTS_IFLOW|CCTS_OFLOW);
338 t.c_cflag &= ~(HUPCL);
339 t.c_cflag |= CLOCAL; /* until modem commands passes */
340 cfsetispeed(&t, speed);
341 cfsetospeed(&t, speed);
342 if (tcsetattr(fd, TCSAFLUSH, &t) < 0) {
343 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
346 sleep(2); /* wait for flakey line to settle */
347 if (hup || terminate)
350 wfd = fdopen(fd, "w+");
352 syslog(LOG_ERR, "%s: can't fdopen %s: %m", username, devicename);
358 dialerstring = dials[dialc++ % diali];
360 syslog(LOG_INFO, "%s: dialer string: %s\\r", username, dialerstring);
361 fprintf(wfd, "%s\r", dialerstring);
365 fintimeout = time(NULL) + script_timeout;
367 printd("waiting for carrier\n");
368 while (time(NULL) < fintimeout && !carrier()) {
370 if (hup || terminate)
375 t.c_cflag &= ~(CLOCAL);
376 if (tcsetattr(fd, TCSANOW, &t) < 0) {
377 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
380 /* Only now we able to receive HUP on carrier drop! */
386 printd("look for login: ");
388 if (getline(buf, BUFSIZ, fd, fintimeout) == 0 || hup || terminate)
391 if (bcmp(buf, annex, strlen(annex)) == 0) {
392 fprintf(wfd, "slip\r");
393 printd("Sent \"slip\"\n");
396 if (bcmp(&buf[1], "sername:", 8) == 0) {
397 fprintf(wfd, "%s\r", username);
398 printd("Sent login: %s\n", username);
401 if (bcmp(&buf[1], "assword:", 8) == 0) {
402 fprintf(wfd, "%s\r", password);
403 printd("Sent password: %s\n", password);
407 if (strstr(&buf[1], "ogin:") != NULL) {
408 fprintf(wfd, "%s\r", username);
409 printd("Sent login: %s\n", username);
412 if (strstr(&buf[1], "assword:") != NULL) {
413 fprintf(wfd, "%s\r", password);
414 printd("Sent password: %s\n", password);
420 sleep(5); /* Wait until login completed */
421 if (hup || terminate)
423 start_time = time(NULL);
429 if (ioctl(fd, TIOCSETD, &disc) < 0) {
430 syslog(LOG_ERR, "%s: ioctl (%s, TIOCSETD): %m",
431 username, devicename);
434 if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
435 syslog(LOG_ERR, "%s: ioctl(SLIOCSUNIT): %m", username);
438 if (ioctl(fd, SLIOCGUNIT, &unitnum) < 0) {
439 syslog(LOG_ERR, "%s: ioctl(SLIOCGUNIT): %m", username);
442 sprintf(unitname, "sl%d", unitnum);
445 signal(SIGURG, sigurg);
446 if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) {
447 syslog(LOG_ERR, "%s: ioctl(SLIOCSKEEPAL): %m", username);
451 if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) {
452 syslog(LOG_ERR, "%s: ioctl(SLIOCSOUTFILL): %m", username);
456 sprintf(buf, "LINE=%d %s %s up",
457 diali ? (dialc - 1) % diali : 0,
458 upscript ? upscript : _PATH_IFCONFIG , unitname);
463 syslog(LOG_INFO, "%s: reconnected on %s (%d tries)", username, unitname, tries);
465 syslog(LOG_INFO, "%s: connected on %s", username, unitname);
469 while (hup == 0 && terminate == 0) {
471 printd("sigpause return\n");
474 return(0); /* not reached */
478 sighup(int sig __unused)
482 if (hup == 0 && logged_in)
483 syslog(LOG_INFO, "%s: got hangup signal", username);
488 sigurg(int sig __unused)
492 if (hup == 0 && logged_in)
493 syslog(LOG_INFO, "%s: got dead line signal", username);
498 sigterm(int sig __unused)
501 printd("terminate\n");
502 if (terminate == 0 && logged_in)
503 syslog(LOG_INFO, "%s: got terminate signal", username);
508 getline(char *buf, int size, int fd, time_t fintimeout)
517 for (i = 0; i < size; i++) {
518 if (hup || terminate)
520 if ((timeout = fintimeout - time(NULL)) <= 0)
523 FD_SET(fd, &readfds);
526 if ((ret = select(fd + 1, &readfds, NULL, NULL, &tv)) < 0) {
528 syslog(LOG_ERR, "%s: getline: select: %m", username);
532 printd("getline: timed out\n");
535 if ((ret = read(fd, &buf[i], 1)) == 1) {
537 if (buf[i] == '\r' || buf[i] == '\0') {
541 if (buf[i] != '\n' && buf[i] != ':')
544 printd("Got %d: %s", i + 1, buf);
549 syslog(LOG_ERR, "%s: getline: read: %m", username);
551 syslog(LOG_ERR, "%s: read returned 0", username);
553 printd("returning %d after %d: %s\n", ret, i, buf);
566 if (ioctl(fd, TIOCMGET, &comstate) < 0) {
567 syslog(LOG_ERR, "%s: ioctl (%s, TIOCMGET): %m",
568 username, devicename);
571 return !!(comstate & TIOCM_CD);
581 if (uucp_lock && locked)
589 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
590 "usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-h] [-l]",
591 " [-L] [-A annexname] [-U upscript] [-D downscript]",
592 " [-t script_timeout] [-W maxtries] [-w retry_pause]",
593 " [-K keepalive] [-O outfill] [-S unit] device user password");