Home | History | Annotate | Line # | Download | only in rlogind
rlogind.c revision 1.31
      1 /*	$NetBSD: rlogind.c,v 1.31 2003/05/17 22:54:55 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.31 2003/05/17 22:54:55 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 #include <sys/param.h>
     90 #include <sys/stat.h>
     91 #include <sys/ioctl.h>
     92 #include <signal.h>
     93 #include <termios.h>
     94 #include <poll.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 			strlcpy(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 			strlcpy(hostnamebuf, saddr, sizeof(hostnamebuf));
    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 		strlcpy(hostnamebuf, hostname, sizeof(hostnamebuf));
    344 		hostname = hostnamebuf;
    345 		if (res0)
    346 			freeaddrinfo(res0);
    347 	} else {
    348 		strlcpy(hostnamebuf, naddr, sizeof(hostnamebuf));
    349 		hostname = hostnamebuf;
    350 	}
    351 
    352 	if (ntohs(*portp) >= IPPORT_RESERVED ||
    353 	    ntohs(*portp) < IPPORT_RESERVED/2) {
    354 		syslog(LOG_NOTICE, "Connection from %s on illegal port",
    355 		       naddr);
    356 		fatal(f, "Permission denied", 0);
    357 	}
    358 #ifdef IP_OPTIONS
    359 	if (fromp->sa_family == AF_INET) {
    360 		u_char optbuf[BUFSIZ/3], *cp;
    361 		char lbuf[BUFSIZ], *lp, *ep;
    362 		int optsize = sizeof(optbuf), ipproto;
    363 		struct protoent *ip;
    364 
    365 		if ((ip = getprotobyname("ip")) != NULL)
    366 			ipproto = ip->p_proto;
    367 		else
    368 			ipproto = IPPROTO_IP;
    369 		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
    370 		    &optsize) == 0 && optsize != 0) {
    371 			lp = lbuf;
    372 			ep = lbuf + sizeof(lbuf);
    373 			for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
    374 				snprintf(lp, ep - lp, " %2.2x", *cp);
    375 			syslog(LOG_NOTICE,
    376 			    "Connection received using IP options (ignored):%s",
    377 			    lbuf);
    378 			if (setsockopt(0, ipproto, IP_OPTIONS,
    379 			    (char *)NULL, optsize) != 0) {
    380 				syslog(LOG_ERR,
    381 				    "setsockopt IP_OPTIONS NULL: %m");
    382 				exit(1);
    383 			}
    384 		}
    385 	}
    386 #endif
    387 	if (do_rlogin(fromp, hostname) == 0)
    388 		authenticated++;
    389 	if (confirmed == 0) {
    390 		write(f, "", 1);
    391 		confirmed = 1;		/* we sent the null! */
    392 	}
    393 	netf = f;
    394 
    395 	pid = forkpty(&master, line, NULL, &win);
    396 	if (pid < 0) {
    397 		if (errno == ENOENT)
    398 			fatal(f, "Out of ptys", 0);
    399 		else
    400 			fatal(f, "Forkpty", 1);
    401 	}
    402 	if (pid == 0) {
    403 		if (f > 2)	/* f should always be 0, but... */
    404 			(void) close(f);
    405 		setup_term(0);
    406 		if (authenticated)
    407 			execl(_PATH_LOGIN, "login", "-p",
    408 			    "-h", hostname, "-f", "--", lusername, (char *)0);
    409 		else
    410 			execl(_PATH_LOGIN, "login", "-p",
    411 			    "-h", hostname, "--", lusername, (char *)0);
    412 		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
    413 		/*NOTREACHED*/
    414 	}
    415 	ioctl(f, FIONBIO, &on);
    416 	ioctl(master, FIONBIO, &on);
    417 	ioctl(master, TIOCPKT, &on);
    418 	signal(SIGCHLD, cleanup);
    419 	protocol(f, master);
    420 	signal(SIGCHLD, SIG_IGN);
    421 	cleanup(0);
    422 }
    423 
    424 char	magic[2] = { 0377, 0377 };
    425 char	oobdata[] = {TIOCPKT_WINDOW};
    426 
    427 /*
    428  * Handle a "control" request (signaled by magic being present)
    429  * in the data stream.  For now, we are only willing to handle
    430  * window size changes.
    431  */
    432 int
    433 control(pty, cp, n)
    434 	int pty;
    435 	char *cp;
    436 	int n;
    437 {
    438 	struct winsize w;
    439 
    440 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
    441 		return (0);
    442 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
    443 	memmove(&w, cp+4, sizeof(w));
    444 	w.ws_row = ntohs(w.ws_row);
    445 	w.ws_col = ntohs(w.ws_col);
    446 	w.ws_xpixel = ntohs(w.ws_xpixel);
    447 	w.ws_ypixel = ntohs(w.ws_ypixel);
    448 	(void)ioctl(pty, TIOCSWINSZ, &w);
    449 	return (4+sizeof (w));
    450 }
    451 
    452 /*
    453  * rlogin "protocol" machine.
    454  */
    455 void
    456 protocol(f, p)
    457 	int f, p;
    458 {
    459 	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
    460 					/* XXX gcc above */
    461 	int pcc = 0, fcc = 0;
    462 	int cc, n;
    463 	char cntl;
    464 	struct pollfd set[2];
    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 	set[0].fd = p;
    474 	set[1].fd = f;
    475 	for (;;) {
    476 		set[0].events = POLLPRI;
    477 		set[1].events = 0;
    478 		if (fcc)
    479 			set[0].events |= POLLOUT;
    480 		else
    481 			set[1].events |= POLLIN;
    482 		if (pcc >= 0) {
    483 			if (pcc)
    484 				set[1].events |= POLLOUT;
    485 			else
    486 				set[0].events |= POLLIN;
    487 		}
    488 		if ((n = poll(set, 2, INFTIM)) < 0) {
    489 			if (errno == EINTR)
    490 				continue;
    491 			fatal(f, "poll", 1);
    492 		}
    493 		if (n == 0) {
    494 			/* shouldn't happen... */
    495 			sleep(5);
    496 			continue;
    497 		}
    498 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
    499 		if (set[0].revents & POLLPRI) {
    500 			cc = read(p, &cntl, 1);
    501 			if (cc == 1 && pkcontrol(cntl)) {
    502 				cntl |= oobdata[0];
    503 				send(f, &cntl, 1, MSG_OOB);
    504 				if (cntl & TIOCPKT_FLUSHWRITE)
    505 					pcc = 0;
    506 			}
    507 		}
    508 		if (set[1].revents & POLLIN) {
    509 			fcc = read(f, fibuf, sizeof(fibuf));
    510 			if (fcc < 0 && errno == EWOULDBLOCK)
    511 				fcc = 0;
    512 			else {
    513 				char *cp;
    514 				int left, n;
    515 
    516 				if (fcc <= 0)
    517 					break;
    518 				fbp = fibuf;
    519 
    520 			top:
    521 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
    522 					if (cp[0] == magic[0] &&
    523 					    cp[1] == magic[1]) {
    524 						left = fcc - (cp-fibuf);
    525 						n = control(p, cp, left);
    526 						if (n) {
    527 							left -= n;
    528 							if (left > 0)
    529 								memmove(cp,
    530 								    cp+n,
    531 								    left);
    532 							fcc -= n;
    533 							goto top; /* n^2 */
    534 						}
    535 					}
    536 			}
    537 		}
    538 
    539 		if (set[0].revents & POLLOUT && fcc > 0) {
    540 			cc = write(p, fbp, fcc);
    541 			if (cc > 0) {
    542 				fcc -= cc;
    543 				fbp += cc;
    544 			}
    545 		}
    546 
    547 		if (set[0].revents & POLLIN) {
    548 			pcc = read(p, pibuf, sizeof (pibuf));
    549 			pbp = pibuf;
    550 			if (pcc < 0 && errno == EWOULDBLOCK)
    551 				pcc = 0;
    552 			else if (pcc <= 0)
    553 				break;
    554 			else if (pibuf[0] == 0) {
    555 				pbp++, pcc--;
    556 			} else {
    557 				if (pkcontrol(pibuf[0])) {
    558 					pibuf[0] |= oobdata[0];
    559 					send(f, &pibuf[0], 1, MSG_OOB);
    560 				}
    561 				pcc = 0;
    562 			}
    563 		}
    564 		if (set[1].revents & POLLOUT && pcc > 0) {
    565 			cc = write(f, pbp, pcc);
    566 			if (cc > 0) {
    567 				pcc -= cc;
    568 				pbp += cc;
    569 			}
    570 		}
    571 	}
    572 }
    573 
    574 void
    575 cleanup(signo)
    576 	int signo;
    577 {
    578 	char *p, c;
    579 
    580 	p = line + sizeof(_PATH_DEV) - 1;
    581 #ifdef SUPPORT_UTMP
    582 	if (logout(p))
    583 		logwtmp(p, "", "");
    584 #endif
    585 #ifdef SUPPORT_UTMPX
    586 	if (logoutx(p, 0, DEAD_PROCESS))
    587 		logwtmpx(p, "", "", 0, DEAD_PROCESS);
    588 #endif
    589 	(void)chmod(line, 0666);
    590 	(void)chown(line, 0, 0);
    591 	c = *p; *p = 'p';
    592 	(void)chmod(line, 0666);
    593 	(void)chown(line, 0, 0);
    594 	*p = c;
    595 	if (ttyaction(line, "rlogind", "root"))
    596 		syslog(LOG_ERR, "%s: ttyaction failed", line);
    597 	shutdown(netf, 2);
    598 	exit(1);
    599 }
    600 
    601 void
    602 fatal(f, msg, syserr)
    603 	int f;
    604 	char *msg;
    605 	int syserr;
    606 {
    607 	int len;
    608 	char buf[BUFSIZ], *bp, *ep;
    609 
    610 	bp = buf;
    611 	ep = buf + sizeof(buf);
    612 
    613 	/*
    614 	 * Prepend binary one to message if we haven't sent
    615 	 * the magic null as confirmation.
    616 	 */
    617 	if (!confirmed)
    618 		*bp++ = '\001';		/* error indicator */
    619 	if (syserr)
    620 		len = snprintf(bp, ep - bp, "rlogind: %s: %s.\r\n",
    621 		    msg, strerror(errno));
    622 	else
    623 		len = snprintf(bp, ep - bp, "rlogind: %s.\r\n", msg);
    624 	(void) write(f, buf, bp + len - buf);
    625 	exit(1);
    626 }
    627 
    628 int
    629 do_rlogin(dest, host)
    630 	struct sockaddr *dest;
    631 	char *host;
    632 {
    633 	int retval;
    634 
    635 	getstr(rusername, sizeof(rusername), "remuser too long");
    636 	getstr(lusername, sizeof(lusername), "locuser too long");
    637 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
    638 
    639 	pwd = getpwnam(lusername);
    640 	if (pwd == NULL) {
    641 		syslog(LOG_INFO,
    642 		    "%s@%s as %s: unknown login.", rusername, host, lusername);
    643 		return (-1);
    644 	}
    645 
    646 	retval = iruserok_sa(dest, dest->sa_len, pwd->pw_uid == 0, rusername,
    647 		lusername);
    648 /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
    649 	if (retval == 0) {
    650 		if (log_success)
    651 			syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
    652 			    rusername, host, lusername);
    653 	} else {
    654 		if (__rcmd_errstr)
    655 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
    656 			    rusername, host, lusername, __rcmd_errstr);
    657 		else
    658 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
    659 			    rusername, host, lusername);
    660 	}
    661 	return(retval);
    662 }
    663 
    664 void
    665 getstr(buf, cnt, errmsg)
    666 	char *buf;
    667 	int cnt;
    668 	char *errmsg;
    669 {
    670 	char c;
    671 
    672 	do {
    673 		if (read(0, &c, 1) != 1)
    674 			exit(1);
    675 		if (--cnt < 0)
    676 			fatal(STDOUT_FILENO, errmsg, 0);
    677 		*buf++ = c;
    678 	} while (c != 0);
    679 }
    680 
    681 
    682 void
    683 setup_term(fd)
    684 	int fd;
    685 {
    686 	char *cp = index(term+ENVSIZE, '/');
    687 	char *speed;
    688 	struct termios tt;
    689 
    690 #ifndef notyet
    691 	tcgetattr(fd, &tt);
    692 	if (cp) {
    693 		*cp++ = '\0';
    694 		speed = cp;
    695 		cp = index(speed, '/');
    696 		if (cp)
    697 			*cp++ = '\0';
    698 		cfsetspeed(&tt, atoi(speed));
    699 	}
    700 
    701 	tt.c_iflag = TTYDEF_IFLAG;
    702 	tt.c_oflag = TTYDEF_OFLAG;
    703 	tt.c_lflag = TTYDEF_LFLAG;
    704 	tcsetattr(fd, TCSAFLUSH, &tt);
    705 #else
    706 	if (cp) {
    707 		*cp++ = '\0';
    708 		speed = cp;
    709 		cp = index(speed, '/');
    710 		if (cp)
    711 			*cp++ = '\0';
    712 		tcgetattr(fd, &tt);
    713 		cfsetspeed(&tt, atoi(speed));
    714 		tcsetattr(fd, TCSAFLUSH, &tt);
    715 	}
    716 #endif
    717 
    718 	env[0] = term;
    719 	env[1] = 0;
    720 	environ = env;
    721 }
    722 
    723 
    724 void
    725 usage()
    726 {
    727 	syslog(LOG_ERR, "usage: rlogind [-alnL]");
    728 }
    729 
    730 /*
    731  * Check whether host h is in our local domain,
    732  * defined as sharing the last two components of the domain part,
    733  * or the entire domain part if the local domain has only one component.
    734  * If either name is unqualified (contains no '.'),
    735  * assume that the host is local, as it will be
    736  * interpreted as such.
    737  */
    738 int
    739 local_domain(h)
    740 	char *h;
    741 {
    742 	char localhost[MAXHOSTNAMELEN + 1];
    743 	char *p1, *p2;
    744 
    745 	localhost[0] = 0;
    746 	(void) gethostname(localhost, sizeof(localhost));
    747 	localhost[sizeof(localhost) - 1] = '\0';
    748 	p1 = topdomain(localhost);
    749 	p2 = topdomain(h);
    750 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
    751 		return (1);
    752 	return (0);
    753 }
    754 
    755 char *
    756 topdomain(h)
    757 	char *h;
    758 {
    759 	char *p;
    760 	char *maybe = NULL;
    761 	int dots = 0;
    762 
    763 	for (p = h + strlen(h); p >= h; p--) {
    764 		if (*p == '.') {
    765 			if (++dots == 2)
    766 				return (p);
    767 			maybe = p;
    768 		}
    769 	}
    770 	return (maybe);
    771 }
    772