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