Home | History | Annotate | Line # | Download | only in rlogind
rlogind.c revision 1.27
      1 /*	$NetBSD: rlogind.c,v 1.27 2002/09/18 20:37:11 mycroft 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.27 2002/09/18 20:37:11 mycroft 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 #include <sys/param.h>
     90 #include <sys/stat.h>
     91 #include <sys/ioctl.h>
     92 #include <sys/poll.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 	for (;;) {
    478 		struct pollfd set[2];
    479 
    480 		set[0].fd = p;
    481 		set[0].events = POLLPRI;
    482 		set[1].fd = f;
    483 		set[1].events = 0;
    484 		if (fcc)
    485 			set[0].events |= POLLOUT;
    486 		else
    487 			set[1].events |= POLLIN;
    488 		if (pcc >= 0) {
    489 			if (pcc)
    490 				set[1].events |= POLLOUT;
    491 			else
    492 				set[0].events |= POLLIN;
    493 		}
    494 		if ((n = poll(set, 2, INFTIM)) < 0) {
    495 			if (errno == EINTR)
    496 				continue;
    497 			fatal(f, "poll", 1);
    498 		}
    499 		if (n == 0) {
    500 			/* shouldn't happen... */
    501 			sleep(5);
    502 			continue;
    503 		}
    504 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
    505 		if (set[0].revents & POLLPRI) {
    506 			cc = read(p, &cntl, 1);
    507 			if (cc == 1 && pkcontrol(cntl)) {
    508 				cntl |= oobdata[0];
    509 				send(f, &cntl, 1, MSG_OOB);
    510 				if (cntl & TIOCPKT_FLUSHWRITE)
    511 					pcc = 0;
    512 			}
    513 		}
    514 		if (set[1].revents & POLLIN) {
    515 			fcc = read(f, fibuf, sizeof(fibuf));
    516 			if (fcc < 0 && errno == EWOULDBLOCK)
    517 				fcc = 0;
    518 			else {
    519 				char *cp;
    520 				int left, n;
    521 
    522 				if (fcc <= 0)
    523 					break;
    524 				fbp = fibuf;
    525 
    526 			top:
    527 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
    528 					if (cp[0] == magic[0] &&
    529 					    cp[1] == magic[1]) {
    530 						left = fcc - (cp-fibuf);
    531 						n = control(p, cp, left);
    532 						if (n) {
    533 							left -= n;
    534 							if (left > 0)
    535 								memmove(cp,
    536 								    cp+n,
    537 								    left);
    538 							fcc -= n;
    539 							goto top; /* n^2 */
    540 						}
    541 					}
    542 			}
    543 		}
    544 
    545 		if (set[0].revents & POLLOUT && fcc > 0) {
    546 			cc = write(p, fbp, fcc);
    547 			if (cc > 0) {
    548 				fcc -= cc;
    549 				fbp += cc;
    550 			}
    551 		}
    552 
    553 		if (set[0].revents & POLLIN) {
    554 			pcc = read(p, pibuf, sizeof (pibuf));
    555 			pbp = pibuf;
    556 			if (pcc < 0 && errno == EWOULDBLOCK)
    557 				pcc = 0;
    558 			else if (pcc <= 0)
    559 				break;
    560 			else if (pibuf[0] == 0) {
    561 				pbp++, pcc--;
    562 			} else {
    563 				if (pkcontrol(pibuf[0])) {
    564 					pibuf[0] |= oobdata[0];
    565 					send(f, &pibuf[0], 1, MSG_OOB);
    566 				}
    567 				pcc = 0;
    568 			}
    569 		}
    570 		if (set[1].revents & POLLOUT && pcc > 0) {
    571 			cc = write(f, pbp, pcc);
    572 			if (cc > 0) {
    573 				pcc -= cc;
    574 				pbp += cc;
    575 			}
    576 		}
    577 	}
    578 }
    579 
    580 void
    581 cleanup(signo)
    582 	int signo;
    583 {
    584 	char *p, c;
    585 
    586 	p = line + sizeof(_PATH_DEV) - 1;
    587 #ifdef SUPPORT_UTMP
    588 	if (logout(p))
    589 		logwtmp(p, "", "");
    590 #endif
    591 #ifdef SUPPORT_UTMPX
    592 	if (logoutx(p, 0, DEAD_PROCESS))
    593 		logwtmpx(p, "", "", 0, DEAD_PROCESS);
    594 #endif
    595 	(void)chmod(line, 0666);
    596 	(void)chown(line, 0, 0);
    597 	c = *p; *p = 'p';
    598 	(void)chmod(line, 0666);
    599 	(void)chown(line, 0, 0);
    600 	*p = c;
    601 	if (ttyaction(line, "rlogind", "root"))
    602 		syslog(LOG_ERR, "%s: ttyaction failed", line);
    603 	shutdown(netf, 2);
    604 	exit(1);
    605 }
    606 
    607 void
    608 fatal(f, msg, syserr)
    609 	int f;
    610 	char *msg;
    611 	int syserr;
    612 {
    613 	int len;
    614 	char buf[BUFSIZ], *bp = buf;
    615 
    616 	/*
    617 	 * Prepend binary one to message if we haven't sent
    618 	 * the magic null as confirmation.
    619 	 */
    620 	if (!confirmed)
    621 		*bp++ = '\01';		/* error indicator */
    622 	if (syserr)
    623 		len = sprintf(bp, "rlogind: %s: %s.\r\n",
    624 		    msg, strerror(errno));
    625 	else
    626 		len = sprintf(bp, "rlogind: %s.\r\n", msg);
    627 	(void) write(f, buf, bp + len - buf);
    628 	exit(1);
    629 }
    630 
    631 int
    632 do_rlogin(dest, host)
    633 	struct sockaddr *dest;
    634 	char *host;
    635 {
    636 	int retval;
    637 
    638 	getstr(rusername, sizeof(rusername), "remuser too long");
    639 	getstr(lusername, sizeof(lusername), "locuser too long");
    640 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
    641 
    642 	pwd = getpwnam(lusername);
    643 	if (pwd == NULL) {
    644 		syslog(LOG_INFO,
    645 		    "%s@%s as %s: unknown login.", rusername, host, lusername);
    646 		return (-1);
    647 	}
    648 
    649 	retval = iruserok_sa(dest, dest->sa_len, pwd->pw_uid == 0, rusername,
    650 		lusername);
    651 /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
    652 	if (retval == 0) {
    653 		if (log_success)
    654 			syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
    655 			    rusername, host, lusername);
    656 	} else {
    657 		if (__rcmd_errstr)
    658 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
    659 			    rusername, host, lusername, __rcmd_errstr);
    660 		else
    661 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
    662 			    rusername, host, lusername);
    663 	}
    664 	return(retval);
    665 }
    666 
    667 void
    668 getstr(buf, cnt, errmsg)
    669 	char *buf;
    670 	int cnt;
    671 	char *errmsg;
    672 {
    673 	char c;
    674 
    675 	do {
    676 		if (read(0, &c, 1) != 1)
    677 			exit(1);
    678 		if (--cnt < 0)
    679 			fatal(STDOUT_FILENO, errmsg, 0);
    680 		*buf++ = c;
    681 	} while (c != 0);
    682 }
    683 
    684 
    685 void
    686 setup_term(fd)
    687 	int fd;
    688 {
    689 	char *cp = index(term+ENVSIZE, '/');
    690 	char *speed;
    691 	struct termios tt;
    692 
    693 #ifndef notyet
    694 	tcgetattr(fd, &tt);
    695 	if (cp) {
    696 		*cp++ = '\0';
    697 		speed = cp;
    698 		cp = index(speed, '/');
    699 		if (cp)
    700 			*cp++ = '\0';
    701 		cfsetspeed(&tt, atoi(speed));
    702 	}
    703 
    704 	tt.c_iflag = TTYDEF_IFLAG;
    705 	tt.c_oflag = TTYDEF_OFLAG;
    706 	tt.c_lflag = TTYDEF_LFLAG;
    707 	tcsetattr(fd, TCSAFLUSH, &tt);
    708 #else
    709 	if (cp) {
    710 		*cp++ = '\0';
    711 		speed = cp;
    712 		cp = index(speed, '/');
    713 		if (cp)
    714 			*cp++ = '\0';
    715 		tcgetattr(fd, &tt);
    716 		cfsetspeed(&tt, atoi(speed));
    717 		tcsetattr(fd, TCSAFLUSH, &tt);
    718 	}
    719 #endif
    720 
    721 	env[0] = term;
    722 	env[1] = 0;
    723 	environ = env;
    724 }
    725 
    726 
    727 void
    728 usage()
    729 {
    730 	syslog(LOG_ERR, "usage: rlogind [-alnL]");
    731 }
    732 
    733 /*
    734  * Check whether host h is in our local domain,
    735  * defined as sharing the last two components of the domain part,
    736  * or the entire domain part if the local domain has only one component.
    737  * If either name is unqualified (contains no '.'),
    738  * assume that the host is local, as it will be
    739  * interpreted as such.
    740  */
    741 int
    742 local_domain(h)
    743 	char *h;
    744 {
    745 	char localhost[MAXHOSTNAMELEN + 1];
    746 	char *p1, *p2;
    747 
    748 	localhost[0] = 0;
    749 	(void) gethostname(localhost, sizeof(localhost));
    750 	localhost[sizeof(localhost) - 1] = '\0';
    751 	p1 = topdomain(localhost);
    752 	p2 = topdomain(h);
    753 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
    754 		return (1);
    755 	return (0);
    756 }
    757 
    758 char *
    759 topdomain(h)
    760 	char *h;
    761 {
    762 	char *p;
    763 	char *maybe = NULL;
    764 	int dots = 0;
    765 
    766 	for (p = h + strlen(h); p >= h; p--) {
    767 		if (*p == '.') {
    768 			if (++dots == 2)
    769 				return (p);
    770 			maybe = p;
    771 		}
    772 	}
    773 	return (maybe);
    774 }
    775