sliplogin.c revision 1.3 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.3 1993/08/01 17:55:46 mycroft 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 "pathnames.h"
97
98 int unit;
99 int slip_mode;
100 int speed;
101 int uid;
102 char loginargs[BUFSIZ];
103 char loginfile[MAXPATHLEN];
104 char loginname[BUFSIZ];
105
106 struct slip_modes {
107 char *sm_name;
108 int sm_value;
109 } modes[] = {
110 "normal", 0,
111 "compress", SC_COMPRESS,
112 "noicmp", SC_NOICMP,
113 "autocomp", SC_AUTOCOMP
114 };
115
116 void
117 findid(name)
118 char *name;
119 {
120 FILE *fp;
121 static char slopt[5][16];
122 static char laddr[16];
123 static char raddr[16];
124 static char mask[16];
125 char user[16];
126 int i, j, n;
127
128 (void)strcpy(loginname, name);
129 if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
130 (void)fprintf(stderr, "sliplogin: %s: %s\n",
131 _PATH_ACCESS, strerror(errno));
132 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
133 exit(1);
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 slip_mode = 0;
147 for (i = 0; i < n - 4; i++) {
148 for (j = 0; j < sizeof(modes)/sizeof(struct slip_modes);
149 j++) {
150 if (strcmp(modes[j].sm_name, slopt[i]) == 0) {
151 slip_mode |= modes[j].sm_value;
152 break;
153 }
154 }
155 }
156
157 /*
158 * we've found the guy we're looking for -- see if
159 * there's a login file we can use. First check for
160 * one specific to this host. If none found, try for
161 * a generic one.
162 */
163 (void)sprintf(loginfile, "%s.%s", _PATH_LOGIN, name);
164 if (access(loginfile, R_OK|X_OK) != 0) {
165 (void)strcpy(loginfile, _PATH_LOGIN);
166 if (access(loginfile, R_OK|X_OK)) {
167 fputs("access denied - no login file\n",
168 stderr);
169 syslog(LOG_ERR,
170 "access denied for %s - no %s\n",
171 name, _PATH_LOGIN);
172 exit(5);
173 }
174 }
175
176 (void) fclose(fp);
177 return;
178 }
179 (void)fprintf(stderr, "SLIP access denied for %s\n", name);
180 syslog(LOG_ERR, "SLIP access denied for %s\n", name);
181 exit(4);
182 /* NOTREACHED */
183 }
184
185 char *
186 sigstr(s)
187 int s;
188 {
189 static char buf[32];
190
191 switch (s) {
192 case SIGHUP: return("HUP");
193 case SIGINT: return("INT");
194 case SIGQUIT: return("QUIT");
195 case SIGILL: return("ILL");
196 case SIGTRAP: return("TRAP");
197 case SIGIOT: return("IOT");
198 case SIGEMT: return("EMT");
199 case SIGFPE: return("FPE");
200 case SIGKILL: return("KILL");
201 case SIGBUS: return("BUS");
202 case SIGSEGV: return("SEGV");
203 case SIGSYS: return("SYS");
204 case SIGPIPE: return("PIPE");
205 case SIGALRM: return("ALRM");
206 case SIGTERM: return("TERM");
207 case SIGURG: return("URG");
208 case SIGSTOP: return("STOP");
209 case SIGTSTP: return("TSTP");
210 case SIGCONT: return("CONT");
211 case SIGCHLD: return("CHLD");
212 case SIGTTIN: return("TTIN");
213 case SIGTTOU: return("TTOU");
214 case SIGIO: return("IO");
215 case SIGXCPU: return("XCPU");
216 case SIGXFSZ: return("XFSZ");
217 case SIGVTALRM: return("VTALRM");
218 case SIGPROF: return("PROF");
219 case SIGWINCH: return("WINCH");
220 #ifdef SIGLOST
221 case SIGLOST: return("LOST");
222 #endif
223 case SIGUSR1: return("USR1");
224 case SIGUSR2: return("USR2");
225 }
226 (void)sprintf(buf, "sig %d", s);
227 return(buf);
228 }
229
230 void
231 hup_handler(s)
232 int s;
233 {
234 char logoutfile[MAXPATHLEN];
235
236 (void)sprintf(logoutfile, "%s.%s", _PATH_LOGOUT, loginname);
237 if (access(logoutfile, R_OK|X_OK) != 0)
238 (void)strcpy(logoutfile, _PATH_LOGOUT);
239 if (access(logoutfile, R_OK|X_OK) == 0) {
240 char logincmd[2*MAXPATHLEN+32];
241
242 (void) sprintf(logincmd, "%s %d %d %s", logoutfile, unit, speed,
243 loginargs);
244 (void) system(logincmd);
245 }
246 (void) close(0);
247 syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
248 sigstr(s));
249 exit(1);
250 /* NOTREACHED */
251 }
252
253 main(argc, argv)
254 int argc;
255 char *argv[];
256 {
257 int fd, s, ldisc, odisc;
258 char *name;
259 #ifdef POSIX
260 struct termios tios, otios;
261 #else
262 struct sgttyb tty, otty;
263 #endif
264 char logincmd[2*BUFSIZ+32];
265 extern uid_t getuid();
266
267 if ((name = strrchr(argv[0], '/')) == NULL)
268 name = argv[0];
269 s = getdtablesize();
270 for (fd = 3 ; fd < s ; fd++)
271 (void) close(fd);
272 openlog(name, LOG_PID, LOG_DAEMON);
273 uid = getuid();
274 if (argc > 1) {
275 findid(argv[1]);
276
277 /*
278 * Disassociate from current controlling terminal, if any,
279 * and ensure that the slip line is our controlling terminal.
280 */
281 #ifdef POSIX
282 if (fork() > 0)
283 exit(0);
284 if (setsid() < 0)
285 perror("setsid");
286 #else
287 if ((fd = open("/dev/tty", O_RDONLY, 0)) >= 0) {
288 extern char *ttyname();
289
290 (void) ioctl(fd, TIOCNOTTY, (caddr_t)0);
291 (void) close(fd);
292 /* open slip tty again to acquire as controlling tty? */
293 fd = open(ttyname(0), O_RDWR, 0);
294 if (fd >= 0)
295 (void) close(fd);
296 }
297 (void) setpgrp(0, getpid());
298 #endif
299 if (argc > 2) {
300 if ((fd = open(argv[2], O_RDWR)) == -1) {
301 perror(argv[2]);
302 exit(2);
303 }
304 (void) dup2(fd, 0);
305 if (fd > 2)
306 close(fd);
307 }
308 #ifdef TIOCSCTTY
309 if (ioctl(0, TIOCSCTTY, (caddr_t)0) != 0)
310 perror("ioctl (TIOCSCTTY)");
311 #endif
312 } else {
313 extern char *getlogin();
314
315 if ((name = getlogin()) == NULL) {
316 (void) fprintf(stderr, "access denied - no username\n");
317 syslog(LOG_ERR, "access denied - getlogin returned 0\n");
318 exit(1);
319 }
320 findid(name);
321 }
322 (void) fchmod(0, 0600);
323 (void) fprintf(stderr, "starting slip login for %s\n", loginname);
324 #ifdef POSIX
325 /* set up the line parameters */
326 if (tcgetattr(0, &tios) < 0) {
327 syslog(LOG_ERR, "tcgetattr: %m");
328 exit(1);
329 }
330 otios = tios;
331 cfmakeraw(&tios);
332 tios.c_iflag &= ~IMAXBEL;
333 if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
334 syslog(LOG_ERR, "tcsetattr: %m");
335 exit(1);
336 }
337 speed = cfgetispeed(&tios);
338 #else
339 /* set up the line parameters */
340 if (ioctl(0, TIOCGETP, (caddr_t)&tty) < 0) {
341 syslog(LOG_ERR, "ioctl (TIOCGETP): %m");
342 exit(1);
343 }
344 otty = tty;
345 speed = tty.sg_ispeed;
346 tty.sg_flags = RAW | ANYP;
347 if (ioctl(0, TIOCSETP, (caddr_t)&tty) < 0) {
348 syslog(LOG_ERR, "ioctl (TIOCSETP): %m");
349 exit(1);
350 }
351 #endif
352 /* find out what ldisc we started with */
353 if (ioctl(0, TIOCGETD, (caddr_t)&odisc) < 0) {
354 syslog(LOG_ERR, "ioctl(TIOCGETD) (1): %m");
355 exit(1);
356 }
357 ldisc = SLIPDISC;
358 if (ioctl(0, TIOCSETD, (caddr_t)&ldisc) < 0) {
359 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
360 exit(1);
361 }
362 /* find out what unit number we were assigned */
363 if (ioctl(0, SLIOCGUNIT, (caddr_t)&unit) < 0) {
364 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
365 exit(1);
366 }
367 (void) signal(SIGHUP, hup_handler);
368 (void) signal(SIGTERM, hup_handler);
369
370 syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
371 (void)sprintf(logincmd, "%s %d %d %s", loginfile, unit, speed,
372 loginargs);
373 /*
374 * aim stdout and errout at /dev/null so logincmd output won't
375 * babble into the slip tty line.
376 */
377 (void) close(1);
378 if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
379 if (fd < 0) {
380 syslog(LOG_ERR, "open /dev/null: %m");
381 exit(1);
382 }
383 (void) dup2(fd, 1);
384 (void) close(fd);
385 }
386 (void) dup2(1, 2);
387
388 /*
389 * Run login and logout scripts as root (real and effective);
390 * current route(8) is setuid root, and checks the real uid
391 * to see whether changes are allowed (or just "route get").
392 */
393 (void) setuid(0);
394 if (s = system(logincmd)) {
395 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
396 loginname, s, loginfile);
397 (void) ioctl(0, TIOCSETD, (caddr_t)&odisc);
398 exit(6);
399 }
400 if (ioctl(0, SLIOCSFLAGS, (caddr_t)&slip_mode) < 0) {
401 syslog(LOG_ERR, "ioctl (SLIOCSFLAGS): %m");
402 exit(1);
403 }
404
405 /* twiddle thumbs until we get a signal */
406 while (1)
407 sigpause(0);
408
409 /* NOTREACHED */
410 }
411