11592Srgrimes/*-
21592Srgrimes * Copyright (c) 1988, 1989, 1992, 1993, 1994
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
496195Sdes * Copyright (c) 2002 Networks Associates Technology, Inc.
596195Sdes * All rights reserved.
61592Srgrimes *
796195Sdes * Portions of this software were developed for the FreeBSD Project by
896195Sdes * ThinkSec AS and NAI Labs, the Security Research Division of Network
996195Sdes * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1096195Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
1196195Sdes *
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
371592Srgrimes#ifndef lint
3829917Smarkmstatic const char copyright[] =
391592Srgrimes"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
401592Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411592Srgrimes#endif /* not lint */
421592Srgrimes
431592Srgrimes#ifndef lint
4431490Scharnier#if 0
4529917Smarkmstatic const char sccsid[] = "@(#)rshd.c	8.2 (Berkeley) 4/6/94";
4631490Scharnier#endif
471592Srgrimes#endif /* not lint */
481592Srgrimes
4998885Smarkm#include <sys/cdefs.h>
5098885Smarkm__FBSDID("$FreeBSD: stable/10/libexec/rshd/rshd.c 321069 2017-07-17 06:37:46Z delphij $");
5198885Smarkm
521592Srgrimes/*
531592Srgrimes * remote shell server:
541592Srgrimes *	[port]\0
5596195Sdes *	ruser\0
5696195Sdes *	luser\0
571592Srgrimes *	command\0
581592Srgrimes *	data
591592Srgrimes */
601592Srgrimes#include <sys/param.h>
611592Srgrimes#include <sys/ioctl.h>
621592Srgrimes#include <sys/time.h>
631592Srgrimes#include <sys/socket.h>
641592Srgrimes
6522454Simp#include <netinet/in_systm.h>
661592Srgrimes#include <netinet/in.h>
6722454Simp#include <netinet/ip.h>
6841445Sdg#include <netinet/tcp.h>
691592Srgrimes#include <arpa/inet.h>
701592Srgrimes#include <netdb.h>
711592Srgrimes
7276094Smarkm#include <err.h>
731592Srgrimes#include <errno.h>
741592Srgrimes#include <fcntl.h>
7545393Sbrian#include <libutil.h>
761592Srgrimes#include <paths.h>
771592Srgrimes#include <pwd.h>
781592Srgrimes#include <signal.h>
7976134Smarkm#include <stdarg.h>
801592Srgrimes#include <stdio.h>
811592Srgrimes#include <stdlib.h>
821592Srgrimes#include <string.h>
831592Srgrimes#include <syslog.h>
841592Srgrimes#include <unistd.h>
8525099Sdavidn#include <login_cap.h>
861592Srgrimes
8774874Smarkm#include <security/pam_appl.h>
8896195Sdes#include <security/openpam.h>
8974874Smarkm#include <sys/wait.h>
9074874Smarkm
9196195Sdesstatic struct pam_conv pamc = { openpam_nullconv, NULL };
9274874Smarkmstatic pam_handle_t *pamh;
9396195Sdesstatic int pam_err;
9474874Smarkm
9574874Smarkm#define PAM_END { \
9696195Sdes	if ((pam_err = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \
9796195Sdes		syslog(LOG_ERR|LOG_AUTH, "pam_setcred(): %s", pam_strerror(pamh, pam_err)); \
9896195Sdes	if ((pam_err = pam_close_session(pamh,0)) != PAM_SUCCESS) \
9996195Sdes		syslog(LOG_ERR|LOG_AUTH, "pam_close_session(): %s", pam_strerror(pamh, pam_err)); \
10096195Sdes	if ((pam_err = pam_end(pamh, pam_err)) != PAM_SUCCESS) \
10196195Sdes		syslog(LOG_ERR|LOG_AUTH, "pam_end(): %s", pam_strerror(pamh, pam_err)); \
10274874Smarkm}
10374874Smarkm
1041592Srgrimesint	keepalive = 1;
1051592Srgrimesint	log_success;		/* If TRUE, log all successful accesses */
1061592Srgrimesint	sent_null;
10741445Sdgint	no_delay;
1081592Srgrimes
10996195Sdesvoid	 doit(struct sockaddr *);
11090334Simpstatic void	 rshd_errx(int, const char *, ...) __printf0like(2, 3);
11190335Simpvoid	 getstr(char *, int, const char *);
11290334Simpint	 local_domain(char *);
11390334Simpchar	*topdomain(char *);
11490334Simpvoid	 usage(void);
1151592Srgrimes
11698885Smarkmchar	 slash[] = "/";
11798885Smarkmchar	 bshell[] = _PATH_BSHELL;
11898885Smarkm
119141589Sru#define	OPTIONS	"aDLln"
12051433Smarkm
1211592Srgrimesint
12290334Simpmain(int argc, char *argv[])
1231592Srgrimes{
1241592Srgrimes	extern int __check_rhosts_file;
1251592Srgrimes	struct linger linger;
126141918Sstefanf	socklen_t fromlen;
127141918Sstefanf	int ch, on = 1;
12856590Sshin	struct sockaddr_storage from;
1291592Srgrimes
1301592Srgrimes	openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
1311592Srgrimes
1321592Srgrimes	opterr = 0;
13324349Simp	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
1341592Srgrimes		switch (ch) {
1351592Srgrimes		case 'a':
13672093Sasmodai			/* ignored for compatibility */
1371592Srgrimes			break;
1381592Srgrimes		case 'l':
1391592Srgrimes			__check_rhosts_file = 0;
1401592Srgrimes			break;
1411592Srgrimes		case 'n':
1421592Srgrimes			keepalive = 0;
1431592Srgrimes			break;
14441445Sdg		case 'D':
14541445Sdg			no_delay = 1;
14641445Sdg			break;
1471592Srgrimes		case 'L':
1481592Srgrimes			log_success = 1;
1491592Srgrimes			break;
1501592Srgrimes		case '?':
1511592Srgrimes		default:
1521592Srgrimes			usage();
1531592Srgrimes			break;
1541592Srgrimes		}
1551592Srgrimes
1561592Srgrimes	argc -= optind;
1571592Srgrimes	argv += optind;
1581592Srgrimes
1591592Srgrimes	fromlen = sizeof (from);
1601592Srgrimes	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
1611592Srgrimes		syslog(LOG_ERR, "getpeername: %m");
16235728Srnordier		exit(1);
1631592Srgrimes	}
1641592Srgrimes	if (keepalive &&
1651592Srgrimes	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
1661592Srgrimes	    sizeof(on)) < 0)
1671592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
1681592Srgrimes	linger.l_onoff = 1;
1691592Srgrimes	linger.l_linger = 60;			/* XXX */
1701592Srgrimes	if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
1711592Srgrimes	    sizeof (linger)) < 0)
1721592Srgrimes		syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
17341445Sdg	if (no_delay &&
17441445Sdg	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
17541445Sdg		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
17696195Sdes	doit((struct sockaddr *)&from);
1771592Srgrimes	/* NOTREACHED */
17829917Smarkm	return(0);
1791592Srgrimes}
1801592Srgrimes
18196195Sdesextern char **environ;
18274874Smarkm
1831592Srgrimesvoid
18496195Sdesdoit(struct sockaddr *fromp)
1851592Srgrimes{
1861592Srgrimes	extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c. */
1871592Srgrimes	struct passwd *pwd;
1881592Srgrimes	u_short port;
1891592Srgrimes	fd_set ready, readfrom;
19096195Sdes	int cc, fd, nfd, pv[2], pid, s;
1911592Srgrimes	int one = 1;
19296195Sdes	const char *cp, *errorstr;
19396195Sdes	char sig, buf[BUFSIZ];
194143907Sdas	char *cmdbuf, luser[16], ruser[16];
19596195Sdes	char rhost[2 * MAXHOSTNAMELEN + 1];
19656590Sshin	char numericname[INET6_ADDRSTRLEN];
19798885Smarkm	int af, srcport;
198143907Sdas	int maxcmdlen;
19925099Sdavidn	login_cap_t *lc;
2001592Srgrimes
201143907Sdas	maxcmdlen = (int)sysconf(_SC_ARG_MAX);
202143907Sdas	if (maxcmdlen <= 0 || (cmdbuf = malloc(maxcmdlen)) == NULL)
203143907Sdas		exit(1);
204143907Sdas
2051592Srgrimes	(void) signal(SIGINT, SIG_DFL);
2061592Srgrimes	(void) signal(SIGQUIT, SIG_DFL);
2071592Srgrimes	(void) signal(SIGTERM, SIG_DFL);
20896195Sdes	af = fromp->sa_family;
20996195Sdes	srcport = ntohs(*((in_port_t *)&fromp->sa_data));
21096195Sdes	if (af == AF_INET) {
21196195Sdes		inet_ntop(af, &((struct sockaddr_in *)fromp)->sin_addr,
21296195Sdes		    numericname, sizeof numericname);
21396195Sdes	} else if (af == AF_INET6) {
21496195Sdes		inet_ntop(af, &((struct sockaddr_in6 *)fromp)->sin6_addr,
21596195Sdes		    numericname, sizeof numericname);
21696195Sdes	} else {
21774874Smarkm		syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
2181592Srgrimes		exit(1);
2191592Srgrimes	}
2201592Srgrimes#ifdef IP_OPTIONS
22196195Sdes	if (af == AF_INET) {
22296195Sdes		u_char optbuf[BUFSIZ/3];
223141918Sstefanf		socklen_t optsize = sizeof(optbuf), ipproto, i;
22496195Sdes		struct protoent *ip;
2251592Srgrimes
22696195Sdes		if ((ip = getprotobyname("ip")) != NULL)
22796195Sdes			ipproto = ip->p_proto;
22896195Sdes		else
22996195Sdes			ipproto = IPPROTO_IP;
23096195Sdes		if (!getsockopt(0, ipproto, IP_OPTIONS, optbuf, &optsize) &&
23196195Sdes		    optsize != 0) {
23296195Sdes			for (i = 0; i < optsize; ) {
23396195Sdes				u_char c = optbuf[i];
23496195Sdes				if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
23596195Sdes					syslog(LOG_NOTICE,
23696195Sdes					    "connection refused from %s with IP option %s",
23796195Sdes					    numericname,
23896195Sdes					    c == IPOPT_LSRR ? "LSRR" : "SSRR");
23996195Sdes					exit(1);
24096195Sdes				}
24196195Sdes				if (c == IPOPT_EOL)
24296195Sdes					break;
24396195Sdes				i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
24422454Simp			}
2451592Srgrimes		}
2461592Srgrimes	}
2471592Srgrimes#endif
2481592Srgrimes
24996195Sdes	if (srcport >= IPPORT_RESERVED ||
25096195Sdes	    srcport < IPPORT_RESERVED/2) {
25151433Smarkm		syslog(LOG_NOTICE|LOG_AUTH,
25251433Smarkm		    "connection from %s on illegal port %u",
25356590Sshin		    numericname,
25496195Sdes		    srcport);
25551433Smarkm		exit(1);
25651433Smarkm	}
2571592Srgrimes
2581592Srgrimes	(void) alarm(60);
2591592Srgrimes	port = 0;
26041860Speter	s = 0;		/* not set or used if port == 0 */
2611592Srgrimes	for (;;) {
2621592Srgrimes		char c;
2631592Srgrimes		if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
2641592Srgrimes			if (cc < 0)
2651592Srgrimes				syslog(LOG_NOTICE, "read: %m");
266146074Sjmallett			shutdown(0, SHUT_RDWR);
2671592Srgrimes			exit(1);
2681592Srgrimes		}
26941860Speter		if (c == 0)
2701592Srgrimes			break;
2711592Srgrimes		port = port * 10 + c - '0';
2721592Srgrimes	}
2731592Srgrimes
2741592Srgrimes	(void) alarm(0);
2751592Srgrimes	if (port != 0) {
2761592Srgrimes		int lport = IPPORT_RESERVED - 1;
27756590Sshin		s = rresvport_af(&lport, af);
2781592Srgrimes		if (s < 0) {
2791592Srgrimes			syslog(LOG_ERR, "can't get stderr port: %m");
2801592Srgrimes			exit(1);
2811592Srgrimes		}
28251433Smarkm		if (port >= IPPORT_RESERVED ||
28351433Smarkm		    port < IPPORT_RESERVED/2) {
28451433Smarkm			syslog(LOG_NOTICE|LOG_AUTH,
28551433Smarkm			    "2nd socket from %s on unreserved port %u",
28656590Sshin			    numericname,
28751433Smarkm			    port);
28851433Smarkm			exit(1);
28951433Smarkm		}
29096195Sdes		*((in_port_t *)&fromp->sa_data) = htons(port);
29196195Sdes		if (connect(s, fromp, fromp->sa_len) < 0) {
2921592Srgrimes			syslog(LOG_INFO, "connect second port %d: %m", port);
2931592Srgrimes			exit(1);
2941592Srgrimes		}
2951592Srgrimes	}
2961592Srgrimes
2971592Srgrimes	errorstr = NULL;
29896195Sdes	realhostname_sa(rhost, sizeof(rhost) - 1, fromp, fromp->sa_len);
29996195Sdes	rhost[sizeof(rhost) - 1] = '\0';
30096195Sdes	/* XXX truncation! */
3011592Srgrimes
30296195Sdes	(void) alarm(60);
30396195Sdes	getstr(ruser, sizeof(ruser), "ruser");
30496195Sdes	getstr(luser, sizeof(luser), "luser");
305143907Sdas	getstr(cmdbuf, maxcmdlen, "command");
30696195Sdes	(void) alarm(0);
30774874Smarkm
30896195Sdes	pam_err = pam_start("rsh", luser, &pamc, &pamh);
30996195Sdes	if (pam_err != PAM_SUCCESS) {
31096195Sdes		syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s",
31196195Sdes		    pam_strerror(pamh, pam_err));
31276134Smarkm		rshd_errx(1, "Login incorrect.");
31374874Smarkm	}
31474874Smarkm
31596195Sdes	if ((pam_err = pam_set_item(pamh, PAM_RUSER, ruser)) != PAM_SUCCESS ||
316226937Sbrueffer	    (pam_err = pam_set_item(pamh, PAM_RHOST, rhost)) != PAM_SUCCESS) {
31796195Sdes		syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s",
31896195Sdes		    pam_strerror(pamh, pam_err));
31976134Smarkm		rshd_errx(1, "Login incorrect.");
32074874Smarkm	}
32174874Smarkm
32296195Sdes	pam_err = pam_authenticate(pamh, 0);
32396195Sdes	if (pam_err == PAM_SUCCESS) {
32496195Sdes		if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) {
325321069Sdelphij			strlcpy(luser, cp, sizeof(luser));
32696195Sdes			/* XXX truncation! */
32796195Sdes		}
32896195Sdes		pam_err = pam_acct_mgmt(pamh, 0);
32974874Smarkm	}
33096195Sdes	if (pam_err != PAM_SUCCESS) {
33196195Sdes		syslog(LOG_INFO|LOG_AUTH,
33296195Sdes		    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
33396195Sdes		    ruser, rhost, luser, pam_strerror(pamh, pam_err), cmdbuf);
33476134Smarkm		rshd_errx(1, "Login incorrect.");
33574874Smarkm	}
33674874Smarkm
3371592Srgrimes	setpwent();
33896195Sdes	pwd = getpwnam(luser);
3391592Srgrimes	if (pwd == NULL) {
3401592Srgrimes		syslog(LOG_INFO|LOG_AUTH,
3411592Srgrimes		    "%s@%s as %s: unknown login. cmd='%.80s'",
34296195Sdes		    ruser, rhost, luser, cmdbuf);
3431592Srgrimes		if (errorstr == NULL)
34476094Smarkm			errorstr = "Login incorrect.";
34596195Sdes		rshd_errx(1, errorstr, rhost);
3461592Srgrimes	}
34774874Smarkm
34825674Sdavidn	lc = login_getpwclass(pwd);
34974874Smarkm	if (pwd->pw_uid)
35074874Smarkm		auth_checknologin(lc);
35174874Smarkm
3521592Srgrimes	if (chdir(pwd->pw_dir) < 0) {
35325099Sdavidn		if (chdir("/") < 0 ||
35425099Sdavidn		    login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
35525099Sdavidn			syslog(LOG_INFO|LOG_AUTH,
35625099Sdavidn			"%s@%s as %s: no home directory. cmd='%.80s'",
35796195Sdes			ruser, rhost, luser, cmdbuf);
35876134Smarkm			rshd_errx(0, "No remote home directory.");
35925099Sdavidn		}
36098885Smarkm		pwd->pw_dir = slash;
3611592Srgrimes	}
3621592Srgrimes
36396195Sdes	if (lc != NULL && fromp->sa_family == AF_INET) {	/*XXX*/
36425099Sdavidn		char	remote_ip[MAXHOSTNAMELEN];
36525099Sdavidn
366321069Sdelphij		strlcpy(remote_ip, numericname, sizeof(remote_ip));
36796195Sdes		/* XXX truncation! */
36896195Sdes		if (!auth_hostok(lc, rhost, remote_ip)) {
36925099Sdavidn			syslog(LOG_INFO|LOG_AUTH,
37025099Sdavidn			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
37196195Sdes			    ruser, rhost, luser, __rcmd_errstr,
37225099Sdavidn			    cmdbuf);
37376134Smarkm			rshd_errx(1, "Login incorrect.");
37425099Sdavidn		}
37576094Smarkm		if (!auth_timeok(lc, time(NULL)))
37676134Smarkm			rshd_errx(1, "Logins not available right now");
37725099Sdavidn	}
3781592Srgrimes
37974874Smarkm	/*
38074874Smarkm	 * PAM modules might add supplementary groups in
38174874Smarkm	 * pam_setcred(), so initialize them first.
38274874Smarkm	 * But we need to open the session as root.
38374874Smarkm	 */
38474874Smarkm	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
38596195Sdes		syslog(LOG_ERR, "setusercontext: %m");
38674874Smarkm		exit(1);
38774874Smarkm	}
38874874Smarkm
38996195Sdes	if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
39096195Sdes		syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, pam_err));
39196195Sdes	} else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
39296195Sdes		syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err));
39374874Smarkm	}
39474874Smarkm
3951592Srgrimes	(void) write(STDERR_FILENO, "\0", 1);
3961592Srgrimes	sent_null = 1;
3971592Srgrimes
3981592Srgrimes	if (port) {
39976094Smarkm		if (pipe(pv) < 0)
40076134Smarkm			rshd_errx(1, "Can't make pipe.");
4011592Srgrimes		pid = fork();
40276094Smarkm		if (pid == -1)
40376134Smarkm			rshd_errx(1, "Can't fork; try again.");
4041592Srgrimes		if (pid) {
40598885Smarkm			(void) close(0);
40698885Smarkm			(void) close(1);
4071592Srgrimes			(void) close(2);
4081592Srgrimes			(void) close(pv[1]);
4091592Srgrimes
4101592Srgrimes			FD_ZERO(&readfrom);
4111592Srgrimes			FD_SET(s, &readfrom);
4121592Srgrimes			FD_SET(pv[0], &readfrom);
4131592Srgrimes			if (pv[0] > s)
4141592Srgrimes				nfd = pv[0];
4151592Srgrimes			else
4161592Srgrimes				nfd = s;
4171592Srgrimes				ioctl(pv[0], FIONBIO, (char *)&one);
4181592Srgrimes
4191592Srgrimes			/* should set s nbio! */
4201592Srgrimes			nfd++;
4211592Srgrimes			do {
4221592Srgrimes				ready = readfrom;
42398885Smarkm				if (select(nfd, &ready, (fd_set *)0,
42498885Smarkm				  (fd_set *)0, (struct timeval *)0) < 0)
42598885Smarkm					break;
4261592Srgrimes				if (FD_ISSET(s, &ready)) {
4271592Srgrimes					int	ret;
4281592Srgrimes						ret = read(s, &sig, 1);
42998885Smarkm				if (ret <= 0)
43098885Smarkm					FD_CLR(s, &readfrom);
43198885Smarkm				else
43298885Smarkm					killpg(pid, sig);
4331592Srgrimes				}
4341592Srgrimes				if (FD_ISSET(pv[0], &ready)) {
4351592Srgrimes					errno = 0;
4361592Srgrimes					cc = read(pv[0], buf, sizeof(buf));
4371592Srgrimes					if (cc <= 0) {
438146074Sjmallett						shutdown(s, SHUT_RDWR);
4391592Srgrimes						FD_CLR(pv[0], &readfrom);
4401592Srgrimes					} else {
44198885Smarkm						(void)write(s, buf, cc);
4421592Srgrimes					}
4431592Srgrimes				}
4441592Srgrimes
4451592Srgrimes			} while (FD_ISSET(s, &readfrom) ||
4461592Srgrimes			    FD_ISSET(pv[0], &readfrom));
44774874Smarkm			PAM_END;
4481592Srgrimes			exit(0);
4491592Srgrimes		}
4501592Srgrimes		(void) close(s);
4511592Srgrimes		(void) close(pv[0]);
4521592Srgrimes		dup2(pv[1], 2);
4531592Srgrimes		close(pv[1]);
4541592Srgrimes	}
45574874Smarkm	else {
45674874Smarkm		pid = fork();
45776094Smarkm		if (pid == -1)
45876134Smarkm			rshd_errx(1, "Can't fork; try again.");
45974874Smarkm		if (pid) {
46074874Smarkm			/* Parent. */
46196195Sdes			while (wait(NULL) > 0 || errno == EINTR)
46296195Sdes				/* nothing */ ;
46374874Smarkm			PAM_END;
46474874Smarkm			exit(0);
46574874Smarkm		}
46674874Smarkm	}
46774874Smarkm
46896195Sdes	for (fd = getdtablesize(); fd > 2; fd--)
46996195Sdes		(void) close(fd);
47096195Sdes	if (setsid() == -1)
47196195Sdes		syslog(LOG_ERR, "setsid() failed: %m");
47296195Sdes	if (setlogin(pwd->pw_name) < 0)
47396195Sdes		syslog(LOG_ERR, "setlogin() failed: %m");
47496195Sdes
4751592Srgrimes	if (*pwd->pw_shell == '\0')
47698885Smarkm		pwd->pw_shell = bshell;
47796195Sdes	(void) pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
47896195Sdes	(void) pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
47996195Sdes	(void) pam_setenv(pamh, "USER", pwd->pw_name, 1);
48096195Sdes	(void) pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
48196195Sdes	environ = pam_getenvlist(pamh);
48296195Sdes	(void) pam_end(pamh, pam_err);
4831592Srgrimes	cp = strrchr(pwd->pw_shell, '/');
4841592Srgrimes	if (cp)
4851592Srgrimes		cp++;
4861592Srgrimes	else
4871592Srgrimes		cp = pwd->pw_shell;
48874874Smarkm
48996195Sdes	if (setusercontext(lc, pwd, pwd->pw_uid,
49096195Sdes		LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
49196195Sdes		syslog(LOG_ERR, "setusercontext(): %m");
49225099Sdavidn		exit(1);
49325099Sdavidn	}
49425099Sdavidn	login_close(lc);
4951592Srgrimes	endpwent();
4961592Srgrimes	if (log_success || pwd->pw_uid == 0) {
4971592Srgrimes		    syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
49896195Sdes			ruser, rhost, luser, cmdbuf);
4991592Srgrimes	}
500127864Smux	execl(pwd->pw_shell, cp, "-c", cmdbuf, (char *)NULL);
50196195Sdes	err(1, "%s", pwd->pw_shell);
5021592Srgrimes	exit(1);
5031592Srgrimes}
5041592Srgrimes
50576134Smarkm/*
50676134Smarkm * Report error to client.  Note: can't be used until second socket has
50776134Smarkm * connected to client, or older clients will hang waiting for that
50876134Smarkm * connection first.
50976134Smarkm */
51076134Smarkm
51176134Smarkmstatic void
51276134Smarkmrshd_errx(int errcode, const char *fmt, ...)
51376134Smarkm{
51476134Smarkm	va_list ap;
51576134Smarkm
51676134Smarkm	va_start(ap, fmt);
51776134Smarkm
51876134Smarkm	if (sent_null == 0)
51976134Smarkm		write(STDERR_FILENO, "\1", 1);
52076134Smarkm
52176134Smarkm	verrx(errcode, fmt, ap);
52276134Smarkm	/* NOTREACHED */
52376134Smarkm}
52476134Smarkm
5251592Srgrimesvoid
52690335Simpgetstr(char *buf, int cnt, const char *error)
5271592Srgrimes{
5281592Srgrimes	char c;
5291592Srgrimes
5301592Srgrimes	do {
5311592Srgrimes		if (read(STDIN_FILENO, &c, 1) != 1)
5321592Srgrimes			exit(1);
5331592Srgrimes		*buf++ = c;
53476125Smarkm		if (--cnt == 0)
53590335Simp			rshd_errx(1, "%s too long", error);
5361592Srgrimes	} while (c != 0);
5371592Srgrimes}
5381592Srgrimes
5391592Srgrimes/*
5401592Srgrimes * Check whether host h is in our local domain,
5411592Srgrimes * defined as sharing the last two components of the domain part,
5421592Srgrimes * or the entire domain part if the local domain has only one component.
5431592Srgrimes * If either name is unqualified (contains no '.'),
5441592Srgrimes * assume that the host is local, as it will be
5451592Srgrimes * interpreted as such.
5461592Srgrimes */
5471592Srgrimesint
54890334Simplocal_domain(char *h)
5491592Srgrimes{
5501592Srgrimes	char localhost[MAXHOSTNAMELEN];
5511592Srgrimes	char *p1, *p2;
5521592Srgrimes
5531592Srgrimes	localhost[0] = 0;
55445422Sbrian	(void) gethostname(localhost, sizeof(localhost) - 1);
55545422Sbrian	localhost[sizeof(localhost) - 1] = '\0';
55696195Sdes	/* XXX truncation! */
5571592Srgrimes	p1 = topdomain(localhost);
5581592Srgrimes	p2 = topdomain(h);
5591592Srgrimes	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
5601592Srgrimes		return (1);
5611592Srgrimes	return (0);
5621592Srgrimes}
5631592Srgrimes
5641592Srgrimeschar *
56590334Simptopdomain(char *h)
5661592Srgrimes{
5671592Srgrimes	char *p, *maybe = NULL;
5681592Srgrimes	int dots = 0;
5691592Srgrimes
5701592Srgrimes	for (p = h + strlen(h); p >= h; p--) {
5711592Srgrimes		if (*p == '.') {
5721592Srgrimes			if (++dots == 2)
5731592Srgrimes				return (p);
5741592Srgrimes			maybe = p;
5751592Srgrimes		}
5761592Srgrimes	}
5771592Srgrimes	return (maybe);
5781592Srgrimes}
5791592Srgrimes
5801592Srgrimesvoid
58190334Simpusage(void)
5821592Srgrimes{
5831592Srgrimes
5841592Srgrimes	syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
5851592Srgrimes	exit(2);
5861592Srgrimes}
587