rlogind.c revision 141918
11592Srgrimes/*-
21592Srgrimes * Copyright (c) 1983, 1988, 1989, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
496196Sdes * Copyright (c) 2002 Networks Associates Technology, Inc.
596196Sdes * All rights reserved.
61592Srgrimes *
796196Sdes * Portions of this software were developed for the FreeBSD Project by
896196Sdes * ThinkSec AS and NAI Labs, the Security Research Division of Network
996196Sdes * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1096196Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
1196196Sdes *
121592Srgrimes * Redistribution and use in source and binary forms, with or without
131592Srgrimes * modification, are permitted provided that the following conditions
141592Srgrimes * are met:
151592Srgrimes * 1. Redistributions of source code must retain the above copyright
161592Srgrimes *    notice, this list of conditions and the following disclaimer.
171592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
181592Srgrimes *    notice, this list of conditions and the following disclaimer in the
191592Srgrimes *    documentation and/or other materials provided with the distribution.
201592Srgrimes * 3. All advertising materials mentioning features or use of this software
211592Srgrimes *    must display the following acknowledgement:
221592Srgrimes *	This product includes software developed by the University of
231592Srgrimes *	California, Berkeley and its contributors.
241592Srgrimes * 4. Neither the name of the University nor the names of its contributors
251592Srgrimes *    may be used to endorse or promote products derived from this software
261592Srgrimes *    without specific prior written permission.
271592Srgrimes *
281592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
291592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
301592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
311592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
321592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
331592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
341592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
361592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
371592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381592Srgrimes * SUCH DAMAGE.
391592Srgrimes */
401592Srgrimes
41114624Sobrien#if 0
421592Srgrimes#ifndef lint
4329916Smarkmstatic const char copyright[] =
441592Srgrimes"@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
451592Srgrimes	The Regents of the University of California.  All rights reserved.\n";
461592Srgrimes#endif /* not lint */
471592Srgrimes
481592Srgrimes#ifndef lint
4929916Smarkmstatic const char sccsid[] = "@(#)rlogind.c	8.1 (Berkeley) 6/4/93";
50114624Sobrien#endif /* not lint */
5131405Scharnier#endif
52114624Sobrien#include <sys/cdefs.h>
53114624Sobrien__FBSDID("$FreeBSD: head/libexec/rlogind/rlogind.c 141918 2005-02-14 17:42:58Z stefanf $");
541592Srgrimes
551592Srgrimes/*
561592Srgrimes * remote login server:
571592Srgrimes *	\0
581592Srgrimes *	remuser\0
591592Srgrimes *	locuser\0
601592Srgrimes *	terminal_type/speed\0
611592Srgrimes *	data
621592Srgrimes */
631592Srgrimes
641592Srgrimes#define	FD_SETSIZE	16		/* don't need many bits for select */
6529916Smarkm#include <sys/types.h>
661592Srgrimes#include <sys/param.h>
671592Srgrimes#include <sys/stat.h>
681592Srgrimes#include <sys/ioctl.h>
691592Srgrimes#include <signal.h>
701592Srgrimes#include <termios.h>
711592Srgrimes
721592Srgrimes#include <sys/socket.h>
731592Srgrimes#include <netinet/in.h>
741592Srgrimes#include <netinet/in_systm.h>
751592Srgrimes#include <netinet/ip.h>
7611486Sdg#include <netinet/tcp.h>
771592Srgrimes#include <arpa/inet.h>
781592Srgrimes#include <netdb.h>
791592Srgrimes
8031405Scharnier#include <errno.h>
8131405Scharnier#include <libutil.h>
8296196Sdes#include <paths.h>
831592Srgrimes#include <pwd.h>
841592Srgrimes#include <syslog.h>
851592Srgrimes#include <stdio.h>
861592Srgrimes#include <stdlib.h>
871592Srgrimes#include <string.h>
8831405Scharnier#include <unistd.h>
891592Srgrimes
9051433Smarkm
911592Srgrimes#ifndef TIOCPKT_WINDOW
921592Srgrimes#define TIOCPKT_WINDOW 0x80
931592Srgrimes#endif
941592Srgrimes
95141588Sru#define		ARGSTR			"Daln"
961592Srgrimes
9756590Sshin/* wrapper for KAME-special getnameinfo() */
9856590Sshin#ifndef NI_WITHSCOPEID
9956590Sshin#define	NI_WITHSCOPEID	0
10056590Sshin#endif
10156590Sshin
1021592Srgrimeschar	*env[2];
1031592Srgrimes#define	NMAX 30
1041592Srgrimeschar	lusername[NMAX+1], rusername[NMAX+1];
1051592Srgrimesstatic	char term[64] = "TERM=";
1061592Srgrimes#define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
1071592Srgrimesint	keepalive = 1;
1081592Srgrimesint	check_all = 0;
10911486Sdgint	no_delay;
1101592Srgrimes
1111592Srgrimesstruct	passwd *pwd;
1121592Srgrimes
11356590Sshinunion sockunion {
11456590Sshin	struct sockinet {
11556590Sshin		u_char si_len;
11656590Sshin		u_char si_family;
11756590Sshin		u_short si_port;
11856590Sshin	} su_si;
11956590Sshin	struct sockaddr_in  su_sin;
12056590Sshin	struct sockaddr_in6 su_sin6;
12156590Sshin};
12256590Sshin#define su_len		su_si.si_len
12356590Sshin#define su_family	su_si.si_family
12456590Sshin#define su_port		su_si.si_port
12556590Sshin
12690377Simpvoid	doit(int, union sockunion *);
12790377Simpint	control(int, char *, int);
12890377Simpvoid	protocol(int, int);
12990377Simpvoid	cleanup(int);
13090377Simpvoid	fatal(int, char *, int);
13190377Simpint	do_rlogin(union sockunion *);
13290377Simpvoid	getstr(char *, int, char *);
13390377Simpvoid	setup_term(int);
13490377Simpint	do_krb_login(struct sockaddr_in *);
13590377Simpvoid	usage(void);
1361592Srgrimes
13751433Smarkm
1381592Srgrimesint
13990377Simpmain(int argc, char *argv[])
1401592Srgrimes{
1411592Srgrimes	extern int __check_rhosts_file;
14256590Sshin	union sockunion from;
143141918Sstefanf	socklen_t fromlen;
144141918Sstefanf	int ch, on;
1451592Srgrimes
1461592Srgrimes	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
1471592Srgrimes
1481592Srgrimes	opterr = 0;
14924349Simp	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
1501592Srgrimes		switch (ch) {
15111486Sdg		case 'D':
15211486Sdg			no_delay = 1;
15311486Sdg			break;
1541592Srgrimes		case 'a':
1551592Srgrimes			check_all = 1;
1561592Srgrimes			break;
1571592Srgrimes		case 'l':
1581592Srgrimes			__check_rhosts_file = 0;
1591592Srgrimes			break;
1601592Srgrimes		case 'n':
1611592Srgrimes			keepalive = 0;
1621592Srgrimes			break;
1631592Srgrimes		case '?':
1641592Srgrimes		default:
1651592Srgrimes			usage();
1661592Srgrimes			break;
1671592Srgrimes		}
1681592Srgrimes	argc -= optind;
1691592Srgrimes	argv += optind;
1701592Srgrimes
1711592Srgrimes	fromlen = sizeof (from);
1721592Srgrimes	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
1731592Srgrimes		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
1741592Srgrimes		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
1751592Srgrimes	}
1761592Srgrimes	on = 1;
1771592Srgrimes	if (keepalive &&
1781592Srgrimes	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
1791592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
18011486Sdg	if (no_delay &&
18111486Sdg	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
18211486Sdg		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
18396196Sdes	if (from.su_family == AF_INET)
18456590Sshin      {
1851592Srgrimes	on = IPTOS_LOWDELAY;
1861592Srgrimes	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1871592Srgrimes		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
18856590Sshin      }
18911486Sdg
1901592Srgrimes	doit(0, &from);
19129916Smarkm	return 0;
1921592Srgrimes}
1931592Srgrimes
1941592Srgrimesint	child;
1951592Srgrimesint	netf;
1961592Srgrimeschar	line[MAXPATHLEN];
1971592Srgrimesint	confirmed;
1981592Srgrimes
1991592Srgrimesstruct winsize win = { 0, 0, 0, 0 };
2001592Srgrimes
2011592Srgrimes
2021592Srgrimesvoid
20390377Simpdoit(int f, union sockunion *fromp)
2041592Srgrimes{
2051592Srgrimes	int master, pid, on = 1;
2061592Srgrimes	int authenticated = 0;
20756590Sshin	char hostname[2 * MAXHOSTNAMELEN + 1];
20856590Sshin	char nameinfo[2 * INET6_ADDRSTRLEN + 1];
2091592Srgrimes	char c;
2101592Srgrimes
2111592Srgrimes	alarm(60);
2121592Srgrimes	read(f, &c, 1);
2131592Srgrimes
2141592Srgrimes	if (c != 0)
2151592Srgrimes		exit(1);
2161592Srgrimes
2171592Srgrimes	alarm(0);
21856590Sshin
21956590Sshin	realhostname_sa(hostname, sizeof(hostname) - 1,
22056590Sshin			    (struct sockaddr *)fromp, fromp->su_len);
22156590Sshin	/* error check ? */
22256590Sshin	fromp->su_port = ntohs((u_short)fromp->su_port);
22324191Simp	hostname[sizeof(hostname) - 1] = '\0';
2241592Srgrimes
2251592Srgrimes	{
22663959Sume		if ((fromp->su_family != AF_INET
22756590Sshin#ifdef INET6
22863959Sume		  && fromp->su_family != AF_INET6
22956590Sshin#endif
23056590Sshin		     ) ||
23156590Sshin		    fromp->su_port >= IPPORT_RESERVED ||
23256590Sshin		    fromp->su_port < IPPORT_RESERVED/2) {
23356590Sshin			getnameinfo((struct sockaddr *)fromp,
23456590Sshin				    fromp->su_len,
23556590Sshin				    nameinfo, sizeof(nameinfo), NULL, 0,
23656590Sshin				    NI_NUMERICHOST|NI_WITHSCOPEID);
23756590Sshin			/* error check ? */
2381592Srgrimes			syslog(LOG_NOTICE, "Connection from %s on illegal port",
23956590Sshin			       nameinfo);
2401592Srgrimes			fatal(f, "Permission denied", 0);
2411592Srgrimes		}
2421592Srgrimes#ifdef IP_OPTIONS
24356590Sshin		if (fromp->su_family == AF_INET)
24496196Sdes	      {
24522455Simp		u_char optbuf[BUFSIZ/3];
246141918Sstefanf		socklen_t optsize = sizeof(optbuf);
247141918Sstefanf		int ipproto, i;
2481592Srgrimes		struct protoent *ip;
2491592Srgrimes
2501592Srgrimes		if ((ip = getprotobyname("ip")) != NULL)
2511592Srgrimes			ipproto = ip->p_proto;
2521592Srgrimes		else
2531592Srgrimes			ipproto = IPPROTO_IP;
2541592Srgrimes		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
2551592Srgrimes		    &optsize) == 0 && optsize != 0) {
25622455Simp			for (i = 0; i < optsize; ) {
25722455Simp				u_char c = optbuf[i];
25822455Simp				if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
25922455Simp					syslog(LOG_NOTICE,
26022455Simp						"Connection refused from %s with IP option %s",
26156590Sshin						inet_ntoa(fromp->su_sin.sin_addr),
26222455Simp						c == IPOPT_LSRR ? "LSRR" : "SSRR");
26322455Simp					exit(1);
26422455Simp				}
26522455Simp				if (c == IPOPT_EOL)
26622455Simp					break;
26722455Simp				i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
2681592Srgrimes			}
2691592Srgrimes		}
27056590Sshin	      }
2711592Srgrimes#endif
2721592Srgrimes		if (do_rlogin(fromp) == 0)
2731592Srgrimes			authenticated++;
2741592Srgrimes	}
2751592Srgrimes	if (confirmed == 0) {
2761592Srgrimes		write(f, "", 1);
2771592Srgrimes		confirmed = 1;		/* we sent the null! */
2781592Srgrimes	}
2791592Srgrimes	netf = f;
2801592Srgrimes
2811592Srgrimes	pid = forkpty(&master, line, NULL, &win);
2821592Srgrimes	if (pid < 0) {
2831592Srgrimes		if (errno == ENOENT)
2841592Srgrimes			fatal(f, "Out of ptys", 0);
2851592Srgrimes		else
2861592Srgrimes			fatal(f, "Forkpty", 1);
2871592Srgrimes	}
2881592Srgrimes	if (pid == 0) {
2891592Srgrimes		if (f > 2)	/* f should always be 0, but... */
2901592Srgrimes			(void) close(f);
2911592Srgrimes		setup_term(0);
29212575Snate		 if (*lusername=='-') {
2932076Sguido			syslog(LOG_ERR, "tried to pass user \"%s\" to login",
2942076Sguido			       lusername);
2952076Sguido			fatal(STDERR_FILENO, "invalid user", 0);
2962076Sguido		}
2971592Srgrimes		if (authenticated) {
2981592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
2991592Srgrimes			    "-h", hostname, "-f", lusername, (char *)NULL);
3001592Srgrimes		} else
3011592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3021592Srgrimes			    "-h", hostname, lusername, (char *)NULL);
3031592Srgrimes		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
3041592Srgrimes		/*NOTREACHED*/
3051592Srgrimes	}
306141588Sru	ioctl(f, FIONBIO, &on);
3071592Srgrimes	ioctl(master, FIONBIO, &on);
3081592Srgrimes	ioctl(master, TIOCPKT, &on);
3091592Srgrimes	signal(SIGCHLD, cleanup);
3101592Srgrimes	protocol(f, master);
3111592Srgrimes	signal(SIGCHLD, SIG_IGN);
3121592Srgrimes	cleanup(0);
3131592Srgrimes}
3141592Srgrimes
3151592Srgrimeschar	magic[2] = { 0377, 0377 };
3161592Srgrimeschar	oobdata[] = {TIOCPKT_WINDOW};
3171592Srgrimes
3181592Srgrimes/*
3191592Srgrimes * Handle a "control" request (signaled by magic being present)
3201592Srgrimes * in the data stream.  For now, we are only willing to handle
3211592Srgrimes * window size changes.
3221592Srgrimes */
3231592Srgrimesint
32490377Simpcontrol(int pty, char *cp, int n)
3251592Srgrimes{
3261592Srgrimes	struct winsize w;
3271592Srgrimes
328114624Sobrien	if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
3291592Srgrimes		return (0);
3301592Srgrimes	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
3311592Srgrimes	bcopy(cp+4, (char *)&w, sizeof(w));
3321592Srgrimes	w.ws_row = ntohs(w.ws_row);
3331592Srgrimes	w.ws_col = ntohs(w.ws_col);
3341592Srgrimes	w.ws_xpixel = ntohs(w.ws_xpixel);
3351592Srgrimes	w.ws_ypixel = ntohs(w.ws_ypixel);
3361592Srgrimes	(void)ioctl(pty, TIOCSWINSZ, &w);
3371592Srgrimes	return (4+sizeof (w));
3381592Srgrimes}
3391592Srgrimes
3401592Srgrimes/*
3411592Srgrimes * rlogin "protocol" machine.
3421592Srgrimes */
3431592Srgrimesvoid
34490377Simpprotocol(int f, int p)
3451592Srgrimes{
34669705Sru	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
34746078Simp	int pcc = 0, fcc = 0;
3481592Srgrimes	int cc, nfd, n;
3491592Srgrimes	char cntl;
3501592Srgrimes
3511592Srgrimes	/*
3521592Srgrimes	 * Must ignore SIGTTOU, otherwise we'll stop
3531592Srgrimes	 * when we try and set slave pty's window shape
3541592Srgrimes	 * (our controlling tty is the master pty).
3551592Srgrimes	 */
3561592Srgrimes	(void) signal(SIGTTOU, SIG_IGN);
3571592Srgrimes	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
3581592Srgrimes	if (f > p)
3591592Srgrimes		nfd = f + 1;
3601592Srgrimes	else
3611592Srgrimes		nfd = p + 1;
3621592Srgrimes	if (nfd > FD_SETSIZE) {
3631592Srgrimes		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
3641592Srgrimes		fatal(f, "internal error (select mask too small)", 0);
3651592Srgrimes	}
3661592Srgrimes	for (;;) {
3671592Srgrimes		fd_set ibits, obits, ebits, *omask;
3681592Srgrimes
3691592Srgrimes		FD_ZERO(&ebits);
3701592Srgrimes		FD_ZERO(&ibits);
3711592Srgrimes		FD_ZERO(&obits);
3721592Srgrimes		omask = (fd_set *)NULL;
3731592Srgrimes		if (fcc) {
3741592Srgrimes			FD_SET(p, &obits);
3751592Srgrimes			omask = &obits;
3761592Srgrimes		} else
3771592Srgrimes			FD_SET(f, &ibits);
37846078Simp		if (pcc >= 0) {
3791592Srgrimes			if (pcc) {
3801592Srgrimes				FD_SET(f, &obits);
3811592Srgrimes				omask = &obits;
3821592Srgrimes			} else
3831592Srgrimes				FD_SET(p, &ibits);
38446078Simp		}
3851592Srgrimes		FD_SET(p, &ebits);
3861592Srgrimes		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
3871592Srgrimes			if (errno == EINTR)
3881592Srgrimes				continue;
3891592Srgrimes			fatal(f, "select", 1);
3901592Srgrimes		}
3911592Srgrimes		if (n == 0) {
3921592Srgrimes			/* shouldn't happen... */
3931592Srgrimes			sleep(5);
3941592Srgrimes			continue;
3951592Srgrimes		}
3961592Srgrimes#define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
3971592Srgrimes		if (FD_ISSET(p, &ebits)) {
3981592Srgrimes			cc = read(p, &cntl, 1);
3991592Srgrimes			if (cc == 1 && pkcontrol(cntl)) {
4001592Srgrimes				cntl |= oobdata[0];
4011592Srgrimes				send(f, &cntl, 1, MSG_OOB);
4021592Srgrimes				if (cntl & TIOCPKT_FLUSHWRITE) {
4031592Srgrimes					pcc = 0;
4041592Srgrimes					FD_CLR(p, &ibits);
4051592Srgrimes				}
4061592Srgrimes			}
4071592Srgrimes		}
4081592Srgrimes		if (FD_ISSET(f, &ibits)) {
409141588Sru			fcc = read(f, fibuf, sizeof(fibuf));
4101592Srgrimes			if (fcc < 0 && errno == EWOULDBLOCK)
4111592Srgrimes				fcc = 0;
4121592Srgrimes			else {
41390377Simp				char *cp;
4141592Srgrimes				int left, n;
4151592Srgrimes
4161592Srgrimes				if (fcc <= 0)
4171592Srgrimes					break;
4181592Srgrimes				fbp = fibuf;
4191592Srgrimes
4201592Srgrimes			top:
4211592Srgrimes				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
4221592Srgrimes					if (cp[0] == magic[0] &&
4231592Srgrimes					    cp[1] == magic[1]) {
4241592Srgrimes						left = fcc - (cp-fibuf);
4251592Srgrimes						n = control(p, cp, left);
4261592Srgrimes						if (n) {
4271592Srgrimes							left -= n;
4281592Srgrimes							if (left > 0)
4291592Srgrimes								bcopy(cp+n, cp, left);
4301592Srgrimes							fcc -= n;
4311592Srgrimes							goto top; /* n^2 */
4321592Srgrimes						}
4331592Srgrimes					}
4341592Srgrimes				FD_SET(p, &obits);		/* try write */
4351592Srgrimes			}
4361592Srgrimes		}
4371592Srgrimes
4381592Srgrimes		if (FD_ISSET(p, &obits) && fcc > 0) {
4391592Srgrimes			cc = write(p, fbp, fcc);
4401592Srgrimes			if (cc > 0) {
4411592Srgrimes				fcc -= cc;
4421592Srgrimes				fbp += cc;
4431592Srgrimes			}
4441592Srgrimes		}
4451592Srgrimes
4461592Srgrimes		if (FD_ISSET(p, &ibits)) {
4471592Srgrimes			pcc = read(p, pibuf, sizeof (pibuf));
4481592Srgrimes			pbp = pibuf;
4491592Srgrimes			if (pcc < 0 && errno == EWOULDBLOCK)
4501592Srgrimes				pcc = 0;
4511592Srgrimes			else if (pcc <= 0)
4521592Srgrimes				break;
4531592Srgrimes			else if (pibuf[0] == 0) {
4541592Srgrimes				pbp++, pcc--;
455141588Sru				FD_SET(f, &obits);	/* try write */
4561592Srgrimes			} else {
4571592Srgrimes				if (pkcontrol(pibuf[0])) {
4581592Srgrimes					pibuf[0] |= oobdata[0];
4591592Srgrimes					send(f, &pibuf[0], 1, MSG_OOB);
4601592Srgrimes				}
4611592Srgrimes				pcc = 0;
4621592Srgrimes			}
4631592Srgrimes		}
4641592Srgrimes		if ((FD_ISSET(f, &obits)) && pcc > 0) {
465141588Sru			cc = write(f, pbp, pcc);
4661592Srgrimes			if (cc < 0 && errno == EWOULDBLOCK) {
4671592Srgrimes				/*
4681592Srgrimes				 * This happens when we try write after read
4691592Srgrimes				 * from p, but some old kernels balk at large
4701592Srgrimes				 * writes even when select returns true.
4711592Srgrimes				 */
4721592Srgrimes				if (!FD_ISSET(p, &ibits))
4731592Srgrimes					sleep(5);
4741592Srgrimes				continue;
4751592Srgrimes			}
4761592Srgrimes			if (cc > 0) {
4771592Srgrimes				pcc -= cc;
4781592Srgrimes				pbp += cc;
4791592Srgrimes			}
4801592Srgrimes		}
4811592Srgrimes	}
4821592Srgrimes}
4831592Srgrimes
4841592Srgrimesvoid
48590377Simpcleanup(int signo)
4861592Srgrimes{
4871592Srgrimes	char *p;
4881592Srgrimes
4891592Srgrimes	p = line + sizeof(_PATH_DEV) - 1;
4901592Srgrimes	if (logout(p))
4911592Srgrimes		logwtmp(p, "", "");
49250132Simp	(void)chflags(line, 0);
4931592Srgrimes	(void)chmod(line, 0666);
4941592Srgrimes	(void)chown(line, 0, 0);
4951592Srgrimes	*p = 'p';
49650132Simp	(void)chflags(line, 0);
4971592Srgrimes	(void)chmod(line, 0666);
4981592Srgrimes	(void)chown(line, 0, 0);
4991592Srgrimes	shutdown(netf, 2);
5001592Srgrimes	exit(1);
5011592Srgrimes}
5021592Srgrimes
5031592Srgrimesvoid
50490377Simpfatal(int f, char *msg, int syserr)
5051592Srgrimes{
5061592Srgrimes	int len;
5071592Srgrimes	char buf[BUFSIZ], *bp = buf;
5081592Srgrimes
5091592Srgrimes	/*
5101592Srgrimes	 * Prepend binary one to message if we haven't sent
5111592Srgrimes	 * the magic null as confirmation.
5121592Srgrimes	 */
5131592Srgrimes	if (!confirmed)
5141592Srgrimes		*bp++ = '\01';		/* error indicator */
5151592Srgrimes	if (syserr)
51664238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
5171592Srgrimes		    msg, strerror(errno));
5181592Srgrimes	else
51964238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
52081991Sbrian	if (len < 0)
52181972Sbrian		len = 0;
5221592Srgrimes	(void) write(f, buf, bp + len - buf);
5231592Srgrimes	exit(1);
5241592Srgrimes}
5251592Srgrimes
5261592Srgrimesint
52790377Simpdo_rlogin(union sockunion *dest)
5281592Srgrimes{
52966755Sru
5301592Srgrimes	getstr(rusername, sizeof(rusername), "remuser too long");
5311592Srgrimes	getstr(lusername, sizeof(lusername), "locuser too long");
5321592Srgrimes	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
5331592Srgrimes
5341592Srgrimes	pwd = getpwnam(lusername);
5351592Srgrimes	if (pwd == NULL)
5361592Srgrimes		return (-1);
5371592Srgrimes	/* XXX why don't we syslog() failure? */
53856590Sshin
53956939Sshin	return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
54056939Sshin			    lusername));
5411592Srgrimes}
5421592Srgrimes
5431592Srgrimesvoid
54490377Simpgetstr(char *buf, int cnt, char *errmsg)
5451592Srgrimes{
5461592Srgrimes	char c;
5471592Srgrimes
5481592Srgrimes	do {
54980381Ssheldonh		if (read(STDIN_FILENO, &c, 1) != 1)
5501592Srgrimes			exit(1);
5511592Srgrimes		if (--cnt < 0)
5521592Srgrimes			fatal(STDOUT_FILENO, errmsg, 0);
5531592Srgrimes		*buf++ = c;
5541592Srgrimes	} while (c != 0);
5551592Srgrimes}
5561592Srgrimes
5571592Srgrimesextern	char **environ;
5581592Srgrimes
5591592Srgrimesvoid
56090377Simpsetup_term(int fd)
5611592Srgrimes{
56290377Simp	char *cp = index(term+ENVSIZE, '/');
5631592Srgrimes	char *speed;
5641592Srgrimes	struct termios tt;
5651592Srgrimes
5661592Srgrimes#ifndef notyet
5671592Srgrimes	tcgetattr(fd, &tt);
5681592Srgrimes	if (cp) {
5691592Srgrimes		*cp++ = '\0';
5701592Srgrimes		speed = cp;
5711592Srgrimes		cp = index(speed, '/');
5721592Srgrimes		if (cp)
5731592Srgrimes			*cp++ = '\0';
5741592Srgrimes		cfsetspeed(&tt, atoi(speed));
5751592Srgrimes	}
5761592Srgrimes
5771592Srgrimes	tt.c_iflag = TTYDEF_IFLAG;
5781592Srgrimes	tt.c_oflag = TTYDEF_OFLAG;
5791592Srgrimes	tt.c_lflag = TTYDEF_LFLAG;
5801592Srgrimes	tcsetattr(fd, TCSAFLUSH, &tt);
5811592Srgrimes#else
5821592Srgrimes	if (cp) {
5831592Srgrimes		*cp++ = '\0';
5841592Srgrimes		speed = cp;
5851592Srgrimes		cp = index(speed, '/');
5861592Srgrimes		if (cp)
5871592Srgrimes			*cp++ = '\0';
5881592Srgrimes		tcgetattr(fd, &tt);
5891592Srgrimes		cfsetspeed(&tt, atoi(speed));
5901592Srgrimes		tcsetattr(fd, TCSAFLUSH, &tt);
5911592Srgrimes	}
5921592Srgrimes#endif
5931592Srgrimes
5941592Srgrimes	env[0] = term;
5951592Srgrimes	env[1] = 0;
5961592Srgrimes	environ = env;
5971592Srgrimes}
5981592Srgrimes
5991592Srgrimesvoid
60090377Simpusage(void)
6011592Srgrimes{
60251433Smarkm	syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");
6031592Srgrimes}
604