rlogind.c revision 216584
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 216584 2010-12-20 08:44:08Z charnier $");
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
971592Srgrimeschar	*env[2];
981592Srgrimes#define	NMAX 30
991592Srgrimeschar	lusername[NMAX+1], rusername[NMAX+1];
1001592Srgrimesstatic	char term[64] = "TERM=";
1011592Srgrimes#define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
1021592Srgrimesint	keepalive = 1;
1031592Srgrimesint	check_all = 0;
10411486Sdgint	no_delay;
1051592Srgrimes
1061592Srgrimesstruct	passwd *pwd;
1071592Srgrimes
10856590Sshinunion sockunion {
10956590Sshin	struct sockinet {
11056590Sshin		u_char si_len;
11156590Sshin		u_char si_family;
11256590Sshin		u_short si_port;
11356590Sshin	} su_si;
11456590Sshin	struct sockaddr_in  su_sin;
11556590Sshin	struct sockaddr_in6 su_sin6;
11656590Sshin};
11756590Sshin#define su_len		su_si.si_len
11856590Sshin#define su_family	su_si.si_family
11956590Sshin#define su_port		su_si.si_port
12056590Sshin
12190377Simpvoid	doit(int, union sockunion *);
12290377Simpint	control(int, char *, int);
12390377Simpvoid	protocol(int, int);
12490377Simpvoid	cleanup(int);
12590377Simpvoid	fatal(int, char *, int);
12690377Simpint	do_rlogin(union sockunion *);
12790377Simpvoid	getstr(char *, int, char *);
12890377Simpvoid	setup_term(int);
12990377Simpint	do_krb_login(struct sockaddr_in *);
13090377Simpvoid	usage(void);
1311592Srgrimes
13251433Smarkm
1331592Srgrimesint
13490377Simpmain(int argc, char *argv[])
1351592Srgrimes{
1361592Srgrimes	extern int __check_rhosts_file;
13756590Sshin	union sockunion from;
138141918Sstefanf	socklen_t fromlen;
139141918Sstefanf	int ch, on;
1401592Srgrimes
1411592Srgrimes	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
1421592Srgrimes
1431592Srgrimes	opterr = 0;
14424349Simp	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
1451592Srgrimes		switch (ch) {
14611486Sdg		case 'D':
14711486Sdg			no_delay = 1;
14811486Sdg			break;
1491592Srgrimes		case 'a':
1501592Srgrimes			check_all = 1;
1511592Srgrimes			break;
1521592Srgrimes		case 'l':
1531592Srgrimes			__check_rhosts_file = 0;
1541592Srgrimes			break;
1551592Srgrimes		case 'n':
1561592Srgrimes			keepalive = 0;
1571592Srgrimes			break;
1581592Srgrimes		case '?':
1591592Srgrimes		default:
1601592Srgrimes			usage();
1611592Srgrimes			break;
1621592Srgrimes		}
1631592Srgrimes	argc -= optind;
1641592Srgrimes	argv += optind;
1651592Srgrimes
1661592Srgrimes	fromlen = sizeof (from);
1671592Srgrimes	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
1681592Srgrimes		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
1691592Srgrimes		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
1701592Srgrimes	}
1711592Srgrimes	on = 1;
1721592Srgrimes	if (keepalive &&
1731592Srgrimes	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
1741592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
17511486Sdg	if (no_delay &&
17611486Sdg	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
17711486Sdg		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
17896196Sdes	if (from.su_family == AF_INET)
17956590Sshin      {
1801592Srgrimes	on = IPTOS_LOWDELAY;
1811592Srgrimes	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1821592Srgrimes		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
18356590Sshin      }
18411486Sdg
1851592Srgrimes	doit(0, &from);
18629916Smarkm	return 0;
1871592Srgrimes}
1881592Srgrimes
1891592Srgrimesint	child;
1901592Srgrimesint	netf;
1911592Srgrimeschar	line[MAXPATHLEN];
1921592Srgrimesint	confirmed;
1931592Srgrimes
1941592Srgrimesstruct winsize win = { 0, 0, 0, 0 };
1951592Srgrimes
1961592Srgrimes
1971592Srgrimesvoid
19890377Simpdoit(int f, union sockunion *fromp)
1991592Srgrimes{
2001592Srgrimes	int master, pid, on = 1;
2011592Srgrimes	int authenticated = 0;
20256590Sshin	char hostname[2 * MAXHOSTNAMELEN + 1];
20356590Sshin	char nameinfo[2 * INET6_ADDRSTRLEN + 1];
2041592Srgrimes	char c;
2051592Srgrimes
2061592Srgrimes	alarm(60);
2071592Srgrimes	read(f, &c, 1);
2081592Srgrimes
2091592Srgrimes	if (c != 0)
2101592Srgrimes		exit(1);
2111592Srgrimes
2121592Srgrimes	alarm(0);
21356590Sshin
21456590Sshin	realhostname_sa(hostname, sizeof(hostname) - 1,
21556590Sshin			    (struct sockaddr *)fromp, fromp->su_len);
21656590Sshin	/* error check ? */
21756590Sshin	fromp->su_port = ntohs((u_short)fromp->su_port);
21824191Simp	hostname[sizeof(hostname) - 1] = '\0';
2191592Srgrimes
2201592Srgrimes	{
22163959Sume		if ((fromp->su_family != AF_INET
22256590Sshin#ifdef INET6
22363959Sume		  && fromp->su_family != AF_INET6
22456590Sshin#endif
22556590Sshin		     ) ||
22656590Sshin		    fromp->su_port >= IPPORT_RESERVED ||
22756590Sshin		    fromp->su_port < IPPORT_RESERVED/2) {
22856590Sshin			getnameinfo((struct sockaddr *)fromp,
22956590Sshin				    fromp->su_len,
23056590Sshin				    nameinfo, sizeof(nameinfo), NULL, 0,
231146187Sume				    NI_NUMERICHOST);
23256590Sshin			/* error check ? */
2331592Srgrimes			syslog(LOG_NOTICE, "Connection from %s on illegal port",
23456590Sshin			       nameinfo);
2351592Srgrimes			fatal(f, "Permission denied", 0);
2361592Srgrimes		}
2371592Srgrimes#ifdef IP_OPTIONS
23856590Sshin		if (fromp->su_family == AF_INET)
23996196Sdes	      {
24022455Simp		u_char optbuf[BUFSIZ/3];
241141918Sstefanf		socklen_t optsize = sizeof(optbuf);
242141918Sstefanf		int ipproto, i;
2431592Srgrimes		struct protoent *ip;
2441592Srgrimes
2451592Srgrimes		if ((ip = getprotobyname("ip")) != NULL)
2461592Srgrimes			ipproto = ip->p_proto;
2471592Srgrimes		else
2481592Srgrimes			ipproto = IPPROTO_IP;
2491592Srgrimes		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
2501592Srgrimes		    &optsize) == 0 && optsize != 0) {
25122455Simp			for (i = 0; i < optsize; ) {
25222455Simp				u_char c = optbuf[i];
25322455Simp				if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
25422455Simp					syslog(LOG_NOTICE,
25522455Simp						"Connection refused from %s with IP option %s",
25656590Sshin						inet_ntoa(fromp->su_sin.sin_addr),
25722455Simp						c == IPOPT_LSRR ? "LSRR" : "SSRR");
25822455Simp					exit(1);
25922455Simp				}
26022455Simp				if (c == IPOPT_EOL)
26122455Simp					break;
26222455Simp				i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
2631592Srgrimes			}
2641592Srgrimes		}
26556590Sshin	      }
2661592Srgrimes#endif
2671592Srgrimes		if (do_rlogin(fromp) == 0)
2681592Srgrimes			authenticated++;
2691592Srgrimes	}
2701592Srgrimes	if (confirmed == 0) {
2711592Srgrimes		write(f, "", 1);
2721592Srgrimes		confirmed = 1;		/* we sent the null! */
2731592Srgrimes	}
2741592Srgrimes	netf = f;
2751592Srgrimes
2761592Srgrimes	pid = forkpty(&master, line, NULL, &win);
2771592Srgrimes	if (pid < 0) {
2781592Srgrimes		if (errno == ENOENT)
2791592Srgrimes			fatal(f, "Out of ptys", 0);
2801592Srgrimes		else
2811592Srgrimes			fatal(f, "Forkpty", 1);
2821592Srgrimes	}
2831592Srgrimes	if (pid == 0) {
2841592Srgrimes		if (f > 2)	/* f should always be 0, but... */
2851592Srgrimes			(void) close(f);
2861592Srgrimes		setup_term(0);
28712575Snate		 if (*lusername=='-') {
2882076Sguido			syslog(LOG_ERR, "tried to pass user \"%s\" to login",
2892076Sguido			       lusername);
2902076Sguido			fatal(STDERR_FILENO, "invalid user", 0);
2912076Sguido		}
2921592Srgrimes		if (authenticated) {
2931592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
2941592Srgrimes			    "-h", hostname, "-f", lusername, (char *)NULL);
2951592Srgrimes		} else
2961592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
2971592Srgrimes			    "-h", hostname, lusername, (char *)NULL);
2981592Srgrimes		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
2991592Srgrimes		/*NOTREACHED*/
3001592Srgrimes	}
301141588Sru	ioctl(f, FIONBIO, &on);
3021592Srgrimes	ioctl(master, FIONBIO, &on);
3031592Srgrimes	ioctl(master, TIOCPKT, &on);
3041592Srgrimes	signal(SIGCHLD, cleanup);
3051592Srgrimes	protocol(f, master);
3061592Srgrimes	signal(SIGCHLD, SIG_IGN);
3071592Srgrimes	cleanup(0);
3081592Srgrimes}
3091592Srgrimes
3101592Srgrimeschar	magic[2] = { 0377, 0377 };
3111592Srgrimeschar	oobdata[] = {TIOCPKT_WINDOW};
3121592Srgrimes
3131592Srgrimes/*
3141592Srgrimes * Handle a "control" request (signaled by magic being present)
3151592Srgrimes * in the data stream.  For now, we are only willing to handle
3161592Srgrimes * window size changes.
3171592Srgrimes */
3181592Srgrimesint
31990377Simpcontrol(int pty, char *cp, int n)
3201592Srgrimes{
3211592Srgrimes	struct winsize w;
3221592Srgrimes
323114624Sobrien	if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
3241592Srgrimes		return (0);
3251592Srgrimes	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
3261592Srgrimes	bcopy(cp+4, (char *)&w, sizeof(w));
3271592Srgrimes	w.ws_row = ntohs(w.ws_row);
3281592Srgrimes	w.ws_col = ntohs(w.ws_col);
3291592Srgrimes	w.ws_xpixel = ntohs(w.ws_xpixel);
3301592Srgrimes	w.ws_ypixel = ntohs(w.ws_ypixel);
3311592Srgrimes	(void)ioctl(pty, TIOCSWINSZ, &w);
3321592Srgrimes	return (4+sizeof (w));
3331592Srgrimes}
3341592Srgrimes
3351592Srgrimes/*
3361592Srgrimes * rlogin "protocol" machine.
3371592Srgrimes */
3381592Srgrimesvoid
33990377Simpprotocol(int f, int p)
3401592Srgrimes{
34169705Sru	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
34246078Simp	int pcc = 0, fcc = 0;
3431592Srgrimes	int cc, nfd, n;
3441592Srgrimes	char cntl;
3451592Srgrimes
3461592Srgrimes	/*
3471592Srgrimes	 * Must ignore SIGTTOU, otherwise we'll stop
3481592Srgrimes	 * when we try and set slave pty's window shape
3491592Srgrimes	 * (our controlling tty is the master pty).
3501592Srgrimes	 */
3511592Srgrimes	(void) signal(SIGTTOU, SIG_IGN);
3521592Srgrimes	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
3531592Srgrimes	if (f > p)
3541592Srgrimes		nfd = f + 1;
3551592Srgrimes	else
3561592Srgrimes		nfd = p + 1;
3571592Srgrimes	if (nfd > FD_SETSIZE) {
3581592Srgrimes		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
3591592Srgrimes		fatal(f, "internal error (select mask too small)", 0);
3601592Srgrimes	}
3611592Srgrimes	for (;;) {
3621592Srgrimes		fd_set ibits, obits, ebits, *omask;
3631592Srgrimes
3641592Srgrimes		FD_ZERO(&ebits);
3651592Srgrimes		FD_ZERO(&ibits);
3661592Srgrimes		FD_ZERO(&obits);
3671592Srgrimes		omask = (fd_set *)NULL;
3681592Srgrimes		if (fcc) {
3691592Srgrimes			FD_SET(p, &obits);
3701592Srgrimes			omask = &obits;
3711592Srgrimes		} else
3721592Srgrimes			FD_SET(f, &ibits);
37346078Simp		if (pcc >= 0) {
3741592Srgrimes			if (pcc) {
3751592Srgrimes				FD_SET(f, &obits);
3761592Srgrimes				omask = &obits;
3771592Srgrimes			} else
3781592Srgrimes				FD_SET(p, &ibits);
37946078Simp		}
3801592Srgrimes		FD_SET(p, &ebits);
3811592Srgrimes		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
3821592Srgrimes			if (errno == EINTR)
3831592Srgrimes				continue;
3841592Srgrimes			fatal(f, "select", 1);
3851592Srgrimes		}
3861592Srgrimes		if (n == 0) {
3871592Srgrimes			/* shouldn't happen... */
3881592Srgrimes			sleep(5);
3891592Srgrimes			continue;
3901592Srgrimes		}
3911592Srgrimes#define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
3921592Srgrimes		if (FD_ISSET(p, &ebits)) {
3931592Srgrimes			cc = read(p, &cntl, 1);
3941592Srgrimes			if (cc == 1 && pkcontrol(cntl)) {
3951592Srgrimes				cntl |= oobdata[0];
3961592Srgrimes				send(f, &cntl, 1, MSG_OOB);
3971592Srgrimes				if (cntl & TIOCPKT_FLUSHWRITE) {
3981592Srgrimes					pcc = 0;
3991592Srgrimes					FD_CLR(p, &ibits);
4001592Srgrimes				}
4011592Srgrimes			}
4021592Srgrimes		}
4031592Srgrimes		if (FD_ISSET(f, &ibits)) {
404141588Sru			fcc = read(f, fibuf, sizeof(fibuf));
4051592Srgrimes			if (fcc < 0 && errno == EWOULDBLOCK)
4061592Srgrimes				fcc = 0;
4071592Srgrimes			else {
40890377Simp				char *cp;
4091592Srgrimes				int left, n;
4101592Srgrimes
4111592Srgrimes				if (fcc <= 0)
4121592Srgrimes					break;
4131592Srgrimes				fbp = fibuf;
4141592Srgrimes
4151592Srgrimes			top:
4161592Srgrimes				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
4171592Srgrimes					if (cp[0] == magic[0] &&
4181592Srgrimes					    cp[1] == magic[1]) {
4191592Srgrimes						left = fcc - (cp-fibuf);
4201592Srgrimes						n = control(p, cp, left);
4211592Srgrimes						if (n) {
4221592Srgrimes							left -= n;
4231592Srgrimes							if (left > 0)
4241592Srgrimes								bcopy(cp+n, cp, left);
4251592Srgrimes							fcc -= n;
4261592Srgrimes							goto top; /* n^2 */
4271592Srgrimes						}
4281592Srgrimes					}
4291592Srgrimes				FD_SET(p, &obits);		/* try write */
4301592Srgrimes			}
4311592Srgrimes		}
4321592Srgrimes
4331592Srgrimes		if (FD_ISSET(p, &obits) && fcc > 0) {
4341592Srgrimes			cc = write(p, fbp, fcc);
4351592Srgrimes			if (cc > 0) {
4361592Srgrimes				fcc -= cc;
4371592Srgrimes				fbp += cc;
4381592Srgrimes			}
4391592Srgrimes		}
4401592Srgrimes
4411592Srgrimes		if (FD_ISSET(p, &ibits)) {
4421592Srgrimes			pcc = read(p, pibuf, sizeof (pibuf));
4431592Srgrimes			pbp = pibuf;
4441592Srgrimes			if (pcc < 0 && errno == EWOULDBLOCK)
4451592Srgrimes				pcc = 0;
4461592Srgrimes			else if (pcc <= 0)
4471592Srgrimes				break;
4481592Srgrimes			else if (pibuf[0] == 0) {
4491592Srgrimes				pbp++, pcc--;
450141588Sru				FD_SET(f, &obits);	/* try write */
4511592Srgrimes			} else {
4521592Srgrimes				if (pkcontrol(pibuf[0])) {
4531592Srgrimes					pibuf[0] |= oobdata[0];
4541592Srgrimes					send(f, &pibuf[0], 1, MSG_OOB);
4551592Srgrimes				}
4561592Srgrimes				pcc = 0;
4571592Srgrimes			}
4581592Srgrimes		}
4591592Srgrimes		if ((FD_ISSET(f, &obits)) && pcc > 0) {
460141588Sru			cc = write(f, pbp, pcc);
4611592Srgrimes			if (cc < 0 && errno == EWOULDBLOCK) {
4621592Srgrimes				/*
4631592Srgrimes				 * This happens when we try write after read
4641592Srgrimes				 * from p, but some old kernels balk at large
4651592Srgrimes				 * writes even when select returns true.
4661592Srgrimes				 */
4671592Srgrimes				if (!FD_ISSET(p, &ibits))
4681592Srgrimes					sleep(5);
4691592Srgrimes				continue;
4701592Srgrimes			}
4711592Srgrimes			if (cc > 0) {
4721592Srgrimes				pcc -= cc;
4731592Srgrimes				pbp += cc;
4741592Srgrimes			}
4751592Srgrimes		}
4761592Srgrimes	}
4771592Srgrimes}
4781592Srgrimes
4791592Srgrimesvoid
480216584Scharniercleanup(int signo __unused)
4811592Srgrimes{
4821592Srgrimes
483146075Sjmallett	shutdown(netf, SHUT_RDWR);
4841592Srgrimes	exit(1);
4851592Srgrimes}
4861592Srgrimes
4871592Srgrimesvoid
48890377Simpfatal(int f, char *msg, int syserr)
4891592Srgrimes{
4901592Srgrimes	int len;
4911592Srgrimes	char buf[BUFSIZ], *bp = buf;
4921592Srgrimes
4931592Srgrimes	/*
4941592Srgrimes	 * Prepend binary one to message if we haven't sent
4951592Srgrimes	 * the magic null as confirmation.
4961592Srgrimes	 */
4971592Srgrimes	if (!confirmed)
4981592Srgrimes		*bp++ = '\01';		/* error indicator */
4991592Srgrimes	if (syserr)
50064238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
5011592Srgrimes		    msg, strerror(errno));
5021592Srgrimes	else
50364238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
50481991Sbrian	if (len < 0)
50581972Sbrian		len = 0;
5061592Srgrimes	(void) write(f, buf, bp + len - buf);
5071592Srgrimes	exit(1);
5081592Srgrimes}
5091592Srgrimes
5101592Srgrimesint
51190377Simpdo_rlogin(union sockunion *dest)
5121592Srgrimes{
51366755Sru
5141592Srgrimes	getstr(rusername, sizeof(rusername), "remuser too long");
5151592Srgrimes	getstr(lusername, sizeof(lusername), "locuser too long");
5161592Srgrimes	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
5171592Srgrimes
5181592Srgrimes	pwd = getpwnam(lusername);
5191592Srgrimes	if (pwd == NULL)
5201592Srgrimes		return (-1);
5211592Srgrimes	/* XXX why don't we syslog() failure? */
52256590Sshin
52356939Sshin	return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
52456939Sshin			    lusername));
5251592Srgrimes}
5261592Srgrimes
5271592Srgrimesvoid
52890377Simpgetstr(char *buf, int cnt, char *errmsg)
5291592Srgrimes{
5301592Srgrimes	char c;
5311592Srgrimes
5321592Srgrimes	do {
53380381Ssheldonh		if (read(STDIN_FILENO, &c, 1) != 1)
5341592Srgrimes			exit(1);
5351592Srgrimes		if (--cnt < 0)
5361592Srgrimes			fatal(STDOUT_FILENO, errmsg, 0);
5371592Srgrimes		*buf++ = c;
5381592Srgrimes	} while (c != 0);
5391592Srgrimes}
5401592Srgrimes
5411592Srgrimesextern	char **environ;
5421592Srgrimes
5431592Srgrimesvoid
54490377Simpsetup_term(int fd)
5451592Srgrimes{
54690377Simp	char *cp = index(term+ENVSIZE, '/');
5471592Srgrimes	char *speed;
548214680Sed	struct termios tt, def;
5491592Srgrimes
5501592Srgrimes#ifndef notyet
5511592Srgrimes	tcgetattr(fd, &tt);
5521592Srgrimes	if (cp) {
5531592Srgrimes		*cp++ = '\0';
5541592Srgrimes		speed = cp;
5551592Srgrimes		cp = index(speed, '/');
5561592Srgrimes		if (cp)
5571592Srgrimes			*cp++ = '\0';
5581592Srgrimes		cfsetspeed(&tt, atoi(speed));
5591592Srgrimes	}
5601592Srgrimes
561214680Sed	cfmakesane(&def);
562214680Sed	tt.c_iflag = def.c_iflag;
563214680Sed	tt.c_oflag = def.c_oflag;
564214680Sed	tt.c_lflag = def.c_lflag;
5651592Srgrimes	tcsetattr(fd, TCSAFLUSH, &tt);
5661592Srgrimes#else
5671592Srgrimes	if (cp) {
5681592Srgrimes		*cp++ = '\0';
5691592Srgrimes		speed = cp;
5701592Srgrimes		cp = index(speed, '/');
5711592Srgrimes		if (cp)
5721592Srgrimes			*cp++ = '\0';
5731592Srgrimes		tcgetattr(fd, &tt);
5741592Srgrimes		cfsetspeed(&tt, atoi(speed));
5751592Srgrimes		tcsetattr(fd, TCSAFLUSH, &tt);
5761592Srgrimes	}
5771592Srgrimes#endif
5781592Srgrimes
5791592Srgrimes	env[0] = term;
5801592Srgrimes	env[1] = 0;
5811592Srgrimes	environ = env;
5821592Srgrimes}
5831592Srgrimes
5841592Srgrimesvoid
58590377Simpusage(void)
5861592Srgrimes{
58751433Smarkm	syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");
5881592Srgrimes}
589