rlogind.c revision 81972
11592Srgrimes/*-
21592Srgrimes * Copyright (c) 1983, 1988, 1989, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * Redistribution and use in source and binary forms, with or without
61592Srgrimes * modification, are permitted provided that the following conditions
71592Srgrimes * are met:
81592Srgrimes * 1. Redistributions of source code must retain the above copyright
91592Srgrimes *    notice, this list of conditions and the following disclaimer.
101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111592Srgrimes *    notice, this list of conditions and the following disclaimer in the
121592Srgrimes *    documentation and/or other materials provided with the distribution.
131592Srgrimes * 3. All advertising materials mentioning features or use of this software
141592Srgrimes *    must display the following acknowledgement:
151592Srgrimes *	This product includes software developed by the University of
161592Srgrimes *	California, Berkeley and its contributors.
171592Srgrimes * 4. Neither the name of the University nor the names of its contributors
181592Srgrimes *    may be used to endorse or promote products derived from this software
191592Srgrimes *    without specific prior written permission.
201592Srgrimes *
211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311592Srgrimes * SUCH DAMAGE.
321592Srgrimes */
331592Srgrimes
341592Srgrimes#ifndef lint
3529916Smarkmstatic const char copyright[] =
361592Srgrimes"@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
371592Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381592Srgrimes#endif /* not lint */
391592Srgrimes
401592Srgrimes#ifndef lint
4131405Scharnier#if 0
4229916Smarkmstatic const char sccsid[] = "@(#)rlogind.c	8.1 (Berkeley) 6/4/93";
4331405Scharnier#endif
4431405Scharnierstatic const char rcsid[] =
4550476Speter  "$FreeBSD: head/libexec/rlogind/rlogind.c 81972 2001-08-20 12:50:21Z brian $";
461592Srgrimes#endif /* not lint */
471592Srgrimes
481592Srgrimes/*
491592Srgrimes * remote login server:
501592Srgrimes *	\0
511592Srgrimes *	remuser\0
521592Srgrimes *	locuser\0
531592Srgrimes *	terminal_type/speed\0
541592Srgrimes *	data
551592Srgrimes */
561592Srgrimes
571592Srgrimes#define	FD_SETSIZE	16		/* don't need many bits for select */
5829916Smarkm#include <sys/types.h>
591592Srgrimes#include <sys/param.h>
601592Srgrimes#include <sys/stat.h>
611592Srgrimes#include <sys/ioctl.h>
621592Srgrimes#include <signal.h>
631592Srgrimes#include <termios.h>
641592Srgrimes
651592Srgrimes#include <sys/socket.h>
661592Srgrimes#include <netinet/in.h>
671592Srgrimes#include <netinet/in_systm.h>
681592Srgrimes#include <netinet/ip.h>
6911486Sdg#include <netinet/tcp.h>
701592Srgrimes#include <arpa/inet.h>
711592Srgrimes#include <netdb.h>
721592Srgrimes
7331405Scharnier#include <errno.h>
7431405Scharnier#include <libutil.h>
751592Srgrimes#include <pwd.h>
761592Srgrimes#include <syslog.h>
771592Srgrimes#include <stdio.h>
781592Srgrimes#include <stdlib.h>
791592Srgrimes#include <string.h>
8031405Scharnier#include <unistd.h>
811592Srgrimes#include "pathnames.h"
821592Srgrimes
8351433Smarkm
841592Srgrimes#ifndef TIOCPKT_WINDOW
851592Srgrimes#define TIOCPKT_WINDOW 0x80
861592Srgrimes#endif
871592Srgrimes
8851433Smarkm#define		ARGSTR			"Dalnx"
891592Srgrimes
9056590Sshin/* wrapper for KAME-special getnameinfo() */
9156590Sshin#ifndef NI_WITHSCOPEID
9256590Sshin#define	NI_WITHSCOPEID	0
9356590Sshin#endif
9456590Sshin
951592Srgrimeschar	*env[2];
961592Srgrimes#define	NMAX 30
971592Srgrimeschar	lusername[NMAX+1], rusername[NMAX+1];
981592Srgrimesstatic	char term[64] = "TERM=";
991592Srgrimes#define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
1001592Srgrimesint	keepalive = 1;
1011592Srgrimesint	check_all = 0;
10211486Sdgint	no_delay;
1031592Srgrimes
1041592Srgrimesstruct	passwd *pwd;
1051592Srgrimes
10656590Sshinunion sockunion {
10756590Sshin	struct sockinet {
10856590Sshin		u_char si_len;
10956590Sshin		u_char si_family;
11056590Sshin		u_short si_port;
11156590Sshin	} su_si;
11256590Sshin	struct sockaddr_in  su_sin;
11356590Sshin	struct sockaddr_in6 su_sin6;
11456590Sshin};
11556590Sshin#define su_len		su_si.si_len
11656590Sshin#define su_family	su_si.si_family
11756590Sshin#define su_port		su_si.si_port
11856590Sshin
11956590Sshinvoid	doit __P((int, union sockunion *));
1201592Srgrimesint	control __P((int, char *, int));
1211592Srgrimesvoid	protocol __P((int, int));
1221592Srgrimesvoid	cleanup __P((int));
1231592Srgrimesvoid	fatal __P((int, char *, int));
12456590Sshinint	do_rlogin __P((union sockunion *));
1251592Srgrimesvoid	getstr __P((char *, int, char *));
1261592Srgrimesvoid	setup_term __P((int));
1271592Srgrimesint	do_krb_login __P((struct sockaddr_in *));
1281592Srgrimesvoid	usage __P((void));
1291592Srgrimes
13051433Smarkm
1311592Srgrimesint
1321592Srgrimesmain(argc, argv)
1331592Srgrimes	int argc;
1341592Srgrimes	char *argv[];
1351592Srgrimes{
1361592Srgrimes	extern int __check_rhosts_file;
13756590Sshin	union sockunion from;
1381592Srgrimes	int ch, fromlen, on;
1391592Srgrimes
1401592Srgrimes	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
1411592Srgrimes
1421592Srgrimes	opterr = 0;
14324349Simp	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
1441592Srgrimes		switch (ch) {
14511486Sdg		case 'D':
14611486Sdg			no_delay = 1;
14711486Sdg			break;
1481592Srgrimes		case 'a':
1491592Srgrimes			check_all = 1;
1501592Srgrimes			break;
1511592Srgrimes		case 'l':
1521592Srgrimes			__check_rhosts_file = 0;
1531592Srgrimes			break;
1541592Srgrimes		case 'n':
1551592Srgrimes			keepalive = 0;
1561592Srgrimes			break;
1571592Srgrimes#ifdef CRYPT
1581592Srgrimes		case 'x':
1591592Srgrimes			doencrypt = 1;
1601592Srgrimes			break;
1611592Srgrimes#endif
1621592Srgrimes		case '?':
1631592Srgrimes		default:
1641592Srgrimes			usage();
1651592Srgrimes			break;
1661592Srgrimes		}
1671592Srgrimes	argc -= optind;
1681592Srgrimes	argv += optind;
1691592Srgrimes
1701592Srgrimes	fromlen = sizeof (from);
1711592Srgrimes	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
1721592Srgrimes		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
1731592Srgrimes		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
1741592Srgrimes	}
1751592Srgrimes	on = 1;
1761592Srgrimes	if (keepalive &&
1771592Srgrimes	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
1781592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
17911486Sdg	if (no_delay &&
18011486Sdg	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
18111486Sdg		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
18256590Sshin        if (from.su_family == AF_INET)
18356590Sshin      {
1841592Srgrimes	on = IPTOS_LOWDELAY;
1851592Srgrimes	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1861592Srgrimes		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
18756590Sshin      }
18811486Sdg
1891592Srgrimes	doit(0, &from);
19029916Smarkm	return 0;
1911592Srgrimes}
1921592Srgrimes
1931592Srgrimesint	child;
1941592Srgrimesint	netf;
1951592Srgrimeschar	line[MAXPATHLEN];
1961592Srgrimesint	confirmed;
1971592Srgrimes
1981592Srgrimesstruct winsize win = { 0, 0, 0, 0 };
1991592Srgrimes
2001592Srgrimes
2011592Srgrimesvoid
2021592Srgrimesdoit(f, fromp)
2031592Srgrimes	int f;
20456590Sshin	union sockunion *fromp;
2051592Srgrimes{
2061592Srgrimes	int master, pid, on = 1;
2071592Srgrimes	int authenticated = 0;
20856590Sshin	char hostname[2 * MAXHOSTNAMELEN + 1];
20956590Sshin	char nameinfo[2 * INET6_ADDRSTRLEN + 1];
2101592Srgrimes	char c;
2111592Srgrimes
2121592Srgrimes	alarm(60);
2131592Srgrimes	read(f, &c, 1);
2141592Srgrimes
2151592Srgrimes	if (c != 0)
2161592Srgrimes		exit(1);
2171592Srgrimes
2181592Srgrimes	alarm(0);
21956590Sshin
22056590Sshin	realhostname_sa(hostname, sizeof(hostname) - 1,
22156590Sshin			    (struct sockaddr *)fromp, fromp->su_len);
22256590Sshin	/* error check ? */
22356590Sshin	fromp->su_port = ntohs((u_short)fromp->su_port);
22424191Simp	hostname[sizeof(hostname) - 1] = '\0';
2251592Srgrimes
2261592Srgrimes	{
22763959Sume		if ((fromp->su_family != AF_INET
22856590Sshin#ifdef INET6
22963959Sume		  && fromp->su_family != AF_INET6
23056590Sshin#endif
23156590Sshin		     ) ||
23256590Sshin		    fromp->su_port >= IPPORT_RESERVED ||
23356590Sshin		    fromp->su_port < IPPORT_RESERVED/2) {
23456590Sshin			getnameinfo((struct sockaddr *)fromp,
23556590Sshin				    fromp->su_len,
23656590Sshin				    nameinfo, sizeof(nameinfo), NULL, 0,
23756590Sshin				    NI_NUMERICHOST|NI_WITHSCOPEID);
23856590Sshin			/* error check ? */
2391592Srgrimes			syslog(LOG_NOTICE, "Connection from %s on illegal port",
24056590Sshin			       nameinfo);
2411592Srgrimes			fatal(f, "Permission denied", 0);
2421592Srgrimes		}
2431592Srgrimes#ifdef IP_OPTIONS
24456590Sshin		if (fromp->su_family == AF_INET)
24556590Sshin              {
24622455Simp		u_char optbuf[BUFSIZ/3];
24722455Simp		int optsize = sizeof(optbuf), 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#ifdef	CRYPT
2801592Srgrimes	if (doencrypt)
28129916Smarkm		(void) des_enc_write(f,
28229916Smarkm				     SECURE_MESSAGE,
28329916Smarkm				     strlen(SECURE_MESSAGE),
28429916Smarkm				     schedule, &kdata->session);
2851592Srgrimes#endif
2861592Srgrimes	netf = f;
2871592Srgrimes
2881592Srgrimes	pid = forkpty(&master, line, NULL, &win);
2891592Srgrimes	if (pid < 0) {
2901592Srgrimes		if (errno == ENOENT)
2911592Srgrimes			fatal(f, "Out of ptys", 0);
2921592Srgrimes		else
2931592Srgrimes			fatal(f, "Forkpty", 1);
2941592Srgrimes	}
2951592Srgrimes	if (pid == 0) {
2961592Srgrimes		if (f > 2)	/* f should always be 0, but... */
2971592Srgrimes			(void) close(f);
2981592Srgrimes		setup_term(0);
29912575Snate		 if (*lusername=='-') {
3002076Sguido			syslog(LOG_ERR, "tried to pass user \"%s\" to login",
3012076Sguido			       lusername);
3022076Sguido			fatal(STDERR_FILENO, "invalid user", 0);
3032076Sguido		}
3041592Srgrimes		if (authenticated) {
3051592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3061592Srgrimes			    "-h", hostname, "-f", lusername, (char *)NULL);
3071592Srgrimes		} else
3081592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3091592Srgrimes			    "-h", hostname, lusername, (char *)NULL);
3101592Srgrimes		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
3111592Srgrimes		/*NOTREACHED*/
3121592Srgrimes	}
3131592Srgrimes#ifdef	CRYPT
3141592Srgrimes	/*
3151592Srgrimes	 * If encrypted, don't turn on NBIO or the des read/write
3161592Srgrimes	 * routines will croak.
3171592Srgrimes	 */
3181592Srgrimes
3191592Srgrimes	if (!doencrypt)
3201592Srgrimes#endif
3211592Srgrimes		ioctl(f, FIONBIO, &on);
3221592Srgrimes	ioctl(master, FIONBIO, &on);
3231592Srgrimes	ioctl(master, TIOCPKT, &on);
3241592Srgrimes	signal(SIGCHLD, cleanup);
3251592Srgrimes	protocol(f, master);
3261592Srgrimes	signal(SIGCHLD, SIG_IGN);
3271592Srgrimes	cleanup(0);
3281592Srgrimes}
3291592Srgrimes
3301592Srgrimeschar	magic[2] = { 0377, 0377 };
3311592Srgrimeschar	oobdata[] = {TIOCPKT_WINDOW};
3321592Srgrimes
3331592Srgrimes/*
3341592Srgrimes * Handle a "control" request (signaled by magic being present)
3351592Srgrimes * in the data stream.  For now, we are only willing to handle
3361592Srgrimes * window size changes.
3371592Srgrimes */
3381592Srgrimesint
3391592Srgrimescontrol(pty, cp, n)
3401592Srgrimes	int pty;
3411592Srgrimes	char *cp;
3421592Srgrimes	int n;
3431592Srgrimes{
3441592Srgrimes	struct winsize w;
3451592Srgrimes
3461592Srgrimes	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
3471592Srgrimes		return (0);
3481592Srgrimes	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
3491592Srgrimes	bcopy(cp+4, (char *)&w, sizeof(w));
3501592Srgrimes	w.ws_row = ntohs(w.ws_row);
3511592Srgrimes	w.ws_col = ntohs(w.ws_col);
3521592Srgrimes	w.ws_xpixel = ntohs(w.ws_xpixel);
3531592Srgrimes	w.ws_ypixel = ntohs(w.ws_ypixel);
3541592Srgrimes	(void)ioctl(pty, TIOCSWINSZ, &w);
3551592Srgrimes	return (4+sizeof (w));
3561592Srgrimes}
3571592Srgrimes
3581592Srgrimes/*
3591592Srgrimes * rlogin "protocol" machine.
3601592Srgrimes */
3611592Srgrimesvoid
3621592Srgrimesprotocol(f, p)
3631592Srgrimes	register int f, p;
3641592Srgrimes{
36569705Sru	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
36646078Simp	int pcc = 0, fcc = 0;
3671592Srgrimes	int cc, nfd, n;
3681592Srgrimes	char cntl;
3691592Srgrimes
3701592Srgrimes	/*
3711592Srgrimes	 * Must ignore SIGTTOU, otherwise we'll stop
3721592Srgrimes	 * when we try and set slave pty's window shape
3731592Srgrimes	 * (our controlling tty is the master pty).
3741592Srgrimes	 */
3751592Srgrimes	(void) signal(SIGTTOU, SIG_IGN);
3761592Srgrimes	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
3771592Srgrimes	if (f > p)
3781592Srgrimes		nfd = f + 1;
3791592Srgrimes	else
3801592Srgrimes		nfd = p + 1;
3811592Srgrimes	if (nfd > FD_SETSIZE) {
3821592Srgrimes		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
3831592Srgrimes		fatal(f, "internal error (select mask too small)", 0);
3841592Srgrimes	}
3851592Srgrimes	for (;;) {
3861592Srgrimes		fd_set ibits, obits, ebits, *omask;
3871592Srgrimes
3881592Srgrimes		FD_ZERO(&ebits);
3891592Srgrimes		FD_ZERO(&ibits);
3901592Srgrimes		FD_ZERO(&obits);
3911592Srgrimes		omask = (fd_set *)NULL;
3921592Srgrimes		if (fcc) {
3931592Srgrimes			FD_SET(p, &obits);
3941592Srgrimes			omask = &obits;
3951592Srgrimes		} else
3961592Srgrimes			FD_SET(f, &ibits);
39746078Simp		if (pcc >= 0) {
3981592Srgrimes			if (pcc) {
3991592Srgrimes				FD_SET(f, &obits);
4001592Srgrimes				omask = &obits;
4011592Srgrimes			} else
4021592Srgrimes				FD_SET(p, &ibits);
40346078Simp		}
4041592Srgrimes		FD_SET(p, &ebits);
4051592Srgrimes		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
4061592Srgrimes			if (errno == EINTR)
4071592Srgrimes				continue;
4081592Srgrimes			fatal(f, "select", 1);
4091592Srgrimes		}
4101592Srgrimes		if (n == 0) {
4111592Srgrimes			/* shouldn't happen... */
4121592Srgrimes			sleep(5);
4131592Srgrimes			continue;
4141592Srgrimes		}
4151592Srgrimes#define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
4161592Srgrimes		if (FD_ISSET(p, &ebits)) {
4171592Srgrimes			cc = read(p, &cntl, 1);
4181592Srgrimes			if (cc == 1 && pkcontrol(cntl)) {
4191592Srgrimes				cntl |= oobdata[0];
4201592Srgrimes				send(f, &cntl, 1, MSG_OOB);
4211592Srgrimes				if (cntl & TIOCPKT_FLUSHWRITE) {
4221592Srgrimes					pcc = 0;
4231592Srgrimes					FD_CLR(p, &ibits);
4241592Srgrimes				}
4251592Srgrimes			}
4261592Srgrimes		}
4271592Srgrimes		if (FD_ISSET(f, &ibits)) {
4281592Srgrimes#ifdef	CRYPT
4291592Srgrimes			if (doencrypt)
43029916Smarkm				fcc = des_enc_read(f, fibuf, sizeof(fibuf),
43129916Smarkm					schedule, &kdata->session);
4321592Srgrimes			else
4331592Srgrimes#endif
4341592Srgrimes				fcc = read(f, fibuf, sizeof(fibuf));
4351592Srgrimes			if (fcc < 0 && errno == EWOULDBLOCK)
4361592Srgrimes				fcc = 0;
4371592Srgrimes			else {
4381592Srgrimes				register char *cp;
4391592Srgrimes				int left, n;
4401592Srgrimes
4411592Srgrimes				if (fcc <= 0)
4421592Srgrimes					break;
4431592Srgrimes				fbp = fibuf;
4441592Srgrimes
4451592Srgrimes			top:
4461592Srgrimes				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
4471592Srgrimes					if (cp[0] == magic[0] &&
4481592Srgrimes					    cp[1] == magic[1]) {
4491592Srgrimes						left = fcc - (cp-fibuf);
4501592Srgrimes						n = control(p, cp, left);
4511592Srgrimes						if (n) {
4521592Srgrimes							left -= n;
4531592Srgrimes							if (left > 0)
4541592Srgrimes								bcopy(cp+n, cp, left);
4551592Srgrimes							fcc -= n;
4561592Srgrimes							goto top; /* n^2 */
4571592Srgrimes						}
4581592Srgrimes					}
4591592Srgrimes				FD_SET(p, &obits);		/* try write */
4601592Srgrimes			}
4611592Srgrimes		}
4621592Srgrimes
4631592Srgrimes		if (FD_ISSET(p, &obits) && fcc > 0) {
4641592Srgrimes			cc = write(p, fbp, fcc);
4651592Srgrimes			if (cc > 0) {
4661592Srgrimes				fcc -= cc;
4671592Srgrimes				fbp += cc;
4681592Srgrimes			}
4691592Srgrimes		}
4701592Srgrimes
4711592Srgrimes		if (FD_ISSET(p, &ibits)) {
4721592Srgrimes			pcc = read(p, pibuf, sizeof (pibuf));
4731592Srgrimes			pbp = pibuf;
4741592Srgrimes			if (pcc < 0 && errno == EWOULDBLOCK)
4751592Srgrimes				pcc = 0;
4761592Srgrimes			else if (pcc <= 0)
4771592Srgrimes				break;
4781592Srgrimes			else if (pibuf[0] == 0) {
4791592Srgrimes				pbp++, pcc--;
4801592Srgrimes#ifdef	CRYPT
4811592Srgrimes				if (!doencrypt)
4821592Srgrimes#endif
4831592Srgrimes					FD_SET(f, &obits);	/* try write */
4841592Srgrimes			} else {
4851592Srgrimes				if (pkcontrol(pibuf[0])) {
4861592Srgrimes					pibuf[0] |= oobdata[0];
4871592Srgrimes					send(f, &pibuf[0], 1, MSG_OOB);
4881592Srgrimes				}
4891592Srgrimes				pcc = 0;
4901592Srgrimes			}
4911592Srgrimes		}
4921592Srgrimes		if ((FD_ISSET(f, &obits)) && pcc > 0) {
4931592Srgrimes#ifdef	CRYPT
4941592Srgrimes			if (doencrypt)
49529916Smarkm				cc = des_enc_write(f, pbp, pcc,
49629916Smarkm					schedule, &kdata->session);
4971592Srgrimes			else
4981592Srgrimes#endif
4991592Srgrimes				cc = write(f, pbp, pcc);
5001592Srgrimes			if (cc < 0 && errno == EWOULDBLOCK) {
5011592Srgrimes				/*
5021592Srgrimes				 * This happens when we try write after read
5031592Srgrimes				 * from p, but some old kernels balk at large
5041592Srgrimes				 * writes even when select returns true.
5051592Srgrimes				 */
5061592Srgrimes				if (!FD_ISSET(p, &ibits))
5071592Srgrimes					sleep(5);
5081592Srgrimes				continue;
5091592Srgrimes			}
5101592Srgrimes			if (cc > 0) {
5111592Srgrimes				pcc -= cc;
5121592Srgrimes				pbp += cc;
5131592Srgrimes			}
5141592Srgrimes		}
5151592Srgrimes	}
5161592Srgrimes}
5171592Srgrimes
5181592Srgrimesvoid
5191592Srgrimescleanup(signo)
5201592Srgrimes	int signo;
5211592Srgrimes{
5221592Srgrimes	char *p;
5231592Srgrimes
5241592Srgrimes	p = line + sizeof(_PATH_DEV) - 1;
5251592Srgrimes	if (logout(p))
5261592Srgrimes		logwtmp(p, "", "");
52750132Simp	(void)chflags(line, 0);
5281592Srgrimes	(void)chmod(line, 0666);
5291592Srgrimes	(void)chown(line, 0, 0);
5301592Srgrimes	*p = 'p';
53150132Simp	(void)chflags(line, 0);
5321592Srgrimes	(void)chmod(line, 0666);
5331592Srgrimes	(void)chown(line, 0, 0);
5341592Srgrimes	shutdown(netf, 2);
5351592Srgrimes	exit(1);
5361592Srgrimes}
5371592Srgrimes
5381592Srgrimesvoid
5391592Srgrimesfatal(f, msg, syserr)
5401592Srgrimes	int f;
5411592Srgrimes	char *msg;
5421592Srgrimes	int syserr;
5431592Srgrimes{
5441592Srgrimes	int len;
5451592Srgrimes	char buf[BUFSIZ], *bp = buf;
5461592Srgrimes
5471592Srgrimes	/*
5481592Srgrimes	 * Prepend binary one to message if we haven't sent
5491592Srgrimes	 * the magic null as confirmation.
5501592Srgrimes	 */
5511592Srgrimes	if (!confirmed)
5521592Srgrimes		*bp++ = '\01';		/* error indicator */
5531592Srgrimes	if (syserr)
55464238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
5551592Srgrimes		    msg, strerror(errno));
5561592Srgrimes	else
55764238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
55881972Sbrian	if (len == -1)
55981972Sbrian		len = 0;
5601592Srgrimes	(void) write(f, buf, bp + len - buf);
5611592Srgrimes	exit(1);
5621592Srgrimes}
5631592Srgrimes
5641592Srgrimesint
5651592Srgrimesdo_rlogin(dest)
56656590Sshin	union sockunion *dest;
5671592Srgrimes{
56866755Sru
5691592Srgrimes	getstr(rusername, sizeof(rusername), "remuser too long");
5701592Srgrimes	getstr(lusername, sizeof(lusername), "locuser too long");
5711592Srgrimes	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
5721592Srgrimes
5731592Srgrimes	pwd = getpwnam(lusername);
5741592Srgrimes	if (pwd == NULL)
5751592Srgrimes		return (-1);
5761592Srgrimes	/* XXX why don't we syslog() failure? */
57756590Sshin
57856939Sshin	return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
57956939Sshin			    lusername));
5801592Srgrimes}
5811592Srgrimes
5821592Srgrimesvoid
5831592Srgrimesgetstr(buf, cnt, errmsg)
5841592Srgrimes	char *buf;
5851592Srgrimes	int cnt;
5861592Srgrimes	char *errmsg;
5871592Srgrimes{
5881592Srgrimes	char c;
5891592Srgrimes
5901592Srgrimes	do {
59180381Ssheldonh		if (read(STDIN_FILENO, &c, 1) != 1)
5921592Srgrimes			exit(1);
5931592Srgrimes		if (--cnt < 0)
5941592Srgrimes			fatal(STDOUT_FILENO, errmsg, 0);
5951592Srgrimes		*buf++ = c;
5961592Srgrimes	} while (c != 0);
5971592Srgrimes}
5981592Srgrimes
5991592Srgrimesextern	char **environ;
6001592Srgrimes
6011592Srgrimesvoid
6021592Srgrimessetup_term(fd)
6031592Srgrimes	int fd;
6041592Srgrimes{
6051592Srgrimes	register char *cp = index(term+ENVSIZE, '/');
6061592Srgrimes	char *speed;
6071592Srgrimes	struct termios tt;
6081592Srgrimes
6091592Srgrimes#ifndef notyet
6101592Srgrimes	tcgetattr(fd, &tt);
6111592Srgrimes	if (cp) {
6121592Srgrimes		*cp++ = '\0';
6131592Srgrimes		speed = cp;
6141592Srgrimes		cp = index(speed, '/');
6151592Srgrimes		if (cp)
6161592Srgrimes			*cp++ = '\0';
6171592Srgrimes		cfsetspeed(&tt, atoi(speed));
6181592Srgrimes	}
6191592Srgrimes
6201592Srgrimes	tt.c_iflag = TTYDEF_IFLAG;
6211592Srgrimes	tt.c_oflag = TTYDEF_OFLAG;
6221592Srgrimes	tt.c_lflag = TTYDEF_LFLAG;
6231592Srgrimes	tcsetattr(fd, TCSAFLUSH, &tt);
6241592Srgrimes#else
6251592Srgrimes	if (cp) {
6261592Srgrimes		*cp++ = '\0';
6271592Srgrimes		speed = cp;
6281592Srgrimes		cp = index(speed, '/');
6291592Srgrimes		if (cp)
6301592Srgrimes			*cp++ = '\0';
6311592Srgrimes		tcgetattr(fd, &tt);
6321592Srgrimes		cfsetspeed(&tt, atoi(speed));
6331592Srgrimes		tcsetattr(fd, TCSAFLUSH, &tt);
6341592Srgrimes	}
6351592Srgrimes#endif
6361592Srgrimes
6371592Srgrimes	env[0] = term;
6381592Srgrimes	env[1] = 0;
6391592Srgrimes	environ = env;
6401592Srgrimes}
6411592Srgrimes
6421592Srgrimesvoid
6431592Srgrimesusage()
6441592Srgrimes{
64551433Smarkm	syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");
6461592Srgrimes}
647