sliplogin.c revision 1.14 1 /* $NetBSD: sliplogin.c,v 1.14 1997/10/17 13:36:53 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: sliplogin.c,v 1.14 1997/10/17 13:36:53 lukem 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/types.h>
77 #include <sys/file.h>
78 #include <sys/param.h>
79 #include <sys/socket.h>
80 #include <sys/stat.h>
81 #include <sys/syslog.h>
82 #include <netdb.h>
83 #include <signal.h>
84 #include <stdlib.h>
85
86 #if BSD >= 199006
87 #define POSIX
88 #endif
89 #ifdef POSIX
90 #include <termios.h>
91 #include <sys/ioctl.h>
92 #include <ttyent.h>
93 #else
94 #include <sgtty.h>
95 #endif
96 #include <net/slip.h>
97
98 #include <ctype.h>
99 #include <err.h>
100 #include <errno.h>
101 #include <stdio.h>
102 #include <string.h>
103 #include <unistd.h>
104 #include "pathnames.h"
105
106 int unit;
107 int speed;
108 int uid;
109 char loginargs[BUFSIZ];
110 char loginfile[MAXPATHLEN];
111 char loginname[BUFSIZ];
112
113 void findid __P((char *));
114 void hup_handler __P((int));
115 int main __P((int, char **));
116 const char *sigstr __P((int));
117
118 void
119 findid(name)
120 char *name;
121 {
122 FILE *fp;
123 static char slopt[5][16];
124 static char laddr[16];
125 static char raddr[16];
126 static char mask[16];
127 char user[16];
128 int n;
129
130 (void)strncpy(loginname, name, sizeof(loginname) - 1);
131 if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
132 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
133 err(1, "%s", _PATH_ACCESS);
134 }
135 while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
136 if (ferror(fp))
137 break;
138 n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n",
139 user, laddr, raddr, mask, slopt[0], slopt[1],
140 slopt[2], slopt[3], slopt[4]);
141 if (user[0] == '#' || isspace(user[0]))
142 continue;
143 if (strcmp(user, name) != 0)
144 continue;
145
146 /*
147 * we've found the guy we're looking for -- see if
148 * there's a login file we can use. First check for
149 * one specific to this host. If none found, try for
150 * a generic one.
151 */
152 (void)snprintf(loginfile, sizeof loginfile, "%s.%s",
153 _PATH_LOGIN, name);
154 if (access(loginfile, R_OK|X_OK) != 0) {
155 (void)strncpy(loginfile, _PATH_LOGIN, sizeof(loginfile) - 1);
156 if (access(loginfile, R_OK|X_OK)) {
157 fputs("access denied - no login file\n",
158 stderr);
159 syslog(LOG_ERR,
160 "access denied for %s - no %s\n",
161 name, _PATH_LOGIN);
162 exit(5);
163 }
164 }
165
166 (void) fclose(fp);
167 return;
168 }
169 syslog(LOG_ERR, "SLIP access denied for %s\n", name);
170 errx(1, "SLIP access denied for %s", name);
171 /* NOTREACHED */
172 }
173
174 const char *
175 sigstr(s)
176 int s;
177 {
178 if (s > 0 && s < NSIG)
179 return(sys_signame[s]);
180 else {
181 static char buf[32];
182
183 (void)snprintf(buf, sizeof buf, "sig %d", s);
184 return(buf);
185 }
186 }
187
188 void
189 hup_handler(s)
190 int s;
191 {
192 char logoutfile[MAXPATHLEN];
193
194 (void)snprintf(logoutfile, sizeof logoutfile, "%s.%s", _PATH_LOGOUT,
195 loginname);
196 if (access(logoutfile, R_OK|X_OK) != 0)
197 (void)strncpy(logoutfile, _PATH_LOGOUT, sizeof(logoutfile) - 1);
198 if (access(logoutfile, R_OK|X_OK) == 0) {
199 char logincmd[2*MAXPATHLEN+32];
200
201 (void)snprintf(logincmd, sizeof logincmd, "%s %d %d %s",
202 logoutfile, unit, speed, loginargs);
203 (void) system(logincmd);
204 }
205 (void) close(0);
206 syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
207 sigstr(s));
208 exit(1);
209 /* NOTREACHED */
210 }
211
212 int
213 main(argc, argv)
214 int argc;
215 char *argv[];
216 {
217 int fd, s, ldisc, odisc;
218 char *name;
219 #ifdef POSIX
220 struct termios tios, otios;
221 #else
222 struct sgttyb tty, otty;
223 #endif
224 char logincmd[2*BUFSIZ+32];
225
226 if ((name = strrchr(argv[0], '/')) == NULL)
227 name = argv[0];
228 s = getdtablesize();
229 for (fd = 3 ; fd < s ; fd++)
230 (void) close(fd);
231 openlog(name, LOG_PID, LOG_DAEMON);
232 uid = getuid();
233 if (argc > 1) {
234 findid(argv[1]);
235
236 /*
237 * Disassociate from current controlling terminal, if any,
238 * and ensure that the slip line is our controlling terminal.
239 */
240 #ifdef POSIX
241 if (fork() > 0)
242 exit(0);
243 if (setsid() < 0)
244 perror("setsid");
245 #else
246 if ((fd = open("/dev/tty", O_RDONLY, 0)) >= 0) {
247 extern char *ttyname();
248
249 (void) ioctl(fd, TIOCNOTTY, (caddr_t)0);
250 (void) close(fd);
251 /* open slip tty again to acquire as controlling tty? */
252 fd = open(ttyname(0), O_RDWR, 0);
253 if (fd >= 0)
254 (void) close(fd);
255 }
256 (void) setpgrp(0, getpid());
257 #endif
258 if (argc > 2) {
259 if ((fd = open(argv[2], O_RDWR)) == -1) {
260 perror(argv[2]);
261 exit(2);
262 }
263 (void) dup2(fd, 0);
264 if (fd > 2)
265 close(fd);
266 }
267 #ifdef TIOCSCTTY
268 if (ioctl(STDIN_FILENO, TIOCSCTTY, (caddr_t)0) != 0)
269 perror("ioctl (TIOCSCTTY)");
270 #endif
271 } else {
272 if ((name = getlogin()) == NULL) {
273 syslog(LOG_ERR,
274 "access denied - getlogin returned 0\n");
275 errx(1, "access denied - no username");
276 }
277 findid(name);
278 }
279 if (!isatty(STDIN_FILENO)) {
280 syslog(LOG_ERR, "stdin not a tty");
281 errx(1, "stdin not a tty");
282 }
283 (void) fchmod(STDIN_FILENO, 0600);
284 warnx("starting slip login for %s", loginname);
285 #ifdef POSIX
286 /* set up the line parameters */
287 if (tcgetattr(STDIN_FILENO, &tios) < 0) {
288 syslog(LOG_ERR, "tcgetattr: %m");
289 exit(1);
290 }
291 otios = tios;
292 cfmakeraw(&tios);
293 tios.c_iflag &= ~IMAXBEL;
294 if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios) < 0) {
295 syslog(LOG_ERR, "tcsetattr: %m");
296 exit(1);
297 }
298 speed = cfgetispeed(&tios);
299 #else
300 /* set up the line parameters */
301 if (ioctl(STDIN_FILENO, TIOCGETP, (caddr_t)&tty) < 0) {
302 syslog(LOG_ERR, "ioctl (TIOCGETP): %m");
303 exit(1);
304 }
305 otty = tty;
306 speed = tty.sg_ispeed;
307 tty.sg_flags = RAW | ANYP;
308 if (ioctl(STDIN_FILENO, TIOCSETP, (caddr_t)&tty) < 0) {
309 syslog(LOG_ERR, "ioctl (TIOCSETP): %m");
310 exit(1);
311 }
312 #endif
313 /* find out what ldisc we started with */
314 if (ioctl(STDIN_FILENO, TIOCGETD, (caddr_t)&odisc) < 0) {
315 syslog(LOG_ERR, "ioctl(TIOCGETD) (1): %m");
316 exit(1);
317 }
318 ldisc = SLIPDISC;
319 if (ioctl(STDIN_FILENO, TIOCSETD, (caddr_t)&ldisc) < 0) {
320 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
321 exit(1);
322 }
323 /* find out what unit number we were assigned */
324 if (ioctl(STDIN_FILENO, SLIOCGUNIT, (caddr_t)&unit) < 0) {
325 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
326 exit(1);
327 }
328 (void) signal(SIGHUP, hup_handler);
329 (void) signal(SIGTERM, hup_handler);
330
331 syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
332 (void)snprintf(logincmd, sizeof logincmd, "%s %d %d %s", loginfile,
333 unit, speed, loginargs);
334 /*
335 * aim stdout and errout at /dev/null so logincmd output won't
336 * babble into the slip tty line.
337 */
338 (void) close(1);
339 if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
340 if (fd < 0) {
341 syslog(LOG_ERR, "open /dev/null: %m");
342 exit(1);
343 }
344 (void) dup2(fd, 1);
345 (void) close(fd);
346 }
347 (void) dup2(1, 2);
348
349 /*
350 * Run login and logout scripts as root (real and effective);
351 * current route(8) is setuid root, and checks the real uid
352 * to see whether changes are allowed (or just "route get").
353 */
354 (void) setuid(0);
355 if ((s = system(logincmd)) != NULL) {
356 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
357 loginname, s, loginfile);
358 (void) ioctl(STDIN_FILENO, TIOCSETD, (caddr_t)&odisc);
359 #ifdef POSIX
360 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &otios);
361 #else
362 (void) ioctl(STDIN_FILENO, TIOCSETP, (caddr_t)&otty);
363 #endif
364 exit(6);
365 }
366
367 /* twiddle thumbs until we get a signal */
368 while (1)
369 sigpause(0);
370
371 /* NOTREACHED */
372 }
373