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