Home | History | Annotate | Line # | Download | only in rlogind
rlogind.c revision 1.20
      1 /*	$NetBSD: rlogind.c,v 1.20 2000/01/31 14:20:13 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1998 WIDE Project.
      5  * 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 WIDE Project and
     18  *    its contributors.
     19  * 4. Neither the name of the project 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 PROJECT 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 PROJECT 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 /*-
     37  * Copyright (c) 1983, 1988, 1989, 1993
     38  *	The Regents of the University of California.  All rights reserved.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the University of
     51  *	California, Berkeley and its contributors.
     52  * 4. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 #ifndef lint
     71 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
     72 	The Regents of the University of California.  All rights reserved.\n");
     73 #if 0
     74 static char sccsid[] = "@(#)rlogind.c	8.2 (Berkeley) 4/28/95";
     75 #else
     76 __RCSID("$NetBSD: rlogind.c,v 1.20 2000/01/31 14:20:13 itojun Exp $");
     77 #endif
     78 #endif /* not lint */
     79 
     80 /*
     81  * remote login server:
     82  *	\0
     83  *	remuser\0
     84  *	locuser\0
     85  *	terminal_type/speed\0
     86  *	data
     87  */
     88 
     89 #define	FD_SETSIZE	16		/* don't need many bits for select */
     90 #include <sys/param.h>
     91 #include <sys/stat.h>
     92 #include <sys/ioctl.h>
     93 #include <signal.h>
     94 #include <termios.h>
     95 
     96 #include <sys/socket.h>
     97 #include <netinet/in.h>
     98 #include <netinet/in_systm.h>
     99 #include <netinet/ip.h>
    100 #include <arpa/inet.h>
    101 #include <netdb.h>
    102 
    103 #include <pwd.h>
    104 #include <syslog.h>
    105 #include <errno.h>
    106 #include <stdio.h>
    107 #include <unistd.h>
    108 #include <stdlib.h>
    109 #include <string.h>
    110 #include <util.h>
    111 #include <utmp.h>
    112 #include "pathnames.h"
    113 
    114 #ifndef TIOCPKT_WINDOW
    115 #define TIOCPKT_WINDOW 0x80
    116 #endif
    117 
    118 #define		OPTIONS			"alnL"
    119 
    120 char	*env[2];
    121 #define	NMAX 30
    122 char	lusername[NMAX+1], rusername[NMAX+1];
    123 static	char term[64] = "TERM=";
    124 #define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
    125 int	keepalive = 1;
    126 int	check_all = 0;
    127 int	log_success = 0;
    128 
    129 struct	passwd *pwd;
    130 
    131 void	doit __P((int, struct sockaddr *));
    132 int	control __P((int, char *, int));
    133 void	protocol __P((int, int));
    134 void	cleanup __P((int));
    135 void	fatal __P((int, char *, int));
    136 int	do_rlogin __P((struct sockaddr *, char *));
    137 void	getstr __P((char *, int, char *));
    138 void	setup_term __P((int));
    139 #if 0
    140 int	do_krb_login __P((union sockunion *));
    141 #endif
    142 void	usage __P((void));
    143 int	local_domain __P((char *));
    144 char	*topdomain __P((char *));
    145 int	main __P((int, char *[]));
    146 
    147 int
    148 main(argc, argv)
    149 	int argc;
    150 	char *argv[];
    151 {
    152 	extern int __check_rhosts_file;
    153 	struct sockaddr_storage from;
    154 	int ch, fromlen, on;
    155 
    156 	openlog("rlogind", LOG_PID, LOG_AUTH);
    157 
    158 	opterr = 0;
    159 	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
    160 		switch (ch) {
    161 		case 'a':
    162 			check_all = 1;
    163 			break;
    164 		case 'l':
    165 			__check_rhosts_file = 0;
    166 			break;
    167 		case 'n':
    168 			keepalive = 0;
    169 			break;
    170 		case 'L':
    171 			log_success = 1;
    172 			break;
    173 		case '?':
    174 		default:
    175 			usage();
    176 			break;
    177 		}
    178 	argc -= optind;
    179 	argv += optind;
    180 
    181 	fromlen = sizeof (from); /* xxx */
    182 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
    183 		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
    184 		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
    185 	}
    186 	on = 1;
    187 	if (keepalive &&
    188 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
    189 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
    190 #if defined(IP_TOS)
    191 	if (((struct sockaddr *)&from)->sa_family == AF_INET) {
    192 		on = IPTOS_LOWDELAY;
    193 		if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
    194 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
    195 	}
    196 #endif
    197 	doit(0, (struct sockaddr *)&from);
    198 	/* NOTREACHED */
    199 #ifdef __GNUC__
    200 	exit(0);
    201 #endif
    202 }
    203 
    204 int	child;
    205 int	netf;
    206 char	line[MAXPATHLEN];
    207 int	confirmed;
    208 
    209 struct winsize win = { 0, 0, 0, 0 };
    210 
    211 
    212 void
    213 doit(f, fromp)
    214 	int f;
    215 	struct sockaddr *fromp;
    216 {
    217 	int master, pid, on = 1;
    218 	int authenticated = 0;
    219 	char utmphost[UT_HOSTSIZE + 1];
    220 	char *hostname;
    221 	char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
    222 	char c;
    223 	char naddr[NI_MAXHOST];
    224 	char saddr[NI_MAXHOST];
    225 	char raddr[NI_MAXHOST];
    226 	int af = fromp->sa_family;
    227 	u_int16_t *portp;
    228 	struct addrinfo hints, *res, *res0;
    229 	int gaierror;
    230 #ifdef NI_WITHSCOPEID
    231 	const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID;
    232 #else
    233 	const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
    234 #endif
    235 
    236 	alarm(60);
    237 	read(f, &c, 1);
    238 
    239 	if (c != 0)
    240 		exit(1);
    241 
    242 	alarm(0);
    243 	switch (af) {
    244 	case AF_INET:
    245 		portp = &((struct sockaddr_in *)fromp)->sin_port;
    246 		break;
    247 #ifdef INET6
    248 	case AF_INET6:
    249 		portp = &((struct sockaddr_in6 *)fromp)->sin6_port;
    250 		break;
    251 #endif
    252 	default:
    253 		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
    254 		exit(1);
    255 	}
    256 	if (getnameinfo((struct sockaddr *)fromp, fromp->sa_len,
    257 		    naddr, sizeof(naddr), NULL, 0, niflags) != 0) {
    258 		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
    259 		exit(1);
    260 	}
    261 
    262 	if (getnameinfo((struct sockaddr *)fromp, fromp->sa_len,
    263 		    saddr, sizeof(saddr), NULL, 0, NI_NAMEREQD) == 0) {
    264 		/*
    265 		 * If name returned by gethostbyaddr is in our domain,
    266 		 * attempt to verify that we haven't been fooled by someone
    267 		 * in a remote net; look up the name and check that this
    268 		 * address corresponds to the name.
    269 		 */
    270 		hostname = saddr;
    271 		if (check_all || local_domain(saddr)) {
    272 			strncpy(hostnamebuf, saddr, sizeof(hostnamebuf) - 1);
    273 			hostnamebuf[sizeof(hostnamebuf) - 1] = 0;
    274 			memset(&hints, 0, sizeof(hints));
    275 			hints.ai_family = fromp->sa_family;
    276 			hints.ai_socktype = SOCK_STREAM;
    277 			hints.ai_flags = AI_CANONNAME;
    278 			gaierror = getaddrinfo(hostnamebuf, "0", &hints, &res0);
    279 			if (gaierror) {
    280 				syslog(LOG_INFO,
    281 				    "Couldn't look up address for %s: %s",
    282 				    hostnamebuf, gai_strerror(gaierror));
    283 				hostname = naddr;
    284 			} else {
    285 				for (res = res0; res; res = res->ai_next) {
    286 					if (res->ai_family != fromp->sa_family)
    287 						continue;
    288 					if (res->ai_addrlen != fromp->sa_len)
    289 						continue;
    290 					if (getnameinfo(res->ai_addr,
    291 						res->ai_addrlen,
    292 						raddr, sizeof(raddr), NULL, 0,
    293 						niflags) == 0
    294 					 && strcmp(naddr, raddr) == 0) {
    295 						hostname = res->ai_canonname
    296 							? res->ai_canonname
    297 							: saddr;
    298 						break;
    299 					}
    300 				}
    301 				if (res == NULL) {
    302 					syslog(LOG_NOTICE,
    303 					  "Host addr %s not listed for host %s",
    304 					    naddr, res0->ai_canonname
    305 						    ? res0->ai_canonname
    306 						    : saddr);
    307 					hostname = naddr;
    308 				}
    309 				freeaddrinfo(res0);
    310 			}
    311 		}
    312 		hostname = strncpy(hostnamebuf, hostname,
    313 				   sizeof(hostnamebuf) - 1);
    314 	} else
    315 		hostname = strncpy(hostnamebuf, naddr,
    316 				   sizeof(hostnamebuf) - 1);
    317 
    318 	hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
    319 
    320 	if (strlen(hostname) < sizeof(utmphost))
    321 		(void)strcpy(utmphost, hostname);
    322 	else
    323 		(void)strncpy(utmphost, hostname, sizeof(utmphost));
    324 	utmphost[sizeof(utmphost) - 1] = '\0';
    325 
    326 	if (ntohs(*portp) >= IPPORT_RESERVED ||
    327 	    ntohs(*portp) < IPPORT_RESERVED/2) {
    328 		syslog(LOG_NOTICE, "Connection from %s on illegal port",
    329 		       naddr);
    330 		fatal(f, "Permission denied", 0);
    331 	}
    332 #ifdef IP_OPTIONS
    333 	if (fromp->sa_family == AF_INET) {
    334 		u_char optbuf[BUFSIZ/3], *cp;
    335 		char lbuf[BUFSIZ], *lp;
    336 		int optsize = sizeof(optbuf), ipproto;
    337 		struct protoent *ip;
    338 
    339 		if ((ip = getprotobyname("ip")) != NULL)
    340 			ipproto = ip->p_proto;
    341 		else
    342 			ipproto = IPPROTO_IP;
    343 		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
    344 		    &optsize) == 0 && optsize != 0) {
    345 			lp = lbuf;
    346 			for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
    347 				sprintf(lp, " %2.2x", *cp);
    348 			syslog(LOG_NOTICE,
    349 			    "Connection received using IP options (ignored):%s",
    350 			    lbuf);
    351 			if (setsockopt(0, ipproto, IP_OPTIONS,
    352 			    (char *)NULL, optsize) != 0) {
    353 				syslog(LOG_ERR,
    354 				    "setsockopt IP_OPTIONS NULL: %m");
    355 				exit(1);
    356 			}
    357 		}
    358 	}
    359 #endif
    360 	if (do_rlogin(fromp, hostname) == 0)
    361 		authenticated++;
    362 	if (confirmed == 0) {
    363 		write(f, "", 1);
    364 		confirmed = 1;		/* we sent the null! */
    365 	}
    366 	netf = f;
    367 
    368 	pid = forkpty(&master, line, NULL, &win);
    369 	if (pid < 0) {
    370 		if (errno == ENOENT)
    371 			fatal(f, "Out of ptys", 0);
    372 		else
    373 			fatal(f, "Forkpty", 1);
    374 	}
    375 	if (pid == 0) {
    376 		if (f > 2)	/* f should always be 0, but... */
    377 			(void) close(f);
    378 		setup_term(0);
    379 		if (authenticated)
    380 			execl(_PATH_LOGIN, "login", "-p",
    381 			    "-h", utmphost, "-f", "--", lusername, (char *)0);
    382 		else
    383 			execl(_PATH_LOGIN, "login", "-p",
    384 			    "-h", utmphost, "--", lusername, (char *)0);
    385 		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
    386 		/*NOTREACHED*/
    387 	}
    388 	ioctl(f, FIONBIO, &on);
    389 	ioctl(master, FIONBIO, &on);
    390 	ioctl(master, TIOCPKT, &on);
    391 	signal(SIGCHLD, cleanup);
    392 	protocol(f, master);
    393 	signal(SIGCHLD, SIG_IGN);
    394 	cleanup(0);
    395 }
    396 
    397 char	magic[2] = { 0377, 0377 };
    398 char	oobdata[] = {TIOCPKT_WINDOW};
    399 
    400 /*
    401  * Handle a "control" request (signaled by magic being present)
    402  * in the data stream.  For now, we are only willing to handle
    403  * window size changes.
    404  */
    405 int
    406 control(pty, cp, n)
    407 	int pty;
    408 	char *cp;
    409 	int n;
    410 {
    411 	struct winsize w;
    412 
    413 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
    414 		return (0);
    415 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
    416 	memmove(&w, cp+4, sizeof(w));
    417 	w.ws_row = ntohs(w.ws_row);
    418 	w.ws_col = ntohs(w.ws_col);
    419 	w.ws_xpixel = ntohs(w.ws_xpixel);
    420 	w.ws_ypixel = ntohs(w.ws_ypixel);
    421 	(void)ioctl(pty, TIOCSWINSZ, &w);
    422 	return (4+sizeof (w));
    423 }
    424 
    425 /*
    426  * rlogin "protocol" machine.
    427  */
    428 void
    429 protocol(f, p)
    430 	int f, p;
    431 {
    432 	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
    433 					/* XXX gcc above */
    434 	int pcc = 0, fcc = 0;
    435 	int cc, nfd, n;
    436 	char cntl;
    437 
    438 	/*
    439 	 * Must ignore SIGTTOU, otherwise we'll stop
    440 	 * when we try and set slave pty's window shape
    441 	 * (our controlling tty is the master pty).
    442 	 */
    443 	(void) signal(SIGTTOU, SIG_IGN);
    444 	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
    445 	if (f > p)
    446 		nfd = f + 1;
    447 	else
    448 		nfd = p + 1;
    449 	if (nfd > FD_SETSIZE) {
    450 		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
    451 		fatal(f, "internal error (select mask too small)", 0);
    452 	}
    453 	for (;;) {
    454 		fd_set ibits, obits, ebits, *omask;
    455 
    456 		FD_ZERO(&ebits);
    457 		FD_ZERO(&ibits);
    458 		FD_ZERO(&obits);
    459 		omask = (fd_set *)NULL;
    460 		if (fcc) {
    461 			FD_SET(p, &obits);
    462 			omask = &obits;
    463 		} else
    464 			FD_SET(f, &ibits);
    465 		if (pcc >= 0) {
    466 			if (pcc) {
    467 				FD_SET(f, &obits);
    468 				omask = &obits;
    469 			} else
    470 				FD_SET(p, &ibits);
    471 		}
    472 		FD_SET(p, &ebits);
    473 		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
    474 			if (errno == EINTR)
    475 				continue;
    476 			fatal(f, "select", 1);
    477 		}
    478 		if (n == 0) {
    479 			/* shouldn't happen... */
    480 			sleep(5);
    481 			continue;
    482 		}
    483 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
    484 		if (FD_ISSET(p, &ebits)) {
    485 			cc = read(p, &cntl, 1);
    486 			if (cc == 1 && pkcontrol(cntl)) {
    487 				cntl |= oobdata[0];
    488 				send(f, &cntl, 1, MSG_OOB);
    489 				if (cntl & TIOCPKT_FLUSHWRITE) {
    490 					pcc = 0;
    491 					FD_CLR(p, &ibits);
    492 				}
    493 			}
    494 		}
    495 		if (FD_ISSET(f, &ibits)) {
    496 				fcc = read(f, fibuf, sizeof(fibuf));
    497 			if (fcc < 0 && errno == EWOULDBLOCK)
    498 				fcc = 0;
    499 			else {
    500 				char *cp;
    501 				int left, n;
    502 
    503 				if (fcc <= 0)
    504 					break;
    505 				fbp = fibuf;
    506 
    507 			top:
    508 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
    509 					if (cp[0] == magic[0] &&
    510 					    cp[1] == magic[1]) {
    511 						left = fcc - (cp-fibuf);
    512 						n = control(p, cp, left);
    513 						if (n) {
    514 							left -= n;
    515 							if (left > 0)
    516 								memmove(cp,
    517 								    cp+n,
    518 								    left);
    519 							fcc -= n;
    520 							goto top; /* n^2 */
    521 						}
    522 					}
    523 				FD_SET(p, &obits);		/* try write */
    524 			}
    525 		}
    526 
    527 		if (FD_ISSET(p, &obits) && fcc > 0) {
    528 			cc = write(p, fbp, fcc);
    529 			if (cc > 0) {
    530 				fcc -= cc;
    531 				fbp += cc;
    532 			}
    533 		}
    534 
    535 		if (FD_ISSET(p, &ibits)) {
    536 			pcc = read(p, pibuf, sizeof (pibuf));
    537 			pbp = pibuf;
    538 			if (pcc < 0 && errno == EWOULDBLOCK)
    539 				pcc = 0;
    540 			else if (pcc <= 0)
    541 				break;
    542 			else if (pibuf[0] == 0) {
    543 				pbp++, pcc--;
    544 					FD_SET(f, &obits);	/* try write */
    545 			} else {
    546 				if (pkcontrol(pibuf[0])) {
    547 					pibuf[0] |= oobdata[0];
    548 					send(f, &pibuf[0], 1, MSG_OOB);
    549 				}
    550 				pcc = 0;
    551 			}
    552 		}
    553 		if ((FD_ISSET(f, &obits)) && pcc > 0) {
    554 				cc = write(f, pbp, pcc);
    555 			if (cc < 0 && errno == EWOULDBLOCK) {
    556 				/*
    557 				 * This happens when we try write after read
    558 				 * from p, but some old kernels balk at large
    559 				 * writes even when select returns true.
    560 				 */
    561 				if (!FD_ISSET(p, &ibits))
    562 					sleep(5);
    563 				continue;
    564 			}
    565 			if (cc > 0) {
    566 				pcc -= cc;
    567 				pbp += cc;
    568 			}
    569 		}
    570 	}
    571 }
    572 
    573 void
    574 cleanup(signo)
    575 	int signo;
    576 {
    577 	char *p, c;
    578 
    579 	p = line + sizeof(_PATH_DEV) - 1;
    580 	if (logout(p))
    581 		logwtmp(p, "", "");
    582 	(void)chmod(line, 0666);
    583 	(void)chown(line, 0, 0);
    584 	c = *p; *p = 'p';
    585 	(void)chmod(line, 0666);
    586 	(void)chown(line, 0, 0);
    587 	*p = c;
    588 	if (ttyaction(line, "rlogind", "root"))
    589 		syslog(LOG_ERR, "%s: ttyaction failed", line);
    590 	shutdown(netf, 2);
    591 	exit(1);
    592 }
    593 
    594 void
    595 fatal(f, msg, syserr)
    596 	int f;
    597 	char *msg;
    598 	int syserr;
    599 {
    600 	int len;
    601 	char buf[BUFSIZ], *bp = buf;
    602 
    603 	/*
    604 	 * Prepend binary one to message if we haven't sent
    605 	 * the magic null as confirmation.
    606 	 */
    607 	if (!confirmed)
    608 		*bp++ = '\01';		/* error indicator */
    609 	if (syserr)
    610 		len = sprintf(bp, "rlogind: %s: %s.\r\n",
    611 		    msg, strerror(errno));
    612 	else
    613 		len = sprintf(bp, "rlogind: %s.\r\n", msg);
    614 	(void) write(f, buf, bp + len - buf);
    615 	exit(1);
    616 }
    617 
    618 int
    619 do_rlogin(dest, host)
    620 	struct sockaddr *dest;
    621 	char *host;
    622 {
    623 	extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c */
    624 	int retval;
    625 
    626 	getstr(rusername, sizeof(rusername), "remuser too long");
    627 	getstr(lusername, sizeof(lusername), "locuser too long");
    628 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
    629 
    630 	pwd = getpwnam(lusername);
    631 	if (pwd == NULL) {
    632 		syslog(LOG_INFO,
    633 		    "%s@%s as %s: unknown login.", rusername, host, lusername);
    634 		return (-1);
    635 	}
    636 
    637 	retval = iruserok_sa(dest, dest->sa_len, pwd->pw_uid == 0, rusername,
    638 		lusername);
    639 /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
    640 	if (retval == 0) {
    641 		if (log_success)
    642 			syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
    643 			    rusername, host, lusername);
    644 	} else {
    645 		if (__rcmd_errstr)
    646 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
    647 			    rusername, host, lusername, __rcmd_errstr);
    648 		else
    649 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
    650 			    rusername, host, lusername);
    651 	}
    652 	return(retval);
    653 }
    654 
    655 void
    656 getstr(buf, cnt, errmsg)
    657 	char *buf;
    658 	int cnt;
    659 	char *errmsg;
    660 {
    661 	char c;
    662 
    663 	do {
    664 		if (read(0, &c, 1) != 1)
    665 			exit(1);
    666 		if (--cnt < 0)
    667 			fatal(STDOUT_FILENO, errmsg, 0);
    668 		*buf++ = c;
    669 	} while (c != 0);
    670 }
    671 
    672 extern	char **environ;
    673 
    674 void
    675 setup_term(fd)
    676 	int fd;
    677 {
    678 	char *cp = index(term+ENVSIZE, '/');
    679 	char *speed;
    680 	struct termios tt;
    681 
    682 #ifndef notyet
    683 	tcgetattr(fd, &tt);
    684 	if (cp) {
    685 		*cp++ = '\0';
    686 		speed = cp;
    687 		cp = index(speed, '/');
    688 		if (cp)
    689 			*cp++ = '\0';
    690 		cfsetspeed(&tt, atoi(speed));
    691 	}
    692 
    693 	tt.c_iflag = TTYDEF_IFLAG;
    694 	tt.c_oflag = TTYDEF_OFLAG;
    695 	tt.c_lflag = TTYDEF_LFLAG;
    696 	tcsetattr(fd, TCSAFLUSH, &tt);
    697 #else
    698 	if (cp) {
    699 		*cp++ = '\0';
    700 		speed = cp;
    701 		cp = index(speed, '/');
    702 		if (cp)
    703 			*cp++ = '\0';
    704 		tcgetattr(fd, &tt);
    705 		cfsetspeed(&tt, atoi(speed));
    706 		tcsetattr(fd, TCSAFLUSH, &tt);
    707 	}
    708 #endif
    709 
    710 	env[0] = term;
    711 	env[1] = 0;
    712 	environ = env;
    713 }
    714 
    715 
    716 void
    717 usage()
    718 {
    719 	syslog(LOG_ERR, "usage: rlogind [-alnL]");
    720 }
    721 
    722 /*
    723  * Check whether host h is in our local domain,
    724  * defined as sharing the last two components of the domain part,
    725  * or the entire domain part if the local domain has only one component.
    726  * If either name is unqualified (contains no '.'),
    727  * assume that the host is local, as it will be
    728  * interpreted as such.
    729  */
    730 int
    731 local_domain(h)
    732 	char *h;
    733 {
    734 	char localhost[MAXHOSTNAMELEN + 1];
    735 	char *p1, *p2;
    736 
    737 	localhost[0] = 0;
    738 	(void) gethostname(localhost, sizeof(localhost));
    739 	localhost[sizeof(localhost) - 1] = '\0';
    740 	p1 = topdomain(localhost);
    741 	p2 = topdomain(h);
    742 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
    743 		return (1);
    744 	return (0);
    745 }
    746 
    747 char *
    748 topdomain(h)
    749 	char *h;
    750 {
    751 	char *p;
    752 	char *maybe = NULL;
    753 	int dots = 0;
    754 
    755 	for (p = h + strlen(h); p >= h; p--) {
    756 		if (*p == '.') {
    757 			if (++dots == 2)
    758 				return (p);
    759 			maybe = p;
    760 		}
    761 	}
    762 	return (maybe);
    763 }
    764