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.
20262435Sbrueffer * 3. Neither the name of the University nor the names of its contributors
211592Srgrimes *    may be used to endorse or promote products derived from this software
221592Srgrimes *    without specific prior written permission.
231592Srgrimes *
241592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341592Srgrimes * SUCH DAMAGE.
351592Srgrimes */
361592Srgrimes
37114624Sobrien#if 0
381592Srgrimes#ifndef lint
3929916Smarkmstatic const char copyright[] =
401592Srgrimes"@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
411592Srgrimes	The Regents of the University of California.  All rights reserved.\n";
421592Srgrimes#endif /* not lint */
431592Srgrimes
441592Srgrimes#ifndef lint
4529916Smarkmstatic const char sccsid[] = "@(#)rlogind.c	8.1 (Berkeley) 6/4/93";
46114624Sobrien#endif /* not lint */
4731405Scharnier#endif
48114624Sobrien#include <sys/cdefs.h>
49114624Sobrien__FBSDID("$FreeBSD$");
501592Srgrimes
511592Srgrimes/*
521592Srgrimes * remote login server:
531592Srgrimes *	\0
541592Srgrimes *	remuser\0
551592Srgrimes *	locuser\0
561592Srgrimes *	terminal_type/speed\0
571592Srgrimes *	data
581592Srgrimes */
591592Srgrimes
601592Srgrimes#define	FD_SETSIZE	16		/* don't need many bits for select */
6129916Smarkm#include <sys/types.h>
621592Srgrimes#include <sys/param.h>
631592Srgrimes#include <sys/stat.h>
641592Srgrimes#include <sys/ioctl.h>
651592Srgrimes#include <signal.h>
661592Srgrimes#include <termios.h>
671592Srgrimes
681592Srgrimes#include <sys/socket.h>
691592Srgrimes#include <netinet/in.h>
701592Srgrimes#include <netinet/in_systm.h>
711592Srgrimes#include <netinet/ip.h>
7211486Sdg#include <netinet/tcp.h>
731592Srgrimes#include <arpa/inet.h>
741592Srgrimes#include <netdb.h>
751592Srgrimes
7631405Scharnier#include <errno.h>
7731405Scharnier#include <libutil.h>
7896196Sdes#include <paths.h>
791592Srgrimes#include <pwd.h>
801592Srgrimes#include <syslog.h>
811592Srgrimes#include <stdio.h>
821592Srgrimes#include <stdlib.h>
831592Srgrimes#include <string.h>
8431405Scharnier#include <unistd.h>
851592Srgrimes
8651433Smarkm
871592Srgrimes#ifndef TIOCPKT_WINDOW
881592Srgrimes#define TIOCPKT_WINDOW 0x80
891592Srgrimes#endif
901592Srgrimes
91141588Sru#define		ARGSTR			"Daln"
921592Srgrimes
931592Srgrimeschar	*env[2];
941592Srgrimes#define	NMAX 30
951592Srgrimeschar	lusername[NMAX+1], rusername[NMAX+1];
961592Srgrimesstatic	char term[64] = "TERM=";
971592Srgrimes#define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
981592Srgrimesint	keepalive = 1;
991592Srgrimesint	check_all = 0;
10011486Sdgint	no_delay;
1011592Srgrimes
1021592Srgrimesstruct	passwd *pwd;
1031592Srgrimes
10456590Sshinunion sockunion {
10556590Sshin	struct sockinet {
10656590Sshin		u_char si_len;
10756590Sshin		u_char si_family;
10856590Sshin		u_short si_port;
10956590Sshin	} su_si;
11056590Sshin	struct sockaddr_in  su_sin;
11156590Sshin	struct sockaddr_in6 su_sin6;
11256590Sshin};
11356590Sshin#define su_len		su_si.si_len
11456590Sshin#define su_family	su_si.si_family
11556590Sshin#define su_port		su_si.si_port
11656590Sshin
11790377Simpvoid	doit(int, union sockunion *);
11890377Simpint	control(int, char *, int);
11990377Simpvoid	protocol(int, int);
12090377Simpvoid	cleanup(int);
12190377Simpvoid	fatal(int, char *, int);
12290377Simpint	do_rlogin(union sockunion *);
12390377Simpvoid	getstr(char *, int, char *);
12490377Simpvoid	setup_term(int);
12590377Simpint	do_krb_login(struct sockaddr_in *);
12690377Simpvoid	usage(void);
1271592Srgrimes
12851433Smarkm
1291592Srgrimesint
13090377Simpmain(int argc, char *argv[])
1311592Srgrimes{
1321592Srgrimes	extern int __check_rhosts_file;
13356590Sshin	union sockunion from;
134141918Sstefanf	socklen_t fromlen;
135141918Sstefanf	int ch, on;
1361592Srgrimes
1371592Srgrimes	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
1381592Srgrimes
1391592Srgrimes	opterr = 0;
14024349Simp	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
1411592Srgrimes		switch (ch) {
14211486Sdg		case 'D':
14311486Sdg			no_delay = 1;
14411486Sdg			break;
1451592Srgrimes		case 'a':
1461592Srgrimes			check_all = 1;
1471592Srgrimes			break;
1481592Srgrimes		case 'l':
1491592Srgrimes			__check_rhosts_file = 0;
1501592Srgrimes			break;
1511592Srgrimes		case 'n':
1521592Srgrimes			keepalive = 0;
1531592Srgrimes			break;
1541592Srgrimes		case '?':
1551592Srgrimes		default:
1561592Srgrimes			usage();
1571592Srgrimes			break;
1581592Srgrimes		}
1591592Srgrimes	argc -= optind;
1601592Srgrimes	argv += optind;
1611592Srgrimes
1621592Srgrimes	fromlen = sizeof (from);
1631592Srgrimes	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
1641592Srgrimes		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
1651592Srgrimes		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
1661592Srgrimes	}
1671592Srgrimes	on = 1;
1681592Srgrimes	if (keepalive &&
1691592Srgrimes	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
1701592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
17111486Sdg	if (no_delay &&
17211486Sdg	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
17311486Sdg		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
17496196Sdes	if (from.su_family == AF_INET)
17556590Sshin      {
1761592Srgrimes	on = IPTOS_LOWDELAY;
1771592Srgrimes	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1781592Srgrimes		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
17956590Sshin      }
18011486Sdg
1811592Srgrimes	doit(0, &from);
18229916Smarkm	return 0;
1831592Srgrimes}
1841592Srgrimes
1851592Srgrimesint	child;
1861592Srgrimesint	netf;
1871592Srgrimeschar	line[MAXPATHLEN];
1881592Srgrimesint	confirmed;
1891592Srgrimes
1901592Srgrimesstruct winsize win = { 0, 0, 0, 0 };
1911592Srgrimes
1921592Srgrimes
1931592Srgrimesvoid
19490377Simpdoit(int f, union sockunion *fromp)
1951592Srgrimes{
1961592Srgrimes	int master, pid, on = 1;
1971592Srgrimes	int authenticated = 0;
19856590Sshin	char hostname[2 * MAXHOSTNAMELEN + 1];
19956590Sshin	char nameinfo[2 * INET6_ADDRSTRLEN + 1];
2001592Srgrimes	char c;
2011592Srgrimes
2021592Srgrimes	alarm(60);
2031592Srgrimes	read(f, &c, 1);
2041592Srgrimes
2051592Srgrimes	if (c != 0)
2061592Srgrimes		exit(1);
2071592Srgrimes
2081592Srgrimes	alarm(0);
20956590Sshin
21056590Sshin	realhostname_sa(hostname, sizeof(hostname) - 1,
21156590Sshin			    (struct sockaddr *)fromp, fromp->su_len);
21256590Sshin	/* error check ? */
21356590Sshin	fromp->su_port = ntohs((u_short)fromp->su_port);
21424191Simp	hostname[sizeof(hostname) - 1] = '\0';
2151592Srgrimes
2161592Srgrimes	{
21763959Sume		if ((fromp->su_family != AF_INET
21856590Sshin#ifdef INET6
21963959Sume		  && fromp->su_family != AF_INET6
22056590Sshin#endif
22156590Sshin		     ) ||
22256590Sshin		    fromp->su_port >= IPPORT_RESERVED ||
22356590Sshin		    fromp->su_port < IPPORT_RESERVED/2) {
22456590Sshin			getnameinfo((struct sockaddr *)fromp,
22556590Sshin				    fromp->su_len,
22656590Sshin				    nameinfo, sizeof(nameinfo), NULL, 0,
227146187Sume				    NI_NUMERICHOST);
22856590Sshin			/* error check ? */
2291592Srgrimes			syslog(LOG_NOTICE, "Connection from %s on illegal port",
23056590Sshin			       nameinfo);
2311592Srgrimes			fatal(f, "Permission denied", 0);
2321592Srgrimes		}
2331592Srgrimes#ifdef IP_OPTIONS
23456590Sshin		if (fromp->su_family == AF_INET)
23596196Sdes	      {
23622455Simp		u_char optbuf[BUFSIZ/3];
237141918Sstefanf		socklen_t optsize = sizeof(optbuf);
238141918Sstefanf		int ipproto, i;
2391592Srgrimes		struct protoent *ip;
2401592Srgrimes
2411592Srgrimes		if ((ip = getprotobyname("ip")) != NULL)
2421592Srgrimes			ipproto = ip->p_proto;
2431592Srgrimes		else
2441592Srgrimes			ipproto = IPPROTO_IP;
2451592Srgrimes		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
2461592Srgrimes		    &optsize) == 0 && optsize != 0) {
24722455Simp			for (i = 0; i < optsize; ) {
24822455Simp				u_char c = optbuf[i];
24922455Simp				if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
25022455Simp					syslog(LOG_NOTICE,
25122455Simp						"Connection refused from %s with IP option %s",
25256590Sshin						inet_ntoa(fromp->su_sin.sin_addr),
25322455Simp						c == IPOPT_LSRR ? "LSRR" : "SSRR");
25422455Simp					exit(1);
25522455Simp				}
25622455Simp				if (c == IPOPT_EOL)
25722455Simp					break;
25822455Simp				i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
2591592Srgrimes			}
2601592Srgrimes		}
26156590Sshin	      }
2621592Srgrimes#endif
2631592Srgrimes		if (do_rlogin(fromp) == 0)
2641592Srgrimes			authenticated++;
2651592Srgrimes	}
2661592Srgrimes	if (confirmed == 0) {
2671592Srgrimes		write(f, "", 1);
2681592Srgrimes		confirmed = 1;		/* we sent the null! */
2691592Srgrimes	}
2701592Srgrimes	netf = f;
2711592Srgrimes
2721592Srgrimes	pid = forkpty(&master, line, NULL, &win);
2731592Srgrimes	if (pid < 0) {
2741592Srgrimes		if (errno == ENOENT)
2751592Srgrimes			fatal(f, "Out of ptys", 0);
2761592Srgrimes		else
2771592Srgrimes			fatal(f, "Forkpty", 1);
2781592Srgrimes	}
2791592Srgrimes	if (pid == 0) {
2801592Srgrimes		if (f > 2)	/* f should always be 0, but... */
2811592Srgrimes			(void) close(f);
2821592Srgrimes		setup_term(0);
28312575Snate		 if (*lusername=='-') {
2842076Sguido			syslog(LOG_ERR, "tried to pass user \"%s\" to login",
2852076Sguido			       lusername);
2862076Sguido			fatal(STDERR_FILENO, "invalid user", 0);
2872076Sguido		}
2881592Srgrimes		if (authenticated) {
2891592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
2901592Srgrimes			    "-h", hostname, "-f", lusername, (char *)NULL);
2911592Srgrimes		} else
2921592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
2931592Srgrimes			    "-h", hostname, lusername, (char *)NULL);
2941592Srgrimes		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
2951592Srgrimes		/*NOTREACHED*/
2961592Srgrimes	}
297141588Sru	ioctl(f, FIONBIO, &on);
2981592Srgrimes	ioctl(master, FIONBIO, &on);
2991592Srgrimes	ioctl(master, TIOCPKT, &on);
3001592Srgrimes	signal(SIGCHLD, cleanup);
3011592Srgrimes	protocol(f, master);
3021592Srgrimes	signal(SIGCHLD, SIG_IGN);
3031592Srgrimes	cleanup(0);
3041592Srgrimes}
3051592Srgrimes
3061592Srgrimeschar	magic[2] = { 0377, 0377 };
3071592Srgrimeschar	oobdata[] = {TIOCPKT_WINDOW};
3081592Srgrimes
3091592Srgrimes/*
3101592Srgrimes * Handle a "control" request (signaled by magic being present)
3111592Srgrimes * in the data stream.  For now, we are only willing to handle
3121592Srgrimes * window size changes.
3131592Srgrimes */
3141592Srgrimesint
31590377Simpcontrol(int pty, char *cp, int n)
3161592Srgrimes{
3171592Srgrimes	struct winsize w;
3181592Srgrimes
319114624Sobrien	if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
3201592Srgrimes		return (0);
3211592Srgrimes	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
3221592Srgrimes	bcopy(cp+4, (char *)&w, sizeof(w));
3231592Srgrimes	w.ws_row = ntohs(w.ws_row);
3241592Srgrimes	w.ws_col = ntohs(w.ws_col);
3251592Srgrimes	w.ws_xpixel = ntohs(w.ws_xpixel);
3261592Srgrimes	w.ws_ypixel = ntohs(w.ws_ypixel);
3271592Srgrimes	(void)ioctl(pty, TIOCSWINSZ, &w);
3281592Srgrimes	return (4+sizeof (w));
3291592Srgrimes}
3301592Srgrimes
3311592Srgrimes/*
3321592Srgrimes * rlogin "protocol" machine.
3331592Srgrimes */
3341592Srgrimesvoid
33590377Simpprotocol(int f, int p)
3361592Srgrimes{
33769705Sru	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
33846078Simp	int pcc = 0, fcc = 0;
3391592Srgrimes	int cc, nfd, n;
3401592Srgrimes	char cntl;
3411592Srgrimes
3421592Srgrimes	/*
3431592Srgrimes	 * Must ignore SIGTTOU, otherwise we'll stop
3441592Srgrimes	 * when we try and set slave pty's window shape
3451592Srgrimes	 * (our controlling tty is the master pty).
3461592Srgrimes	 */
3471592Srgrimes	(void) signal(SIGTTOU, SIG_IGN);
3481592Srgrimes	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
3491592Srgrimes	if (f > p)
3501592Srgrimes		nfd = f + 1;
3511592Srgrimes	else
3521592Srgrimes		nfd = p + 1;
3531592Srgrimes	if (nfd > FD_SETSIZE) {
3541592Srgrimes		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
3551592Srgrimes		fatal(f, "internal error (select mask too small)", 0);
3561592Srgrimes	}
3571592Srgrimes	for (;;) {
3581592Srgrimes		fd_set ibits, obits, ebits, *omask;
3591592Srgrimes
3601592Srgrimes		FD_ZERO(&ebits);
3611592Srgrimes		FD_ZERO(&ibits);
3621592Srgrimes		FD_ZERO(&obits);
3631592Srgrimes		omask = (fd_set *)NULL;
3641592Srgrimes		if (fcc) {
3651592Srgrimes			FD_SET(p, &obits);
3661592Srgrimes			omask = &obits;
3671592Srgrimes		} else
3681592Srgrimes			FD_SET(f, &ibits);
36946078Simp		if (pcc >= 0) {
3701592Srgrimes			if (pcc) {
3711592Srgrimes				FD_SET(f, &obits);
3721592Srgrimes				omask = &obits;
3731592Srgrimes			} else
3741592Srgrimes				FD_SET(p, &ibits);
37546078Simp		}
3761592Srgrimes		FD_SET(p, &ebits);
3771592Srgrimes		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
3781592Srgrimes			if (errno == EINTR)
3791592Srgrimes				continue;
3801592Srgrimes			fatal(f, "select", 1);
3811592Srgrimes		}
3821592Srgrimes		if (n == 0) {
3831592Srgrimes			/* shouldn't happen... */
3841592Srgrimes			sleep(5);
3851592Srgrimes			continue;
3861592Srgrimes		}
3871592Srgrimes#define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
3881592Srgrimes		if (FD_ISSET(p, &ebits)) {
3891592Srgrimes			cc = read(p, &cntl, 1);
3901592Srgrimes			if (cc == 1 && pkcontrol(cntl)) {
3911592Srgrimes				cntl |= oobdata[0];
3921592Srgrimes				send(f, &cntl, 1, MSG_OOB);
3931592Srgrimes				if (cntl & TIOCPKT_FLUSHWRITE) {
3941592Srgrimes					pcc = 0;
3951592Srgrimes					FD_CLR(p, &ibits);
3961592Srgrimes				}
3971592Srgrimes			}
3981592Srgrimes		}
3991592Srgrimes		if (FD_ISSET(f, &ibits)) {
400141588Sru			fcc = read(f, fibuf, sizeof(fibuf));
4011592Srgrimes			if (fcc < 0 && errno == EWOULDBLOCK)
4021592Srgrimes				fcc = 0;
4031592Srgrimes			else {
40490377Simp				char *cp;
4051592Srgrimes				int left, n;
4061592Srgrimes
4071592Srgrimes				if (fcc <= 0)
4081592Srgrimes					break;
4091592Srgrimes				fbp = fibuf;
4101592Srgrimes
4111592Srgrimes			top:
4121592Srgrimes				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
4131592Srgrimes					if (cp[0] == magic[0] &&
4141592Srgrimes					    cp[1] == magic[1]) {
4151592Srgrimes						left = fcc - (cp-fibuf);
4161592Srgrimes						n = control(p, cp, left);
4171592Srgrimes						if (n) {
4181592Srgrimes							left -= n;
4191592Srgrimes							if (left > 0)
4201592Srgrimes								bcopy(cp+n, cp, left);
4211592Srgrimes							fcc -= n;
4221592Srgrimes							goto top; /* n^2 */
4231592Srgrimes						}
4241592Srgrimes					}
4251592Srgrimes				FD_SET(p, &obits);		/* try write */
4261592Srgrimes			}
4271592Srgrimes		}
4281592Srgrimes
4291592Srgrimes		if (FD_ISSET(p, &obits) && fcc > 0) {
4301592Srgrimes			cc = write(p, fbp, fcc);
4311592Srgrimes			if (cc > 0) {
4321592Srgrimes				fcc -= cc;
4331592Srgrimes				fbp += cc;
4341592Srgrimes			}
4351592Srgrimes		}
4361592Srgrimes
4371592Srgrimes		if (FD_ISSET(p, &ibits)) {
4381592Srgrimes			pcc = read(p, pibuf, sizeof (pibuf));
4391592Srgrimes			pbp = pibuf;
4401592Srgrimes			if (pcc < 0 && errno == EWOULDBLOCK)
4411592Srgrimes				pcc = 0;
4421592Srgrimes			else if (pcc <= 0)
4431592Srgrimes				break;
4441592Srgrimes			else if (pibuf[0] == 0) {
4451592Srgrimes				pbp++, pcc--;
446141588Sru				FD_SET(f, &obits);	/* try write */
4471592Srgrimes			} else {
4481592Srgrimes				if (pkcontrol(pibuf[0])) {
4491592Srgrimes					pibuf[0] |= oobdata[0];
4501592Srgrimes					send(f, &pibuf[0], 1, MSG_OOB);
4511592Srgrimes				}
4521592Srgrimes				pcc = 0;
4531592Srgrimes			}
4541592Srgrimes		}
4551592Srgrimes		if ((FD_ISSET(f, &obits)) && pcc > 0) {
456141588Sru			cc = write(f, pbp, pcc);
4571592Srgrimes			if (cc < 0 && errno == EWOULDBLOCK) {
4581592Srgrimes				/*
4591592Srgrimes				 * This happens when we try write after read
4601592Srgrimes				 * from p, but some old kernels balk at large
4611592Srgrimes				 * writes even when select returns true.
4621592Srgrimes				 */
4631592Srgrimes				if (!FD_ISSET(p, &ibits))
4641592Srgrimes					sleep(5);
4651592Srgrimes				continue;
4661592Srgrimes			}
4671592Srgrimes			if (cc > 0) {
4681592Srgrimes				pcc -= cc;
4691592Srgrimes				pbp += cc;
4701592Srgrimes			}
4711592Srgrimes		}
4721592Srgrimes	}
4731592Srgrimes}
4741592Srgrimes
4751592Srgrimesvoid
476216584Scharniercleanup(int signo __unused)
4771592Srgrimes{
4781592Srgrimes
479146075Sjmallett	shutdown(netf, SHUT_RDWR);
4801592Srgrimes	exit(1);
4811592Srgrimes}
4821592Srgrimes
4831592Srgrimesvoid
48490377Simpfatal(int f, char *msg, int syserr)
4851592Srgrimes{
4861592Srgrimes	int len;
4871592Srgrimes	char buf[BUFSIZ], *bp = buf;
4881592Srgrimes
4891592Srgrimes	/*
4901592Srgrimes	 * Prepend binary one to message if we haven't sent
4911592Srgrimes	 * the magic null as confirmation.
4921592Srgrimes	 */
4931592Srgrimes	if (!confirmed)
4941592Srgrimes		*bp++ = '\01';		/* error indicator */
4951592Srgrimes	if (syserr)
49664238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
4971592Srgrimes		    msg, strerror(errno));
4981592Srgrimes	else
49964238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
50081991Sbrian	if (len < 0)
50181972Sbrian		len = 0;
5021592Srgrimes	(void) write(f, buf, bp + len - buf);
5031592Srgrimes	exit(1);
5041592Srgrimes}
5051592Srgrimes
5061592Srgrimesint
50790377Simpdo_rlogin(union sockunion *dest)
5081592Srgrimes{
50966755Sru
5101592Srgrimes	getstr(rusername, sizeof(rusername), "remuser too long");
5111592Srgrimes	getstr(lusername, sizeof(lusername), "locuser too long");
5121592Srgrimes	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
5131592Srgrimes
5141592Srgrimes	pwd = getpwnam(lusername);
5151592Srgrimes	if (pwd == NULL)
5161592Srgrimes		return (-1);
5171592Srgrimes	/* XXX why don't we syslog() failure? */
51856590Sshin
51956939Sshin	return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
52056939Sshin			    lusername));
5211592Srgrimes}
5221592Srgrimes
5231592Srgrimesvoid
52490377Simpgetstr(char *buf, int cnt, char *errmsg)
5251592Srgrimes{
5261592Srgrimes	char c;
5271592Srgrimes
5281592Srgrimes	do {
52980381Ssheldonh		if (read(STDIN_FILENO, &c, 1) != 1)
5301592Srgrimes			exit(1);
5311592Srgrimes		if (--cnt < 0)
5321592Srgrimes			fatal(STDOUT_FILENO, errmsg, 0);
5331592Srgrimes		*buf++ = c;
5341592Srgrimes	} while (c != 0);
5351592Srgrimes}
5361592Srgrimes
5371592Srgrimesextern	char **environ;
5381592Srgrimes
5391592Srgrimesvoid
54090377Simpsetup_term(int fd)
5411592Srgrimes{
542229403Sed	char *cp;
5431592Srgrimes	char *speed;
544214680Sed	struct termios tt, def;
5451592Srgrimes
546229403Sed	cp = strchr(term + ENVSIZE, '/');
5471592Srgrimes#ifndef notyet
5481592Srgrimes	tcgetattr(fd, &tt);
5491592Srgrimes	if (cp) {
5501592Srgrimes		*cp++ = '\0';
5511592Srgrimes		speed = cp;
552229403Sed		cp = strchr(speed, '/');
5531592Srgrimes		if (cp)
5541592Srgrimes			*cp++ = '\0';
5551592Srgrimes		cfsetspeed(&tt, atoi(speed));
5561592Srgrimes	}
5571592Srgrimes
558214680Sed	cfmakesane(&def);
559214680Sed	tt.c_iflag = def.c_iflag;
560214680Sed	tt.c_oflag = def.c_oflag;
561214680Sed	tt.c_lflag = def.c_lflag;
5621592Srgrimes	tcsetattr(fd, TCSAFLUSH, &tt);
5631592Srgrimes#else
5641592Srgrimes	if (cp) {
5651592Srgrimes		*cp++ = '\0';
5661592Srgrimes		speed = cp;
567229403Sed		cp = strchr(speed, '/');
5681592Srgrimes		if (cp)
5691592Srgrimes			*cp++ = '\0';
5701592Srgrimes		tcgetattr(fd, &tt);
5711592Srgrimes		cfsetspeed(&tt, atoi(speed));
5721592Srgrimes		tcsetattr(fd, TCSAFLUSH, &tt);
5731592Srgrimes	}
5741592Srgrimes#endif
5751592Srgrimes
5761592Srgrimes	env[0] = term;
5771592Srgrimes	env[1] = 0;
5781592Srgrimes	environ = env;
5791592Srgrimes}
5801592Srgrimes
5811592Srgrimesvoid
58290377Simpusage(void)
5831592Srgrimes{
58451433Smarkm	syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");
5851592Srgrimes}
586