xref: /dragonfly/usr.sbin/sliplogin/sliplogin.c (revision 984263bc)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  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 static char copyright[] =
36 "@(#) Copyright (c) 1990, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)sliplogin.c	8.2 (Berkeley) 2/1/94";
42 static char rscid[] = "@(#)$FreeBSD: src/usr.sbin/sliplogin/sliplogin.c,v 1.9.6.2 2001/07/19 05:21:28 kris 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/file.h>
74 #include <sys/stat.h>
75 #include <syslog.h>
76 #include <netdb.h>
77 
78 #include <termios.h>
79 #include <sys/ioctl.h>
80 #include <net/slip.h>
81 #include <net/if.h>
82 
83 #include <stdio.h>
84 #include <errno.h>
85 #include <ctype.h>
86 #include <paths.h>
87 #include <string.h>
88 #include <unistd.h>
89 #include <stdlib.h>
90 #include <signal.h>
91 #include "pathnames.h"
92 
93 extern char **environ;
94 
95 static char *restricted_environ[] = {
96 	"PATH=" _PATH_STDPATH,
97 	NULL
98 };
99 
100 int	unit;
101 int	slip_mode;
102 speed_t speed;
103 int	uid;
104 int     keepal;
105 int     outfill;
106 int     slunit;
107 char	loginargs[BUFSIZ];
108 char	loginfile[MAXPATHLEN];
109 char	loginname[BUFSIZ];
110 static char raddr[32];			/* remote address */
111 char ifname[IFNAMSIZ];          	/* interface name */
112 static 	char pidfilename[MAXPATHLEN];   /* name of pid file */
113 static 	char iffilename[MAXPATHLEN];    /* name of if file */
114 static	pid_t	pid;			/* our pid */
115 
116 char *
117 make_ipaddr(void)
118 {
119 static char address[20] ="";
120 struct hostent *he;
121 unsigned long ipaddr;
122 
123 address[0] = '\0';
124 if ((he = gethostbyname(raddr)) != NULL) {
125 	ipaddr = ntohl(*(long *)he->h_addr_list[0]);
126 	sprintf(address, "%lu.%lu.%lu.%lu",
127 		ipaddr >> 24,
128 		(ipaddr & 0x00ff0000) >> 16,
129 		(ipaddr & 0x0000ff00) >> 8,
130 		(ipaddr & 0x000000ff));
131 	}
132 
133 return address;
134 }
135 
136 struct slip_modes {
137 	char	*sm_name;
138 	int	sm_or_flag;
139 	int	sm_and_flag;
140 }	 modes[] = {
141 	"normal",	0        , 0        ,
142 	"compress",	IFF_LINK0, IFF_LINK2,
143 	"noicmp",	IFF_LINK1, 0        ,
144 	"autocomp",	IFF_LINK2, IFF_LINK0,
145 };
146 
147 void
148 findid(name)
149 	char *name;
150 {
151 	FILE *fp;
152 	static char slopt[5][16];
153 	static char laddr[16];
154 	static char mask[16];
155 	char   slparmsfile[MAXPATHLEN];
156 	char user[16];
157 	char buf[128];
158 	int i, j, n;
159 
160 	environ = restricted_environ; /* minimal protection for system() */
161 
162 	(void)strncpy(loginname, name, sizeof(loginname)-1);
163 	loginname[sizeof(loginname)-1] = '\0';
164 
165 	if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
166 	accfile_err:
167 		syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
168 		exit(1);
169 	}
170 	while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
171 		if (ferror(fp))
172 			goto accfile_err;
173 		if (loginargs[0] == '#' || isspace(loginargs[0]))
174 			continue;
175 		n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n",
176                         user, laddr, raddr, mask, slopt[0], slopt[1],
177 			slopt[2], slopt[3], slopt[4]);
178 		if (n < 4) {
179 			syslog(LOG_ERR, "%s: wrong format\n", _PATH_ACCESS);
180 			exit(1);
181 		}
182 		if (strcmp(user, name) != 0)
183 			continue;
184 
185 		(void) fclose(fp);
186 
187 		slip_mode = 0;
188 		for (i = 0; i < n - 4; i++) {
189 			for (j = 0; j < sizeof(modes)/sizeof(struct slip_modes);
190 				j++) {
191 				if (strcmp(modes[j].sm_name, slopt[i]) == 0) {
192 					slip_mode |= (modes[j].sm_or_flag);
193 					slip_mode &= ~(modes[j].sm_and_flag);
194 					break;
195 				}
196 			}
197 		}
198 
199 		/*
200 		 * we've found the guy we're looking for -- see if
201 		 * there's a login file we can use.  First check for
202 		 * one specific to this host.  If none found, try for
203 		 * a generic one.
204 		 */
205 		(void)snprintf(loginfile, sizeof(loginfile), "%s.%s", _PATH_LOGIN, name);
206 		if (access(loginfile, R_OK|X_OK) != 0) {
207 			(void)strncpy(loginfile, _PATH_LOGIN, sizeof(loginfile)-1);
208 			loginfile[sizeof(loginfile)-1] = '\0';
209 			if (access(loginfile, R_OK|X_OK)) {
210 				syslog(LOG_ERR,
211 				       "access denied for %s - no %s\n",
212 				       name, _PATH_LOGIN);
213 				exit(5);
214 			}
215 		}
216 		(void)snprintf(slparmsfile, sizeof(slparmsfile), "%s.%s", _PATH_SLPARMS, name);
217 		if (access(slparmsfile, R_OK|X_OK) != 0) {
218 			(void)strncpy(slparmsfile, _PATH_SLPARMS, sizeof(slparmsfile)-1);
219 			slparmsfile[sizeof(slparmsfile)-1] = '\0';
220 			if (access(slparmsfile, R_OK|X_OK))
221 				*slparmsfile = '\0';
222 		}
223 		keepal = outfill = 0;
224 		slunit = -1;
225 		if (*slparmsfile) {
226 			if ((fp = fopen(slparmsfile, "r")) == NULL) {
227 			slfile_err:
228 				syslog(LOG_ERR, "%s: %m\n", slparmsfile);
229 				exit(1);
230 			}
231 			n = 0;
232 			while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
233 				if (ferror(fp))
234 					goto slfile_err;
235 				if (buf[0] == '#' || isspace(buf[0]))
236 					continue;
237 				n = sscanf(buf, "%d %d %d", &keepal, &outfill, &slunit);
238 				if (n < 1) {
239 				slwrong_fmt:
240 					syslog(LOG_ERR, "%s: wrong format\n", slparmsfile);
241 					exit(1);
242 				}
243 				(void) fclose(fp);
244 				break;
245 			}
246 			if (n == 0)
247 				goto slwrong_fmt;
248 		}
249 
250 		return;
251 	}
252 	syslog(LOG_ERR, "SLIP access denied for %s\n", name);
253 	exit(4);
254 	/* NOTREACHED */
255 }
256 
257 char *
258 sigstr(s)
259 	int s;
260 {
261 	static char buf[32];
262 
263 	switch (s) {
264 	case SIGHUP:	return("HUP");
265 	case SIGINT:	return("INT");
266 	case SIGQUIT:	return("QUIT");
267 	case SIGILL:	return("ILL");
268 	case SIGTRAP:	return("TRAP");
269 	case SIGIOT:	return("IOT");
270 	case SIGEMT:	return("EMT");
271 	case SIGFPE:	return("FPE");
272 	case SIGKILL:	return("KILL");
273 	case SIGBUS:	return("BUS");
274 	case SIGSEGV:	return("SEGV");
275 	case SIGSYS:	return("SYS");
276 	case SIGPIPE:	return("PIPE");
277 	case SIGALRM:	return("ALRM");
278 	case SIGTERM:	return("TERM");
279 	case SIGURG:	return("URG");
280 	case SIGSTOP:	return("STOP");
281 	case SIGTSTP:	return("TSTP");
282 	case SIGCONT:	return("CONT");
283 	case SIGCHLD:	return("CHLD");
284 	case SIGTTIN:	return("TTIN");
285 	case SIGTTOU:	return("TTOU");
286 	case SIGIO:	return("IO");
287 	case SIGXCPU:	return("XCPU");
288 	case SIGXFSZ:	return("XFSZ");
289 	case SIGVTALRM:	return("VTALRM");
290 	case SIGPROF:	return("PROF");
291 	case SIGWINCH:	return("WINCH");
292 #ifdef SIGLOST
293 	case SIGLOST:	return("LOST");
294 #endif
295 	case SIGUSR1:	return("USR1");
296 	case SIGUSR2:	return("USR2");
297 	}
298 	(void)snprintf(buf, sizeof(buf), "sig %d", s);
299 	return(buf);
300 }
301 
302 void
303 hup_handler(s)
304 	int s;
305 {
306 	char logoutfile[MAXPATHLEN];
307 
308 	(void) close(0);
309 	seteuid(0);
310 	(void)snprintf(logoutfile, sizeof(logoutfile), "%s.%s", _PATH_LOGOUT, loginname);
311 	if (access(logoutfile, R_OK|X_OK) != 0) {
312 		(void)strncpy(logoutfile, _PATH_LOGOUT, sizeof(logoutfile)-1);
313 		logoutfile[sizeof(logoutfile)-1] = '\0';
314 	}
315 	if (access(logoutfile, R_OK|X_OK) == 0) {
316 		char logincmd[2*MAXPATHLEN+32];
317 
318 		(void) snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", logoutfile, unit, speed, loginargs);
319 		(void) system(logincmd);
320 	}
321 	syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
322 	       sigstr(s));
323 	if (unlink(pidfilename) < 0 && errno != ENOENT)
324 		syslog(LOG_WARNING, "unable to delete pid file: %m");
325 	if (unlink(iffilename) < 0 && errno != ENOENT)
326 		syslog(LOG_WARNING, "unable to delete if file: %m");
327 	exit(1);
328 	/* NOTREACHED */
329 }
330 
331 
332 /* Modify the slip line mode and add any compression or no-icmp flags. */
333 void line_flags(unit)
334 	int unit;
335 {
336 	struct ifreq ifr;
337 	int s;
338 
339 	/* open a socket as the handle to the interface */
340 	s = socket(AF_INET, SOCK_DGRAM, 0);
341 	if (s < 0) {
342 		syslog(LOG_ERR, "socket: %m");
343 		exit(1);
344 	}
345 	sprintf(ifr.ifr_name, "sl%d", unit);
346 
347 	/* get the flags for the interface */
348 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
349 		syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
350 		exit(1);
351         }
352 
353 	/* Assert any compression or no-icmp flags. */
354 #define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2))
355 	ifr.ifr_flags &= SLMASK;
356 	ifr.ifr_flags |= slip_mode;
357 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
358 		syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m");
359 		exit(1);
360 	}
361         close(s);
362 }
363 
364 
365 main(argc, argv)
366 	int argc;
367 	char *argv[];
368 {
369 	int fd, s, ldisc;
370 	char *name;
371 	struct termios tios, otios;
372 	char logincmd[2*BUFSIZ+32];
373 	extern uid_t getuid();
374 
375 	FILE *pidfile;				/* pid file */
376 	FILE *iffile;				/* interfaces file */
377 	char *p;
378 	int n;
379 	char devnam[MAXPATHLEN] = _PATH_TTY;   /* Device name */
380 
381 	if ((name = strrchr(argv[0], '/')) == NULL)
382 		name = argv[0];
383 	s = getdtablesize();
384 	for (fd = 3 ; fd < s ; fd++)
385 		(void) close(fd);
386 	openlog(name, LOG_PID|LOG_PERROR, LOG_DAEMON);
387 	uid = getuid();
388 	if (argc > 1) {
389 		findid(argv[1]);
390 
391 		/*
392 		 * Disassociate from current controlling terminal, if any,
393 		 * and ensure that the slip line is our controlling terminal.
394 		 */
395 		if (daemon(1, 1)) {
396 			syslog(LOG_ERR, "daemon(1, 1): %m");
397 			exit(1);
398 		}
399 		if (argc > 2) {
400 			if ((fd = open(argv[2], O_RDWR)) == -1) {
401 				syslog(LOG_ERR, "open %s: %m", argv[2]);
402 				exit(2);
403 			}
404 			(void) dup2(fd, 0);
405 			if (fd > 2)
406 				close(fd);
407 		}
408 		if (ioctl(0, TIOCSCTTY, 0) == -1) {
409 			syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m");
410 			exit(1);
411 		}
412 		if (tcsetpgrp(0, getpid()) < 0) {
413 			syslog(LOG_ERR, "tcsetpgrp failed: %m");
414 			exit(1);
415 		}
416 	} else {
417 		if ((name = getlogin()) == NULL) {
418 			syslog(LOG_ERR, "access denied - login name not found\n");
419 			exit(1);
420 		}
421 		findid(name);
422 	}
423 	(void) fchmod(0, 0600);
424 	(void) fprintf(stderr, "starting slip login for %s\n", loginname);
425         (void) fprintf(stderr, "your address is %s\n\n", make_ipaddr());
426 
427 	(void) fflush(stderr);
428 	sleep(1);
429 
430 	/* set up the line parameters */
431 	if (tcgetattr(0, &tios) < 0) {
432 		syslog(LOG_ERR, "tcgetattr: %m");
433 		exit(1);
434 	}
435 	otios = tios;
436 	cfmakeraw(&tios);
437 	if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
438 		syslog(LOG_ERR, "tcsetattr: %m");
439 		exit(1);
440 	}
441 	speed = cfgetispeed(&tios);
442 
443 	ldisc = SLIPDISC;
444 	if (ioctl(0, TIOCSETD, &ldisc) < 0) {
445 		syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
446 		exit(1);
447 	}
448 	if (slunit >= 0 && ioctl(0, SLIOCSUNIT, &slunit) < 0) {
449 		syslog(LOG_ERR, "ioctl (SLIOCSUNIT): %m");
450 		exit(1);
451 	}
452 	/* find out what unit number we were assigned */
453 	if (ioctl(0, SLIOCGUNIT, &unit) < 0) {
454 		syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
455 		exit(1);
456 	}
457 	(void) signal(SIGHUP, hup_handler);
458 	(void) signal(SIGTERM, hup_handler);
459 
460 	if (keepal > 0) {
461 		(void) signal(SIGURG, hup_handler);
462 		if (ioctl(0, SLIOCSKEEPAL, &keepal) < 0) {
463 			syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m");
464 			exit(1);
465 		}
466 	}
467 	if (outfill > 0 && ioctl(0, SLIOCSOUTFILL, &outfill) < 0) {
468 		syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m");
469 		exit(1);
470 	}
471 
472         /* write pid to file */
473 	pid = getpid();
474 	(void) sprintf(ifname, "sl%d", unit);
475 	(void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
476 	if ((pidfile = fopen(pidfilename, "w")) != NULL) {
477 		fprintf(pidfile, "%d\n", pid);
478 		(void) fclose(pidfile);
479 	} else {
480 		syslog(LOG_ERR, "Failed to create pid file %s: %m",
481 				pidfilename);
482 		pidfilename[0] = 0;
483 	}
484 
485         /* write interface unit number to file */
486 	p = ttyname(0);
487 	if (p)
488 		strcpy(devnam, p);
489 	for (n = strlen(devnam); n > 0; n--)
490 		if (devnam[n] == '/') {
491 			n++;
492 			break;
493 		}
494 	(void) sprintf(iffilename, "%s%s.if", _PATH_VARRUN, &devnam[n]);
495 	if ((iffile = fopen(iffilename, "w")) != NULL) {
496 		fprintf(iffile, "sl%d\n", unit);
497 		(void) fclose(iffile);
498 	} else {
499 		syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename);
500 		iffilename[0] = 0;
501 	}
502 
503 
504 	syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
505 	(void)snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", loginfile, unit, speed,
506 		      loginargs);
507 	/*
508 	 * aim stdout and errout at /dev/null so logincmd output won't
509 	 * babble into the slip tty line.
510 	 */
511 	(void) close(1);
512 	if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
513 		if (fd < 0) {
514 			syslog(LOG_ERR, "open %s: %m", _PATH_DEVNULL);
515 			exit(1);
516 		}
517 		(void) dup2(fd, 1);
518 		(void) close(fd);
519 	}
520 	(void) dup2(1, 2);
521 
522 	/*
523 	 * Run login and logout scripts as root (real and effective);
524 	 * current route(8) is setuid root, and checks the real uid
525 	 * to see whether changes are allowed (or just "route get").
526 	 */
527 	(void) setuid(0);
528 	if (s = system(logincmd)) {
529 		syslog(LOG_ERR, "%s login failed: exit status %d from %s",
530 		       loginname, s, loginfile);
531 		exit(6);
532 	}
533 
534 	/* Handle any compression or no-icmp flags. */
535 	line_flags(unit);
536 
537 	/* reset uid to users' to allow the user to give a signal. */
538 	seteuid(uid);
539 	/* twiddle thumbs until we get a signal */
540 	while (1)
541 		sigpause(0);
542 
543 	/* NOTREACHED */
544 }
545