rshd.c revision 146074
1218792Snp/*-
2237436Snp * Copyright (c) 1988, 1989, 1992, 1993, 1994
3218792Snp *	The Regents of the University of California.  All rights reserved.
4218792Snp * Copyright (c) 2002 Networks Associates Technology, Inc.
5218792Snp * All rights reserved.
6218792Snp *
7218792Snp * Portions of this software were developed for the FreeBSD Project by
8218792Snp * ThinkSec AS and NAI Labs, the Security Research Division of Network
9218792Snp * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
10218792Snp * ("CBOSS"), as part of the DARPA CHATS research program.
11218792Snp *
12218792Snp * Redistribution and use in source and binary forms, with or without
13218792Snp * modification, are permitted provided that the following conditions
14218792Snp * are met:
15218792Snp * 1. Redistributions of source code must retain the above copyright
16218792Snp *    notice, this list of conditions and the following disclaimer.
17218792Snp * 2. Redistributions in binary form must reproduce the above copyright
18218792Snp *    notice, this list of conditions and the following disclaimer in the
19218792Snp *    documentation and/or other materials provided with the distribution.
20218792Snp * 3. All advertising materials mentioning features or use of this software
21218792Snp *    must display the following acknowledgement:
22218792Snp *	This product includes software developed by the University of
23218792Snp *	California, Berkeley and its contributors.
24218792Snp * 4. Neither the name of the University nor the names of its contributors
25218792Snp *    may be used to endorse or promote products derived from this software
26218792Snp *    without specific prior written permission.
27218792Snp *
28218792Snp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29218792Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30237263Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31237263Snp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32218792Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33218792Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34218792Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35228561Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36218792Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37220649Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38246385Snp * SUCH DAMAGE.
39246385Snp */
40246385Snp
41246385Snp#ifndef lint
42246385Snpstatic const char copyright[] =
43246385Snp"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
44218792Snp	The Regents of the University of California.  All rights reserved.\n";
45218792Snp#endif /* not lint */
46218792Snp
47218792Snp#ifndef lint
48218792Snp#if 0
49218792Snpstatic const char sccsid[] = "@(#)rshd.c	8.2 (Berkeley) 4/6/94";
50218792Snp#endif
51218792Snp#endif /* not lint */
52218792Snp
53218792Snp#include <sys/cdefs.h>
54218792Snp__FBSDID("$FreeBSD: head/libexec/rshd/rshd.c 146074 2005-05-11 02:41:39Z jmallett $");
55218792Snp
56218792Snp/*
57218792Snp * remote shell server:
58218792Snp *	[port]\0
59218792Snp *	ruser\0
60218792Snp *	luser\0
61218792Snp *	command\0
62218792Snp *	data
63218792Snp */
64218792Snp#include <sys/param.h>
65218792Snp#include <sys/ioctl.h>
66218792Snp#include <sys/time.h>
67218792Snp#include <sys/socket.h>
68218792Snp
69218792Snp#include <netinet/in_systm.h>
70218792Snp#include <netinet/in.h>
71218792Snp#include <netinet/ip.h>
72218792Snp#include <netinet/tcp.h>
73218792Snp#include <arpa/inet.h>
74218792Snp#include <netdb.h>
75218792Snp
76218792Snp#include <err.h>
77218792Snp#include <errno.h>
78218792Snp#include <fcntl.h>
79218792Snp#include <libutil.h>
80218792Snp#include <paths.h>
81218792Snp#include <pwd.h>
82218792Snp#include <signal.h>
83218792Snp#include <stdarg.h>
84218792Snp#include <stdio.h>
85218792Snp#include <stdlib.h>
86218792Snp#include <string.h>
87218792Snp#include <syslog.h>
88218792Snp#include <unistd.h>
89218792Snp#include <login_cap.h>
90218792Snp
91218792Snp#include <security/pam_appl.h>
92218792Snp#include <security/openpam.h>
93218792Snp#include <sys/wait.h>
94218792Snp
95218792Snpstatic struct pam_conv pamc = { openpam_nullconv, NULL };
96218792Snpstatic pam_handle_t *pamh;
97218792Snpstatic int pam_err;
98218792Snp
99218792Snp#define PAM_END { \
100218792Snp	if ((pam_err = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \
101218792Snp		syslog(LOG_ERR|LOG_AUTH, "pam_setcred(): %s", pam_strerror(pamh, pam_err)); \
102218792Snp	if ((pam_err = pam_close_session(pamh,0)) != PAM_SUCCESS) \
103218792Snp		syslog(LOG_ERR|LOG_AUTH, "pam_close_session(): %s", pam_strerror(pamh, pam_err)); \
104218792Snp	if ((pam_err = pam_end(pamh, pam_err)) != PAM_SUCCESS) \
105218792Snp		syslog(LOG_ERR|LOG_AUTH, "pam_end(): %s", pam_strerror(pamh, pam_err)); \
106218792Snp}
107218792Snp
108218792Snpint	keepalive = 1;
109218792Snpint	log_success;		/* If TRUE, log all successful accesses */
110218792Snpint	sent_null;
111218792Snpint	no_delay;
112218792Snp
113218792Snpvoid	 doit(struct sockaddr *);
114218792Snpstatic void	 rshd_errx(int, const char *, ...) __printf0like(2, 3);
115218792Snpvoid	 getstr(char *, int, const char *);
116218792Snpint	 local_domain(char *);
117218792Snpchar	*topdomain(char *);
118218792Snpvoid	 usage(void);
119218792Snp
120218792Snpchar	 slash[] = "/";
121218792Snpchar	 bshell[] = _PATH_BSHELL;
122218792Snp
123218792Snp#define	OPTIONS	"aDLln"
124218792Snp
125218792Snpint
126218792Snpmain(int argc, char *argv[])
127218792Snp{
128218792Snp	extern int __check_rhosts_file;
129218792Snp	struct linger linger;
130218792Snp	socklen_t fromlen;
131218792Snp	int ch, on = 1;
132218792Snp	struct sockaddr_storage from;
133218792Snp
134218792Snp	openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
135218792Snp
136218792Snp	opterr = 0;
137218792Snp	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
138218792Snp		switch (ch) {
139218792Snp		case 'a':
140218792Snp			/* ignored for compatibility */
141218792Snp			break;
142218792Snp		case 'l':
143237436Snp			__check_rhosts_file = 0;
144237436Snp			break;
145237436Snp		case 'n':
146237436Snp			keepalive = 0;
147237436Snp			break;
148237436Snp		case 'D':
149237436Snp			no_delay = 1;
150237436Snp			break;
151237436Snp		case 'L':
152237436Snp			log_success = 1;
153237436Snp			break;
154237436Snp		case '?':
155237436Snp		default:
156237436Snp			usage();
157247355Snp			break;
158247355Snp		}
159247355Snp
160247355Snp	argc -= optind;
161247355Snp	argv += optind;
162247355Snp
163247355Snp	fromlen = sizeof (from);
164247355Snp	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
165247355Snp		syslog(LOG_ERR, "getpeername: %m");
166247355Snp		exit(1);
167247355Snp	}
168247355Snp	if (keepalive &&
169247355Snp	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
170247355Snp	    sizeof(on)) < 0)
171247355Snp		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
172247355Snp	linger.l_onoff = 1;
173247355Snp	linger.l_linger = 60;			/* XXX */
174247355Snp	if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
175247355Snp	    sizeof (linger)) < 0)
176247355Snp		syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
177247355Snp	if (no_delay &&
178247355Snp	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
179250090Snp		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
180247355Snp	doit((struct sockaddr *)&from);
181247355Snp	/* NOTREACHED */
182247355Snp	return(0);
183247355Snp}
184247355Snp
185218792Snpextern char **environ;
186218792Snp
187218792Snpvoid
188218792Snpdoit(struct sockaddr *fromp)
189218792Snp{
190218792Snp	extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c. */
191218792Snp	struct passwd *pwd;
192218792Snp	u_short port;
193218792Snp	fd_set ready, readfrom;
194218792Snp	int cc, fd, nfd, pv[2], pid, s;
195218792Snp	int one = 1;
196218792Snp	const char *cp, *errorstr;
197218792Snp	char sig, buf[BUFSIZ];
198218792Snp	char *cmdbuf, luser[16], ruser[16];
199218792Snp	char rhost[2 * MAXHOSTNAMELEN + 1];
200218792Snp	char numericname[INET6_ADDRSTRLEN];
201218792Snp	int af, srcport;
202218792Snp	int maxcmdlen;
203218792Snp	login_cap_t *lc;
204218792Snp
205218792Snp	maxcmdlen = (int)sysconf(_SC_ARG_MAX);
206218792Snp	if (maxcmdlen <= 0 || (cmdbuf = malloc(maxcmdlen)) == NULL)
207218792Snp		exit(1);
208218792Snp
209218792Snp	(void) signal(SIGINT, SIG_DFL);
210218792Snp	(void) signal(SIGQUIT, SIG_DFL);
211218792Snp	(void) signal(SIGTERM, SIG_DFL);
212218792Snp	af = fromp->sa_family;
213218792Snp	srcport = ntohs(*((in_port_t *)&fromp->sa_data));
214218792Snp	if (af == AF_INET) {
215218792Snp		inet_ntop(af, &((struct sockaddr_in *)fromp)->sin_addr,
216218792Snp		    numericname, sizeof numericname);
217218792Snp	} else if (af == AF_INET6) {
218218792Snp		inet_ntop(af, &((struct sockaddr_in6 *)fromp)->sin6_addr,
219218792Snp		    numericname, sizeof numericname);
220218792Snp	} else {
221218792Snp		syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
222218792Snp		exit(1);
223218792Snp	}
224218792Snp#ifdef IP_OPTIONS
225218792Snp	if (af == AF_INET) {
226218792Snp		u_char optbuf[BUFSIZ/3];
227218792Snp		socklen_t optsize = sizeof(optbuf), ipproto, i;
228218792Snp		struct protoent *ip;
229218792Snp
230218792Snp		if ((ip = getprotobyname("ip")) != NULL)
231218792Snp			ipproto = ip->p_proto;
232218792Snp		else
233218792Snp			ipproto = IPPROTO_IP;
234218792Snp		if (!getsockopt(0, ipproto, IP_OPTIONS, optbuf, &optsize) &&
235218792Snp		    optsize != 0) {
236218792Snp			for (i = 0; i < optsize; ) {
237218792Snp				u_char c = optbuf[i];
238218792Snp				if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
239228561Snp					syslog(LOG_NOTICE,
240218792Snp					    "connection refused from %s with IP option %s",
241218792Snp					    numericname,
242218792Snp					    c == IPOPT_LSRR ? "LSRR" : "SSRR");
243218792Snp					exit(1);
244218792Snp				}
245218792Snp				if (c == IPOPT_EOL)
246218792Snp					break;
247218792Snp				i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
248218792Snp			}
249218792Snp		}
250218792Snp	}
251218792Snp#endif
252218792Snp
253218792Snp	if (srcport >= IPPORT_RESERVED ||
254218792Snp	    srcport < IPPORT_RESERVED/2) {
255218792Snp		syslog(LOG_NOTICE|LOG_AUTH,
256218792Snp		    "connection from %s on illegal port %u",
257218792Snp		    numericname,
258218792Snp		    srcport);
259218792Snp		exit(1);
260218792Snp	}
261218792Snp
262286271Snp	(void) alarm(60);
263286271Snp	port = 0;
264218792Snp	s = 0;		/* not set or used if port == 0 */
265218792Snp	for (;;) {
266218792Snp		char c;
267218792Snp		if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
268218792Snp			if (cc < 0)
269218792Snp				syslog(LOG_NOTICE, "read: %m");
270218792Snp			shutdown(0, SHUT_RDWR);
271218792Snp			exit(1);
272218792Snp		}
273218792Snp		if (c == 0)
274218792Snp			break;
275218792Snp		port = port * 10 + c - '0';
276218792Snp	}
277218792Snp
278218792Snp	(void) alarm(0);
279218792Snp	if (port != 0) {
280218792Snp		int lport = IPPORT_RESERVED - 1;
281218792Snp		s = rresvport_af(&lport, af);
282218792Snp		if (s < 0) {
283218792Snp			syslog(LOG_ERR, "can't get stderr port: %m");
284218792Snp			exit(1);
285218792Snp		}
286218792Snp		if (port >= IPPORT_RESERVED ||
287218792Snp		    port < IPPORT_RESERVED/2) {
288218792Snp			syslog(LOG_NOTICE|LOG_AUTH,
289286271Snp			    "2nd socket from %s on unreserved port %u",
290286271Snp			    numericname,
291218792Snp			    port);
292218792Snp			exit(1);
293218792Snp		}
294218792Snp		*((in_port_t *)&fromp->sa_data) = htons(port);
295218792Snp		if (connect(s, fromp, fromp->sa_len) < 0) {
296218792Snp			syslog(LOG_INFO, "connect second port %d: %m", port);
297218792Snp			exit(1);
298218792Snp		}
299218792Snp	}
300218792Snp
301218792Snp	errorstr = NULL;
302247355Snp	realhostname_sa(rhost, sizeof(rhost) - 1, fromp, fromp->sa_len);
303247355Snp	rhost[sizeof(rhost) - 1] = '\0';
304247355Snp	/* XXX truncation! */
305247355Snp
306247355Snp	(void) alarm(60);
307218792Snp	getstr(ruser, sizeof(ruser), "ruser");
308218792Snp	getstr(luser, sizeof(luser), "luser");
309247355Snp	getstr(cmdbuf, maxcmdlen, "command");
310247355Snp	(void) alarm(0);
311218792Snp
312218792Snp	pam_err = pam_start("rsh", luser, &pamc, &pamh);
313218792Snp	if (pam_err != PAM_SUCCESS) {
314218792Snp		syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s",
315218792Snp		    pam_strerror(pamh, pam_err));
316218792Snp		rshd_errx(1, "Login incorrect.");
317248925Snp	}
318218792Snp
319218792Snp	if ((pam_err = pam_set_item(pamh, PAM_RUSER, ruser)) != PAM_SUCCESS ||
320218792Snp	    (pam_err = pam_set_item(pamh, PAM_RHOST, rhost) != PAM_SUCCESS)) {
321218792Snp		syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s",
322218792Snp		    pam_strerror(pamh, pam_err));
323218792Snp		rshd_errx(1, "Login incorrect.");
324218792Snp	}
325218792Snp
326248925Snp	pam_err = pam_authenticate(pamh, 0);
327218792Snp	if (pam_err == PAM_SUCCESS) {
328218792Snp		if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) {
329248925Snp			strncpy(luser, cp, sizeof(luser));
330248925Snp			luser[sizeof(luser) - 1] = '\0';
331218792Snp			/* XXX truncation! */
332248925Snp		}
333248925Snp		pam_err = pam_acct_mgmt(pamh, 0);
334248925Snp	}
335248925Snp	if (pam_err != PAM_SUCCESS) {
336248925Snp		syslog(LOG_INFO|LOG_AUTH,
337248925Snp		    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
338248925Snp		    ruser, rhost, luser, pam_strerror(pamh, pam_err), cmdbuf);
339248925Snp		rshd_errx(1, "Login incorrect.");
340248925Snp	}
341248925Snp
342248925Snp	setpwent();
343248925Snp	pwd = getpwnam(luser);
344248925Snp	if (pwd == NULL) {
345248925Snp		syslog(LOG_INFO|LOG_AUTH,
346248925Snp		    "%s@%s as %s: unknown login. cmd='%.80s'",
347248925Snp		    ruser, rhost, luser, cmdbuf);
348248925Snp		if (errorstr == NULL)
349218792Snp			errorstr = "Login incorrect.";
350248925Snp		rshd_errx(1, errorstr, rhost);
351248925Snp	}
352248925Snp
353248925Snp	lc = login_getpwclass(pwd);
354248925Snp	if (pwd->pw_uid)
355248925Snp		auth_checknologin(lc);
356218792Snp
357218792Snp	if (chdir(pwd->pw_dir) < 0) {
358218792Snp		if (chdir("/") < 0 ||
359248925Snp		    login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
360218792Snp			syslog(LOG_INFO|LOG_AUTH,
361218792Snp			"%s@%s as %s: no home directory. cmd='%.80s'",
362237436Snp			ruser, rhost, luser, cmdbuf);
363218792Snp			rshd_errx(0, "No remote home directory.");
364218792Snp		}
365218792Snp		pwd->pw_dir = slash;
366218792Snp	}
367218792Snp
368218792Snp	if (lc != NULL && fromp->sa_family == AF_INET) {	/*XXX*/
369218792Snp		char	remote_ip[MAXHOSTNAMELEN];
370218792Snp
371218792Snp		strncpy(remote_ip, numericname,
372218792Snp			sizeof(remote_ip) - 1);
373218792Snp		remote_ip[sizeof(remote_ip) - 1] = 0;
374218792Snp		/* XXX truncation! */
375218792Snp		if (!auth_hostok(lc, rhost, remote_ip)) {
376218792Snp			syslog(LOG_INFO|LOG_AUTH,
377218792Snp			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
378218792Snp			    ruser, rhost, luser, __rcmd_errstr,
379218792Snp			    cmdbuf);
380218792Snp			rshd_errx(1, "Login incorrect.");
381218792Snp		}
382218792Snp		if (!auth_timeok(lc, time(NULL)))
383218792Snp			rshd_errx(1, "Logins not available right now");
384248925Snp	}
385248925Snp
386218792Snp	/*
387248925Snp	 * PAM modules might add supplementary groups in
388248925Snp	 * pam_setcred(), so initialize them first.
389248925Snp	 * But we need to open the session as root.
390248925Snp	 */
391248925Snp	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
392248925Snp		syslog(LOG_ERR, "setusercontext: %m");
393248925Snp		exit(1);
394248925Snp	}
395248925Snp
396248925Snp	if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
397248925Snp		syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, pam_err));
398248925Snp	} else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
399248925Snp		syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err));
400248925Snp	}
401248925Snp
402248925Snp	(void) write(STDERR_FILENO, "\0", 1);
403248925Snp	sent_null = 1;
404248925Snp
405248925Snp	if (port) {
406248925Snp		if (pipe(pv) < 0)
407248925Snp			rshd_errx(1, "Can't make pipe.");
408248925Snp		pid = fork();
409248925Snp		if (pid == -1)
410248925Snp			rshd_errx(1, "Can't fork; try again.");
411248925Snp		if (pid) {
412248925Snp			(void) close(0);
413248925Snp			(void) close(1);
414218792Snp			(void) close(2);
415248925Snp			(void) close(pv[1]);
416248925Snp
417248925Snp			FD_ZERO(&readfrom);
418248925Snp			FD_SET(s, &readfrom);
419218792Snp			FD_SET(pv[0], &readfrom);
420248925Snp			if (pv[0] > s)
421218792Snp				nfd = pv[0];
422218792Snp			else
423218792Snp				nfd = s;
424248925Snp				ioctl(pv[0], FIONBIO, (char *)&one);
425218792Snp
426218792Snp			/* should set s nbio! */
427237436Snp			nfd++;
428218792Snp			do {
429218792Snp				ready = readfrom;
430218792Snp				if (select(nfd, &ready, (fd_set *)0,
431218792Snp				  (fd_set *)0, (struct timeval *)0) < 0)
432218792Snp					break;
433218792Snp				if (FD_ISSET(s, &ready)) {
434218792Snp					int	ret;
435218792Snp						ret = read(s, &sig, 1);
436218792Snp				if (ret <= 0)
437218792Snp					FD_CLR(s, &readfrom);
438218792Snp				else
439218792Snp					killpg(pid, sig);
440218792Snp				}
441218792Snp				if (FD_ISSET(pv[0], &ready)) {
442218792Snp					errno = 0;
443218792Snp					cc = read(pv[0], buf, sizeof(buf));
444218792Snp					if (cc <= 0) {
445218792Snp						shutdown(s, SHUT_RDWR);
446218792Snp						FD_CLR(pv[0], &readfrom);
447218792Snp					} else {
448218792Snp						(void)write(s, buf, cc);
449218792Snp					}
450218792Snp				}
451218792Snp
452218792Snp			} while (FD_ISSET(s, &readfrom) ||
453218792Snp			    FD_ISSET(pv[0], &readfrom));
454218792Snp			PAM_END;
455218792Snp			exit(0);
456218792Snp		}
457218792Snp		(void) close(s);
458218792Snp		(void) close(pv[0]);
459218792Snp		dup2(pv[1], 2);
460218792Snp		close(pv[1]);
461218792Snp	}
462218792Snp	else {
463218792Snp		pid = fork();
464218792Snp		if (pid == -1)
465218792Snp			rshd_errx(1, "Can't fork; try again.");
466218792Snp		if (pid) {
467218792Snp			/* Parent. */
468218792Snp			while (wait(NULL) > 0 || errno == EINTR)
469218792Snp				/* nothing */ ;
470218792Snp			PAM_END;
471218792Snp			exit(0);
472218792Snp		}
473218792Snp	}
474218792Snp
475218792Snp	for (fd = getdtablesize(); fd > 2; fd--)
476248925Snp		(void) close(fd);
477248925Snp	if (setsid() == -1)
478218792Snp		syslog(LOG_ERR, "setsid() failed: %m");
479218792Snp	if (setlogin(pwd->pw_name) < 0)
480218792Snp		syslog(LOG_ERR, "setlogin() failed: %m");
481218792Snp
482218792Snp	if (*pwd->pw_shell == '\0')
483218792Snp		pwd->pw_shell = bshell;
484218792Snp	(void) pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
485218792Snp	(void) pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
486218792Snp	(void) pam_setenv(pamh, "USER", pwd->pw_name, 1);
487218792Snp	(void) pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
488218792Snp	environ = pam_getenvlist(pamh);
489218792Snp	(void) pam_end(pamh, pam_err);
490218792Snp	cp = strrchr(pwd->pw_shell, '/');
491218792Snp	if (cp)
492218792Snp		cp++;
493218792Snp	else
494218792Snp		cp = pwd->pw_shell;
495218792Snp
496218792Snp	if (setusercontext(lc, pwd, pwd->pw_uid,
497218792Snp		LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
498218792Snp		syslog(LOG_ERR, "setusercontext(): %m");
499218792Snp		exit(1);
500218792Snp	}
501218792Snp	login_close(lc);
502218792Snp	endpwent();
503218792Snp	if (log_success || pwd->pw_uid == 0) {
504218792Snp		    syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
505218792Snp			ruser, rhost, luser, cmdbuf);
506218792Snp	}
507218792Snp	execl(pwd->pw_shell, cp, "-c", cmdbuf, (char *)NULL);
508218792Snp	err(1, "%s", pwd->pw_shell);
509218792Snp	exit(1);
510218792Snp}
511218792Snp
512218792Snp/*
513218792Snp * Report error to client.  Note: can't be used until second socket has
514218792Snp * connected to client, or older clients will hang waiting for that
515248925Snp * connection first.
516218792Snp */
517250090Snp
518218792Snpstatic void
519218792Snprshd_errx(int errcode, const char *fmt, ...)
520218792Snp{
521218792Snp	va_list ap;
522218792Snp
523218792Snp	va_start(ap, fmt);
524218792Snp
525218792Snp	if (sent_null == 0)
526218792Snp		write(STDERR_FILENO, "\1", 1);
527218792Snp
528218792Snp	verrx(errcode, fmt, ap);
529218792Snp	/* NOTREACHED */
530218792Snp}
531218792Snp
532218792Snpvoid
533218792Snpgetstr(char *buf, int cnt, const char *error)
534218792Snp{
535218792Snp	char c;
536218792Snp
537218792Snp	do {
538218792Snp		if (read(STDIN_FILENO, &c, 1) != 1)
539218792Snp			exit(1);
540218792Snp		*buf++ = c;
541218792Snp		if (--cnt == 0)
542218792Snp			rshd_errx(1, "%s too long", error);
543218792Snp	} while (c != 0);
544218792Snp}
545218792Snp
546218792Snp/*
547218792Snp * Check whether host h is in our local domain,
548218792Snp * defined as sharing the last two components of the domain part,
549218792Snp * or the entire domain part if the local domain has only one component.
550218792Snp * If either name is unqualified (contains no '.'),
551218792Snp * assume that the host is local, as it will be
552218792Snp * interpreted as such.
553218792Snp */
554218792Snpint
555218792Snplocal_domain(char *h)
556218792Snp{
557218792Snp	char localhost[MAXHOSTNAMELEN];
558218792Snp	char *p1, *p2;
559218792Snp
560218792Snp	localhost[0] = 0;
561218792Snp	(void) gethostname(localhost, sizeof(localhost) - 1);
562218792Snp	localhost[sizeof(localhost) - 1] = '\0';
563218792Snp	/* XXX truncation! */
564218792Snp	p1 = topdomain(localhost);
565218792Snp	p2 = topdomain(h);
566218792Snp	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
567218792Snp		return (1);
568218792Snp	return (0);
569218792Snp}
570218792Snp
571218792Snpchar *
572218792Snptopdomain(char *h)
573218792Snp{
574218792Snp	char *p, *maybe = NULL;
575218792Snp	int dots = 0;
576218792Snp
577218792Snp	for (p = h + strlen(h); p >= h; p--) {
578218792Snp		if (*p == '.') {
579218792Snp			if (++dots == 2)
580218792Snp				return (p);
581218792Snp			maybe = p;
582218792Snp		}
583218792Snp	}
584218792Snp	return (maybe);
585218792Snp}
586218792Snp
587218792Snpvoid
588218792Snpusage(void)
589218792Snp{
590218792Snp
591218792Snp	syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
592218792Snp	exit(2);
593218792Snp}
594218792Snp