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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1990, 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)startslip.c 8.1 (Berkeley) 6/5/93";
44 static const char rcsid[] =
48 #include <sys/types.h>
66 #define DEFAULT_BAUD B9600
67 int speed = DEFAULT_BAUD;
68 #define FC_NONE 0 /* flow control: none */
69 #define FC_HW 1 /* flow control: hardware (RTS/CTS) */
70 int flowcontrol = FC_NONE;
71 int modem_control = 1; /* !CLOCAL+HUPCL iff we watch carrier. */
73 int uucp_lock = 0; /* uucp locking */
78 int locked = 0; /* uucp lock active */
80 int wait_time = 60; /* then back off */
81 int script_timeout = 90; /* connect script default timeout */
82 time_t conn_time, start_time;
83 int MAXTRIES = 6; /* w/60 sec and doubling, takes an hour */
84 #define PIDFILE "%sstartslip.%s.pid"
87 char *dials[MAXDIALS];
92 char *dvname, *devicename;
99 #define syslog fprintf
100 #define LOG_ERR stderr
101 #define LOG_INFO stderr
105 #define printd if (debug) printf
107 int carrier __P((void));
108 void down __P((int));
109 int getline __P((char *, int, int, time_t));
110 static void usage __P((void));
119 void sighup(), sigterm(), sigurg();
121 char *dialerstring = 0, buf[BUFSIZ];
122 int unitnum, keepal = 0, outfill = 0;
125 char *upscript = NULL, *downscript = NULL;
126 int first = 1, tries = 0;
132 while ((ch = getopt(argc, argv, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
138 speed = atoi(optarg);
141 if (diali >= MAXDIALS)
142 errx(1, "max dial strings number (%d) exceeded", MAXDIALS);
143 dials[diali++] = strdup(optarg);
146 script_timeout = atoi(optarg);
149 wait_time = atoi(optarg);
152 MAXTRIES = atoi(optarg);
155 annex = strdup(optarg);
158 upscript = strdup(optarg);
161 downscript = strdup(optarg);
173 keepal = atoi(optarg);
176 outfill = atoi(optarg);
179 sl_unit = atoi(optarg);
192 * Copy these so they exist after we clobber them.
194 devicename = strdup(argv[0]);
195 username = strdup(argv[1]);
196 password = strdup(argv[2]);
199 * Security hack. Do not want private information such as the
200 * password and possible phone number to be left around.
201 * So we clobber the arguments.
203 for (ap = argv - optind + 1; ap < argv + 3; ap++)
204 for (cp = *ap; *cp != 0; cp++)
207 openlog("startslip", LOG_PID|LOG_PERROR, LOG_DAEMON);
210 setbuf(stdout, NULL);
212 signal(SIGTERM, sigterm);
213 if ((dvname = strrchr(devicename, '/')) == NULL)
217 sprintf(pidfile, PIDFILE, _PATH_VARRUN, dvname);
218 if ((pfd = fopen(pidfile, "r")) != NULL) {
219 if (fscanf(pfd, "%ld\n", &lpid) == 1) {
221 if (pid == lpid && pid > 0)
225 pfd = NULL; /* not remove pidfile yet */
226 sleep(5); /* allow down script to be completed */
229 signal(SIGHUP, SIG_IGN);
230 signal(SIGURG, SIG_IGN);
235 conn_time = time(NULL) - start_time;
242 } else if (fd >= 0) {
245 conn_time = time(NULL) - start_time;
253 syslog(LOG_INFO, "%s: connection time elapsed: %ld secs",
254 username, (long)conn_time);
255 sprintf(buf, "LINE=%d %s %s down",
256 diali ? (dialc - 1) % diali : 0,
257 downscript ? downscript : "/sbin/ifconfig" , unitname);
264 if (MAXTRIES > 0 && tries > MAXTRIES) {
265 syslog(LOG_ERR, "%s: exiting login after %d tries", username, tries);
272 syslog(LOG_INFO, "%s: sleeping %d seconds (%d tries)",
273 username, wait_time * (tries - 1), tries);
274 sleep(wait_time * (tries - 1));
279 if (daemon(1, debug) < 0) {
280 syslog(LOG_ERR, "%s: daemon: %m", username);
285 printd("restart: pid %ld: ", (long)pid);
286 if ((pfd = fopen(pidfile, "w")) != NULL) {
287 fprintf(pfd, "%ld\n", (long)pid);
293 if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
294 if (res != UU_LOCK_INUSE)
295 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
296 syslog(LOG_ERR, "%s: can't lock %s", username, devicename);
301 if ((fd = open(devicename, O_RDWR | O_NONBLOCK)) < 0) {
302 syslog(LOG_ERR, "%s: open %s: %m", username, devicename);
313 signal(SIGHUP, sighup);
314 if (ioctl(fd, TIOCSCTTY, 0) < 0) {
315 syslog(LOG_ERR, "%s: ioctl (TIOCSCTTY): %m", username);
318 if (tcsetpgrp(fd, getpid()) < 0) {
319 syslog(LOG_ERR, "%s: tcsetpgrp failed: %m", username);
323 if (tcgetattr(fd, &t) < 0) {
324 syslog(LOG_ERR, "%s: tcgetattr(%s): %m", username, devicename);
328 switch (flowcontrol) {
330 t.c_cflag |= (CRTS_IFLOW|CCTS_OFLOW);
333 t.c_cflag &= ~(CRTS_IFLOW|CCTS_OFLOW);
339 t.c_cflag &= ~(HUPCL);
340 t.c_cflag |= CLOCAL; /* until modem commands passes */
341 cfsetispeed(&t, speed);
342 cfsetospeed(&t, speed);
343 if (tcsetattr(fd, TCSAFLUSH, &t) < 0) {
344 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
347 sleep(2); /* wait for flakey line to settle */
348 if (hup || terminate)
351 wfd = fdopen(fd, "w+");
353 syslog(LOG_ERR, "%s: can't fdopen %s: %m", username, devicename);
359 dialerstring = dials[dialc++ % diali];
361 syslog(LOG_INFO, "%s: dialer string: %s\\r", username, dialerstring);
362 fprintf(wfd, "%s\r", dialerstring);
366 fintimeout = time(NULL) + script_timeout;
368 printd("waiting for carrier\n");
369 while (time(NULL) < fintimeout && !carrier()) {
371 if (hup || terminate)
376 t.c_cflag &= ~(CLOCAL);
377 if (tcsetattr(fd, TCSANOW, &t) < 0) {
378 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
381 /* Only now we able to receive HUP on carrier drop! */
387 printd("look for login: ");
389 if (getline(buf, BUFSIZ, fd, fintimeout) == 0 || hup || terminate)
392 if (bcmp(buf, annex, strlen(annex)) == 0) {
393 fprintf(wfd, "slip\r");
394 printd("Sent \"slip\"\n");
397 if (bcmp(&buf[1], "sername:", 8) == 0) {
398 fprintf(wfd, "%s\r", username);
399 printd("Sent login: %s\n", username);
402 if (bcmp(&buf[1], "assword:", 8) == 0) {
403 fprintf(wfd, "%s\r", password);
404 printd("Sent password: %s\n", password);
408 if (strstr(&buf[1], "ogin:") != NULL) {
409 fprintf(wfd, "%s\r", username);
410 printd("Sent login: %s\n", username);
413 if (strstr(&buf[1], "assword:") != NULL) {
414 fprintf(wfd, "%s\r", password);
415 printd("Sent password: %s\n", password);
421 sleep(5); /* Wait until login completed */
422 if (hup || terminate)
424 start_time = time(NULL);
430 if (ioctl(fd, TIOCSETD, &disc) < 0) {
431 syslog(LOG_ERR, "%s: ioctl (%s, TIOCSETD): %m",
432 username, devicename);
435 if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
436 syslog(LOG_ERR, "%s: ioctl(SLIOCSUNIT): %m", username);
439 if (ioctl(fd, SLIOCGUNIT, &unitnum) < 0) {
440 syslog(LOG_ERR, "%s: ioctl(SLIOCGUNIT): %m", username);
443 sprintf(unitname, "sl%d", unitnum);
446 signal(SIGURG, sigurg);
447 if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) {
448 syslog(LOG_ERR, "%s: ioctl(SLIOCSKEEPAL): %m", username);
452 if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) {
453 syslog(LOG_ERR, "%s: ioctl(SLIOCSOUTFILL): %m", username);
457 sprintf(buf, "LINE=%d %s %s up",
458 diali ? (dialc - 1) % diali : 0,
459 upscript ? upscript : "/sbin/ifconfig" , unitname);
464 syslog(LOG_INFO, "%s: reconnected on %s (%d tries)", username, unitname, tries);
466 syslog(LOG_INFO, "%s: connected on %s", username, unitname);
470 while (hup == 0 && terminate == 0) {
472 printd("sigpause return\n");
475 return(0); /* not reached */
483 if (hup == 0 && logged_in)
484 syslog(LOG_INFO, "%s: got hangup signal", username);
493 if (hup == 0 && logged_in)
494 syslog(LOG_INFO, "%s: got dead line signal", username);
502 printd("terminate\n");
503 if (terminate == 0 && logged_in)
504 syslog(LOG_INFO, "%s: got terminate signal", username);
509 getline(buf, size, fd, fintimeout)
521 for (i = 0; i < size; i++) {
522 if (hup || terminate)
524 if ((timeout = fintimeout - time(NULL)) <= 0)
527 FD_SET(fd, &readfds);
530 if ((ret = select(fd + 1, &readfds, NULL, NULL, &tv)) < 0) {
532 syslog(LOG_ERR, "%s: getline: select: %m", username);
536 printd("getline: timed out\n");
539 if ((ret = read(fd, &buf[i], 1)) == 1) {
541 if (buf[i] == '\r' || buf[i] == '\0') {
545 if (buf[i] != '\n' && buf[i] != ':')
548 printd("Got %d: %s", i + 1, buf);
553 syslog(LOG_ERR, "%s: getline: read: %m", username);
555 syslog(LOG_ERR, "%s: read returned 0", username);
557 printd("returning %d after %d: %s\n", ret, i, buf);
570 if (ioctl(fd, TIOCMGET, &comstate) < 0) {
571 syslog(LOG_ERR, "%s: ioctl (%s, TIOCMGET): %m",
572 username, devicename);
575 return !!(comstate & TIOCM_CD);
585 if (uucp_lock && locked)
593 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
594 "usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-h] [-l]",
595 " [-L] [-A annexname] [-U upscript] [-D downscript]",
596 " [-t script_timeout] [-W maxtries] [-w retry_pause]",
597 " [-K keepalive] [-O outfill] [-S unit] device user passwd");