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[] =
45 "$FreeBSD: src/sbin/startslip/startslip.c,v 1.31.2.1 2000/05/07 18:26:51 joe Exp $";
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 if (snprintf(pidfile, sizeof(pidfile), PIDFILE, _PATH_VARRUN, dvname) >= sizeof(pidfile))
220 if ((pfd = fopen(pidfile, "r")) != NULL) {
221 if (fscanf(pfd, "%ld\n", &lpid) == 1) {
223 if (pid == lpid && pid > 0)
227 pfd = NULL; /* not remove pidfile yet */
228 sleep(5); /* allow down script to be completed */
231 signal(SIGHUP, SIG_IGN);
232 signal(SIGURG, SIG_IGN);
237 conn_time = time(NULL) - start_time;
244 } else if (fd >= 0) {
247 conn_time = time(NULL) - start_time;
255 syslog(LOG_INFO, "%s: connection time elapsed: %ld secs",
256 username, (long)conn_time);
257 sprintf(buf, "LINE=%d %s %s down",
258 diali ? (dialc - 1) % diali : 0,
259 downscript ? downscript : "/sbin/ifconfig" , unitname);
266 if (MAXTRIES > 0 && tries > MAXTRIES) {
267 syslog(LOG_ERR, "%s: exiting login after %d tries", username, tries);
274 syslog(LOG_INFO, "%s: sleeping %d seconds (%d tries)",
275 username, wait_time * (tries - 1), tries);
276 sleep(wait_time * (tries - 1));
281 if (daemon(1, debug) < 0) {
282 syslog(LOG_ERR, "%s: daemon: %m", username);
287 printd("restart: pid %ld: ", (long)pid);
288 if ((pfd = fopen(pidfile, "w")) != NULL) {
289 fprintf(pfd, "%ld\n", (long)pid);
295 if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
296 if (res != UU_LOCK_INUSE)
297 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
298 syslog(LOG_ERR, "%s: can't lock %s", username, devicename);
303 if ((fd = open(devicename, O_RDWR | O_NONBLOCK)) < 0) {
304 syslog(LOG_ERR, "%s: open %s: %m", username, devicename);
315 signal(SIGHUP, sighup);
316 if (ioctl(fd, TIOCSCTTY, 0) < 0) {
317 syslog(LOG_ERR, "%s: ioctl (TIOCSCTTY): %m", username);
320 if (tcsetpgrp(fd, getpid()) < 0) {
321 syslog(LOG_ERR, "%s: tcsetpgrp failed: %m", username);
325 if (tcgetattr(fd, &t) < 0) {
326 syslog(LOG_ERR, "%s: tcgetattr(%s): %m", username, devicename);
330 switch (flowcontrol) {
332 t.c_cflag |= (CRTS_IFLOW|CCTS_OFLOW);
335 t.c_cflag &= ~(CRTS_IFLOW|CCTS_OFLOW);
341 t.c_cflag &= ~(HUPCL);
342 t.c_cflag |= CLOCAL; /* until modem commands passes */
343 cfsetispeed(&t, speed);
344 cfsetospeed(&t, speed);
345 if (tcsetattr(fd, TCSAFLUSH, &t) < 0) {
346 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
349 sleep(2); /* wait for flakey line to settle */
350 if (hup || terminate)
353 wfd = fdopen(fd, "w+");
355 syslog(LOG_ERR, "%s: can't fdopen %s: %m", username, devicename);
361 dialerstring = dials[dialc++ % diali];
363 syslog(LOG_INFO, "%s: dialer string: %s\\r", username, dialerstring);
364 fprintf(wfd, "%s\r", dialerstring);
368 fintimeout = time(NULL) + script_timeout;
370 printd("waiting for carrier\n");
371 while (time(NULL) < fintimeout && !carrier()) {
373 if (hup || terminate)
378 t.c_cflag &= ~(CLOCAL);
379 if (tcsetattr(fd, TCSANOW, &t) < 0) {
380 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
383 /* Only now we able to receive HUP on carrier drop! */
389 printd("look for login: ");
391 if (getline(buf, BUFSIZ, fd, fintimeout) == 0 || hup || terminate)
394 if (bcmp(buf, annex, strlen(annex)) == 0) {
395 fprintf(wfd, "slip\r");
396 printd("Sent \"slip\"\n");
399 if (bcmp(&buf[1], "sername:", 8) == 0) {
400 fprintf(wfd, "%s\r", username);
401 printd("Sent login: %s\n", username);
404 if (bcmp(&buf[1], "assword:", 8) == 0) {
405 fprintf(wfd, "%s\r", password);
406 printd("Sent password: %s\n", password);
410 if (strstr(&buf[1], "ogin:") != NULL) {
411 fprintf(wfd, "%s\r", username);
412 printd("Sent login: %s\n", username);
415 if (strstr(&buf[1], "assword:") != NULL) {
416 fprintf(wfd, "%s\r", password);
417 printd("Sent password: %s\n", password);
423 sleep(5); /* Wait until login completed */
424 if (hup || terminate)
426 start_time = time(NULL);
432 if (ioctl(fd, TIOCSETD, &disc) < 0) {
433 syslog(LOG_ERR, "%s: ioctl (%s, TIOCSETD): %m",
434 username, devicename);
437 if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
438 syslog(LOG_ERR, "%s: ioctl(SLIOCSUNIT): %m", username);
441 if (ioctl(fd, SLIOCGUNIT, &unitnum) < 0) {
442 syslog(LOG_ERR, "%s: ioctl(SLIOCGUNIT): %m", username);
445 sprintf(unitname, "sl%d", unitnum);
448 signal(SIGURG, sigurg);
449 if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) {
450 syslog(LOG_ERR, "%s: ioctl(SLIOCSKEEPAL): %m", username);
454 if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) {
455 syslog(LOG_ERR, "%s: ioctl(SLIOCSOUTFILL): %m", username);
459 sprintf(buf, "LINE=%d %s %s up",
460 diali ? (dialc - 1) % diali : 0,
461 upscript ? upscript : "/sbin/ifconfig" , unitname);
466 syslog(LOG_INFO, "%s: reconnected on %s (%d tries)", username, unitname, tries);
468 syslog(LOG_INFO, "%s: connected on %s", username, unitname);
472 while (hup == 0 && terminate == 0) {
474 printd("sigpause return\n");
477 return(0); /* not reached */
485 if (hup == 0 && logged_in)
486 syslog(LOG_INFO, "%s: got hangup signal", username);
495 if (hup == 0 && logged_in)
496 syslog(LOG_INFO, "%s: got dead line signal", username);
504 printd("terminate\n");
505 if (terminate == 0 && logged_in)
506 syslog(LOG_INFO, "%s: got terminate signal", username);
511 getline(buf, size, fd, fintimeout)
523 for (i = 0; i < size; i++) {
524 if (hup || terminate)
526 if ((timeout = fintimeout - time(NULL)) <= 0)
529 FD_SET(fd, &readfds);
532 if ((ret = select(fd + 1, &readfds, NULL, NULL, &tv)) < 0) {
534 syslog(LOG_ERR, "%s: getline: select: %m", username);
538 printd("getline: timed out\n");
541 if ((ret = read(fd, &buf[i], 1)) == 1) {
543 if (buf[i] == '\r' || buf[i] == '\0') {
547 if (buf[i] != '\n' && buf[i] != ':')
550 printd("Got %d: %s", i + 1, buf);
555 syslog(LOG_ERR, "%s: getline: read: %m", username);
557 syslog(LOG_ERR, "%s: read returned 0", username);
559 printd("returning %d after %d: %s\n", ret, i, buf);
572 if (ioctl(fd, TIOCMGET, &comstate) < 0) {
573 syslog(LOG_ERR, "%s: ioctl (%s, TIOCMGET): %m",
574 username, devicename);
577 return !!(comstate & TIOCM_CD);
587 if (uucp_lock && locked)
595 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
596 "usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-h] [-l]",
597 " [-L] [-A annexname] [-U upscript] [-D downscript]",
598 " [-t script_timeout] [-W maxtries] [-w retry_pause]",
599 " [-K keepalive] [-O outfill] [-S unit] device user passwd");