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