Commit | Line | Data |
---|---|---|
61ba62c5 KM |
1 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * %sccs.include.redist.c% | |
6 | */ | |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
10 | "@(#) Copyright (c) 1990 The Regents of the University of California.\n\ | |
11 | All rights reserved.\n"; | |
12 | #endif /* not lint */ | |
13 | ||
14 | #ifndef lint | |
dc20c96e | 15 | static char sccsid[] = "@(#)startslip.c 5.3 (Berkeley) %G%"; |
61ba62c5 KM |
16 | #endif /* not lint */ |
17 | ||
18 | #include <sys/param.h> | |
9bdb4436 | 19 | #include <sgtty.h> |
61ba62c5 KM |
20 | #include <sys/socket.h> |
21 | #include <sys/syslog.h> | |
22 | #include <netinet/in.h> | |
23 | #include <net/if.h> | |
24 | #include <net/if_slvar.h> | |
61ba62c5 KM |
25 | #include <netdb.h> |
26 | #include <errno.h> | |
27 | #include <fcntl.h> | |
28 | #include <stdio.h> | |
9bdb4436 | 29 | #include <signal.h> |
61ba62c5 KM |
30 | |
31 | #define DEFAULT_BAUD B9600 | |
32 | int speed = DEFAULT_BAUD; | |
9bdb4436 | 33 | int hup; |
61ba62c5 KM |
34 | #ifdef DEBUG |
35 | int debug = 1; | |
36 | #undef LOG_ERR | |
37 | #undef LOG_INFO | |
38 | #define syslog fprintf | |
39 | #define LOG_ERR stderr | |
40 | #define LOG_INFO stderr | |
41 | #else | |
42 | int debug = 0; | |
43 | #endif | |
44 | #define printd if (debug) printf | |
45 | ||
46 | main(argc, argv) | |
47 | int argc; | |
48 | char **argv; | |
49 | { | |
50 | extern char *optarg; | |
51 | extern int optind; | |
9bdb4436 | 52 | int ch, disc; |
61ba62c5 KM |
53 | int fd = -1, sighup(); |
54 | FILE *wfd; | |
55 | char *dialerstring = 0, buf[BUFSIZ]; | |
56 | struct sgttyb sgtty; | |
57 | int first = 0; | |
58 | ||
59 | while ((ch = getopt(argc, argv, "ds:")) != EOF) | |
60 | switch(ch) { | |
61 | case 'd': | |
62 | debug = 1; | |
63 | break; | |
64 | case 's': | |
65 | dialerstring = optarg; | |
66 | break; | |
67 | case '?': | |
68 | default: | |
69 | usage(); | |
70 | } | |
71 | argc -= optind; | |
72 | argv += optind; | |
73 | ||
74 | if (argc != 3) | |
75 | usage(); | |
76 | ||
77 | openlog("startslip", LOG_PID, LOG_DAEMON); | |
78 | ||
79 | #if BSD <= 43 | |
9bdb4436 | 80 | if (debug == 0 && (fd = open("/dev/tty", 0)) >= 0) { |
61ba62c5 KM |
81 | ioctl(fd, TIOCNOTTY, 0); |
82 | close(fd); | |
83 | } | |
84 | #endif | |
85 | ||
86 | signal(SIGHUP, sighup); | |
87 | restart: | |
9bdb4436 | 88 | hup = 0; |
61ba62c5 KM |
89 | printd("restart\n"); |
90 | if (fd >= 0) | |
91 | close(fd); | |
dc20c96e KM |
92 | if (!first) { |
93 | if (fork() > 0) | |
94 | exit(0); | |
95 | printd("(pid %d)\n", getpid()); | |
96 | #ifdef TIOCSCTTY | |
97 | if (setsid() == -1) | |
98 | perror("setsid"); | |
99 | #endif | |
100 | } else | |
61ba62c5 KM |
101 | sleep(2); |
102 | if ((fd = open(argv[0], O_RDWR)) < 0) { | |
103 | perror(argv[0]); | |
104 | syslog(LOG_ERR, "startslip: open %s: %m\n", argv[0]); | |
105 | if (first) | |
106 | exit(1); | |
107 | else { | |
108 | sleep(5*60); | |
109 | goto restart; | |
110 | } | |
111 | } | |
112 | printd("open %d\n", fd); | |
dc20c96e KM |
113 | #ifdef TIOCSCTTY |
114 | if (ioctl(fd, TIOCSCTTY, 0) < 0) | |
115 | perror("ioctl (TIOCSCTTY)"); | |
116 | #endif | |
9bdb4436 KM |
117 | if (debug) { |
118 | if (ioctl(fd, TIOCGETD, &disc) < 0) | |
119 | perror("ioctl(TIOCSETD)"); | |
120 | printf("disc was %d\n", disc); | |
121 | } | |
122 | disc = TTYDISC; | |
123 | if (ioctl(fd, TIOCSETD, &disc) < 0) { | |
124 | perror("ioctl(TIOCSETD)"); | |
125 | syslog(LOG_ERR, "startslip: %s: ioctl (TIOCSETD 0): %m\n", | |
126 | argv[0]); | |
127 | } | |
61ba62c5 KM |
128 | if (dialerstring) { |
129 | (void) write(fd, dialerstring, strlen(dialerstring)); | |
130 | (void) write(fd, "\n", 1); | |
131 | } | |
132 | if (ioctl(fd, TIOCGETP, &sgtty) < 0) { | |
133 | perror("ioctl (TIOCGETP)"); | |
134 | exit(2); | |
135 | } | |
136 | sgtty.sg_flags = RAW | ANYP; | |
137 | sgtty.sg_erase = sgtty.sg_kill = 0377; | |
138 | sgtty.sg_ispeed = sgtty.sg_ospeed = speed; | |
139 | if (ioctl(fd, TIOCSETP, &sgtty) < 0) { | |
140 | perror("ioctl (TIOCSETP)"); | |
141 | syslog(LOG_ERR, "startslip: %s: ioctl (TIOCSETP): %m\n", | |
142 | argv[0]); | |
143 | exit(2); | |
144 | } | |
145 | printd("ioctl\n"); | |
146 | /* | |
147 | * Log in | |
148 | */ | |
9bdb4436 | 149 | wfd = fdopen(fd, "w+"); |
61ba62c5 KM |
150 | printd("fdopen\n"); |
151 | if (wfd == NULL) { | |
152 | syslog(LOG_ERR, "startslip: can't fdopen slip line\n"); | |
153 | exit(10); | |
154 | } | |
155 | putc('\n', wfd); | |
156 | while (fflush(wfd), getline(buf, BUFSIZ, fd) != NULL) { | |
9bdb4436 | 157 | if (hup != 0) |
dc20c96e | 158 | goto cleanup; |
61ba62c5 KM |
159 | if (bcmp(&buf[1], "ogin:", 5) == 0) { |
160 | fprintf(wfd, "%s\r", argv[1]); | |
161 | continue; | |
162 | } | |
163 | if (bcmp(&buf[1], "assword:", 8) == 0) { | |
164 | fprintf(wfd, "%s\r", argv[2]); | |
165 | fflush(wfd); | |
166 | break; | |
167 | } | |
168 | } | |
169 | printd("login\n"); | |
170 | /* | |
171 | * Attach | |
172 | */ | |
9bdb4436 KM |
173 | disc = SLIPDISC; |
174 | if (ioctl(fd, TIOCSETD, &disc) < 0) { | |
61ba62c5 | 175 | perror("ioctl(TIOCSETD)"); |
9bdb4436 | 176 | syslog(LOG_ERR, "startslip: %s: ioctl (TIOCSETD SLIP): %m\n", |
61ba62c5 KM |
177 | argv[0]); |
178 | exit(1); | |
179 | } | |
9bdb4436 KM |
180 | printd("setd\n"); |
181 | disc = SC_COMPRESS; | |
182 | if (ioctl(fd, SLIOCSFLAGS, (caddr_t)&disc) < 0) { | |
61ba62c5 KM |
183 | perror("ioctl(SLIOCFLAGS)"); |
184 | syslog(LOG_ERR, "ioctl (SLIOCSFLAGS): %m"); | |
185 | exit(1); | |
186 | } | |
dc20c96e KM |
187 | if (!first++ && debug == 0) { |
188 | close(0); | |
189 | close(1); | |
190 | close(2); | |
191 | (void) open("/dev/null", O_RDWR); | |
192 | (void) dup2(0, 1); | |
193 | (void) dup2(0, 2); | |
61ba62c5 KM |
194 | } |
195 | (void) system("ifconfig sl0 up"); | |
dc20c96e | 196 | while (hup == 0) { |
9bdb4436 | 197 | sigpause(0L); |
dc20c96e KM |
198 | printd("sigpause return "); |
199 | } | |
200 | cleanup: | |
201 | printd("close\n"); | |
61ba62c5 | 202 | fclose(wfd); |
dc20c96e | 203 | close(fd); |
9bdb4436 KM |
204 | #ifdef TIOCSCTTY |
205 | if (fork() > 0) | |
206 | exit(0); | |
dc20c96e | 207 | printd("(pid %d)\n", getpid()); |
9bdb4436 KM |
208 | if (setsid() == -1) |
209 | perror("setsid"); | |
210 | sleep(5); | |
211 | #endif | |
61ba62c5 KM |
212 | goto restart; |
213 | } | |
214 | ||
215 | sighup() | |
216 | { | |
217 | ||
9bdb4436 | 218 | printd("hup\n"); |
dc20c96e KM |
219 | if (hup == 0) |
220 | syslog(LOG_INFO, "startslip: hangup signal\n"); | |
9bdb4436 | 221 | hup = 1; |
61ba62c5 KM |
222 | } |
223 | ||
224 | getline(buf, size, fd) | |
225 | char *buf; | |
226 | int size, fd; | |
227 | { | |
228 | register int i; | |
dc20c96e | 229 | int ret; |
61ba62c5 KM |
230 | |
231 | size--; | |
232 | for (i = 0; i < size; i++) { | |
9bdb4436 KM |
233 | if (hup) |
234 | return (0); | |
dc20c96e | 235 | if ((ret = read(fd, &buf[i], 1)) == 1) { |
61ba62c5 KM |
236 | buf[i] &= 0177; |
237 | if (buf[i] == '\r') | |
238 | buf[i] = '\n'; | |
239 | if (buf[i] != '\n' && buf[i] != ':') | |
240 | continue; | |
241 | buf[i + 1] = '\0'; | |
242 | if (debug) | |
9bdb4436 | 243 | fprintf(stderr, "Got %d: \"%s\"\n", i + 1, buf); |
61ba62c5 KM |
244 | return (i+1); |
245 | } | |
dc20c96e KM |
246 | if (ret < 0) { |
247 | perror("getline: read (sleeping)"); | |
248 | buf[i] = '\0'; | |
249 | sleep(60); | |
250 | printd("returning 0 after %d: \"%s\"\n", i, buf); | |
251 | return (0); | |
252 | } | |
61ba62c5 | 253 | } |
9bdb4436 | 254 | return (0); |
61ba62c5 KM |
255 | } |
256 | ||
257 | usage() | |
258 | { | |
259 | fprintf(stderr, "usage: startslip [-d] [-s string] dev user passwd\n"); | |
260 | exit(1); | |
261 | } |