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