rlogind.c revision 46078
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[] =
4546078Simp	"$Id: rlogind.c,v 1.22 1999/04/07 08:27:42 brian Exp $";
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
831592Srgrimes#ifndef TIOCPKT_WINDOW
841592Srgrimes#define TIOCPKT_WINDOW 0x80
851592Srgrimes#endif
861592Srgrimes
871592Srgrimes#ifdef	KERBEROS
8814024Smarkm#include <des.h>
8929916Smarkm#include <krb.h>
901592Srgrimes#define	SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n"
911592Srgrimes
921592SrgrimesAUTH_DAT	*kdata;
931592SrgrimesKTEXT		ticket;
941592Srgrimesu_char		auth_buf[sizeof(AUTH_DAT)];
951592Srgrimesu_char		tick_buf[sizeof(KTEXT_ST)];
961592SrgrimesKey_schedule	schedule;
971592Srgrimesint		doencrypt, retval, use_kerberos, vacuous;
981592Srgrimes
9911486Sdg#define		ARGSTR			"Dalnkvx"
1001592Srgrimes#else
10111486Sdg#define		ARGSTR			"Daln"
1021592Srgrimes#endif	/* KERBEROS */
1031592Srgrimes
1041592Srgrimeschar	*env[2];
1051592Srgrimes#define	NMAX 30
1061592Srgrimeschar	lusername[NMAX+1], rusername[NMAX+1];
1071592Srgrimesstatic	char term[64] = "TERM=";
1081592Srgrimes#define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
1091592Srgrimesint	keepalive = 1;
1101592Srgrimesint	check_all = 0;
11111486Sdgint	no_delay;
1121592Srgrimes
1131592Srgrimesstruct	passwd *pwd;
1141592Srgrimes
1151592Srgrimesvoid	doit __P((int, struct sockaddr_in *));
1161592Srgrimesint	control __P((int, char *, int));
1171592Srgrimesvoid	protocol __P((int, int));
1181592Srgrimesvoid	cleanup __P((int));
1191592Srgrimesvoid	fatal __P((int, char *, int));
1201592Srgrimesint	do_rlogin __P((struct sockaddr_in *));
1211592Srgrimesvoid	getstr __P((char *, int, char *));
1221592Srgrimesvoid	setup_term __P((int));
1231592Srgrimesint	do_krb_login __P((struct sockaddr_in *));
1241592Srgrimesvoid	usage __P((void));
1251592Srgrimes
1261592Srgrimesint
1271592Srgrimesmain(argc, argv)
1281592Srgrimes	int argc;
1291592Srgrimes	char *argv[];
1301592Srgrimes{
1311592Srgrimes	extern int __check_rhosts_file;
1321592Srgrimes	struct sockaddr_in from;
1331592Srgrimes	int ch, fromlen, on;
1341592Srgrimes
1351592Srgrimes	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
1361592Srgrimes
1371592Srgrimes	opterr = 0;
13824349Simp	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
1391592Srgrimes		switch (ch) {
14011486Sdg		case 'D':
14111486Sdg			no_delay = 1;
14211486Sdg			break;
1431592Srgrimes		case 'a':
1441592Srgrimes			check_all = 1;
1451592Srgrimes			break;
1461592Srgrimes		case 'l':
1471592Srgrimes			__check_rhosts_file = 0;
1481592Srgrimes			break;
1491592Srgrimes		case 'n':
1501592Srgrimes			keepalive = 0;
1511592Srgrimes			break;
1521592Srgrimes#ifdef KERBEROS
1531592Srgrimes		case 'k':
1541592Srgrimes			use_kerberos = 1;
1551592Srgrimes			break;
1561592Srgrimes		case 'v':
1571592Srgrimes			vacuous = 1;
1581592Srgrimes			break;
1591592Srgrimes#ifdef CRYPT
1601592Srgrimes		case 'x':
1611592Srgrimes			doencrypt = 1;
1621592Srgrimes			break;
1631592Srgrimes#endif
1641592Srgrimes#endif
1651592Srgrimes		case '?':
1661592Srgrimes		default:
1671592Srgrimes			usage();
1681592Srgrimes			break;
1691592Srgrimes		}
1701592Srgrimes	argc -= optind;
1711592Srgrimes	argv += optind;
1721592Srgrimes
1731592Srgrimes#ifdef	KERBEROS
1741592Srgrimes	if (use_kerberos && vacuous) {
1751592Srgrimes		usage();
1761592Srgrimes		fatal(STDERR_FILENO, "only one of -k and -v allowed", 0);
1771592Srgrimes	}
1781592Srgrimes#endif
1791592Srgrimes	fromlen = sizeof (from);
1801592Srgrimes	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
1811592Srgrimes		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
1821592Srgrimes		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
1831592Srgrimes	}
1841592Srgrimes	on = 1;
1851592Srgrimes	if (keepalive &&
1861592Srgrimes	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
1871592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
18811486Sdg	if (no_delay &&
18911486Sdg	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
19011486Sdg		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
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");
19411486Sdg
1951592Srgrimes	doit(0, &from);
19629916Smarkm	return 0;
1971592Srgrimes}
1981592Srgrimes
1991592Srgrimesint	child;
2001592Srgrimesint	netf;
2011592Srgrimeschar	line[MAXPATHLEN];
2021592Srgrimesint	confirmed;
2031592Srgrimes
2041592Srgrimesstruct winsize win = { 0, 0, 0, 0 };
2051592Srgrimes
2061592Srgrimes
2071592Srgrimesvoid
2081592Srgrimesdoit(f, fromp)
2091592Srgrimes	int f;
2101592Srgrimes	struct sockaddr_in *fromp;
2111592Srgrimes{
2121592Srgrimes	int master, pid, on = 1;
2131592Srgrimes	int authenticated = 0;
21445422Sbrian	char hostname[MAXHOSTNAMELEN];
2151592Srgrimes	char c;
2161592Srgrimes
2171592Srgrimes	alarm(60);
2181592Srgrimes	read(f, &c, 1);
2191592Srgrimes
2201592Srgrimes	if (c != 0)
2211592Srgrimes		exit(1);
2221592Srgrimes#ifdef	KERBEROS
2231592Srgrimes	if (vacuous)
2241592Srgrimes		fatal(f, "Remote host requires Kerberos authentication", 0);
2251592Srgrimes#endif
2261592Srgrimes
2271592Srgrimes	alarm(0);
2281592Srgrimes	fromp->sin_port = ntohs((u_short)fromp->sin_port);
22945422Sbrian	realhostname(hostname, sizeof(hostname) - 1, &fromp->sin_addr);
23024191Simp	hostname[sizeof(hostname) - 1] = '\0';
2311592Srgrimes
2321592Srgrimes#ifdef	KERBEROS
2331592Srgrimes	if (use_kerberos) {
2341592Srgrimes		retval = do_krb_login(fromp);
2351592Srgrimes		if (retval == 0)
2361592Srgrimes			authenticated++;
2371592Srgrimes		else if (retval > 0)
2381592Srgrimes			fatal(f, krb_err_txt[retval], 0);
2391592Srgrimes		write(f, &c, 1);
2401592Srgrimes		confirmed = 1;		/* we sent the null! */
2411592Srgrimes	} else
2421592Srgrimes#endif
2431592Srgrimes	{
2441592Srgrimes		if (fromp->sin_family != AF_INET ||
2451592Srgrimes		    fromp->sin_port >= IPPORT_RESERVED ||
2461592Srgrimes		    fromp->sin_port < IPPORT_RESERVED/2) {
2471592Srgrimes			syslog(LOG_NOTICE, "Connection from %s on illegal port",
2481592Srgrimes				inet_ntoa(fromp->sin_addr));
2491592Srgrimes			fatal(f, "Permission denied", 0);
2501592Srgrimes		}
2511592Srgrimes#ifdef IP_OPTIONS
2521592Srgrimes		{
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",
26822455Simp						inet_ntoa(fromp->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		}
2771592Srgrimes		}
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	KERBEROS
2871592Srgrimes#ifdef	CRYPT
2881592Srgrimes	if (doencrypt)
28929916Smarkm		(void) des_enc_write(f,
29029916Smarkm				     SECURE_MESSAGE,
29129916Smarkm				     strlen(SECURE_MESSAGE),
29229916Smarkm				     schedule, &kdata->session);
2931592Srgrimes#endif
2941592Srgrimes#endif
2951592Srgrimes	netf = f;
2961592Srgrimes
2971592Srgrimes	pid = forkpty(&master, line, NULL, &win);
2981592Srgrimes	if (pid < 0) {
2991592Srgrimes		if (errno == ENOENT)
3001592Srgrimes			fatal(f, "Out of ptys", 0);
3011592Srgrimes		else
3021592Srgrimes			fatal(f, "Forkpty", 1);
3031592Srgrimes	}
3041592Srgrimes	if (pid == 0) {
3051592Srgrimes		if (f > 2)	/* f should always be 0, but... */
3061592Srgrimes			(void) close(f);
3071592Srgrimes		setup_term(0);
30812575Snate		 if (*lusername=='-') {
3092076Sguido			syslog(LOG_ERR, "tried to pass user \"%s\" to login",
3102076Sguido			       lusername);
3112076Sguido			fatal(STDERR_FILENO, "invalid user", 0);
3122076Sguido		}
3131592Srgrimes		if (authenticated) {
3141592Srgrimes#ifdef	KERBEROS
3151592Srgrimes			if (use_kerberos && (pwd->pw_uid == 0))
3161592Srgrimes				syslog(LOG_INFO|LOG_AUTH,
3171592Srgrimes				    "ROOT Kerberos login from %s.%s@%s on %s\n",
3181592Srgrimes				    kdata->pname, kdata->pinst, kdata->prealm,
3191592Srgrimes				    hostname);
3201592Srgrimes#endif
3211592Srgrimes
3221592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3231592Srgrimes			    "-h", hostname, "-f", lusername, (char *)NULL);
3241592Srgrimes		} else
3251592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3261592Srgrimes			    "-h", hostname, lusername, (char *)NULL);
3271592Srgrimes		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
3281592Srgrimes		/*NOTREACHED*/
3291592Srgrimes	}
3301592Srgrimes#ifdef	CRYPT
3311592Srgrimes#ifdef	KERBEROS
3321592Srgrimes	/*
3331592Srgrimes	 * If encrypted, don't turn on NBIO or the des read/write
3341592Srgrimes	 * routines will croak.
3351592Srgrimes	 */
3361592Srgrimes
3371592Srgrimes	if (!doencrypt)
3381592Srgrimes#endif
3391592Srgrimes#endif
3401592Srgrimes		ioctl(f, FIONBIO, &on);
3411592Srgrimes	ioctl(master, FIONBIO, &on);
3421592Srgrimes	ioctl(master, TIOCPKT, &on);
3431592Srgrimes	signal(SIGCHLD, cleanup);
3441592Srgrimes	protocol(f, master);
3451592Srgrimes	signal(SIGCHLD, SIG_IGN);
3461592Srgrimes	cleanup(0);
3471592Srgrimes}
3481592Srgrimes
3491592Srgrimeschar	magic[2] = { 0377, 0377 };
3501592Srgrimeschar	oobdata[] = {TIOCPKT_WINDOW};
3511592Srgrimes
3521592Srgrimes/*
3531592Srgrimes * Handle a "control" request (signaled by magic being present)
3541592Srgrimes * in the data stream.  For now, we are only willing to handle
3551592Srgrimes * window size changes.
3561592Srgrimes */
3571592Srgrimesint
3581592Srgrimescontrol(pty, cp, n)
3591592Srgrimes	int pty;
3601592Srgrimes	char *cp;
3611592Srgrimes	int n;
3621592Srgrimes{
3631592Srgrimes	struct winsize w;
3641592Srgrimes
3651592Srgrimes	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
3661592Srgrimes		return (0);
3671592Srgrimes	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
3681592Srgrimes	bcopy(cp+4, (char *)&w, sizeof(w));
3691592Srgrimes	w.ws_row = ntohs(w.ws_row);
3701592Srgrimes	w.ws_col = ntohs(w.ws_col);
3711592Srgrimes	w.ws_xpixel = ntohs(w.ws_xpixel);
3721592Srgrimes	w.ws_ypixel = ntohs(w.ws_ypixel);
3731592Srgrimes	(void)ioctl(pty, TIOCSWINSZ, &w);
3741592Srgrimes	return (4+sizeof (w));
3751592Srgrimes}
3761592Srgrimes
3771592Srgrimes/*
3781592Srgrimes * rlogin "protocol" machine.
3791592Srgrimes */
3801592Srgrimesvoid
3811592Srgrimesprotocol(f, p)
3821592Srgrimes	register int f, p;
3831592Srgrimes{
3841592Srgrimes	char pibuf[1024+1], fibuf[1024], *pbp, *fbp;
38546078Simp	int pcc = 0, fcc = 0;
3861592Srgrimes	int cc, nfd, n;
3871592Srgrimes	char cntl;
3881592Srgrimes
3891592Srgrimes	/*
3901592Srgrimes	 * Must ignore SIGTTOU, otherwise we'll stop
3911592Srgrimes	 * when we try and set slave pty's window shape
3921592Srgrimes	 * (our controlling tty is the master pty).
3931592Srgrimes	 */
3941592Srgrimes	(void) signal(SIGTTOU, SIG_IGN);
3951592Srgrimes	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
3961592Srgrimes	if (f > p)
3971592Srgrimes		nfd = f + 1;
3981592Srgrimes	else
3991592Srgrimes		nfd = p + 1;
4001592Srgrimes	if (nfd > FD_SETSIZE) {
4011592Srgrimes		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
4021592Srgrimes		fatal(f, "internal error (select mask too small)", 0);
4031592Srgrimes	}
4041592Srgrimes	for (;;) {
4051592Srgrimes		fd_set ibits, obits, ebits, *omask;
4061592Srgrimes
4071592Srgrimes		FD_ZERO(&ebits);
4081592Srgrimes		FD_ZERO(&ibits);
4091592Srgrimes		FD_ZERO(&obits);
4101592Srgrimes		omask = (fd_set *)NULL;
4111592Srgrimes		if (fcc) {
4121592Srgrimes			FD_SET(p, &obits);
4131592Srgrimes			omask = &obits;
4141592Srgrimes		} else
4151592Srgrimes			FD_SET(f, &ibits);
41646078Simp		if (pcc >= 0) {
4171592Srgrimes			if (pcc) {
4181592Srgrimes				FD_SET(f, &obits);
4191592Srgrimes				omask = &obits;
4201592Srgrimes			} else
4211592Srgrimes				FD_SET(p, &ibits);
42246078Simp		}
4231592Srgrimes		FD_SET(p, &ebits);
4241592Srgrimes		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
4251592Srgrimes			if (errno == EINTR)
4261592Srgrimes				continue;
4271592Srgrimes			fatal(f, "select", 1);
4281592Srgrimes		}
4291592Srgrimes		if (n == 0) {
4301592Srgrimes			/* shouldn't happen... */
4311592Srgrimes			sleep(5);
4321592Srgrimes			continue;
4331592Srgrimes		}
4341592Srgrimes#define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
4351592Srgrimes		if (FD_ISSET(p, &ebits)) {
4361592Srgrimes			cc = read(p, &cntl, 1);
4371592Srgrimes			if (cc == 1 && pkcontrol(cntl)) {
4381592Srgrimes				cntl |= oobdata[0];
4391592Srgrimes				send(f, &cntl, 1, MSG_OOB);
4401592Srgrimes				if (cntl & TIOCPKT_FLUSHWRITE) {
4411592Srgrimes					pcc = 0;
4421592Srgrimes					FD_CLR(p, &ibits);
4431592Srgrimes				}
4441592Srgrimes			}
4451592Srgrimes		}
4461592Srgrimes		if (FD_ISSET(f, &ibits)) {
4471592Srgrimes#ifdef	CRYPT
4481592Srgrimes#ifdef	KERBEROS
4491592Srgrimes			if (doencrypt)
45029916Smarkm				fcc = des_enc_read(f, fibuf, sizeof(fibuf),
45129916Smarkm					schedule, &kdata->session);
4521592Srgrimes			else
4531592Srgrimes#endif
4541592Srgrimes#endif
4551592Srgrimes				fcc = read(f, fibuf, sizeof(fibuf));
4561592Srgrimes			if (fcc < 0 && errno == EWOULDBLOCK)
4571592Srgrimes				fcc = 0;
4581592Srgrimes			else {
4591592Srgrimes				register char *cp;
4601592Srgrimes				int left, n;
4611592Srgrimes
4621592Srgrimes				if (fcc <= 0)
4631592Srgrimes					break;
4641592Srgrimes				fbp = fibuf;
4651592Srgrimes
4661592Srgrimes			top:
4671592Srgrimes				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
4681592Srgrimes					if (cp[0] == magic[0] &&
4691592Srgrimes					    cp[1] == magic[1]) {
4701592Srgrimes						left = fcc - (cp-fibuf);
4711592Srgrimes						n = control(p, cp, left);
4721592Srgrimes						if (n) {
4731592Srgrimes							left -= n;
4741592Srgrimes							if (left > 0)
4751592Srgrimes								bcopy(cp+n, cp, left);
4761592Srgrimes							fcc -= n;
4771592Srgrimes							goto top; /* n^2 */
4781592Srgrimes						}
4791592Srgrimes					}
4801592Srgrimes				FD_SET(p, &obits);		/* try write */
4811592Srgrimes			}
4821592Srgrimes		}
4831592Srgrimes
4841592Srgrimes		if (FD_ISSET(p, &obits) && fcc > 0) {
4851592Srgrimes			cc = write(p, fbp, fcc);
4861592Srgrimes			if (cc > 0) {
4871592Srgrimes				fcc -= cc;
4881592Srgrimes				fbp += cc;
4891592Srgrimes			}
4901592Srgrimes		}
4911592Srgrimes
4921592Srgrimes		if (FD_ISSET(p, &ibits)) {
4931592Srgrimes			pcc = read(p, pibuf, sizeof (pibuf));
4941592Srgrimes			pbp = pibuf;
4951592Srgrimes			if (pcc < 0 && errno == EWOULDBLOCK)
4961592Srgrimes				pcc = 0;
4971592Srgrimes			else if (pcc <= 0)
4981592Srgrimes				break;
4991592Srgrimes			else if (pibuf[0] == 0) {
5001592Srgrimes				pbp++, pcc--;
5011592Srgrimes#ifdef	CRYPT
5021592Srgrimes#ifdef	KERBEROS
5031592Srgrimes				if (!doencrypt)
5041592Srgrimes#endif
5051592Srgrimes#endif
5061592Srgrimes					FD_SET(f, &obits);	/* try write */
5071592Srgrimes			} else {
5081592Srgrimes				if (pkcontrol(pibuf[0])) {
5091592Srgrimes					pibuf[0] |= oobdata[0];
5101592Srgrimes					send(f, &pibuf[0], 1, MSG_OOB);
5111592Srgrimes				}
5121592Srgrimes				pcc = 0;
5131592Srgrimes			}
5141592Srgrimes		}
5151592Srgrimes		if ((FD_ISSET(f, &obits)) && pcc > 0) {
5161592Srgrimes#ifdef	CRYPT
5171592Srgrimes#ifdef	KERBEROS
5181592Srgrimes			if (doencrypt)
51929916Smarkm				cc = des_enc_write(f, pbp, pcc,
52029916Smarkm					schedule, &kdata->session);
5211592Srgrimes			else
5221592Srgrimes#endif
5231592Srgrimes#endif
5241592Srgrimes				cc = write(f, pbp, pcc);
5251592Srgrimes			if (cc < 0 && errno == EWOULDBLOCK) {
5261592Srgrimes				/*
5271592Srgrimes				 * This happens when we try write after read
5281592Srgrimes				 * from p, but some old kernels balk at large
5291592Srgrimes				 * writes even when select returns true.
5301592Srgrimes				 */
5311592Srgrimes				if (!FD_ISSET(p, &ibits))
5321592Srgrimes					sleep(5);
5331592Srgrimes				continue;
5341592Srgrimes			}
5351592Srgrimes			if (cc > 0) {
5361592Srgrimes				pcc -= cc;
5371592Srgrimes				pbp += cc;
5381592Srgrimes			}
5391592Srgrimes		}
5401592Srgrimes	}
5411592Srgrimes}
5421592Srgrimes
5431592Srgrimesvoid
5441592Srgrimescleanup(signo)
5451592Srgrimes	int signo;
5461592Srgrimes{
5471592Srgrimes	char *p;
5481592Srgrimes
5491592Srgrimes	p = line + sizeof(_PATH_DEV) - 1;
5501592Srgrimes	if (logout(p))
5511592Srgrimes		logwtmp(p, "", "");
5521592Srgrimes	(void)chmod(line, 0666);
5531592Srgrimes	(void)chown(line, 0, 0);
5541592Srgrimes	*p = 'p';
5551592Srgrimes	(void)chmod(line, 0666);
5561592Srgrimes	(void)chown(line, 0, 0);
5571592Srgrimes	shutdown(netf, 2);
5581592Srgrimes	exit(1);
5591592Srgrimes}
5601592Srgrimes
5611592Srgrimesvoid
5621592Srgrimesfatal(f, msg, syserr)
5631592Srgrimes	int f;
5641592Srgrimes	char *msg;
5651592Srgrimes	int syserr;
5661592Srgrimes{
5671592Srgrimes	int len;
5681592Srgrimes	char buf[BUFSIZ], *bp = buf;
5691592Srgrimes
5701592Srgrimes	/*
5711592Srgrimes	 * Prepend binary one to message if we haven't sent
5721592Srgrimes	 * the magic null as confirmation.
5731592Srgrimes	 */
5741592Srgrimes	if (!confirmed)
5751592Srgrimes		*bp++ = '\01';		/* error indicator */
5761592Srgrimes	if (syserr)
5771592Srgrimes		len = sprintf(bp, "rlogind: %s: %s.\r\n",
5781592Srgrimes		    msg, strerror(errno));
5791592Srgrimes	else
5801592Srgrimes		len = sprintf(bp, "rlogind: %s.\r\n", msg);
5811592Srgrimes	(void) write(f, buf, bp + len - buf);
5821592Srgrimes	exit(1);
5831592Srgrimes}
5841592Srgrimes
5851592Srgrimesint
5861592Srgrimesdo_rlogin(dest)
5871592Srgrimes	struct sockaddr_in *dest;
5881592Srgrimes{
5891592Srgrimes	getstr(rusername, sizeof(rusername), "remuser too long");
5901592Srgrimes	getstr(lusername, sizeof(lusername), "locuser too long");
5911592Srgrimes	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
5921592Srgrimes
5931592Srgrimes	pwd = getpwnam(lusername);
5941592Srgrimes	if (pwd == NULL)
5951592Srgrimes		return (-1);
5961592Srgrimes	/* XXX why don't we syslog() failure? */
59712434Speter	return (iruserok(dest->sin_addr.s_addr, pwd->pw_uid == 0,
59812434Speter		rusername, lusername));
5991592Srgrimes}
6001592Srgrimes
6011592Srgrimesvoid
6021592Srgrimesgetstr(buf, cnt, errmsg)
6031592Srgrimes	char *buf;
6041592Srgrimes	int cnt;
6051592Srgrimes	char *errmsg;
6061592Srgrimes{
6071592Srgrimes	char c;
6081592Srgrimes
6091592Srgrimes	do {
6101592Srgrimes		if (read(0, &c, 1) != 1)
6111592Srgrimes			exit(1);
6121592Srgrimes		if (--cnt < 0)
6131592Srgrimes			fatal(STDOUT_FILENO, errmsg, 0);
6141592Srgrimes		*buf++ = c;
6151592Srgrimes	} while (c != 0);
6161592Srgrimes}
6171592Srgrimes
6181592Srgrimesextern	char **environ;
6191592Srgrimes
6201592Srgrimesvoid
6211592Srgrimessetup_term(fd)
6221592Srgrimes	int fd;
6231592Srgrimes{
6241592Srgrimes	register char *cp = index(term+ENVSIZE, '/');
6251592Srgrimes	char *speed;
6261592Srgrimes	struct termios tt;
6271592Srgrimes
6281592Srgrimes#ifndef notyet
6291592Srgrimes	tcgetattr(fd, &tt);
6301592Srgrimes	if (cp) {
6311592Srgrimes		*cp++ = '\0';
6321592Srgrimes		speed = cp;
6331592Srgrimes		cp = index(speed, '/');
6341592Srgrimes		if (cp)
6351592Srgrimes			*cp++ = '\0';
6361592Srgrimes		cfsetspeed(&tt, atoi(speed));
6371592Srgrimes	}
6381592Srgrimes
6391592Srgrimes	tt.c_iflag = TTYDEF_IFLAG;
6401592Srgrimes	tt.c_oflag = TTYDEF_OFLAG;
6411592Srgrimes	tt.c_lflag = TTYDEF_LFLAG;
6421592Srgrimes	tcsetattr(fd, TCSAFLUSH, &tt);
6431592Srgrimes#else
6441592Srgrimes	if (cp) {
6451592Srgrimes		*cp++ = '\0';
6461592Srgrimes		speed = cp;
6471592Srgrimes		cp = index(speed, '/');
6481592Srgrimes		if (cp)
6491592Srgrimes			*cp++ = '\0';
6501592Srgrimes		tcgetattr(fd, &tt);
6511592Srgrimes		cfsetspeed(&tt, atoi(speed));
6521592Srgrimes		tcsetattr(fd, TCSAFLUSH, &tt);
6531592Srgrimes	}
6541592Srgrimes#endif
6551592Srgrimes
6561592Srgrimes	env[0] = term;
6571592Srgrimes	env[1] = 0;
6581592Srgrimes	environ = env;
6591592Srgrimes}
6601592Srgrimes
6611592Srgrimes#ifdef	KERBEROS
6621592Srgrimes#define	VERSION_SIZE	9
6631592Srgrimes
6641592Srgrimes/*
6651592Srgrimes * Do the remote kerberos login to the named host with the
6661592Srgrimes * given inet address
6671592Srgrimes *
6681592Srgrimes * Return 0 on valid authorization
6691592Srgrimes * Return -1 on valid authentication, no authorization
6701592Srgrimes * Return >0 for error conditions
6711592Srgrimes */
6721592Srgrimesint
6731592Srgrimesdo_krb_login(dest)
6741592Srgrimes	struct sockaddr_in *dest;
6751592Srgrimes{
6761592Srgrimes	int rc;
6771592Srgrimes	char instance[INST_SZ], version[VERSION_SIZE];
6781592Srgrimes	long authopts = 0L;	/* !mutual */
6791592Srgrimes	struct sockaddr_in faddr;
6801592Srgrimes
6811592Srgrimes	kdata = (AUTH_DAT *) auth_buf;
6821592Srgrimes	ticket = (KTEXT) tick_buf;
6831592Srgrimes
6841592Srgrimes	instance[0] = '*';
6851592Srgrimes	instance[1] = '\0';
6861592Srgrimes
6871592Srgrimes#ifdef	CRYPT
6881592Srgrimes	if (doencrypt) {
6891592Srgrimes		rc = sizeof(faddr);
6901592Srgrimes		if (getsockname(0, (struct sockaddr *)&faddr, &rc))
6911592Srgrimes			return (-1);
6921592Srgrimes		authopts = KOPT_DO_MUTUAL;
6931592Srgrimes		rc = krb_recvauth(
6941592Srgrimes			authopts, 0,
6951592Srgrimes			ticket, "rcmd",
6961592Srgrimes			instance, dest, &faddr,
6971592Srgrimes			kdata, "", schedule, version);
69829916Smarkm		 des_set_key(&kdata->session, schedule);
6991592Srgrimes
7001592Srgrimes	} else
7011592Srgrimes#endif
7021592Srgrimes		rc = krb_recvauth(
7031592Srgrimes			authopts, 0,
7041592Srgrimes			ticket, "rcmd",
7051592Srgrimes			instance, dest, (struct sockaddr_in *) 0,
70618449Spst			kdata, "", NULL, version);
7071592Srgrimes
7081592Srgrimes	if (rc != KSUCCESS)
7091592Srgrimes		return (rc);
7101592Srgrimes
7111592Srgrimes	getstr(lusername, sizeof(lusername), "locuser");
7121592Srgrimes	/* get the "cmd" in the rcmd protocol */
7131592Srgrimes	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type");
7141592Srgrimes
7151592Srgrimes	pwd = getpwnam(lusername);
7161592Srgrimes	if (pwd == NULL)
7171592Srgrimes		return (-1);
7181592Srgrimes
7191592Srgrimes	/* returns nonzero for no access */
7201592Srgrimes	if (kuserok(kdata, lusername) != 0)
7211592Srgrimes		return (-1);
7228870Srgrimes
7231592Srgrimes	return (0);
7241592Srgrimes
7251592Srgrimes}
7261592Srgrimes#endif /* KERBEROS */
7271592Srgrimes
7281592Srgrimesvoid
7291592Srgrimesusage()
7301592Srgrimes{
7311592Srgrimes#ifdef KERBEROS
73211486Sdg	syslog(LOG_ERR, "usage: rlogind [-Daln] [-k | -v]");
7331592Srgrimes#else
73411486Sdg	syslog(LOG_ERR, "usage: rlogind [-Daln]");
7351592Srgrimes#endif
7361592Srgrimes}
737