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