Home | History | Annotate | Line # | Download | only in rlogind
rlogind.c revision 1.35
      1 /*	$NetBSD: rlogind.c,v 1.35 2004/11/15 21:01:18 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. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 #include <sys/cdefs.h>
     66 #ifndef lint
     67 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
     68 	The Regents of the University of California.  All rights reserved.\n");
     69 #if 0
     70 static char sccsid[] = "@(#)rlogind.c	8.2 (Berkeley) 4/28/95";
     71 #else
     72 __RCSID("$NetBSD: rlogind.c,v 1.35 2004/11/15 21:01:18 christos Exp $");
     73 #endif
     74 #endif /* not lint */
     75 
     76 /*
     77  * remote login server:
     78  *	\0
     79  *	remuser\0
     80  *	locuser\0
     81  *	terminal_type/speed\0
     82  *	data
     83  */
     84 
     85 #include <sys/param.h>
     86 #include <sys/stat.h>
     87 #include <sys/ioctl.h>
     88 #include <signal.h>
     89 #include <termios.h>
     90 #include <poll.h>
     91 #include <vis.h>
     92 
     93 #include <sys/socket.h>
     94 #include <netinet/in.h>
     95 #include <netinet/in_systm.h>
     96 #include <netinet/ip.h>
     97 #include <arpa/inet.h>
     98 #include <netdb.h>
     99 
    100 #include <pwd.h>
    101 #include <syslog.h>
    102 #include <errno.h>
    103 #include <stdio.h>
    104 #include <unistd.h>
    105 #include <stdlib.h>
    106 #include <string.h>
    107 #include <util.h>
    108 #include "pathnames.h"
    109 
    110 #ifndef TIOCPKT_WINDOW
    111 #define TIOCPKT_WINDOW 0x80
    112 #endif
    113 
    114 #define		OPTIONS			"alnL"
    115 
    116 char	*env[2];
    117 #define	NMAX 30
    118 char	lusername[NMAX+1], rusername[NMAX+1];
    119 static	char term[64] = "TERM=";
    120 #define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
    121 int	keepalive = 1;
    122 int	check_all = 0;
    123 int	log_success = 0;
    124 
    125 struct	passwd *pwd;
    126 
    127 void	doit __P((int, struct sockaddr_storage *));
    128 int	control __P((int, char *, int));
    129 void	protocol __P((int, int));
    130 void	cleanup __P((int));
    131 void	fatal __P((int, const char *, int));
    132 int	do_rlogin __P((struct sockaddr *, char *));
    133 void	getstr __P((char *, int, const char *));
    134 void	setup_term __P((int));
    135 #if 0
    136 int	do_krb_login __P((union sockunion *));
    137 #endif
    138 void	usage __P((void));
    139 int	local_domain __P((char *));
    140 char	*topdomain __P((char *));
    141 int	main __P((int, char *[]));
    142 
    143 extern int __check_rhosts_file;
    144 extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c */
    145 extern char **environ;
    146 
    147 int
    148 main(argc, argv)
    149 	int argc;
    150 	char *argv[];
    151 {
    152 	struct sockaddr_storage from;
    153 	int ch, on;
    154 	socklen_t fromlen = sizeof(from);
    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 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
    182 		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
    183 		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
    184 	}
    185 #ifdef INET6
    186 	if (from.ss_family == AF_INET6 &&
    187 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr) &&
    188 	    sizeof(struct sockaddr_in) <= sizeof(from)) {
    189 		struct sockaddr_in sin4;
    190 		struct sockaddr_in6 *sin6;
    191 		const int off = sizeof(struct sockaddr_in6) -
    192 		    sizeof(struct sockaddr_in);
    193 
    194 		sin6 = (struct sockaddr_in6 *)&from;
    195 		memset(&sin4, 0, sizeof(sin4));
    196 		sin4.sin_family = AF_INET;
    197 		sin4.sin_len = sizeof(struct sockaddr_in);
    198 		memcpy(&sin4.sin_addr, &sin6->sin6_addr.s6_addr[off],
    199 		    sizeof(sin4.sin_addr));
    200 		memcpy(&from, &sin4, sizeof(sin4));
    201 	}
    202 #else
    203 	if (from.ss_family == AF_INET6 &&
    204 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) {
    205 		char hbuf[NI_MAXHOST];
    206 		if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf,
    207 				sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
    208 			strlcpy(hbuf, "invalid", sizeof(hbuf));
    209 		}
    210 		syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)\n",
    211 		    hbuf);
    212 		exit(1);
    213 	}
    214 #endif
    215 	on = 1;
    216 	if (keepalive &&
    217 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
    218 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
    219 #if defined(IP_TOS)
    220 	if (from.ss_family == AF_INET) {
    221 		on = IPTOS_LOWDELAY;
    222 		if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
    223 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
    224 	}
    225 #endif
    226 	doit(0, &from);
    227 	/* NOTREACHED */
    228 #ifdef __GNUC__
    229 	exit(0);
    230 #endif
    231 }
    232 
    233 int	child;
    234 int	netf;
    235 char	line[MAXPATHLEN];
    236 int	confirmed;
    237 
    238 struct winsize win = { 0, 0, 0, 0 };
    239 
    240 
    241 void
    242 doit(f, fromp)
    243 	int f;
    244 	struct sockaddr_storage *fromp;
    245 {
    246 	int master, pid, on = 1;
    247 	int authenticated = 0;
    248 	char *hostname;
    249 	char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
    250 	char hostaddrbuf[sizeof(*fromp) * 4 + 1];
    251 	char c;
    252 	char naddr[NI_MAXHOST];
    253 	char saddr[NI_MAXHOST];
    254 	char raddr[NI_MAXHOST];
    255 	int af = fromp->ss_family;
    256 	u_int16_t *portp;
    257 	struct addrinfo hints, *res, *res0;
    258 	int gaierror;
    259 	socklen_t fromlen = fromp->ss_len > sizeof(*fromp)
    260 	    ? sizeof(*fromp) : fromp->ss_len;
    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, fromlen,
    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, fromlen,
    294 		    saddr, sizeof(saddr), NULL, 0, NI_NAMEREQD) == 0) {
    295 		/*
    296 		 * If name returned by getnameinfo is in our domain,
    297 		 * attempt to verify that we haven't been fooled by someone
    298 		 * in a remote net; look up the name and check that this
    299 		 * address corresponds to the name.
    300 		 */
    301 		hostname = saddr;
    302 		res0 = NULL;
    303 		if (check_all || local_domain(saddr)) {
    304 			strlcpy(hostnamebuf, saddr, sizeof(hostnamebuf));
    305 			memset(&hints, 0, sizeof(hints));
    306 			hints.ai_family = fromp->ss_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_NOTICE,
    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->ss_family)
    318 						continue;
    319 					if (res->ai_addrlen != fromp->ss_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 			}
    341 		}
    342 		strlcpy(hostnamebuf, hostname, sizeof(hostnamebuf));
    343 		hostname = hostnamebuf;
    344 		if (res0)
    345 			freeaddrinfo(res0);
    346 	} else {
    347 		strlcpy(hostnamebuf, naddr, sizeof(hostnamebuf));
    348 		hostname = hostnamebuf;
    349 	}
    350 
    351 	if (ntohs(*portp) >= IPPORT_RESERVED ||
    352 	    ntohs(*portp) < IPPORT_RESERVED/2) {
    353 		syslog(LOG_NOTICE, "Connection from %s on illegal port",
    354 		       naddr);
    355 		fatal(f, "Permission denied", 0);
    356 	}
    357 #ifdef IP_OPTIONS
    358 	if (fromp->ss_family == AF_INET) {
    359 		u_char optbuf[BUFSIZ/3], *cp;
    360 		char lbuf[BUFSIZ], *lp, *ep;
    361 		int optsize = sizeof(optbuf), ipproto;
    362 		struct protoent *ip;
    363 
    364 		if ((ip = getprotobyname("ip")) != NULL)
    365 			ipproto = ip->p_proto;
    366 		else
    367 			ipproto = IPPROTO_IP;
    368 		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
    369 		    &optsize) == 0 && optsize != 0) {
    370 			lp = lbuf;
    371 			ep = lbuf + sizeof(lbuf);
    372 			for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
    373 				snprintf(lp, ep - lp, " %2.2x", *cp);
    374 			syslog(LOG_NOTICE,
    375 			    "Connection received using IP options (ignored):%s",
    376 			    lbuf);
    377 			if (setsockopt(0, ipproto, IP_OPTIONS,
    378 			    (char *)NULL, optsize) != 0) {
    379 				syslog(LOG_ERR,
    380 				    "setsockopt IP_OPTIONS NULL: %m");
    381 				exit(1);
    382 			}
    383 		}
    384 	}
    385 #endif
    386 	if (do_rlogin((struct sockaddr *)fromp, hostname) == 0)
    387 		authenticated++;
    388 	if (confirmed == 0) {
    389 		write(f, "", 1);
    390 		confirmed = 1;		/* we sent the null! */
    391 	}
    392 	netf = f;
    393 
    394 	pid = forkpty(&master, line, NULL, &win);
    395 	if (pid < 0) {
    396 		if (errno == ENOENT)
    397 			fatal(f, "Out of ptys", 0);
    398 		else
    399 			fatal(f, "Forkpty", 1);
    400 	}
    401 	if (pid == 0) {
    402 		if (f > 2)	/* f should always be 0, but... */
    403 			(void) close(f);
    404 		setup_term(0);
    405 		(void)strvisx(hostaddrbuf, (const char *)(const void *)fromp,
    406 		    sizeof(*fromp), VIS_WHITE);
    407 		if (authenticated)
    408 			execl(_PATH_LOGIN, "login", "-p",
    409 			    "-h", hostname, "-a", hostaddrbuf,
    410 			    "-f", "--", lusername, (char *)0);
    411 		else
    412 			execl(_PATH_LOGIN, "login", "-p",
    413 			    "-h", hostname, "-a", hostaddrbuf,
    414 			    "--", lusername, (char *)0);
    415 		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
    416 		/*NOTREACHED*/
    417 	}
    418 	ioctl(f, FIONBIO, &on);
    419 	ioctl(master, FIONBIO, &on);
    420 	ioctl(master, TIOCPKT, &on);
    421 	signal(SIGCHLD, cleanup);
    422 	protocol(f, master);
    423 	signal(SIGCHLD, SIG_IGN);
    424 	cleanup(0);
    425 }
    426 
    427 char	magic[2] = { 0377, 0377 };
    428 char	oobdata[] = {TIOCPKT_WINDOW};
    429 
    430 /*
    431  * Handle a "control" request (signaled by magic being present)
    432  * in the data stream.  For now, we are only willing to handle
    433  * window size changes.
    434  */
    435 int
    436 control(pty, cp, n)
    437 	int pty;
    438 	char *cp;
    439 	int n;
    440 {
    441 	struct winsize w;
    442 
    443 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
    444 		return (0);
    445 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
    446 	memmove(&w, cp+4, sizeof(w));
    447 	w.ws_row = ntohs(w.ws_row);
    448 	w.ws_col = ntohs(w.ws_col);
    449 	w.ws_xpixel = ntohs(w.ws_xpixel);
    450 	w.ws_ypixel = ntohs(w.ws_ypixel);
    451 	(void)ioctl(pty, TIOCSWINSZ, &w);
    452 	return (4+sizeof (w));
    453 }
    454 
    455 /*
    456  * rlogin "protocol" machine.
    457  */
    458 void
    459 protocol(f, p)
    460 	int f, p;
    461 {
    462 	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
    463 					/* XXX gcc above */
    464 	int pcc = 0, fcc = 0;
    465 	int cc, nfd;
    466 	char cntl;
    467 	struct pollfd set[2];
    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 	set[0].fd = p;
    477 	set[1].fd = f;
    478 	for (;;) {
    479 		set[0].events = POLLPRI;
    480 		set[1].events = 0;
    481 		if (fcc)
    482 			set[0].events |= POLLOUT;
    483 		else
    484 			set[1].events |= POLLIN;
    485 		if (pcc >= 0) {
    486 			if (pcc)
    487 				set[1].events |= POLLOUT;
    488 			else
    489 				set[0].events |= POLLIN;
    490 		}
    491 		if ((nfd = poll(set, 2, INFTIM)) < 0) {
    492 			if (errno == EINTR)
    493 				continue;
    494 			fatal(f, "poll", 1);
    495 		}
    496 		if (nfd == 0) {
    497 			/* shouldn't happen... */
    498 			sleep(5);
    499 			continue;
    500 		}
    501 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
    502 		if (set[0].revents & POLLPRI) {
    503 			cc = read(p, &cntl, 1);
    504 			if (cc == 1 && pkcontrol(cntl)) {
    505 				cntl |= oobdata[0];
    506 				send(f, &cntl, 1, MSG_OOB);
    507 				if (cntl & TIOCPKT_FLUSHWRITE)
    508 					pcc = 0;
    509 			}
    510 		}
    511 		if (set[1].revents & POLLIN) {
    512 			fcc = read(f, fibuf, sizeof(fibuf));
    513 			if (fcc < 0 && errno == EWOULDBLOCK)
    514 				fcc = 0;
    515 			else {
    516 				char *cp;
    517 				int left, n;
    518 
    519 				if (fcc <= 0)
    520 					break;
    521 				fbp = fibuf;
    522 
    523 			top:
    524 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
    525 					if (cp[0] == magic[0] &&
    526 					    cp[1] == magic[1]) {
    527 						left = fcc - (cp-fibuf);
    528 						n = control(p, cp, left);
    529 						if (n) {
    530 							left -= n;
    531 							if (left > 0)
    532 								memmove(cp,
    533 								    cp+n,
    534 								    left);
    535 							fcc -= n;
    536 							goto top; /* n^2 */
    537 						}
    538 					}
    539 			}
    540 		}
    541 
    542 		if (set[0].revents & POLLOUT && fcc > 0) {
    543 			cc = write(p, fbp, fcc);
    544 			if (cc > 0) {
    545 				fcc -= cc;
    546 				fbp += cc;
    547 			}
    548 		}
    549 
    550 		if (set[0].revents & POLLIN) {
    551 			pcc = read(p, pibuf, sizeof (pibuf));
    552 			pbp = pibuf;
    553 			if (pcc < 0 && errno == EWOULDBLOCK)
    554 				pcc = 0;
    555 			else if (pcc <= 0)
    556 				break;
    557 			else if (pibuf[0] == 0) {
    558 				pbp++, pcc--;
    559 			} else {
    560 				if (pkcontrol(pibuf[0])) {
    561 					pibuf[0] |= oobdata[0];
    562 					send(f, &pibuf[0], 1, MSG_OOB);
    563 				}
    564 				pcc = 0;
    565 			}
    566 		}
    567 		if (set[1].revents & POLLOUT && pcc > 0) {
    568 			cc = write(f, pbp, pcc);
    569 			if (cc > 0) {
    570 				pcc -= cc;
    571 				pbp += cc;
    572 			}
    573 		}
    574 	}
    575 }
    576 
    577 void
    578 cleanup(signo)
    579 	int signo;
    580 {
    581 	char *p, c;
    582 
    583 	p = line + sizeof(_PATH_DEV) - 1;
    584 #ifdef SUPPORT_UTMP
    585 	if (logout(p))
    586 		logwtmp(p, "", "");
    587 #endif
    588 #ifdef SUPPORT_UTMPX
    589 	if (logoutx(p, 0, DEAD_PROCESS))
    590 		logwtmpx(p, "", "", 0, DEAD_PROCESS);
    591 #endif
    592 	(void)chmod(line, 0666);
    593 	(void)chown(line, 0, 0);
    594 	c = *p; *p = 'p';
    595 	(void)chmod(line, 0666);
    596 	(void)chown(line, 0, 0);
    597 	*p = c;
    598 	if (ttyaction(line, "rlogind", "root"))
    599 		syslog(LOG_ERR, "%s: ttyaction failed", line);
    600 	shutdown(netf, 2);
    601 	exit(1);
    602 }
    603 
    604 void
    605 fatal(f, msg, syserr)
    606 	int f;
    607 	const char *msg;
    608 	int syserr;
    609 {
    610 	int len;
    611 	char buf[BUFSIZ], *bp, *ep;
    612 
    613 	bp = buf;
    614 	ep = buf + sizeof(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++ = '\001';		/* error indicator */
    622 	if (syserr)
    623 		len = snprintf(bp, ep - bp, "rlogind: %s: %s.\r\n",
    624 		    msg, strerror(errno));
    625 	else
    626 		len = snprintf(bp, ep - 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 	const 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