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