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