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