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
33 * @(#) Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)startslip.c 8.1 (Berkeley) 6/5/93
35 * $FreeBSD: src/sbin/startslip/startslip.c,v 1.31.2.1 2000/05/07 18:26:51 joe Exp $
36 * $DragonFly: src/sbin/startslip/startslip.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
39 #include <sys/types.h>
57 #define DEFAULT_BAUD B9600
58 int speed = DEFAULT_BAUD;
59 #define FC_NONE 0 /* flow control: none */
60 #define FC_HW 1 /* flow control: hardware (RTS/CTS) */
61 int flowcontrol = FC_NONE;
62 int modem_control = 1; /* !CLOCAL+HUPCL iff we watch carrier. */
64 int uucp_lock = 0; /* uucp locking */
69 int locked = 0; /* uucp lock active */
71 int wait_time = 60; /* then back off */
72 int script_timeout = 90; /* connect script default timeout */
73 time_t conn_time, start_time;
74 int MAXTRIES = 6; /* w/60 sec and doubling, takes an hour */
75 #define PIDFILE "%sstartslip.%s.pid"
78 char *dials[MAXDIALS];
83 char *dvname, *devicename;
90 #define syslog fprintf
91 #define LOG_ERR stderr
92 #define LOG_INFO stderr
96 #define printd if (debug) printf
98 int carrier __P((void));
100 int getline __P((char *, int, int, time_t));
101 static void usage __P((void));
110 void sighup(), sigterm(), sigurg();
112 char *dialerstring = 0, buf[BUFSIZ];
113 int unitnum, keepal = 0, outfill = 0;
116 char *upscript = NULL, *downscript = NULL;
117 int first = 1, tries = 0;
123 while ((ch = getopt(argc, argv, "dhlb:s:t:w:A:U:D:W:K:O:S:L")) != -1)
129 speed = atoi(optarg);
132 if (diali >= MAXDIALS)
133 errx(1, "max dial strings number (%d) exceeded", MAXDIALS);
134 dials[diali++] = strdup(optarg);
137 script_timeout = atoi(optarg);
140 wait_time = atoi(optarg);
143 MAXTRIES = atoi(optarg);
146 annex = strdup(optarg);
149 upscript = strdup(optarg);
152 downscript = strdup(optarg);
164 keepal = atoi(optarg);
167 outfill = atoi(optarg);
170 sl_unit = atoi(optarg);
183 * Copy these so they exist after we clobber them.
185 devicename = strdup(argv[0]);
186 username = strdup(argv[1]);
187 password = strdup(argv[2]);
190 * Security hack. Do not want private information such as the
191 * password and possible phone number to be left around.
192 * So we clobber the arguments.
194 for (ap = argv - optind + 1; ap < argv + 3; ap++)
195 for (cp = *ap; *cp != 0; cp++)
198 openlog("startslip", LOG_PID|LOG_PERROR, LOG_DAEMON);
201 setbuf(stdout, NULL);
203 signal(SIGTERM, sigterm);
204 if ((dvname = strrchr(devicename, '/')) == NULL)
208 if (snprintf(pidfile, sizeof(pidfile), PIDFILE, _PATH_VARRUN, dvname) >= sizeof(pidfile))
211 if ((pfd = fopen(pidfile, "r")) != NULL) {
212 if (fscanf(pfd, "%ld\n", &lpid) == 1) {
214 if (pid == lpid && pid > 0)
218 pfd = NULL; /* not remove pidfile yet */
219 sleep(5); /* allow down script to be completed */
222 signal(SIGHUP, SIG_IGN);
223 signal(SIGURG, SIG_IGN);
228 conn_time = time(NULL) - start_time;
235 } else if (fd >= 0) {
238 conn_time = time(NULL) - start_time;
246 syslog(LOG_INFO, "%s: connection time elapsed: %ld secs",
247 username, (long)conn_time);
248 sprintf(buf, "LINE=%d %s %s down",
249 diali ? (dialc - 1) % diali : 0,
250 downscript ? downscript : "/sbin/ifconfig" , unitname);
257 if (MAXTRIES > 0 && tries > MAXTRIES) {
258 syslog(LOG_ERR, "%s: exiting login after %d tries", username, tries);
265 syslog(LOG_INFO, "%s: sleeping %d seconds (%d tries)",
266 username, wait_time * (tries - 1), tries);
267 sleep(wait_time * (tries - 1));
272 if (daemon(1, debug) < 0) {
273 syslog(LOG_ERR, "%s: daemon: %m", username);
278 printd("restart: pid %ld: ", (long)pid);
279 if ((pfd = fopen(pidfile, "w")) != NULL) {
280 fprintf(pfd, "%ld\n", (long)pid);
286 if ((res = uu_lock(dvname)) != UU_LOCK_OK) {
287 if (res != UU_LOCK_INUSE)
288 syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res));
289 syslog(LOG_ERR, "%s: can't lock %s", username, devicename);
294 if ((fd = open(devicename, O_RDWR | O_NONBLOCK)) < 0) {
295 syslog(LOG_ERR, "%s: open %s: %m", username, devicename);
306 signal(SIGHUP, sighup);
307 if (ioctl(fd, TIOCSCTTY, 0) < 0) {
308 syslog(LOG_ERR, "%s: ioctl (TIOCSCTTY): %m", username);
311 if (tcsetpgrp(fd, getpid()) < 0) {
312 syslog(LOG_ERR, "%s: tcsetpgrp failed: %m", username);
316 if (tcgetattr(fd, &t) < 0) {
317 syslog(LOG_ERR, "%s: tcgetattr(%s): %m", username, devicename);
321 switch (flowcontrol) {
323 t.c_cflag |= (CRTS_IFLOW|CCTS_OFLOW);
326 t.c_cflag &= ~(CRTS_IFLOW|CCTS_OFLOW);
332 t.c_cflag &= ~(HUPCL);
333 t.c_cflag |= CLOCAL; /* until modem commands passes */
334 cfsetispeed(&t, speed);
335 cfsetospeed(&t, speed);
336 if (tcsetattr(fd, TCSAFLUSH, &t) < 0) {
337 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
340 sleep(2); /* wait for flakey line to settle */
341 if (hup || terminate)
344 wfd = fdopen(fd, "w+");
346 syslog(LOG_ERR, "%s: can't fdopen %s: %m", username, devicename);
352 dialerstring = dials[dialc++ % diali];
354 syslog(LOG_INFO, "%s: dialer string: %s\\r", username, dialerstring);
355 fprintf(wfd, "%s\r", dialerstring);
359 fintimeout = time(NULL) + script_timeout;
361 printd("waiting for carrier\n");
362 while (time(NULL) < fintimeout && !carrier()) {
364 if (hup || terminate)
369 t.c_cflag &= ~(CLOCAL);
370 if (tcsetattr(fd, TCSANOW, &t) < 0) {
371 syslog(LOG_ERR, "%s: tcsetattr(%s): %m", username, devicename);
374 /* Only now we able to receive HUP on carrier drop! */
380 printd("look for login: ");
382 if (getline(buf, BUFSIZ, fd, fintimeout) == 0 || hup || terminate)
385 if (bcmp(buf, annex, strlen(annex)) == 0) {
386 fprintf(wfd, "slip\r");
387 printd("Sent \"slip\"\n");
390 if (bcmp(&buf[1], "sername:", 8) == 0) {
391 fprintf(wfd, "%s\r", username);
392 printd("Sent login: %s\n", username);
395 if (bcmp(&buf[1], "assword:", 8) == 0) {
396 fprintf(wfd, "%s\r", password);
397 printd("Sent password: %s\n", password);
401 if (strstr(&buf[1], "ogin:") != NULL) {
402 fprintf(wfd, "%s\r", username);
403 printd("Sent login: %s\n", username);
406 if (strstr(&buf[1], "assword:") != NULL) {
407 fprintf(wfd, "%s\r", password);
408 printd("Sent password: %s\n", password);
414 sleep(5); /* Wait until login completed */
415 if (hup || terminate)
417 start_time = time(NULL);
423 if (ioctl(fd, TIOCSETD, &disc) < 0) {
424 syslog(LOG_ERR, "%s: ioctl (%s, TIOCSETD): %m",
425 username, devicename);
428 if (sl_unit >= 0 && ioctl(fd, SLIOCSUNIT, &sl_unit) < 0) {
429 syslog(LOG_ERR, "%s: ioctl(SLIOCSUNIT): %m", username);
432 if (ioctl(fd, SLIOCGUNIT, &unitnum) < 0) {
433 syslog(LOG_ERR, "%s: ioctl(SLIOCGUNIT): %m", username);
436 sprintf(unitname, "sl%d", unitnum);
439 signal(SIGURG, sigurg);
440 if (ioctl(fd, SLIOCSKEEPAL, &keepal) < 0) {
441 syslog(LOG_ERR, "%s: ioctl(SLIOCSKEEPAL): %m", username);
445 if (outfill > 0 && ioctl(fd, SLIOCSOUTFILL, &outfill) < 0) {
446 syslog(LOG_ERR, "%s: ioctl(SLIOCSOUTFILL): %m", username);
450 sprintf(buf, "LINE=%d %s %s up",
451 diali ? (dialc - 1) % diali : 0,
452 upscript ? upscript : "/sbin/ifconfig" , unitname);
457 syslog(LOG_INFO, "%s: reconnected on %s (%d tries)", username, unitname, tries);
459 syslog(LOG_INFO, "%s: connected on %s", username, unitname);
463 while (hup == 0 && terminate == 0) {
465 printd("sigpause return\n");
468 return(0); /* not reached */
476 if (hup == 0 && logged_in)
477 syslog(LOG_INFO, "%s: got hangup signal", username);
486 if (hup == 0 && logged_in)
487 syslog(LOG_INFO, "%s: got dead line signal", username);
495 printd("terminate\n");
496 if (terminate == 0 && logged_in)
497 syslog(LOG_INFO, "%s: got terminate signal", username);
502 getline(buf, size, fd, fintimeout)
514 for (i = 0; i < size; i++) {
515 if (hup || terminate)
517 if ((timeout = fintimeout - time(NULL)) <= 0)
520 FD_SET(fd, &readfds);
523 if ((ret = select(fd + 1, &readfds, NULL, NULL, &tv)) < 0) {
525 syslog(LOG_ERR, "%s: getline: select: %m", username);
529 printd("getline: timed out\n");
532 if ((ret = read(fd, &buf[i], 1)) == 1) {
534 if (buf[i] == '\r' || buf[i] == '\0') {
538 if (buf[i] != '\n' && buf[i] != ':')
541 printd("Got %d: %s", i + 1, buf);
546 syslog(LOG_ERR, "%s: getline: read: %m", username);
548 syslog(LOG_ERR, "%s: read returned 0", username);
550 printd("returning %d after %d: %s\n", ret, i, buf);
563 if (ioctl(fd, TIOCMGET, &comstate) < 0) {
564 syslog(LOG_ERR, "%s: ioctl (%s, TIOCMGET): %m",
565 username, devicename);
568 return !!(comstate & TIOCM_CD);
578 if (uucp_lock && locked)
586 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
587 "usage: startslip [-d] [-b speed] [-s string1 [-s string2 [...]]] [-h] [-l]",
588 " [-L] [-A annexname] [-U upscript] [-D downscript]",
589 " [-t script_timeout] [-W maxtries] [-w retry_pause]",
590 " [-K keepalive] [-O outfill] [-S unit] device user passwd");