Home | History | Annotate | Line # | Download | only in rlogind
rlogind.c revision 1.23
      1 /*	$NetBSD: rlogind.c,v 1.23 2001/01/10 01:41:38 lukem 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.23 2001/01/10 01:41:38 lukem 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 #ifdef INET6
    187 	if (((struct sockaddr *)&from)->sa_family == AF_INET6 &&
    188 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr) &&
    189 	    sizeof(struct sockaddr_in) <= sizeof(from)) {
    190 		struct sockaddr_in sin;
    191 		struct sockaddr_in6 *sin6;
    192 		const int off = sizeof(struct sockaddr_in6) -
    193 		    sizeof(struct sockaddr_in);
    194 
    195 		sin6 = (struct sockaddr_in6 *)&from;
    196 		memset(&sin, 0, sizeof(sin));
    197 		sin.sin_family = AF_INET;
    198 		sin.sin_len = sizeof(struct sockaddr_in);
    199 		memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[off],
    200 		    sizeof(sin.sin_addr));
    201 		memcpy(&from, &sin, sizeof(sin));
    202 		fromlen = sin.sin_len;
    203 	}
    204 #else
    205 	if (((struct sockaddr *)&from)->sa_family == AF_INET6 &&
    206 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) {
    207 		char hbuf[NI_MAXHOST];
    208 		if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf,
    209 				sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
    210 			strncpy(hbuf, "invalid", sizeof(hbuf));
    211 		}
    212 		syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)\n",
    213 		    hbuf);
    214 		exit(1);
    215 	}
    216 #endif
    217 	on = 1;
    218 	if (keepalive &&
    219 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
    220 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
    221 #if defined(IP_TOS)
    222 	if (((struct sockaddr *)&from)->sa_family == AF_INET) {
    223 		on = IPTOS_LOWDELAY;
    224 		if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
    225 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
    226 	}
    227 #endif
    228 	doit(0, (struct sockaddr *)&from);
    229 	/* NOTREACHED */
    230 #ifdef __GNUC__
    231 	exit(0);
    232 #endif
    233 }
    234 
    235 int	child;
    236 int	netf;
    237 char	line[MAXPATHLEN];
    238 int	confirmed;
    239 
    240 struct winsize win = { 0, 0, 0, 0 };
    241 
    242 
    243 void
    244 doit(f, fromp)
    245 	int f;
    246 	struct sockaddr *fromp;
    247 {
    248 	int master, pid, on = 1;
    249 	int authenticated = 0;
    250 	char utmphost[UT_HOSTSIZE + 1];
    251 	char *hostname;
    252 	char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
    253 	char c;
    254 	char naddr[NI_MAXHOST];
    255 	char saddr[NI_MAXHOST];
    256 	char raddr[NI_MAXHOST];
    257 	int af = fromp->sa_family;
    258 	u_int16_t *portp;
    259 	struct addrinfo hints, *res, *res0;
    260 	int gaierror;
    261 #ifdef NI_WITHSCOPEID
    262 	const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID;
    263 #else
    264 	const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
    265 #endif
    266 
    267 	alarm(60);
    268 	read(f, &c, 1);
    269 
    270 	if (c != 0)
    271 		exit(1);
    272 
    273 	alarm(0);
    274 	switch (af) {
    275 	case AF_INET:
    276 		portp = &((struct sockaddr_in *)fromp)->sin_port;
    277 		break;
    278 #ifdef INET6
    279 	case AF_INET6:
    280 		portp = &((struct sockaddr_in6 *)fromp)->sin6_port;
    281 		break;
    282 #endif
    283 	default:
    284 		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
    285 		exit(1);
    286 	}
    287 	if (getnameinfo((struct sockaddr *)fromp, fromp->sa_len,
    288 		    naddr, sizeof(naddr), NULL, 0, niflags) != 0) {
    289 		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
    290 		exit(1);
    291 	}
    292 
    293 	if (getnameinfo((struct sockaddr *)fromp, fromp->sa_len,
    294 		    saddr, sizeof(saddr), NULL, 0, NI_NAMEREQD) == 0) {
    295 		/*
    296 		 * If name returned by gethostbyaddr is in our domain,
    297 		 * attempt to verify that we haven't been fooled by someone
    298 		 * in a remote net; look up the name and check that this
    299 		 * address corresponds to the name.
    300 		 */
    301 		hostname = saddr;
    302 		res0 = NULL;
    303 		if (check_all || local_domain(saddr)) {
    304 			strncpy(hostnamebuf, saddr, sizeof(hostnamebuf) - 1);
    305 			hostnamebuf[sizeof(hostnamebuf) - 1] = 0;
    306 			memset(&hints, 0, sizeof(hints));
    307 			hints.ai_family = fromp->sa_family;
    308 			hints.ai_socktype = SOCK_STREAM;
    309 			hints.ai_flags = AI_CANONNAME;
    310 			gaierror = getaddrinfo(hostnamebuf, "0", &hints, &res0);
    311 			if (gaierror) {
    312 				syslog(LOG_NOTICE,
    313 				    "Couldn't look up address for %s: %s",
    314 				    hostnamebuf, gai_strerror(gaierror));
    315 				hostname = naddr;
    316 			} else {
    317 				for (res = res0; res; res = res->ai_next) {
    318 					if (res->ai_family != fromp->sa_family)
    319 						continue;
    320 					if (res->ai_addrlen != fromp->sa_len)
    321 						continue;
    322 					if (getnameinfo(res->ai_addr,
    323 						res->ai_addrlen,
    324 						raddr, sizeof(raddr), NULL, 0,
    325 						niflags) == 0
    326 					 && strcmp(naddr, raddr) == 0) {
    327 						hostname = res->ai_canonname
    328 							? res->ai_canonname
    329 							: saddr;
    330 						break;
    331 					}
    332 				}
    333 				if (res == NULL) {
    334 					syslog(LOG_NOTICE,
    335 					  "Host addr %s not listed for host %s",
    336 					    naddr, res0->ai_canonname
    337 						    ? res0->ai_canonname
    338 						    : saddr);
    339 					hostname = naddr;
    340 				}
    341 			}
    342 		}
    343 		hostname = strncpy(hostnamebuf, hostname,
    344 				   sizeof(hostnamebuf) - 1);
    345 		if (res0)
    346 			freeaddrinfo(res0);
    347 	} else
    348 		hostname = strncpy(hostnamebuf, naddr,
    349 				   sizeof(hostnamebuf) - 1);
    350 
    351 	hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
    352 
    353 	if (strlen(hostname) < sizeof(utmphost))
    354 		(void)strcpy(utmphost, hostname);
    355 	else
    356 		(void)strncpy(utmphost, hostname, sizeof(utmphost));
    357 	utmphost[sizeof(utmphost) - 1] = '\0';
    358 
    359 	if (ntohs(*portp) >= IPPORT_RESERVED ||
    360 	    ntohs(*portp) < IPPORT_RESERVED/2) {
    361 		syslog(LOG_NOTICE, "Connection from %s on illegal port",
    362 		       naddr);
    363 		fatal(f, "Permission denied", 0);
    364 	}
    365 #ifdef IP_OPTIONS
    366 	if (fromp->sa_family == AF_INET) {
    367 		u_char optbuf[BUFSIZ/3], *cp;
    368 		char lbuf[BUFSIZ], *lp;
    369 		int optsize = sizeof(optbuf), ipproto;
    370 		struct protoent *ip;
    371 
    372 		if ((ip = getprotobyname("ip")) != NULL)
    373 			ipproto = ip->p_proto;
    374 		else
    375 			ipproto = IPPROTO_IP;
    376 		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
    377 		    &optsize) == 0 && optsize != 0) {
    378 			lp = lbuf;
    379 			for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
    380 				sprintf(lp, " %2.2x", *cp);
    381 			syslog(LOG_NOTICE,
    382 			    "Connection received using IP options (ignored):%s",
    383 			    lbuf);
    384 			if (setsockopt(0, ipproto, IP_OPTIONS,
    385 			    (char *)NULL, optsize) != 0) {
    386 				syslog(LOG_ERR,
    387 				    "setsockopt IP_OPTIONS NULL: %m");
    388 				exit(1);
    389 			}
    390 		}
    391 	}
    392 #endif
    393 	if (do_rlogin(fromp, hostname) == 0)
    394 		authenticated++;
    395 	if (confirmed == 0) {
    396 		write(f, "", 1);
    397 		confirmed = 1;		/* we sent the null! */
    398 	}
    399 	netf = f;
    400 
    401 	pid = forkpty(&master, line, NULL, &win);
    402 	if (pid < 0) {
    403 		if (errno == ENOENT)
    404 			fatal(f, "Out of ptys", 0);
    405 		else
    406 			fatal(f, "Forkpty", 1);
    407 	}
    408 	if (pid == 0) {
    409 		if (f > 2)	/* f should always be 0, but... */
    410 			(void) close(f);
    411 		setup_term(0);
    412 		if (authenticated)
    413 			execl(_PATH_LOGIN, "login", "-p",
    414 			    "-h", utmphost, "-f", "--", lusername, (char *)0);
    415 		else
    416 			execl(_PATH_LOGIN, "login", "-p",
    417 			    "-h", utmphost, "--", lusername, (char *)0);
    418 		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
    419 		/*NOTREACHED*/
    420 	}
    421 	ioctl(f, FIONBIO, &on);
    422 	ioctl(master, FIONBIO, &on);
    423 	ioctl(master, TIOCPKT, &on);
    424 	signal(SIGCHLD, cleanup);
    425 	protocol(f, master);
    426 	signal(SIGCHLD, SIG_IGN);
    427 	cleanup(0);
    428 }
    429 
    430 char	magic[2] = { 0377, 0377 };
    431 char	oobdata[] = {TIOCPKT_WINDOW};
    432 
    433 /*
    434  * Handle a "control" request (signaled by magic being present)
    435  * in the data stream.  For now, we are only willing to handle
    436  * window size changes.
    437  */
    438 int
    439 control(pty, cp, n)
    440 	int pty;
    441 	char *cp;
    442 	int n;
    443 {
    444 	struct winsize w;
    445 
    446 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
    447 		return (0);
    448 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
    449 	memmove(&w, cp+4, sizeof(w));
    450 	w.ws_row = ntohs(w.ws_row);
    451 	w.ws_col = ntohs(w.ws_col);
    452 	w.ws_xpixel = ntohs(w.ws_xpixel);
    453 	w.ws_ypixel = ntohs(w.ws_ypixel);
    454 	(void)ioctl(pty, TIOCSWINSZ, &w);
    455 	return (4+sizeof (w));
    456 }
    457 
    458 /*
    459  * rlogin "protocol" machine.
    460  */
    461 void
    462 protocol(f, p)
    463 	int f, p;
    464 {
    465 	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
    466 					/* XXX gcc above */
    467 	int pcc = 0, fcc = 0;
    468 	int cc, nfd, n;
    469 	char cntl;
    470 
    471 	/*
    472 	 * Must ignore SIGTTOU, otherwise we'll stop
    473 	 * when we try and set slave pty's window shape
    474 	 * (our controlling tty is the master pty).
    475 	 */
    476 	(void) signal(SIGTTOU, SIG_IGN);
    477 	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
    478 	if (f > p)
    479 		nfd = f + 1;
    480 	else
    481 		nfd = p + 1;
    482 	if (nfd > FD_SETSIZE) {
    483 		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
    484 		fatal(f, "internal error (select mask too small)", 0);
    485 	}
    486 	for (;;) {
    487 		fd_set ibits, obits, ebits, *omask;
    488 
    489 		FD_ZERO(&ebits);
    490 		FD_ZERO(&ibits);
    491 		FD_ZERO(&obits);
    492 		omask = (fd_set *)NULL;
    493 		if (fcc) {
    494 			FD_SET(p, &obits);
    495 			omask = &obits;
    496 		} else
    497 			FD_SET(f, &ibits);
    498 		if (pcc >= 0) {
    499 			if (pcc) {
    500 				FD_SET(f, &obits);
    501 				omask = &obits;
    502 			} else
    503 				FD_SET(p, &ibits);
    504 		}
    505 		FD_SET(p, &ebits);
    506 		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
    507 			if (errno == EINTR)
    508 				continue;
    509 			fatal(f, "select", 1);
    510 		}
    511 		if (n == 0) {
    512 			/* shouldn't happen... */
    513 			sleep(5);
    514 			continue;
    515 		}
    516 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
    517 		if (FD_ISSET(p, &ebits)) {
    518 			cc = read(p, &cntl, 1);
    519 			if (cc == 1 && pkcontrol(cntl)) {
    520 				cntl |= oobdata[0];
    521 				send(f, &cntl, 1, MSG_OOB);
    522 				if (cntl & TIOCPKT_FLUSHWRITE) {
    523 					pcc = 0;
    524 					FD_CLR(p, &ibits);
    525 				}
    526 			}
    527 		}
    528 		if (FD_ISSET(f, &ibits)) {
    529 				fcc = read(f, fibuf, sizeof(fibuf));
    530 			if (fcc < 0 && errno == EWOULDBLOCK)
    531 				fcc = 0;
    532 			else {
    533 				char *cp;
    534 				int left, n;
    535 
    536 				if (fcc <= 0)
    537 					break;
    538 				fbp = fibuf;
    539 
    540 			top:
    541 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
    542 					if (cp[0] == magic[0] &&
    543 					    cp[1] == magic[1]) {
    544 						left = fcc - (cp-fibuf);
    545 						n = control(p, cp, left);
    546 						if (n) {
    547 							left -= n;
    548 							if (left > 0)
    549 								memmove(cp,
    550 								    cp+n,
    551 								    left);
    552 							fcc -= n;
    553 							goto top; /* n^2 */
    554 						}
    555 					}
    556 				FD_SET(p, &obits);		/* try write */
    557 			}
    558 		}
    559 
    560 		if (FD_ISSET(p, &obits) && fcc > 0) {
    561 			cc = write(p, fbp, fcc);
    562 			if (cc > 0) {
    563 				fcc -= cc;
    564 				fbp += cc;
    565 			}
    566 		}
    567 
    568 		if (FD_ISSET(p, &ibits)) {
    569 			pcc = read(p, pibuf, sizeof (pibuf));
    570 			pbp = pibuf;
    571 			if (pcc < 0 && errno == EWOULDBLOCK)
    572 				pcc = 0;
    573 			else if (pcc <= 0)
    574 				break;
    575 			else if (pibuf[0] == 0) {
    576 				pbp++, pcc--;
    577 					FD_SET(f, &obits);	/* try write */
    578 			} else {
    579 				if (pkcontrol(pibuf[0])) {
    580 					pibuf[0] |= oobdata[0];
    581 					send(f, &pibuf[0], 1, MSG_OOB);
    582 				}
    583 				pcc = 0;
    584 			}
    585 		}
    586 		if ((FD_ISSET(f, &obits)) && pcc > 0) {
    587 				cc = write(f, pbp, pcc);
    588 			if (cc < 0 && errno == EWOULDBLOCK) {
    589 				/*
    590 				 * This happens when we try write after read
    591 				 * from p, but some old kernels balk at large
    592 				 * writes even when select returns true.
    593 				 */
    594 				if (!FD_ISSET(p, &ibits))
    595 					sleep(5);
    596 				continue;
    597 			}
    598 			if (cc > 0) {
    599 				pcc -= cc;
    600 				pbp += cc;
    601 			}
    602 		}
    603 	}
    604 }
    605 
    606 void
    607 cleanup(signo)
    608 	int signo;
    609 {
    610 	char *p, c;
    611 
    612 	p = line + sizeof(_PATH_DEV) - 1;
    613 	if (logout(p))
    614 		logwtmp(p, "", "");
    615 	(void)chmod(line, 0666);
    616 	(void)chown(line, 0, 0);
    617 	c = *p; *p = 'p';
    618 	(void)chmod(line, 0666);
    619 	(void)chown(line, 0, 0);
    620 	*p = c;
    621 	if (ttyaction(line, "rlogind", "root"))
    622 		syslog(LOG_ERR, "%s: ttyaction failed", line);
    623 	shutdown(netf, 2);
    624 	exit(1);
    625 }
    626 
    627 void
    628 fatal(f, msg, syserr)
    629 	int f;
    630 	char *msg;
    631 	int syserr;
    632 {
    633 	int len;
    634 	char buf[BUFSIZ], *bp = buf;
    635 
    636 	/*
    637 	 * Prepend binary one to message if we haven't sent
    638 	 * the magic null as confirmation.
    639 	 */
    640 	if (!confirmed)
    641 		*bp++ = '\01';		/* error indicator */
    642 	if (syserr)
    643 		len = sprintf(bp, "rlogind: %s: %s.\r\n",
    644 		    msg, strerror(errno));
    645 	else
    646 		len = sprintf(bp, "rlogind: %s.\r\n", msg);
    647 	(void) write(f, buf, bp + len - buf);
    648 	exit(1);
    649 }
    650 
    651 int
    652 do_rlogin(dest, host)
    653 	struct sockaddr *dest;
    654 	char *host;
    655 {
    656 	extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c */
    657 	int retval;
    658 
    659 	getstr(rusername, sizeof(rusername), "remuser too long");
    660 	getstr(lusername, sizeof(lusername), "locuser too long");
    661 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
    662 
    663 	pwd = getpwnam(lusername);
    664 	if (pwd == NULL) {
    665 		syslog(LOG_INFO,
    666 		    "%s@%s as %s: unknown login.", rusername, host, lusername);
    667 		return (-1);
    668 	}
    669 
    670 	retval = iruserok_sa(dest, dest->sa_len, pwd->pw_uid == 0, rusername,
    671 		lusername);
    672 /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
    673 	if (retval == 0) {
    674 		if (log_success)
    675 			syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
    676 			    rusername, host, lusername);
    677 	} else {
    678 		if (__rcmd_errstr)
    679 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
    680 			    rusername, host, lusername, __rcmd_errstr);
    681 		else
    682 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
    683 			    rusername, host, lusername);
    684 	}
    685 	return(retval);
    686 }
    687 
    688 void
    689 getstr(buf, cnt, errmsg)
    690 	char *buf;
    691 	int cnt;
    692 	char *errmsg;
    693 {
    694 	char c;
    695 
    696 	do {
    697 		if (read(0, &c, 1) != 1)
    698 			exit(1);
    699 		if (--cnt < 0)
    700 			fatal(STDOUT_FILENO, errmsg, 0);
    701 		*buf++ = c;
    702 	} while (c != 0);
    703 }
    704 
    705 extern	char **environ;
    706 
    707 void
    708 setup_term(fd)
    709 	int fd;
    710 {
    711 	char *cp = index(term+ENVSIZE, '/');
    712 	char *speed;
    713 	struct termios tt;
    714 
    715 #ifndef notyet
    716 	tcgetattr(fd, &tt);
    717 	if (cp) {
    718 		*cp++ = '\0';
    719 		speed = cp;
    720 		cp = index(speed, '/');
    721 		if (cp)
    722 			*cp++ = '\0';
    723 		cfsetspeed(&tt, atoi(speed));
    724 	}
    725 
    726 	tt.c_iflag = TTYDEF_IFLAG;
    727 	tt.c_oflag = TTYDEF_OFLAG;
    728 	tt.c_lflag = TTYDEF_LFLAG;
    729 	tcsetattr(fd, TCSAFLUSH, &tt);
    730 #else
    731 	if (cp) {
    732 		*cp++ = '\0';
    733 		speed = cp;
    734 		cp = index(speed, '/');
    735 		if (cp)
    736 			*cp++ = '\0';
    737 		tcgetattr(fd, &tt);
    738 		cfsetspeed(&tt, atoi(speed));
    739 		tcsetattr(fd, TCSAFLUSH, &tt);
    740 	}
    741 #endif
    742 
    743 	env[0] = term;
    744 	env[1] = 0;
    745 	environ = env;
    746 }
    747 
    748 
    749 void
    750 usage()
    751 {
    752 	syslog(LOG_ERR, "usage: rlogind [-alnL]");
    753 }
    754 
    755 /*
    756  * Check whether host h is in our local domain,
    757  * defined as sharing the last two components of the domain part,
    758  * or the entire domain part if the local domain has only one component.
    759  * If either name is unqualified (contains no '.'),
    760  * assume that the host is local, as it will be
    761  * interpreted as such.
    762  */
    763 int
    764 local_domain(h)
    765 	char *h;
    766 {
    767 	char localhost[MAXHOSTNAMELEN + 1];
    768 	char *p1, *p2;
    769 
    770 	localhost[0] = 0;
    771 	(void) gethostname(localhost, sizeof(localhost));
    772 	localhost[sizeof(localhost) - 1] = '\0';
    773 	p1 = topdomain(localhost);
    774 	p2 = topdomain(h);
    775 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
    776 		return (1);
    777 	return (0);
    778 }
    779 
    780 char *
    781 topdomain(h)
    782 	char *h;
    783 {
    784 	char *p;
    785 	char *maybe = NULL;
    786 	int dots = 0;
    787 
    788 	for (p = h + strlen(h); p >= h; p--) {
    789 		if (*p == '.') {
    790 			if (++dots == 2)
    791 				return (p);
    792 			maybe = p;
    793 		}
    794 	}
    795 	return (maybe);
    796 }
    797