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