sliplogin.c revision 1.6 1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)sliplogin.c 5.6 (Berkeley) 3/2/91";*/
42 static char rcsid[] = "$Id: sliplogin.c,v 1.6 1993/12/10 13:19:20 cgd Exp $";
43 #endif /* not lint */
44
45 /*
46 * sliplogin.c
47 * [MUST BE RUN SUID, SLOPEN DOES A SUSER()!]
48 *
49 * This program initializes its own tty port to be an async TCP/IP interface.
50 * It sets the line discipline to slip, invokes a shell script to initialize
51 * the network interface, then pauses forever waiting for hangup.
52 *
53 * It is a remote descendant of several similar programs with incestuous ties:
54 * - Kirk Smith's slipconf, modified by Richard Johnsson @ DEC WRL.
55 * - slattach, probably by Rick Adams but touched by countless hordes.
56 * - the original sliplogin for 4.2bsd, Doug Kingston the mover behind it.
57 *
58 * There are two forms of usage:
59 *
60 * "sliplogin"
61 * Invoked simply as "sliplogin", the program looks up the username
62 * in the file /etc/slip.hosts.
63 * If an entry is found, the line on fd0 is configured for SLIP operation
64 * as specified in the file.
65 *
66 * "sliplogin IPhostlogin </dev/ttyb"
67 * Invoked by root with a username, the name is looked up in the
68 * /etc/slip.hosts file and if found fd0 is configured as in case 1.
69 */
70
71 #include <sys/param.h>
72 #include <sys/socket.h>
73 #include <sys/signal.h>
74 #include <sys/file.h>
75 #include <sys/syslog.h>
76 #include <netdb.h>
77
78 #if BSD >= 199006
79 #define POSIX
80 #endif
81 #ifdef POSIX
82 #include <sys/termios.h>
83 #include <sys/ioctl.h>
84 #include <ttyent.h>
85 #else
86 #include <sgtty.h>
87 #endif
88 #include <netinet/in.h>
89 #include <net/if.h>
90 #include <net/if_slvar.h>
91
92 #include <stdio.h>
93 #include <errno.h>
94 #include <ctype.h>
95 #include <string.h>
96 #include <unistd.h>
97 #include "pathnames.h"
98
99 int unit;
100 int speed;
101 int uid;
102 char loginargs[BUFSIZ];
103 char loginfile[MAXPATHLEN];
104 char loginname[BUFSIZ];
105
106 void
107 findid(name)
108 char *name;
109 {
110 FILE *fp;
111 static char slopt[5][16];
112 static char laddr[16];
113 static char raddr[16];
114 static char mask[16];
115 char user[16];
116 int i, j, n;
117
118 (void)strcpy(loginname, name);
119 if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
120 (void)fprintf(stderr, "sliplogin: %s: %s\n",
121 _PATH_ACCESS, strerror(errno));
122 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
123 exit(1);
124 }
125 while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
126 if (ferror(fp))
127 break;
128 n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n",
129 user, laddr, raddr, mask, slopt[0], slopt[1],
130 slopt[2], slopt[3], slopt[4]);
131 if (user[0] == '#' || isspace(user[0]))
132 continue;
133 if (strcmp(user, name) != 0)
134 continue;
135
136 /*
137 * we've found the guy we're looking for -- see if
138 * there's a login file we can use. First check for
139 * one specific to this host. If none found, try for
140 * a generic one.
141 */
142 (void)sprintf(loginfile, "%s.%s", _PATH_LOGIN, name);
143 if (access(loginfile, R_OK|X_OK) != 0) {
144 (void)strcpy(loginfile, _PATH_LOGIN);
145 if (access(loginfile, R_OK|X_OK)) {
146 fputs("access denied - no login file\n",
147 stderr);
148 syslog(LOG_ERR,
149 "access denied for %s - no %s\n",
150 name, _PATH_LOGIN);
151 exit(5);
152 }
153 }
154
155 (void) fclose(fp);
156 return;
157 }
158 (void)fprintf(stderr, "SLIP access denied for %s\n", name);
159 syslog(LOG_ERR, "SLIP access denied for %s\n", name);
160 exit(4);
161 /* NOTREACHED */
162 }
163
164 const char *
165 sigstr(s)
166 int s;
167 {
168 if (s > 0 && s < NSIG)
169 return(sys_signame[s]);
170 else {
171 static char buf[32];
172 (void)sprintf(buf, "sig %d", s);
173 return(buf);
174 }
175 }
176
177 void
178 hup_handler(s)
179 int s;
180 {
181 char logoutfile[MAXPATHLEN];
182
183 (void)sprintf(logoutfile, "%s.%s", _PATH_LOGOUT, loginname);
184 if (access(logoutfile, R_OK|X_OK) != 0)
185 (void)strcpy(logoutfile, _PATH_LOGOUT);
186 if (access(logoutfile, R_OK|X_OK) == 0) {
187 char logincmd[2*MAXPATHLEN+32];
188
189 (void) sprintf(logincmd, "%s %d %d %s", logoutfile, unit, speed,
190 loginargs);
191 (void) system(logincmd);
192 }
193 (void) close(0);
194 syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
195 sigstr(s));
196 exit(1);
197 /* NOTREACHED */
198 }
199
200 main(argc, argv)
201 int argc;
202 char *argv[];
203 {
204 int fd, s, ldisc, odisc;
205 char *name;
206 #ifdef POSIX
207 struct termios tios, otios;
208 #else
209 struct sgttyb tty, otty;
210 #endif
211 char logincmd[2*BUFSIZ+32];
212 extern uid_t getuid();
213
214 if ((name = strrchr(argv[0], '/')) == NULL)
215 name = argv[0];
216 s = getdtablesize();
217 for (fd = 3 ; fd < s ; fd++)
218 (void) close(fd);
219 openlog(name, LOG_PID, LOG_DAEMON);
220 uid = getuid();
221 if (argc > 1) {
222 findid(argv[1]);
223
224 /*
225 * Disassociate from current controlling terminal, if any,
226 * and ensure that the slip line is our controlling terminal.
227 */
228 #ifdef POSIX
229 if (fork() > 0)
230 exit(0);
231 if (setsid() < 0)
232 perror("setsid");
233 #else
234 if ((fd = open("/dev/tty", O_RDONLY, 0)) >= 0) {
235 extern char *ttyname();
236
237 (void) ioctl(fd, TIOCNOTTY, (caddr_t)0);
238 (void) close(fd);
239 /* open slip tty again to acquire as controlling tty? */
240 fd = open(ttyname(0), O_RDWR, 0);
241 if (fd >= 0)
242 (void) close(fd);
243 }
244 (void) setpgrp(0, getpid());
245 #endif
246 if (argc > 2) {
247 if ((fd = open(argv[2], O_RDWR)) == -1) {
248 perror(argv[2]);
249 exit(2);
250 }
251 (void) dup2(fd, 0);
252 if (fd > 2)
253 close(fd);
254 }
255 #ifdef TIOCSCTTY
256 if (ioctl(0, TIOCSCTTY, (caddr_t)0) != 0)
257 perror("ioctl (TIOCSCTTY)");
258 #endif
259 } else {
260 extern char *getlogin();
261
262 if ((name = getlogin()) == NULL) {
263 (void) fprintf(stderr, "access denied - no username\n");
264 syslog(LOG_ERR, "access denied - getlogin returned 0\n");
265 exit(1);
266 }
267 findid(name);
268 }
269 (void) fchmod(0, 0600);
270 (void) fprintf(stderr, "starting slip login for %s\n", loginname);
271 #ifdef POSIX
272 /* set up the line parameters */
273 if (tcgetattr(0, &tios) < 0) {
274 syslog(LOG_ERR, "tcgetattr: %m");
275 exit(1);
276 }
277 otios = tios;
278 cfmakeraw(&tios);
279 tios.c_iflag &= ~IMAXBEL;
280 if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
281 syslog(LOG_ERR, "tcsetattr: %m");
282 exit(1);
283 }
284 speed = cfgetispeed(&tios);
285 #else
286 /* set up the line parameters */
287 if (ioctl(0, TIOCGETP, (caddr_t)&tty) < 0) {
288 syslog(LOG_ERR, "ioctl (TIOCGETP): %m");
289 exit(1);
290 }
291 otty = tty;
292 speed = tty.sg_ispeed;
293 tty.sg_flags = RAW | ANYP;
294 if (ioctl(0, TIOCSETP, (caddr_t)&tty) < 0) {
295 syslog(LOG_ERR, "ioctl (TIOCSETP): %m");
296 exit(1);
297 }
298 #endif
299 /* find out what ldisc we started with */
300 if (ioctl(0, TIOCGETD, (caddr_t)&odisc) < 0) {
301 syslog(LOG_ERR, "ioctl(TIOCGETD) (1): %m");
302 exit(1);
303 }
304 ldisc = SLIPDISC;
305 if (ioctl(0, TIOCSETD, (caddr_t)&ldisc) < 0) {
306 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
307 exit(1);
308 }
309 /* find out what unit number we were assigned */
310 if (ioctl(0, SLIOCGUNIT, (caddr_t)&unit) < 0) {
311 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
312 exit(1);
313 }
314 (void) signal(SIGHUP, hup_handler);
315 (void) signal(SIGTERM, hup_handler);
316
317 syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
318 (void)sprintf(logincmd, "%s %d %d %s", loginfile, unit, speed,
319 loginargs);
320 /*
321 * aim stdout and errout at /dev/null so logincmd output won't
322 * babble into the slip tty line.
323 */
324 (void) close(1);
325 if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
326 if (fd < 0) {
327 syslog(LOG_ERR, "open /dev/null: %m");
328 exit(1);
329 }
330 (void) dup2(fd, 1);
331 (void) close(fd);
332 }
333 (void) dup2(1, 2);
334
335 /*
336 * Run login and logout scripts as root (real and effective);
337 * current route(8) is setuid root, and checks the real uid
338 * to see whether changes are allowed (or just "route get").
339 */
340 (void) setuid(0);
341 if (s = system(logincmd)) {
342 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
343 loginname, s, loginfile);
344 (void) ioctl(0, TIOCSETD, (caddr_t)&odisc);
345 exit(6);
346 }
347
348 /* twiddle thumbs until we get a signal */
349 while (1)
350 sigpause(0);
351
352 /* NOTREACHED */
353 }
354