rlogind.c revision 56590
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 56590 2000-01-25 14:52:10Z shin $";
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#ifndef NO_PAM
8451433Smarkm#include <security/pam_appl.h>
8551433Smarkm#include <security/pam_misc.h>
8651433Smarkm#endif
8751433Smarkm
881592Srgrimes#ifndef TIOCPKT_WINDOW
891592Srgrimes#define TIOCPKT_WINDOW 0x80
901592Srgrimes#endif
911592Srgrimes
9251433Smarkm#define		ARGSTR			"Dalnx"
931592Srgrimes
9456590Sshin/* wrapper for KAME-special getnameinfo() */
9556590Sshin#ifndef NI_WITHSCOPEID
9656590Sshin#define	NI_WITHSCOPEID	0
9756590Sshin#endif
9856590Sshin
991592Srgrimeschar	*env[2];
1001592Srgrimes#define	NMAX 30
1011592Srgrimeschar	lusername[NMAX+1], rusername[NMAX+1];
1021592Srgrimesstatic	char term[64] = "TERM=";
1031592Srgrimes#define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
1041592Srgrimesint	keepalive = 1;
1051592Srgrimesint	check_all = 0;
10611486Sdgint	no_delay;
1071592Srgrimes
1081592Srgrimesstruct	passwd *pwd;
1091592Srgrimes
11056590Sshinunion sockunion {
11156590Sshin	struct sockinet {
11256590Sshin		u_char si_len;
11356590Sshin		u_char si_family;
11456590Sshin		u_short si_port;
11556590Sshin	} su_si;
11656590Sshin	struct sockaddr_in  su_sin;
11756590Sshin	struct sockaddr_in6 su_sin6;
11856590Sshin};
11956590Sshin#define su_len		su_si.si_len
12056590Sshin#define su_family	su_si.si_family
12156590Sshin#define su_port		su_si.si_port
12256590Sshin
12356590Sshinvoid	doit __P((int, union sockunion *));
1241592Srgrimesint	control __P((int, char *, int));
1251592Srgrimesvoid	protocol __P((int, int));
1261592Srgrimesvoid	cleanup __P((int));
1271592Srgrimesvoid	fatal __P((int, char *, int));
12856590Sshinint	do_rlogin __P((union sockunion *));
1291592Srgrimesvoid	getstr __P((char *, int, char *));
1301592Srgrimesvoid	setup_term __P((int));
1311592Srgrimesint	do_krb_login __P((struct sockaddr_in *));
1321592Srgrimesvoid	usage __P((void));
1331592Srgrimes
13451433Smarkm#ifndef NO_PAM
13551433Smarkmextern int auth_pam __P((char *));
13651433Smarkm#endif
13751433Smarkm
1381592Srgrimesint
1391592Srgrimesmain(argc, argv)
1401592Srgrimes	int argc;
1411592Srgrimes	char *argv[];
1421592Srgrimes{
1431592Srgrimes	extern int __check_rhosts_file;
14456590Sshin	union sockunion from;
1451592Srgrimes	int ch, fromlen, on;
1461592Srgrimes
1471592Srgrimes	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
1481592Srgrimes
1491592Srgrimes	opterr = 0;
15024349Simp	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
1511592Srgrimes		switch (ch) {
15211486Sdg		case 'D':
15311486Sdg			no_delay = 1;
15411486Sdg			break;
1551592Srgrimes		case 'a':
1561592Srgrimes			check_all = 1;
1571592Srgrimes			break;
1581592Srgrimes		case 'l':
1591592Srgrimes			__check_rhosts_file = 0;
1601592Srgrimes			break;
1611592Srgrimes		case 'n':
1621592Srgrimes			keepalive = 0;
1631592Srgrimes			break;
1641592Srgrimes#ifdef CRYPT
1651592Srgrimes		case 'x':
1661592Srgrimes			doencrypt = 1;
1671592Srgrimes			break;
1681592Srgrimes#endif
1691592Srgrimes		case '?':
1701592Srgrimes		default:
1711592Srgrimes			usage();
1721592Srgrimes			break;
1731592Srgrimes		}
1741592Srgrimes	argc -= optind;
1751592Srgrimes	argv += optind;
1761592Srgrimes
1771592Srgrimes	fromlen = sizeof (from);
1781592Srgrimes	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
1791592Srgrimes		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
1801592Srgrimes		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
1811592Srgrimes	}
1821592Srgrimes	on = 1;
1831592Srgrimes	if (keepalive &&
1841592Srgrimes	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
1851592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
18611486Sdg	if (no_delay &&
18711486Sdg	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
18811486Sdg		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
18956590Sshin        if (from.su_family == AF_INET)
19056590Sshin      {
1911592Srgrimes	on = IPTOS_LOWDELAY;
1921592Srgrimes	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1931592Srgrimes		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
19456590Sshin      }
19511486Sdg
1961592Srgrimes	doit(0, &from);
19729916Smarkm	return 0;
1981592Srgrimes}
1991592Srgrimes
2001592Srgrimesint	child;
2011592Srgrimesint	netf;
2021592Srgrimeschar	line[MAXPATHLEN];
2031592Srgrimesint	confirmed;
2041592Srgrimes
2051592Srgrimesstruct winsize win = { 0, 0, 0, 0 };
2061592Srgrimes
2071592Srgrimes
2081592Srgrimesvoid
2091592Srgrimesdoit(f, fromp)
2101592Srgrimes	int f;
21156590Sshin	union sockunion *fromp;
2121592Srgrimes{
2131592Srgrimes	int master, pid, on = 1;
2141592Srgrimes	int authenticated = 0;
21556590Sshin	char hostname[2 * MAXHOSTNAMELEN + 1];
21656590Sshin	char nameinfo[2 * INET6_ADDRSTRLEN + 1];
2171592Srgrimes	char c;
2181592Srgrimes
2191592Srgrimes	alarm(60);
2201592Srgrimes	read(f, &c, 1);
2211592Srgrimes
2221592Srgrimes	if (c != 0)
2231592Srgrimes		exit(1);
2241592Srgrimes
2251592Srgrimes	alarm(0);
22656590Sshin
22756590Sshin	realhostname_sa(hostname, sizeof(hostname) - 1,
22856590Sshin			    (struct sockaddr *)fromp, fromp->su_len);
22956590Sshin	/* error check ? */
23056590Sshin	fromp->su_port = ntohs((u_short)fromp->su_port);
23124191Simp	hostname[sizeof(hostname) - 1] = '\0';
2321592Srgrimes
2331592Srgrimes	{
23456590Sshin		if ((fromp->su_family != AF_INET &&
23556590Sshin#ifdef INET6
23656590Sshin		     fromp->su_family != AF_INET6
23756590Sshin#endif
23856590Sshin		     ) ||
23956590Sshin		    fromp->su_port >= IPPORT_RESERVED ||
24056590Sshin		    fromp->su_port < IPPORT_RESERVED/2) {
24156590Sshin			getnameinfo((struct sockaddr *)fromp,
24256590Sshin				    fromp->su_len,
24356590Sshin				    nameinfo, sizeof(nameinfo), NULL, 0,
24456590Sshin				    NI_NUMERICHOST|NI_WITHSCOPEID);
24556590Sshin			/* error check ? */
2461592Srgrimes			syslog(LOG_NOTICE, "Connection from %s on illegal port",
24756590Sshin			       nameinfo);
2481592Srgrimes			fatal(f, "Permission denied", 0);
2491592Srgrimes		}
2501592Srgrimes#ifdef IP_OPTIONS
25156590Sshin		if (fromp->su_family == AF_INET)
25256590Sshin              {
25322455Simp		u_char optbuf[BUFSIZ/3];
25422455Simp		int optsize = sizeof(optbuf), ipproto, i;
2551592Srgrimes		struct protoent *ip;
2561592Srgrimes
2571592Srgrimes		if ((ip = getprotobyname("ip")) != NULL)
2581592Srgrimes			ipproto = ip->p_proto;
2591592Srgrimes		else
2601592Srgrimes			ipproto = IPPROTO_IP;
2611592Srgrimes		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
2621592Srgrimes		    &optsize) == 0 && optsize != 0) {
26322455Simp			for (i = 0; i < optsize; ) {
26422455Simp				u_char c = optbuf[i];
26522455Simp				if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
26622455Simp					syslog(LOG_NOTICE,
26722455Simp						"Connection refused from %s with IP option %s",
26856590Sshin						inet_ntoa(fromp->su_sin.sin_addr),
26922455Simp						c == IPOPT_LSRR ? "LSRR" : "SSRR");
27022455Simp					exit(1);
27122455Simp				}
27222455Simp				if (c == IPOPT_EOL)
27322455Simp					break;
27422455Simp				i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
2751592Srgrimes			}
2761592Srgrimes		}
27756590Sshin	      }
2781592Srgrimes#endif
2791592Srgrimes		if (do_rlogin(fromp) == 0)
2801592Srgrimes			authenticated++;
2811592Srgrimes	}
2821592Srgrimes	if (confirmed == 0) {
2831592Srgrimes		write(f, "", 1);
2841592Srgrimes		confirmed = 1;		/* we sent the null! */
2851592Srgrimes	}
2861592Srgrimes#ifdef	CRYPT
2871592Srgrimes	if (doencrypt)
28829916Smarkm		(void) des_enc_write(f,
28929916Smarkm				     SECURE_MESSAGE,
29029916Smarkm				     strlen(SECURE_MESSAGE),
29129916Smarkm				     schedule, &kdata->session);
2921592Srgrimes#endif
2931592Srgrimes	netf = f;
2941592Srgrimes
2951592Srgrimes	pid = forkpty(&master, line, NULL, &win);
2961592Srgrimes	if (pid < 0) {
2971592Srgrimes		if (errno == ENOENT)
2981592Srgrimes			fatal(f, "Out of ptys", 0);
2991592Srgrimes		else
3001592Srgrimes			fatal(f, "Forkpty", 1);
3011592Srgrimes	}
3021592Srgrimes	if (pid == 0) {
3031592Srgrimes		if (f > 2)	/* f should always be 0, but... */
3041592Srgrimes			(void) close(f);
3051592Srgrimes		setup_term(0);
30612575Snate		 if (*lusername=='-') {
3072076Sguido			syslog(LOG_ERR, "tried to pass user \"%s\" to login",
3082076Sguido			       lusername);
3092076Sguido			fatal(STDERR_FILENO, "invalid user", 0);
3102076Sguido		}
3111592Srgrimes		if (authenticated) {
3121592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3131592Srgrimes			    "-h", hostname, "-f", lusername, (char *)NULL);
3141592Srgrimes		} else
3151592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3161592Srgrimes			    "-h", hostname, lusername, (char *)NULL);
3171592Srgrimes		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
3181592Srgrimes		/*NOTREACHED*/
3191592Srgrimes	}
3201592Srgrimes#ifdef	CRYPT
3211592Srgrimes	/*
3221592Srgrimes	 * If encrypted, don't turn on NBIO or the des read/write
3231592Srgrimes	 * routines will croak.
3241592Srgrimes	 */
3251592Srgrimes
3261592Srgrimes	if (!doencrypt)
3271592Srgrimes#endif
3281592Srgrimes		ioctl(f, FIONBIO, &on);
3291592Srgrimes	ioctl(master, FIONBIO, &on);
3301592Srgrimes	ioctl(master, TIOCPKT, &on);
3311592Srgrimes	signal(SIGCHLD, cleanup);
3321592Srgrimes	protocol(f, master);
3331592Srgrimes	signal(SIGCHLD, SIG_IGN);
3341592Srgrimes	cleanup(0);
3351592Srgrimes}
3361592Srgrimes
3371592Srgrimeschar	magic[2] = { 0377, 0377 };
3381592Srgrimeschar	oobdata[] = {TIOCPKT_WINDOW};
3391592Srgrimes
3401592Srgrimes/*
3411592Srgrimes * Handle a "control" request (signaled by magic being present)
3421592Srgrimes * in the data stream.  For now, we are only willing to handle
3431592Srgrimes * window size changes.
3441592Srgrimes */
3451592Srgrimesint
3461592Srgrimescontrol(pty, cp, n)
3471592Srgrimes	int pty;
3481592Srgrimes	char *cp;
3491592Srgrimes	int n;
3501592Srgrimes{
3511592Srgrimes	struct winsize w;
3521592Srgrimes
3531592Srgrimes	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
3541592Srgrimes		return (0);
3551592Srgrimes	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
3561592Srgrimes	bcopy(cp+4, (char *)&w, sizeof(w));
3571592Srgrimes	w.ws_row = ntohs(w.ws_row);
3581592Srgrimes	w.ws_col = ntohs(w.ws_col);
3591592Srgrimes	w.ws_xpixel = ntohs(w.ws_xpixel);
3601592Srgrimes	w.ws_ypixel = ntohs(w.ws_ypixel);
3611592Srgrimes	(void)ioctl(pty, TIOCSWINSZ, &w);
3621592Srgrimes	return (4+sizeof (w));
3631592Srgrimes}
3641592Srgrimes
3651592Srgrimes/*
3661592Srgrimes * rlogin "protocol" machine.
3671592Srgrimes */
3681592Srgrimesvoid
3691592Srgrimesprotocol(f, p)
3701592Srgrimes	register int f, p;
3711592Srgrimes{
3721592Srgrimes	char pibuf[1024+1], fibuf[1024], *pbp, *fbp;
37346078Simp	int pcc = 0, fcc = 0;
3741592Srgrimes	int cc, nfd, n;
3751592Srgrimes	char cntl;
3761592Srgrimes
3771592Srgrimes	/*
3781592Srgrimes	 * Must ignore SIGTTOU, otherwise we'll stop
3791592Srgrimes	 * when we try and set slave pty's window shape
3801592Srgrimes	 * (our controlling tty is the master pty).
3811592Srgrimes	 */
3821592Srgrimes	(void) signal(SIGTTOU, SIG_IGN);
3831592Srgrimes	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
3841592Srgrimes	if (f > p)
3851592Srgrimes		nfd = f + 1;
3861592Srgrimes	else
3871592Srgrimes		nfd = p + 1;
3881592Srgrimes	if (nfd > FD_SETSIZE) {
3891592Srgrimes		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
3901592Srgrimes		fatal(f, "internal error (select mask too small)", 0);
3911592Srgrimes	}
3921592Srgrimes	for (;;) {
3931592Srgrimes		fd_set ibits, obits, ebits, *omask;
3941592Srgrimes
3951592Srgrimes		FD_ZERO(&ebits);
3961592Srgrimes		FD_ZERO(&ibits);
3971592Srgrimes		FD_ZERO(&obits);
3981592Srgrimes		omask = (fd_set *)NULL;
3991592Srgrimes		if (fcc) {
4001592Srgrimes			FD_SET(p, &obits);
4011592Srgrimes			omask = &obits;
4021592Srgrimes		} else
4031592Srgrimes			FD_SET(f, &ibits);
40446078Simp		if (pcc >= 0) {
4051592Srgrimes			if (pcc) {
4061592Srgrimes				FD_SET(f, &obits);
4071592Srgrimes				omask = &obits;
4081592Srgrimes			} else
4091592Srgrimes				FD_SET(p, &ibits);
41046078Simp		}
4111592Srgrimes		FD_SET(p, &ebits);
4121592Srgrimes		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
4131592Srgrimes			if (errno == EINTR)
4141592Srgrimes				continue;
4151592Srgrimes			fatal(f, "select", 1);
4161592Srgrimes		}
4171592Srgrimes		if (n == 0) {
4181592Srgrimes			/* shouldn't happen... */
4191592Srgrimes			sleep(5);
4201592Srgrimes			continue;
4211592Srgrimes		}
4221592Srgrimes#define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
4231592Srgrimes		if (FD_ISSET(p, &ebits)) {
4241592Srgrimes			cc = read(p, &cntl, 1);
4251592Srgrimes			if (cc == 1 && pkcontrol(cntl)) {
4261592Srgrimes				cntl |= oobdata[0];
4271592Srgrimes				send(f, &cntl, 1, MSG_OOB);
4281592Srgrimes				if (cntl & TIOCPKT_FLUSHWRITE) {
4291592Srgrimes					pcc = 0;
4301592Srgrimes					FD_CLR(p, &ibits);
4311592Srgrimes				}
4321592Srgrimes			}
4331592Srgrimes		}
4341592Srgrimes		if (FD_ISSET(f, &ibits)) {
4351592Srgrimes#ifdef	CRYPT
4361592Srgrimes			if (doencrypt)
43729916Smarkm				fcc = des_enc_read(f, fibuf, sizeof(fibuf),
43829916Smarkm					schedule, &kdata->session);
4391592Srgrimes			else
4401592Srgrimes#endif
4411592Srgrimes				fcc = read(f, fibuf, sizeof(fibuf));
4421592Srgrimes			if (fcc < 0 && errno == EWOULDBLOCK)
4431592Srgrimes				fcc = 0;
4441592Srgrimes			else {
4451592Srgrimes				register char *cp;
4461592Srgrimes				int left, n;
4471592Srgrimes
4481592Srgrimes				if (fcc <= 0)
4491592Srgrimes					break;
4501592Srgrimes				fbp = fibuf;
4511592Srgrimes
4521592Srgrimes			top:
4531592Srgrimes				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
4541592Srgrimes					if (cp[0] == magic[0] &&
4551592Srgrimes					    cp[1] == magic[1]) {
4561592Srgrimes						left = fcc - (cp-fibuf);
4571592Srgrimes						n = control(p, cp, left);
4581592Srgrimes						if (n) {
4591592Srgrimes							left -= n;
4601592Srgrimes							if (left > 0)
4611592Srgrimes								bcopy(cp+n, cp, left);
4621592Srgrimes							fcc -= n;
4631592Srgrimes							goto top; /* n^2 */
4641592Srgrimes						}
4651592Srgrimes					}
4661592Srgrimes				FD_SET(p, &obits);		/* try write */
4671592Srgrimes			}
4681592Srgrimes		}
4691592Srgrimes
4701592Srgrimes		if (FD_ISSET(p, &obits) && fcc > 0) {
4711592Srgrimes			cc = write(p, fbp, fcc);
4721592Srgrimes			if (cc > 0) {
4731592Srgrimes				fcc -= cc;
4741592Srgrimes				fbp += cc;
4751592Srgrimes			}
4761592Srgrimes		}
4771592Srgrimes
4781592Srgrimes		if (FD_ISSET(p, &ibits)) {
4791592Srgrimes			pcc = read(p, pibuf, sizeof (pibuf));
4801592Srgrimes			pbp = pibuf;
4811592Srgrimes			if (pcc < 0 && errno == EWOULDBLOCK)
4821592Srgrimes				pcc = 0;
4831592Srgrimes			else if (pcc <= 0)
4841592Srgrimes				break;
4851592Srgrimes			else if (pibuf[0] == 0) {
4861592Srgrimes				pbp++, pcc--;
4871592Srgrimes#ifdef	CRYPT
4881592Srgrimes				if (!doencrypt)
4891592Srgrimes#endif
4901592Srgrimes					FD_SET(f, &obits);	/* try write */
4911592Srgrimes			} else {
4921592Srgrimes				if (pkcontrol(pibuf[0])) {
4931592Srgrimes					pibuf[0] |= oobdata[0];
4941592Srgrimes					send(f, &pibuf[0], 1, MSG_OOB);
4951592Srgrimes				}
4961592Srgrimes				pcc = 0;
4971592Srgrimes			}
4981592Srgrimes		}
4991592Srgrimes		if ((FD_ISSET(f, &obits)) && pcc > 0) {
5001592Srgrimes#ifdef	CRYPT
5011592Srgrimes			if (doencrypt)
50229916Smarkm				cc = des_enc_write(f, pbp, pcc,
50329916Smarkm					schedule, &kdata->session);
5041592Srgrimes			else
5051592Srgrimes#endif
5061592Srgrimes				cc = write(f, pbp, pcc);
5071592Srgrimes			if (cc < 0 && errno == EWOULDBLOCK) {
5081592Srgrimes				/*
5091592Srgrimes				 * This happens when we try write after read
5101592Srgrimes				 * from p, but some old kernels balk at large
5111592Srgrimes				 * writes even when select returns true.
5121592Srgrimes				 */
5131592Srgrimes				if (!FD_ISSET(p, &ibits))
5141592Srgrimes					sleep(5);
5151592Srgrimes				continue;
5161592Srgrimes			}
5171592Srgrimes			if (cc > 0) {
5181592Srgrimes				pcc -= cc;
5191592Srgrimes				pbp += cc;
5201592Srgrimes			}
5211592Srgrimes		}
5221592Srgrimes	}
5231592Srgrimes}
5241592Srgrimes
5251592Srgrimesvoid
5261592Srgrimescleanup(signo)
5271592Srgrimes	int signo;
5281592Srgrimes{
5291592Srgrimes	char *p;
5301592Srgrimes
5311592Srgrimes	p = line + sizeof(_PATH_DEV) - 1;
5321592Srgrimes	if (logout(p))
5331592Srgrimes		logwtmp(p, "", "");
53450132Simp	(void)chflags(line, 0);
5351592Srgrimes	(void)chmod(line, 0666);
5361592Srgrimes	(void)chown(line, 0, 0);
5371592Srgrimes	*p = 'p';
53850132Simp	(void)chflags(line, 0);
5391592Srgrimes	(void)chmod(line, 0666);
5401592Srgrimes	(void)chown(line, 0, 0);
5411592Srgrimes	shutdown(netf, 2);
5421592Srgrimes	exit(1);
5431592Srgrimes}
5441592Srgrimes
5451592Srgrimesvoid
5461592Srgrimesfatal(f, msg, syserr)
5471592Srgrimes	int f;
5481592Srgrimes	char *msg;
5491592Srgrimes	int syserr;
5501592Srgrimes{
5511592Srgrimes	int len;
5521592Srgrimes	char buf[BUFSIZ], *bp = buf;
5531592Srgrimes
5541592Srgrimes	/*
5551592Srgrimes	 * Prepend binary one to message if we haven't sent
5561592Srgrimes	 * the magic null as confirmation.
5571592Srgrimes	 */
5581592Srgrimes	if (!confirmed)
5591592Srgrimes		*bp++ = '\01';		/* error indicator */
5601592Srgrimes	if (syserr)
5611592Srgrimes		len = sprintf(bp, "rlogind: %s: %s.\r\n",
5621592Srgrimes		    msg, strerror(errno));
5631592Srgrimes	else
5641592Srgrimes		len = sprintf(bp, "rlogind: %s.\r\n", msg);
5651592Srgrimes	(void) write(f, buf, bp + len - buf);
5661592Srgrimes	exit(1);
5671592Srgrimes}
5681592Srgrimes
5691592Srgrimesint
5701592Srgrimesdo_rlogin(dest)
57156590Sshin	union sockunion *dest;
5721592Srgrimes{
57351433Smarkm	int retval;
57456590Sshin	int af;
57556590Sshin	char *addr;
57651433Smarkm
5771592Srgrimes	getstr(rusername, sizeof(rusername), "remuser too long");
5781592Srgrimes	getstr(lusername, sizeof(lusername), "locuser too long");
5791592Srgrimes	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
5801592Srgrimes
58151433Smarkm#ifndef  NO_PAM
58251433Smarkm	retval = auth_pam(lusername);
58351433Smarkm
58451433Smarkm	if (retval) {
58551433Smarkm		if (retval == -1) {
58651433Smarkm			syslog(LOG_ERR, "PAM authentication failed");
58751433Smarkm		}
58851433Smarkm		else {
58951433Smarkm			syslog(LOG_ERR,
59051433Smarkm				"User %s failed PAM authentication", lusername);
59151433Smarkm			exit(1);
59251433Smarkm		}
59351433Smarkm	}
59451433Smarkm#endif
5951592Srgrimes	pwd = getpwnam(lusername);
5961592Srgrimes	if (pwd == NULL)
5971592Srgrimes		return (-1);
5981592Srgrimes	/* XXX why don't we syslog() failure? */
59956590Sshin
60056590Sshin	af = dest->su_family;
60156590Sshin	switch (af) {
60256590Sshin	case AF_INET:
60356590Sshin		addr = (char *)&dest->su_sin.sin_addr;
60456590Sshin		break;
60556590Sshin#ifdef INET6
60656590Sshin	case AF_INET6:
60756590Sshin		addr = (char *)&dest->su_sin6.sin6_addr;
60856590Sshin		break;
60956590Sshin#endif
61056590Sshin	default:
61156590Sshin		return -1;	/*EAFNOSUPPORT*/
61256590Sshin	}
61356590Sshin
61456590Sshin	return (iruserok_af(addr, pwd->pw_uid == 0, rusername, lusername, af));
6151592Srgrimes}
6161592Srgrimes
6171592Srgrimesvoid
6181592Srgrimesgetstr(buf, cnt, errmsg)
6191592Srgrimes	char *buf;
6201592Srgrimes	int cnt;
6211592Srgrimes	char *errmsg;
6221592Srgrimes{
6231592Srgrimes	char c;
6241592Srgrimes
6251592Srgrimes	do {
6261592Srgrimes		if (read(0, &c, 1) != 1)
6271592Srgrimes			exit(1);
6281592Srgrimes		if (--cnt < 0)
6291592Srgrimes			fatal(STDOUT_FILENO, errmsg, 0);
6301592Srgrimes		*buf++ = c;
6311592Srgrimes	} while (c != 0);
6321592Srgrimes}
6331592Srgrimes
6341592Srgrimesextern	char **environ;
6351592Srgrimes
6361592Srgrimesvoid
6371592Srgrimessetup_term(fd)
6381592Srgrimes	int fd;
6391592Srgrimes{
6401592Srgrimes	register char *cp = index(term+ENVSIZE, '/');
6411592Srgrimes	char *speed;
6421592Srgrimes	struct termios tt;
6431592Srgrimes
6441592Srgrimes#ifndef notyet
6451592Srgrimes	tcgetattr(fd, &tt);
6461592Srgrimes	if (cp) {
6471592Srgrimes		*cp++ = '\0';
6481592Srgrimes		speed = cp;
6491592Srgrimes		cp = index(speed, '/');
6501592Srgrimes		if (cp)
6511592Srgrimes			*cp++ = '\0';
6521592Srgrimes		cfsetspeed(&tt, atoi(speed));
6531592Srgrimes	}
6541592Srgrimes
6551592Srgrimes	tt.c_iflag = TTYDEF_IFLAG;
6561592Srgrimes	tt.c_oflag = TTYDEF_OFLAG;
6571592Srgrimes	tt.c_lflag = TTYDEF_LFLAG;
6581592Srgrimes	tcsetattr(fd, TCSAFLUSH, &tt);
6591592Srgrimes#else
6601592Srgrimes	if (cp) {
6611592Srgrimes		*cp++ = '\0';
6621592Srgrimes		speed = cp;
6631592Srgrimes		cp = index(speed, '/');
6641592Srgrimes		if (cp)
6651592Srgrimes			*cp++ = '\0';
6661592Srgrimes		tcgetattr(fd, &tt);
6671592Srgrimes		cfsetspeed(&tt, atoi(speed));
6681592Srgrimes		tcsetattr(fd, TCSAFLUSH, &tt);
6691592Srgrimes	}
6701592Srgrimes#endif
6711592Srgrimes
6721592Srgrimes	env[0] = term;
6731592Srgrimes	env[1] = 0;
6741592Srgrimes	environ = env;
6751592Srgrimes}
6761592Srgrimes
6771592Srgrimesvoid
6781592Srgrimesusage()
6791592Srgrimes{
68051433Smarkm	syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");
6811592Srgrimes}
682