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