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