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