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.
20262136Sbrueffer * 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>
79296109Spfg#include <poll.h>
801592Srgrimes#include <pwd.h>
811592Srgrimes#include <syslog.h>
821592Srgrimes#include <stdio.h>
831592Srgrimes#include <stdlib.h>
841592Srgrimes#include <string.h>
8531405Scharnier#include <unistd.h>
86301417Slidl#ifdef USE_BLACKLIST
87301417Slidl#include <blacklist.h>
88301417Slidl#endif
891592Srgrimes
901592Srgrimes#ifndef TIOCPKT_WINDOW
911592Srgrimes#define TIOCPKT_WINDOW 0x80
921592Srgrimes#endif
931592Srgrimes
94141588Sru#define		ARGSTR			"Daln"
951592Srgrimes
961592Srgrimeschar	*env[2];
971592Srgrimes#define	NMAX 30
981592Srgrimeschar	lusername[NMAX+1], rusername[NMAX+1];
991592Srgrimesstatic	char term[64] = "TERM=";
1001592Srgrimes#define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
1011592Srgrimesint	keepalive = 1;
1021592Srgrimesint	check_all = 0;
10311486Sdgint	no_delay;
1041592Srgrimes
1051592Srgrimesstruct	passwd *pwd;
1061592Srgrimes
10756590Sshinunion sockunion {
10856590Sshin	struct sockinet {
10956590Sshin		u_char si_len;
11056590Sshin		u_char si_family;
11156590Sshin		u_short si_port;
11256590Sshin	} su_si;
11356590Sshin	struct sockaddr_in  su_sin;
11456590Sshin	struct sockaddr_in6 su_sin6;
11556590Sshin};
11656590Sshin#define su_len		su_si.si_len
11756590Sshin#define su_family	su_si.si_family
11856590Sshin#define su_port		su_si.si_port
11956590Sshin
12090377Simpvoid	doit(int, union sockunion *);
12190377Simpint	control(int, char *, int);
12290377Simpvoid	protocol(int, int);
12390377Simpvoid	cleanup(int);
12490377Simpvoid	fatal(int, char *, int);
12590377Simpint	do_rlogin(union sockunion *);
12690377Simpvoid	getstr(char *, int, char *);
12790377Simpvoid	setup_term(int);
12890377Simpint	do_krb_login(struct sockaddr_in *);
12990377Simpvoid	usage(void);
1301592Srgrimes
13151433Smarkm
1321592Srgrimesint
13390377Simpmain(int argc, char *argv[])
1341592Srgrimes{
1351592Srgrimes	extern int __check_rhosts_file;
13656590Sshin	union sockunion from;
137141918Sstefanf	socklen_t fromlen;
138141918Sstefanf	int ch, on;
1391592Srgrimes
1401592Srgrimes	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
1411592Srgrimes
1421592Srgrimes	opterr = 0;
14324349Simp	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
1441592Srgrimes		switch (ch) {
14511486Sdg		case 'D':
14611486Sdg			no_delay = 1;
14711486Sdg			break;
1481592Srgrimes		case 'a':
1491592Srgrimes			check_all = 1;
1501592Srgrimes			break;
1511592Srgrimes		case 'l':
1521592Srgrimes			__check_rhosts_file = 0;
1531592Srgrimes			break;
1541592Srgrimes		case 'n':
1551592Srgrimes			keepalive = 0;
1561592Srgrimes			break;
1571592Srgrimes		case '?':
1581592Srgrimes		default:
1591592Srgrimes			usage();
1601592Srgrimes			break;
1611592Srgrimes		}
1621592Srgrimes	argc -= optind;
1631592Srgrimes	argv += optind;
1641592Srgrimes
1651592Srgrimes	fromlen = sizeof (from);
1661592Srgrimes	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
1671592Srgrimes		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
1681592Srgrimes		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
1691592Srgrimes	}
1701592Srgrimes	on = 1;
1711592Srgrimes	if (keepalive &&
1721592Srgrimes	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
1731592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
17411486Sdg	if (no_delay &&
17511486Sdg	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
17611486Sdg		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
17796196Sdes	if (from.su_family == AF_INET)
17856590Sshin      {
1791592Srgrimes	on = IPTOS_LOWDELAY;
1801592Srgrimes	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1811592Srgrimes		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
18256590Sshin      }
18311486Sdg
1841592Srgrimes	doit(0, &from);
18529916Smarkm	return 0;
1861592Srgrimes}
1871592Srgrimes
1881592Srgrimesint	child;
1891592Srgrimesint	netf;
1901592Srgrimeschar	line[MAXPATHLEN];
1911592Srgrimesint	confirmed;
1921592Srgrimes
1931592Srgrimesstruct winsize win = { 0, 0, 0, 0 };
1941592Srgrimes
1951592Srgrimes
1961592Srgrimesvoid
19790377Simpdoit(int f, union sockunion *fromp)
1981592Srgrimes{
1991592Srgrimes	int master, pid, on = 1;
2001592Srgrimes	int authenticated = 0;
20156590Sshin	char hostname[2 * MAXHOSTNAMELEN + 1];
20256590Sshin	char nameinfo[2 * INET6_ADDRSTRLEN + 1];
2031592Srgrimes	char c;
2041592Srgrimes
2051592Srgrimes	alarm(60);
2061592Srgrimes	read(f, &c, 1);
2071592Srgrimes
2081592Srgrimes	if (c != 0)
2091592Srgrimes		exit(1);
2101592Srgrimes
2111592Srgrimes	alarm(0);
21256590Sshin
21356590Sshin	realhostname_sa(hostname, sizeof(hostname) - 1,
21456590Sshin			    (struct sockaddr *)fromp, fromp->su_len);
21556590Sshin	/* error check ? */
21656590Sshin	fromp->su_port = ntohs((u_short)fromp->su_port);
21724191Simp	hostname[sizeof(hostname) - 1] = '\0';
2181592Srgrimes
2191592Srgrimes	{
22063959Sume		if ((fromp->su_family != AF_INET
22156590Sshin#ifdef INET6
22263959Sume		  && fromp->su_family != AF_INET6
22356590Sshin#endif
22456590Sshin		     ) ||
22556590Sshin		    fromp->su_port >= IPPORT_RESERVED ||
22656590Sshin		    fromp->su_port < IPPORT_RESERVED/2) {
22756590Sshin			getnameinfo((struct sockaddr *)fromp,
22856590Sshin				    fromp->su_len,
22956590Sshin				    nameinfo, sizeof(nameinfo), NULL, 0,
230146187Sume				    NI_NUMERICHOST);
23156590Sshin			/* error check ? */
2321592Srgrimes			syslog(LOG_NOTICE, "Connection from %s on illegal port",
23356590Sshin			       nameinfo);
234301417Slidl#ifdef USE_BLACKLIST
235301417Slidl			blacklist(1, STDIN_FILENO, "illegal port");
236301417Slidl#endif
2371592Srgrimes			fatal(f, "Permission denied", 0);
2381592Srgrimes		}
2391592Srgrimes#ifdef IP_OPTIONS
24056590Sshin		if (fromp->su_family == AF_INET)
24196196Sdes	      {
24222455Simp		u_char optbuf[BUFSIZ/3];
243141918Sstefanf		socklen_t optsize = sizeof(optbuf);
244141918Sstefanf		int ipproto, i;
2451592Srgrimes		struct protoent *ip;
2461592Srgrimes
2471592Srgrimes		if ((ip = getprotobyname("ip")) != NULL)
2481592Srgrimes			ipproto = ip->p_proto;
2491592Srgrimes		else
2501592Srgrimes			ipproto = IPPROTO_IP;
2511592Srgrimes		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
2521592Srgrimes		    &optsize) == 0 && optsize != 0) {
25322455Simp			for (i = 0; i < optsize; ) {
25422455Simp				u_char c = optbuf[i];
25522455Simp				if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
25622455Simp					syslog(LOG_NOTICE,
25722455Simp						"Connection refused from %s with IP option %s",
25856590Sshin						inet_ntoa(fromp->su_sin.sin_addr),
25922455Simp						c == IPOPT_LSRR ? "LSRR" : "SSRR");
260301417Slidl#ifdef USE_BLACKLIST
261301417Slidl					blacklist(1, STDIN_FILENO, "source routing present");
262301417Slidl#endif
26322455Simp					exit(1);
26422455Simp				}
26522455Simp				if (c == IPOPT_EOL)
26622455Simp					break;
26722455Simp				i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
2681592Srgrimes			}
2691592Srgrimes		}
27056590Sshin	      }
2711592Srgrimes#endif
2721592Srgrimes		if (do_rlogin(fromp) == 0)
2731592Srgrimes			authenticated++;
2741592Srgrimes	}
2751592Srgrimes	if (confirmed == 0) {
2761592Srgrimes		write(f, "", 1);
2771592Srgrimes		confirmed = 1;		/* we sent the null! */
2781592Srgrimes	}
2791592Srgrimes	netf = f;
2801592Srgrimes
2811592Srgrimes	pid = forkpty(&master, line, NULL, &win);
2821592Srgrimes	if (pid < 0) {
2831592Srgrimes		if (errno == ENOENT)
2841592Srgrimes			fatal(f, "Out of ptys", 0);
2851592Srgrimes		else
2861592Srgrimes			fatal(f, "Forkpty", 1);
2871592Srgrimes	}
2881592Srgrimes	if (pid == 0) {
2891592Srgrimes		if (f > 2)	/* f should always be 0, but... */
2901592Srgrimes			(void) close(f);
2911592Srgrimes		setup_term(0);
292301417Slidl		if (*lusername=='-') {
2932076Sguido			syslog(LOG_ERR, "tried to pass user \"%s\" to login",
2942076Sguido			       lusername);
295301417Slidl#ifdef USE_BLACKLIST
296301417Slidl			blacklist(1, STDIN_FILENO, "invalid user");
297301417Slidl#endif
2982076Sguido			fatal(STDERR_FILENO, "invalid user", 0);
2992076Sguido		}
300301417Slidl#ifdef USE_BLACKLIST
301301417Slidl		blacklist(0, STDIN_FILENO, "success");
302301417Slidl#endif
3031592Srgrimes		if (authenticated) {
3041592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3051592Srgrimes			    "-h", hostname, "-f", lusername, (char *)NULL);
3061592Srgrimes		} else
3071592Srgrimes			execl(_PATH_LOGIN, "login", "-p",
3081592Srgrimes			    "-h", hostname, lusername, (char *)NULL);
3091592Srgrimes		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
3101592Srgrimes		/*NOTREACHED*/
3111592Srgrimes	}
312141588Sru	ioctl(f, FIONBIO, &on);
3131592Srgrimes	ioctl(master, FIONBIO, &on);
3141592Srgrimes	ioctl(master, TIOCPKT, &on);
3151592Srgrimes	signal(SIGCHLD, cleanup);
3161592Srgrimes	protocol(f, master);
3171592Srgrimes	signal(SIGCHLD, SIG_IGN);
3181592Srgrimes	cleanup(0);
3191592Srgrimes}
3201592Srgrimes
3211592Srgrimeschar	magic[2] = { 0377, 0377 };
3221592Srgrimeschar	oobdata[] = {TIOCPKT_WINDOW};
3231592Srgrimes
3241592Srgrimes/*
3251592Srgrimes * Handle a "control" request (signaled by magic being present)
3261592Srgrimes * in the data stream.  For now, we are only willing to handle
3271592Srgrimes * window size changes.
3281592Srgrimes */
3291592Srgrimesint
33090377Simpcontrol(int pty, char *cp, int n)
3311592Srgrimes{
3321592Srgrimes	struct winsize w;
3331592Srgrimes
334114624Sobrien	if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
3351592Srgrimes		return (0);
3361592Srgrimes	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
3371592Srgrimes	bcopy(cp+4, (char *)&w, sizeof(w));
3381592Srgrimes	w.ws_row = ntohs(w.ws_row);
3391592Srgrimes	w.ws_col = ntohs(w.ws_col);
3401592Srgrimes	w.ws_xpixel = ntohs(w.ws_xpixel);
3411592Srgrimes	w.ws_ypixel = ntohs(w.ws_ypixel);
3421592Srgrimes	(void)ioctl(pty, TIOCSWINSZ, &w);
3431592Srgrimes	return (4+sizeof (w));
3441592Srgrimes}
3451592Srgrimes
3461592Srgrimes/*
3471592Srgrimes * rlogin "protocol" machine.
3481592Srgrimes */
3491592Srgrimesvoid
35090377Simpprotocol(int f, int p)
3511592Srgrimes{
35269705Sru	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
35346078Simp	int pcc = 0, fcc = 0;
3541592Srgrimes	int cc, nfd, n;
3551592Srgrimes	char cntl;
3561592Srgrimes
3571592Srgrimes	/*
3581592Srgrimes	 * Must ignore SIGTTOU, otherwise we'll stop
3591592Srgrimes	 * when we try and set slave pty's window shape
3601592Srgrimes	 * (our controlling tty is the master pty).
3611592Srgrimes	 */
3621592Srgrimes	(void) signal(SIGTTOU, SIG_IGN);
3631592Srgrimes	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
3641592Srgrimes	if (f > p)
3651592Srgrimes		nfd = f + 1;
3661592Srgrimes	else
3671592Srgrimes		nfd = p + 1;
3681592Srgrimes	for (;;) {
369296109Spfg		struct pollfd set[2];
3701592Srgrimes
371296109Spfg		set[0].fd = p;
372296109Spfg		set[0].events = POLLPRI;
373296109Spfg		set[1].fd = f;
374296109Spfg		set[1].events = 0;
375296109Spfg		if (fcc)
376296109Spfg			set[0].events |= POLLOUT;
377296109Spfg		else
378296109Spfg			set[1].events |= POLLIN;
37946078Simp		if (pcc >= 0) {
380296109Spfg			if (pcc)
381296109Spfg				set[1].events |= POLLOUT;
382296109Spfg			else
383296109Spfg				set[0].events |= POLLIN;
38446078Simp		}
385296109Spfg		if ((n = poll(set, 2, INFTIM)) < 0) {
3861592Srgrimes			if (errno == EINTR)
3871592Srgrimes				continue;
388296109Spfg			fatal(f, "poll", 1);
3891592Srgrimes		}
3901592Srgrimes		if (n == 0) {
3911592Srgrimes			/* shouldn't happen... */
3921592Srgrimes			sleep(5);
3931592Srgrimes			continue;
3941592Srgrimes		}
3951592Srgrimes#define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
396296109Spfg		if (set[0].revents & POLLPRI) {
3971592Srgrimes			cc = read(p, &cntl, 1);
3981592Srgrimes			if (cc == 1 && pkcontrol(cntl)) {
3991592Srgrimes				cntl |= oobdata[0];
4001592Srgrimes				send(f, &cntl, 1, MSG_OOB);
401296109Spfg				if (cntl & TIOCPKT_FLUSHWRITE)
4021592Srgrimes					pcc = 0;
4031592Srgrimes			}
4041592Srgrimes		}
405296109Spfg		if (set[1].revents & POLLIN) {
406141588Sru			fcc = read(f, fibuf, sizeof(fibuf));
4071592Srgrimes			if (fcc < 0 && errno == EWOULDBLOCK)
4081592Srgrimes				fcc = 0;
4091592Srgrimes			else {
41090377Simp				char *cp;
4111592Srgrimes				int left, n;
4121592Srgrimes
4131592Srgrimes				if (fcc <= 0)
4141592Srgrimes					break;
4151592Srgrimes				fbp = fibuf;
4161592Srgrimes
4171592Srgrimes			top:
4181592Srgrimes				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
4191592Srgrimes					if (cp[0] == magic[0] &&
4201592Srgrimes					    cp[1] == magic[1]) {
4211592Srgrimes						left = fcc - (cp-fibuf);
4221592Srgrimes						n = control(p, cp, left);
4231592Srgrimes						if (n) {
4241592Srgrimes							left -= n;
4251592Srgrimes							if (left > 0)
4261592Srgrimes								bcopy(cp+n, cp, left);
4271592Srgrimes							fcc -= n;
4281592Srgrimes							goto top; /* n^2 */
4291592Srgrimes						}
4301592Srgrimes					}
4311592Srgrimes			}
4321592Srgrimes		}
4331592Srgrimes
434296109Spfg		if (set[0].revents & POLLOUT && fcc > 0) {
4351592Srgrimes			cc = write(p, fbp, fcc);
4361592Srgrimes			if (cc > 0) {
4371592Srgrimes				fcc -= cc;
4381592Srgrimes				fbp += cc;
4391592Srgrimes			}
4401592Srgrimes		}
4411592Srgrimes
442296109Spfg		if (set[0].revents & POLLIN) {
4431592Srgrimes			pcc = read(p, pibuf, sizeof (pibuf));
4441592Srgrimes			pbp = pibuf;
4451592Srgrimes			if (pcc < 0 && errno == EWOULDBLOCK)
4461592Srgrimes				pcc = 0;
4471592Srgrimes			else if (pcc <= 0)
4481592Srgrimes				break;
4491592Srgrimes			else if (pibuf[0] == 0) {
4501592Srgrimes				pbp++, pcc--;
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		}
459296109Spfg		if (set[1].revents & POLLOUT && pcc > 0) {
460141588Sru			cc = write(f, pbp, pcc);
4611592Srgrimes			if (cc > 0) {
4621592Srgrimes				pcc -= cc;
4631592Srgrimes				pbp += cc;
4641592Srgrimes			}
4651592Srgrimes		}
4661592Srgrimes	}
4671592Srgrimes}
4681592Srgrimes
4691592Srgrimesvoid
470216584Scharniercleanup(int signo __unused)
4711592Srgrimes{
4721592Srgrimes
473146075Sjmallett	shutdown(netf, SHUT_RDWR);
4741592Srgrimes	exit(1);
4751592Srgrimes}
4761592Srgrimes
4771592Srgrimesvoid
47890377Simpfatal(int f, char *msg, int syserr)
4791592Srgrimes{
4801592Srgrimes	int len;
4811592Srgrimes	char buf[BUFSIZ], *bp = buf;
4821592Srgrimes
4831592Srgrimes	/*
4841592Srgrimes	 * Prepend binary one to message if we haven't sent
4851592Srgrimes	 * the magic null as confirmation.
4861592Srgrimes	 */
4871592Srgrimes	if (!confirmed)
4881592Srgrimes		*bp++ = '\01';		/* error indicator */
4891592Srgrimes	if (syserr)
49064238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
4911592Srgrimes		    msg, strerror(errno));
4921592Srgrimes	else
49364238Skris		len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
49481991Sbrian	if (len < 0)
49581972Sbrian		len = 0;
4961592Srgrimes	(void) write(f, buf, bp + len - buf);
4971592Srgrimes	exit(1);
4981592Srgrimes}
4991592Srgrimes
5001592Srgrimesint
50190377Simpdo_rlogin(union sockunion *dest)
5021592Srgrimes{
50366755Sru
5041592Srgrimes	getstr(rusername, sizeof(rusername), "remuser too long");
5051592Srgrimes	getstr(lusername, sizeof(lusername), "locuser too long");
5061592Srgrimes	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
5071592Srgrimes
5081592Srgrimes	pwd = getpwnam(lusername);
5091592Srgrimes	if (pwd == NULL)
5101592Srgrimes		return (-1);
5111592Srgrimes	/* XXX why don't we syslog() failure? */
51256590Sshin
51356939Sshin	return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
51456939Sshin			    lusername));
5151592Srgrimes}
5161592Srgrimes
5171592Srgrimesvoid
51890377Simpgetstr(char *buf, int cnt, char *errmsg)
5191592Srgrimes{
5201592Srgrimes	char c;
5211592Srgrimes
5221592Srgrimes	do {
52380381Ssheldonh		if (read(STDIN_FILENO, &c, 1) != 1)
5241592Srgrimes			exit(1);
525301417Slidl		if (--cnt < 0) {
526301417Slidl#ifdef USE_BLACKLIST
527301417Slidl			blacklist(1, STDIN_FILENO, "buffer overflow");
528301417Slidl#endif
5291592Srgrimes			fatal(STDOUT_FILENO, errmsg, 0);
530301417Slidl		}
5311592Srgrimes		*buf++ = c;
5321592Srgrimes	} while (c != 0);
5331592Srgrimes}
5341592Srgrimes
5351592Srgrimesextern	char **environ;
5361592Srgrimes
5371592Srgrimesvoid
53890377Simpsetup_term(int fd)
5391592Srgrimes{
540229403Sed	char *cp;
5411592Srgrimes	char *speed;
542214680Sed	struct termios tt, def;
5431592Srgrimes
544229403Sed	cp = strchr(term + ENVSIZE, '/');
5451592Srgrimes#ifndef notyet
5461592Srgrimes	tcgetattr(fd, &tt);
5471592Srgrimes	if (cp) {
5481592Srgrimes		*cp++ = '\0';
5491592Srgrimes		speed = cp;
550229403Sed		cp = strchr(speed, '/');
5511592Srgrimes		if (cp)
5521592Srgrimes			*cp++ = '\0';
5531592Srgrimes		cfsetspeed(&tt, atoi(speed));
5541592Srgrimes	}
5551592Srgrimes
556214680Sed	cfmakesane(&def);
557214680Sed	tt.c_iflag = def.c_iflag;
558214680Sed	tt.c_oflag = def.c_oflag;
559214680Sed	tt.c_lflag = def.c_lflag;
5601592Srgrimes	tcsetattr(fd, TCSAFLUSH, &tt);
5611592Srgrimes#else
5621592Srgrimes	if (cp) {
5631592Srgrimes		*cp++ = '\0';
5641592Srgrimes		speed = cp;
565229403Sed		cp = strchr(speed, '/');
5661592Srgrimes		if (cp)
5671592Srgrimes			*cp++ = '\0';
5681592Srgrimes		tcgetattr(fd, &tt);
5691592Srgrimes		cfsetspeed(&tt, atoi(speed));
5701592Srgrimes		tcsetattr(fd, TCSAFLUSH, &tt);
5711592Srgrimes	}
5721592Srgrimes#endif
5731592Srgrimes
5741592Srgrimes	env[0] = term;
5751592Srgrimes	env[1] = 0;
5761592Srgrimes	environ = env;
5771592Srgrimes}
5781592Srgrimes
5791592Srgrimesvoid
58090377Simpusage(void)
5811592Srgrimes{
58251433Smarkm	syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");
5831592Srgrimes}
584