rshd.c revision 29917
1/*-
2 * Copyright (c) 1988, 1989, 1992, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	$Id: rshd.c,v 1.18 1997/07/18 21:04:19 wosch Exp $
34 */
35
36#ifndef lint
37static const char copyright[] =
38"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
39	The Regents of the University of California.  All rights reserved.\n";
40#endif /* not lint */
41
42#ifndef lint
43static const char sccsid[] = "@(#)rshd.c	8.2 (Berkeley) 4/6/94";
44#endif /* not lint */
45
46/*
47 * remote shell server:
48 *	[port]\0
49 *	remuser\0
50 *	locuser\0
51 *	command\0
52 *	data
53 */
54#include <sys/param.h>
55#include <sys/ioctl.h>
56#include <sys/time.h>
57#include <sys/socket.h>
58
59#include <netinet/in_systm.h>
60#include <netinet/in.h>
61#include <netinet/ip.h>
62#include <arpa/inet.h>
63#include <netdb.h>
64
65#include <errno.h>
66#include <fcntl.h>
67#include <paths.h>
68#include <pwd.h>
69#include <signal.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#include <syslog.h>
74#include <unistd.h>
75#ifdef	LOGIN_CAP
76#include <login_cap.h>
77#endif
78
79int	keepalive = 1;
80int	check_all;
81int	log_success;		/* If TRUE, log all successful accesses */
82int	sent_null;
83
84void	 doit __P((struct sockaddr_in *));
85void	 error __P((const char *, ...));
86void	 getstr __P((char *, int, char *));
87int	 local_domain __P((char *));
88char	*topdomain __P((char *));
89void	 usage __P((void));
90
91#ifdef	KERBEROS
92#include <des.h>
93#include <krb.h>
94#define	VERSION_SIZE	9
95#define SECURE_MESSAGE  "This rsh session is using DES encryption for all transmissions.\r\n"
96#define	OPTIONS		"alnkvxL"
97char	authbuf[sizeof(AUTH_DAT)];
98char	tickbuf[sizeof(KTEXT_ST)];
99int	doencrypt, use_kerberos, vacuous;
100Key_schedule	schedule;
101#else
102#define	OPTIONS	"alnL"
103#endif
104
105int
106main(argc, argv)
107	int argc;
108	char *argv[];
109{
110	extern int __check_rhosts_file;
111	struct linger linger;
112	int ch, on = 1, fromlen;
113	struct sockaddr_in from;
114
115	openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
116
117	opterr = 0;
118	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
119		switch (ch) {
120		case 'a':
121			check_all = 1;
122			break;
123		case 'l':
124			__check_rhosts_file = 0;
125			break;
126		case 'n':
127			keepalive = 0;
128			break;
129#ifdef	KERBEROS
130		case 'k':
131			use_kerberos = 1;
132			break;
133
134		case 'v':
135			vacuous = 1;
136			break;
137
138#ifdef CRYPT
139		case 'x':
140			doencrypt = 1;
141			break;
142#endif
143#endif
144		case 'L':
145			log_success = 1;
146			break;
147		case '?':
148		default:
149			usage();
150			break;
151		}
152
153	argc -= optind;
154	argv += optind;
155
156#ifdef	KERBEROS
157	if (use_kerberos && vacuous) {
158		syslog(LOG_ERR, "only one of -k and -v allowed");
159		exit(2);
160	}
161#ifdef CRYPT
162	if (doencrypt && !use_kerberos) {
163		syslog(LOG_ERR, "-k is required for -x");
164		exit(2);
165	}
166#endif
167#endif
168
169	fromlen = sizeof (from);
170	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
171		syslog(LOG_ERR, "getpeername: %m");
172		_exit(1);
173	}
174	if (keepalive &&
175	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
176	    sizeof(on)) < 0)
177		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
178	linger.l_onoff = 1;
179	linger.l_linger = 60;			/* XXX */
180	if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
181	    sizeof (linger)) < 0)
182		syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
183	doit(&from);
184	/* NOTREACHED */
185	return(0);
186}
187
188char	username[20] = "USER=";
189char	homedir[64] = "HOME=";
190char	shell[64] = "SHELL=";
191char	path[100] = "PATH=";
192char	*envinit[] =
193	    {homedir, shell, path, username, 0};
194char	**environ;
195
196void
197doit(fromp)
198	struct sockaddr_in *fromp;
199{
200	extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c. */
201	struct hostent *hp;
202	struct passwd *pwd;
203	u_short port;
204	fd_set ready, readfrom;
205	int cc, nfd, pv[2], pid, s;
206	int one = 1;
207	char *hostname, *errorstr, *errorhost;
208	char *cp, sig, buf[BUFSIZ];
209	char cmdbuf[NCARGS+1], locuser[16], remuser[16];
210	char remotehost[2 * MAXHOSTNAMELEN + 1];
211	char fromhost[2 * MAXHOSTNAMELEN + 1];
212#ifdef	LOGIN_CAP
213	login_cap_t *lc;
214#endif
215
216#ifdef	KERBEROS
217	AUTH_DAT	*kdata = (AUTH_DAT *) NULL;
218	KTEXT		ticket = (KTEXT) NULL;
219	char		instance[INST_SZ], version[VERSION_SIZE];
220	struct		sockaddr_in	fromaddr;
221	int		rc;
222	long		authopts;
223	int		pv1[2], pv2[2];
224	fd_set		wready, writeto;
225
226	fromaddr = *fromp;
227#endif
228
229	(void) signal(SIGINT, SIG_DFL);
230	(void) signal(SIGQUIT, SIG_DFL);
231	(void) signal(SIGTERM, SIG_DFL);
232#ifdef DEBUG
233	{ int t = open(_PATH_TTY, 2);
234	  if (t >= 0) {
235		ioctl(t, TIOCNOTTY, (char *)0);
236		(void) close(t);
237	  }
238	}
239#endif
240	fromp->sin_port = ntohs((u_short)fromp->sin_port);
241	if (fromp->sin_family != AF_INET) {
242		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n",
243		    fromp->sin_family);
244		exit(1);
245	}
246#ifdef IP_OPTIONS
247      {
248	u_char optbuf[BUFSIZ/3];
249	int optsize = sizeof(optbuf), ipproto, i;
250	struct protoent *ip;
251
252	if ((ip = getprotobyname("ip")) != NULL)
253		ipproto = ip->p_proto;
254	else
255		ipproto = IPPROTO_IP;
256	if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
257	    optsize != 0) {
258		for (i = 0; i < optsize; ) {
259			u_char c = optbuf[i];
260			if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
261				syslog(LOG_NOTICE,
262					"Connection refused from %s with IP option %s",
263					inet_ntoa(fromp->sin_addr),
264					c == IPOPT_LSRR ? "LSRR" : "SSRR");
265				exit(1);
266			}
267			if (c == IPOPT_EOL)
268				break;
269			i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
270		}
271	}
272      }
273#endif
274
275#ifdef	KERBEROS
276	if (!use_kerberos)
277#endif
278		if (fromp->sin_port >= IPPORT_RESERVED ||
279		    fromp->sin_port < IPPORT_RESERVED/2) {
280			syslog(LOG_NOTICE|LOG_AUTH,
281			    "Connection from %s on illegal port %u",
282			    inet_ntoa(fromp->sin_addr),
283			    fromp->sin_port);
284			exit(1);
285		}
286
287	(void) alarm(60);
288	port = 0;
289	for (;;) {
290		char c;
291		if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
292			if (cc < 0)
293				syslog(LOG_NOTICE, "read: %m");
294			shutdown(0, 1+1);
295			exit(1);
296		}
297		if (c== 0)
298			break;
299		port = port * 10 + c - '0';
300	}
301
302	(void) alarm(0);
303	if (port != 0) {
304		int lport = IPPORT_RESERVED - 1;
305		s = rresvport(&lport);
306		if (s < 0) {
307			syslog(LOG_ERR, "can't get stderr port: %m");
308			exit(1);
309		}
310#ifdef	KERBEROS
311		if (!use_kerberos)
312#endif
313			if (port >= IPPORT_RESERVED ||
314			    port < IPPORT_RESERVED/2) {
315				syslog(LOG_NOTICE|LOG_AUTH,
316				    "2nd socket from %s on unreserved port %u",
317				    inet_ntoa(fromp->sin_addr),
318				    port);
319				exit(1);
320			}
321		fromp->sin_port = htons(port);
322		if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
323			syslog(LOG_INFO, "connect second port %d: %m", port);
324			exit(1);
325		}
326	}
327
328#ifdef	KERBEROS
329	if (vacuous) {
330		error("rshd: remote host requires Kerberos authentication\n");
331		exit(1);
332	}
333#endif
334
335#ifdef notdef
336	/* from inetd, socket is already on 0, 1, 2 */
337	dup2(f, 0);
338	dup2(f, 1);
339	dup2(f, 2);
340#endif
341	errorstr = NULL;
342	hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
343		fromp->sin_family);
344	if (hp) {
345		/*
346		 * If name returned by gethostbyaddr is in our domain,
347		 * attempt to verify that we haven't been fooled by someone
348		 * in a remote net; look up the name and check that this
349		 * address corresponds to the name.
350		 */
351		strncpy(fromhost, hp->h_name, sizeof(fromhost) - 1);
352		fromhost[sizeof(fromhost) - 1] = 0;
353		hostname = fromhost;
354#ifdef	KERBEROS
355		if (!use_kerberos)
356#endif
357		if (check_all || local_domain(hp->h_name)) {
358			strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
359			remotehost[sizeof(remotehost) - 1] = 0;
360			errorhost = remotehost;
361			hp = gethostbyname(remotehost);
362			if (hp == NULL) {
363				syslog(LOG_INFO,
364				    "Couldn't look up address for %s",
365				    remotehost);
366				errorstr =
367				"Couldn't look up address for your host (%s)\n";
368				strncpy(fromhost, inet_ntoa(fromp->sin_addr),
369					sizeof(fromhost) - 1);
370				fromhost[sizeof(fromhost) - 1] = 0;
371				hostname = fromhost;
372			} else for (; ; hp->h_addr_list++) {
373				if (hp->h_addr_list[0] == NULL) {
374					syslog(LOG_NOTICE,
375					  "Host addr %s not listed for host %s",
376					    inet_ntoa(fromp->sin_addr),
377					    hp->h_name);
378					errorstr =
379					    "Host address mismatch for %s\n";
380					strncpy(fromhost, inet_ntoa(fromp->sin_addr),
381						sizeof(fromhost) - 1);
382					fromhost[sizeof(fromhost) - 1] = 0;
383					hostname = fromhost;
384					break;
385				}
386				if (!bcmp(hp->h_addr_list[0],
387				    (caddr_t)&fromp->sin_addr,
388				    sizeof(fromp->sin_addr))) {
389					hostname = remotehost;
390					break;
391				}
392			}
393		}
394	} else {
395		strncpy(fromhost, inet_ntoa(fromp->sin_addr),
396			sizeof(fromhost) - 1);
397		fromhost[sizeof(fromhost) - 1] = 0;
398		errorhost = hostname = fromhost;
399	}
400
401#ifdef	KERBEROS
402	if (use_kerberos) {
403		kdata = (AUTH_DAT *) authbuf;
404		ticket = (KTEXT) tickbuf;
405		authopts = 0L;
406		strcpy(instance, "*");
407		version[VERSION_SIZE - 1] = '\0';
408#ifdef CRYPT
409		if (doencrypt) {
410			struct sockaddr_in local_addr;
411			rc = sizeof(local_addr);
412			if (getsockname(0, (struct sockaddr *)&local_addr,
413			    &rc) < 0) {
414				syslog(LOG_ERR, "getsockname: %m");
415				error("rlogind: getsockname: %m");
416				exit(1);
417			}
418			authopts = KOPT_DO_MUTUAL;
419			rc = krb_recvauth(authopts, 0, ticket,
420				"rcmd", instance, &fromaddr,
421				&local_addr, kdata, "", schedule,
422				version);
423			des_set_key(&kdata->session, schedule);
424		} else
425#endif
426			rc = krb_recvauth(authopts, 0, ticket, "rcmd",
427				instance, &fromaddr,
428				(struct sockaddr_in *) 0,
429				kdata, "", NULL, version);
430		if (rc != KSUCCESS) {
431			error("Kerberos authentication failure: %s\n",
432				  krb_err_txt[rc]);
433			exit(1);
434		}
435	} else
436#endif
437		getstr(remuser, sizeof(remuser), "remuser");
438
439	getstr(locuser, sizeof(locuser), "locuser");
440	getstr(cmdbuf, sizeof(cmdbuf), "command");
441	setpwent();
442	pwd = getpwnam(locuser);
443	if (pwd == NULL) {
444		syslog(LOG_INFO|LOG_AUTH,
445		    "%s@%s as %s: unknown login. cmd='%.80s'",
446		    remuser, hostname, locuser, cmdbuf);
447		if (errorstr == NULL)
448			errorstr = "Login incorrect.\n";
449		goto fail;
450	}
451#ifdef	LOGIN_CAP
452	lc = login_getpwclass(pwd);
453#endif
454	if (chdir(pwd->pw_dir) < 0) {
455#ifdef	LOGIN_CAP
456		if (chdir("/") < 0 ||
457		    login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
458			syslog(LOG_INFO|LOG_AUTH,
459			"%s@%s as %s: no home directory. cmd='%.80s'",
460			remuser, hostname, locuser, cmdbuf);
461			error("No remote home directory.\n");
462			exit(0);
463		}
464#else
465		(void) chdir("/");
466#ifdef notdef
467		syslog(LOG_INFO|LOG_AUTH,
468		    "%s@%s as %s: no home directory. cmd='%.80s'",
469		    remuser, hostname, locuser, cmdbuf);
470		error("No remote directory.\n");
471		exit(1);
472#endif
473#endif
474		pwd->pw_dir = "/";
475	}
476
477#ifdef	KERBEROS
478	if (use_kerberos) {
479		if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
480			if (kuserok(kdata, locuser) != 0) {
481				syslog(LOG_INFO|LOG_AUTH,
482				    "Kerberos rsh denied to %s.%s@%s",
483				    kdata->pname, kdata->pinst, kdata->prealm);
484				error("Login incorrect.\n");
485				exit(1);
486			}
487		}
488	} else
489#endif
490
491		if (errorstr ||
492		    (pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
493		    (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
494		    iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
495		    remuser, locuser) < 0)) {
496			if (__rcmd_errstr)
497				syslog(LOG_INFO|LOG_AUTH,
498			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
499				    remuser, hostname, locuser, __rcmd_errstr,
500				    cmdbuf);
501			else
502				syslog(LOG_INFO|LOG_AUTH,
503			    "%s@%s as %s: permission denied. cmd='%.80s'",
504				    remuser, hostname, locuser, cmdbuf);
505fail:
506			if (errorstr == NULL)
507				errorstr = "Login incorrect.\n";
508			error(errorstr, errorhost);
509			exit(1);
510		}
511
512	if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
513		error("Logins currently disabled.\n");
514		exit(1);
515	}
516#ifdef	LOGIN_CAP
517	if (lc != NULL) {
518		char	remote_ip[MAXHOSTNAMELEN];
519
520		strncpy(remote_ip, inet_ntoa(fromp->sin_addr),
521			sizeof(remote_ip) - 1);
522		remote_ip[sizeof(remote_ip) - 1] = 0;
523		if (!auth_hostok(lc, fromhost, remote_ip)) {
524			syslog(LOG_INFO|LOG_AUTH,
525			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
526			    remuser, hostname, locuser, __rcmd_errstr,
527			    cmdbuf);
528			error("Login incorrect.\n");
529			exit(1);
530		}
531		if (!auth_timeok(lc, time(NULL))) {
532			error("Logins not available right now\n");
533			exit(1);
534		}
535	}
536#endif	/* !LOGIN_CAP */
537#if	BSD > 43
538	/* before fork, while we're session leader */
539	if (setlogin(pwd->pw_name) < 0)
540		syslog(LOG_ERR, "setlogin() failed: %m");
541#endif
542
543	(void) write(STDERR_FILENO, "\0", 1);
544	sent_null = 1;
545
546	if (port) {
547		if (pipe(pv) < 0) {
548			error("Can't make pipe.\n");
549			exit(1);
550		}
551#ifdef CRYPT
552#ifdef KERBEROS
553		if (doencrypt) {
554			if (pipe(pv1) < 0) {
555				error("Can't make 2nd pipe.\n");
556				exit(1);
557			}
558			if (pipe(pv2) < 0) {
559				error("Can't make 3rd pipe.\n");
560				exit(1);
561			}
562		}
563#endif
564#endif
565		pid = fork();
566		if (pid == -1)  {
567			error("Can't fork; try again.\n");
568			exit(1);
569		}
570		if (pid) {
571#ifdef CRYPT
572#ifdef KERBEROS
573			if (doencrypt) {
574				static char msg[] = SECURE_MESSAGE;
575				(void) close(pv1[1]);
576				(void) close(pv2[1]);
577				des_enc_write(s, msg, sizeof(msg) - 1,
578					schedule, &kdata->session);
579
580			} else
581#endif
582#endif
583			{
584				(void) close(0);
585				(void) close(1);
586			}
587			(void) close(2);
588			(void) close(pv[1]);
589
590			FD_ZERO(&readfrom);
591			FD_SET(s, &readfrom);
592			FD_SET(pv[0], &readfrom);
593			if (pv[0] > s)
594				nfd = pv[0];
595			else
596				nfd = s;
597#ifdef CRYPT
598#ifdef KERBEROS
599			if (doencrypt) {
600				FD_ZERO(&writeto);
601				FD_SET(pv2[0], &writeto);
602				FD_SET(pv1[0], &readfrom);
603
604				nfd = MAX(nfd, pv2[0]);
605				nfd = MAX(nfd, pv1[0]);
606			} else
607#endif
608#endif
609				ioctl(pv[0], FIONBIO, (char *)&one);
610
611			/* should set s nbio! */
612			nfd++;
613			do {
614				ready = readfrom;
615#ifdef CRYPT
616#ifdef KERBEROS
617				if (doencrypt) {
618					wready = writeto;
619					if (select(nfd, &ready,
620					    &wready, (fd_set *) 0,
621					    (struct timeval *) 0) < 0)
622						break;
623				} else
624#endif
625#endif
626					if (select(nfd, &ready, (fd_set *)0,
627					  (fd_set *)0, (struct timeval *)0) < 0)
628						break;
629				if (FD_ISSET(s, &ready)) {
630					int	ret;
631#ifdef CRYPT
632#ifdef KERBEROS
633					if (doencrypt)
634						ret = des_enc_read(s, &sig, 1,
635						schedule, &kdata->session);
636					else
637#endif
638#endif
639						ret = read(s, &sig, 1);
640					if (ret <= 0)
641						FD_CLR(s, &readfrom);
642					else
643						killpg(pid, sig);
644				}
645				if (FD_ISSET(pv[0], &ready)) {
646					errno = 0;
647					cc = read(pv[0], buf, sizeof(buf));
648					if (cc <= 0) {
649						shutdown(s, 1+1);
650						FD_CLR(pv[0], &readfrom);
651					} else {
652#ifdef CRYPT
653#ifdef KERBEROS
654						if (doencrypt)
655							(void)
656							  des_enc_write(s, buf, cc,
657								schedule, &kdata->session);
658						else
659#endif
660#endif
661							(void)
662							  write(s, buf, cc);
663					}
664				}
665#ifdef CRYPT
666#ifdef KERBEROS
667				if (doencrypt && FD_ISSET(pv1[0], &ready)) {
668					errno = 0;
669					cc = read(pv1[0], buf, sizeof(buf));
670					if (cc <= 0) {
671						shutdown(pv1[0], 1+1);
672						FD_CLR(pv1[0], &readfrom);
673					} else
674						(void) des_enc_write(STDOUT_FILENO,
675						    buf, cc,
676							schedule, &kdata->session);
677				}
678
679				if (doencrypt && FD_ISSET(pv2[0], &wready)) {
680					errno = 0;
681					cc = des_enc_read(STDIN_FILENO,
682					    buf, sizeof(buf),
683						schedule, &kdata->session);
684					if (cc <= 0) {
685						shutdown(pv2[0], 1+1);
686						FD_CLR(pv2[0], &writeto);
687					} else
688						(void) write(pv2[0], buf, cc);
689				}
690#endif
691#endif
692
693			} while (FD_ISSET(s, &readfrom) ||
694#ifdef CRYPT
695#ifdef KERBEROS
696			    (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
697#endif
698#endif
699			    FD_ISSET(pv[0], &readfrom));
700			exit(0);
701		}
702		setpgrp(0, getpid());
703		(void) close(s);
704		(void) close(pv[0]);
705#ifdef CRYPT
706#ifdef KERBEROS
707		if (doencrypt) {
708			close(pv1[0]); close(pv2[0]);
709			dup2(pv1[1], 1);
710			dup2(pv2[1], 0);
711			close(pv1[1]);
712			close(pv2[1]);
713		}
714#endif
715#endif
716		dup2(pv[1], 2);
717		close(pv[1]);
718	}
719	if (*pwd->pw_shell == '\0')
720		pwd->pw_shell = _PATH_BSHELL;
721	environ = envinit;
722	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
723	strcat(path, _PATH_DEFPATH);
724	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
725	strncat(username, pwd->pw_name, sizeof(username)-6);
726	cp = strrchr(pwd->pw_shell, '/');
727	if (cp)
728		cp++;
729	else
730		cp = pwd->pw_shell;
731#ifdef	LOGIN_CAP
732	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
733                syslog(LOG_ERR, "setusercontext: %m");
734		exit(1);
735	}
736	login_close(lc);
737#else
738	(void) setgid((gid_t)pwd->pw_gid);
739	initgroups(pwd->pw_name, pwd->pw_gid);
740	(void) setuid((uid_t)pwd->pw_uid);
741#endif
742	endpwent();
743	if (log_success || pwd->pw_uid == 0) {
744#ifdef	KERBEROS
745		if (use_kerberos)
746		    syslog(LOG_INFO|LOG_AUTH,
747			"Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'",
748			kdata->pname, kdata->pinst, kdata->prealm,
749			hostname, locuser, cmdbuf);
750		else
751#endif
752		    syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
753			remuser, hostname, locuser, cmdbuf);
754	}
755	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
756	perror(pwd->pw_shell);
757	exit(1);
758}
759
760/*
761 * Report error to client.  Note: can't be used until second socket has
762 * connected to client, or older clients will hang waiting for that
763 * connection first.
764 */
765#if __STDC__
766#include <stdarg.h>
767#else
768#include <varargs.h>
769#endif
770
771void
772#if __STDC__
773error(const char *fmt, ...)
774#else
775error(fmt, va_alist)
776	char *fmt;
777        va_dcl
778#endif
779{
780	va_list ap;
781	int len;
782	char *bp, buf[BUFSIZ];
783#if __STDC__
784	va_start(ap, fmt);
785#else
786	va_start(ap);
787#endif
788	bp = buf;
789	if (sent_null == 0) {
790		*bp++ = 1;
791		len = 1;
792	} else
793		len = 0;
794	(void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
795	(void)write(STDERR_FILENO, buf, len + strlen(bp));
796}
797
798void
799getstr(buf, cnt, err)
800	char *buf, *err;
801	int cnt;
802{
803	char c;
804
805	do {
806		if (read(STDIN_FILENO, &c, 1) != 1)
807			exit(1);
808		*buf++ = c;
809		if (--cnt == 0) {
810			error("%s too long\n", err);
811			exit(1);
812		}
813	} while (c != 0);
814}
815
816/*
817 * Check whether host h is in our local domain,
818 * defined as sharing the last two components of the domain part,
819 * or the entire domain part if the local domain has only one component.
820 * If either name is unqualified (contains no '.'),
821 * assume that the host is local, as it will be
822 * interpreted as such.
823 */
824int
825local_domain(h)
826	char *h;
827{
828	char localhost[MAXHOSTNAMELEN];
829	char *p1, *p2;
830
831	localhost[0] = 0;
832	(void) gethostname(localhost, sizeof(localhost));
833	p1 = topdomain(localhost);
834	p2 = topdomain(h);
835	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
836		return (1);
837	return (0);
838}
839
840char *
841topdomain(h)
842	char *h;
843{
844	char *p, *maybe = NULL;
845	int dots = 0;
846
847	for (p = h + strlen(h); p >= h; p--) {
848		if (*p == '.') {
849			if (++dots == 2)
850				return (p);
851			maybe = p;
852		}
853	}
854	return (maybe);
855}
856
857void
858usage()
859{
860
861	syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
862	exit(2);
863}
864