rshd.c revision 56939
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 56939 2000-02-01 15:55:56Z 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_sa(fromp, fromp->su_len, pwd->pw_uid == 0,
412				 remuser, locuser) < 0)) {
413			if (__rcmd_errstr)
414				syslog(LOG_INFO|LOG_AUTH,
415			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
416				    remuser, fromhost, locuser, __rcmd_errstr,
417				    cmdbuf);
418			else
419				syslog(LOG_INFO|LOG_AUTH,
420			    "%s@%s as %s: permission denied. cmd='%.80s'",
421				    remuser, fromhost, locuser, cmdbuf);
422fail:
423			if (errorstr == NULL)
424				errorstr = "Login incorrect.\n";
425			error(errorstr, fromhost);
426			exit(1);
427		}
428
429	if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
430		error("Logins currently disabled.\n");
431		exit(1);
432	}
433#ifdef	LOGIN_CAP
434	if (lc != NULL && fromp->su_family == AF_INET) {	/*XXX*/
435		char	remote_ip[MAXHOSTNAMELEN];
436
437		strncpy(remote_ip, numericname,
438			sizeof(remote_ip) - 1);
439		remote_ip[sizeof(remote_ip) - 1] = 0;
440		if (!auth_hostok(lc, fromhost, remote_ip)) {
441			syslog(LOG_INFO|LOG_AUTH,
442			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
443			    remuser, fromhost, locuser, __rcmd_errstr,
444			    cmdbuf);
445			error("Login incorrect.\n");
446			exit(1);
447		}
448		if (!auth_timeok(lc, time(NULL))) {
449			error("Logins not available right now\n");
450			exit(1);
451		}
452	}
453#endif	/* !LOGIN_CAP */
454#if	BSD > 43
455	/* before fork, while we're session leader */
456	if (setlogin(pwd->pw_name) < 0)
457		syslog(LOG_ERR, "setlogin() failed: %m");
458#endif
459
460	(void) write(STDERR_FILENO, "\0", 1);
461	sent_null = 1;
462
463	if (port) {
464		if (pipe(pv) < 0) {
465			error("Can't make pipe.\n");
466			exit(1);
467		}
468#ifdef CRYPT
469		if (doencrypt) {
470			if (pipe(pv1) < 0) {
471				error("Can't make 2nd pipe.\n");
472				exit(1);
473			}
474			if (pipe(pv2) < 0) {
475				error("Can't make 3rd pipe.\n");
476				exit(1);
477			}
478		}
479#endif
480		pid = fork();
481		if (pid == -1)  {
482			error("Can't fork; try again.\n");
483			exit(1);
484		}
485		if (pid) {
486#ifdef CRYPT
487			if (doencrypt) {
488				static char msg[] = SECURE_MESSAGE;
489				(void) close(pv1[1]);
490				(void) close(pv2[1]);
491				des_enc_write(s, msg, sizeof(msg) - 1,
492					schedule, &kdata->session);
493
494			} else
495#endif
496			{
497				(void) close(0);
498				(void) close(1);
499			}
500			(void) close(2);
501			(void) close(pv[1]);
502
503			FD_ZERO(&readfrom);
504			FD_SET(s, &readfrom);
505			FD_SET(pv[0], &readfrom);
506			if (pv[0] > s)
507				nfd = pv[0];
508			else
509				nfd = s;
510#ifdef CRYPT
511			if (doencrypt) {
512				FD_ZERO(&writeto);
513				FD_SET(pv2[0], &writeto);
514				FD_SET(pv1[0], &readfrom);
515
516				nfd = MAX(nfd, pv2[0]);
517				nfd = MAX(nfd, pv1[0]);
518			} else
519#endif
520				ioctl(pv[0], FIONBIO, (char *)&one);
521
522			/* should set s nbio! */
523			nfd++;
524			do {
525				ready = readfrom;
526#ifdef CRYPT
527				if (doencrypt) {
528					wready = writeto;
529					if (select(nfd, &ready,
530					    &wready, (fd_set *) 0,
531					    (struct timeval *) 0) < 0)
532						break;
533				} else
534#endif
535					if (select(nfd, &ready, (fd_set *)0,
536					  (fd_set *)0, (struct timeval *)0) < 0)
537						break;
538				if (FD_ISSET(s, &ready)) {
539					int	ret;
540#ifdef CRYPT
541					if (doencrypt)
542						ret = des_enc_read(s, &sig, 1,
543						schedule, &kdata->session);
544					else
545#endif
546						ret = read(s, &sig, 1);
547					if (ret <= 0)
548						FD_CLR(s, &readfrom);
549					else
550						killpg(pid, sig);
551				}
552				if (FD_ISSET(pv[0], &ready)) {
553					errno = 0;
554					cc = read(pv[0], buf, sizeof(buf));
555					if (cc <= 0) {
556						shutdown(s, 1+1);
557						FD_CLR(pv[0], &readfrom);
558					} else {
559#ifdef CRYPT
560						if (doencrypt)
561							(void)
562							  des_enc_write(s, buf, cc,
563								schedule, &kdata->session);
564						else
565#endif
566							(void)
567							  write(s, buf, cc);
568					}
569				}
570#ifdef CRYPT
571				if (doencrypt && FD_ISSET(pv1[0], &ready)) {
572					errno = 0;
573					cc = read(pv1[0], buf, sizeof(buf));
574					if (cc <= 0) {
575						shutdown(pv1[0], 1+1);
576						FD_CLR(pv1[0], &readfrom);
577					} else
578						(void) des_enc_write(STDOUT_FILENO,
579						    buf, cc,
580							schedule, &kdata->session);
581				}
582
583				if (doencrypt && FD_ISSET(pv2[0], &wready)) {
584					errno = 0;
585					cc = des_enc_read(STDIN_FILENO,
586					    buf, sizeof(buf),
587						schedule, &kdata->session);
588					if (cc <= 0) {
589						shutdown(pv2[0], 1+1);
590						FD_CLR(pv2[0], &writeto);
591					} else
592						(void) write(pv2[0], buf, cc);
593				}
594#endif
595
596			} while (FD_ISSET(s, &readfrom) ||
597#ifdef CRYPT
598			    (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
599#endif
600			    FD_ISSET(pv[0], &readfrom));
601			exit(0);
602		}
603		setpgrp(0, getpid());
604		(void) close(s);
605		(void) close(pv[0]);
606#ifdef CRYPT
607		if (doencrypt) {
608			close(pv1[0]); close(pv2[0]);
609			dup2(pv1[1], 1);
610			dup2(pv2[1], 0);
611			close(pv1[1]);
612			close(pv2[1]);
613		}
614#endif
615		dup2(pv[1], 2);
616		close(pv[1]);
617	}
618	if (*pwd->pw_shell == '\0')
619		pwd->pw_shell = _PATH_BSHELL;
620	environ = envinit;
621	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
622	strcat(path, _PATH_DEFPATH);
623	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
624	strncat(username, pwd->pw_name, sizeof(username)-6);
625	cp = strrchr(pwd->pw_shell, '/');
626	if (cp)
627		cp++;
628	else
629		cp = pwd->pw_shell;
630#ifdef	LOGIN_CAP
631	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
632                syslog(LOG_ERR, "setusercontext: %m");
633		exit(1);
634	}
635	login_close(lc);
636#else
637	(void) setgid((gid_t)pwd->pw_gid);
638	initgroups(pwd->pw_name, pwd->pw_gid);
639	(void) setuid((uid_t)pwd->pw_uid);
640#endif
641	endpwent();
642	if (log_success || pwd->pw_uid == 0) {
643		    syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
644			remuser, fromhost, locuser, cmdbuf);
645	}
646	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
647	perror(pwd->pw_shell);
648	exit(1);
649}
650
651/*
652 * Report error to client.  Note: can't be used until second socket has
653 * connected to client, or older clients will hang waiting for that
654 * connection first.
655 */
656#if __STDC__
657#include <stdarg.h>
658#else
659#include <varargs.h>
660#endif
661
662void
663#if __STDC__
664error(const char *fmt, ...)
665#else
666error(fmt, va_alist)
667	char *fmt;
668        va_dcl
669#endif
670{
671	va_list ap;
672	int len;
673	char *bp, buf[BUFSIZ];
674#if __STDC__
675	va_start(ap, fmt);
676#else
677	va_start(ap);
678#endif
679	bp = buf;
680	if (sent_null == 0) {
681		*bp++ = 1;
682		len = 1;
683	} else
684		len = 0;
685	(void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
686	(void)write(STDERR_FILENO, buf, len + strlen(bp));
687}
688
689void
690getstr(buf, cnt, err)
691	char *buf, *err;
692	int cnt;
693{
694	char c;
695
696	do {
697		if (read(STDIN_FILENO, &c, 1) != 1)
698			exit(1);
699		*buf++ = c;
700		if (--cnt == 0) {
701			error("%s too long\n", err);
702			exit(1);
703		}
704	} while (c != 0);
705}
706
707/*
708 * Check whether host h is in our local domain,
709 * defined as sharing the last two components of the domain part,
710 * or the entire domain part if the local domain has only one component.
711 * If either name is unqualified (contains no '.'),
712 * assume that the host is local, as it will be
713 * interpreted as such.
714 */
715int
716local_domain(h)
717	char *h;
718{
719	char localhost[MAXHOSTNAMELEN];
720	char *p1, *p2;
721
722	localhost[0] = 0;
723	(void) gethostname(localhost, sizeof(localhost) - 1);
724	localhost[sizeof(localhost) - 1] = '\0';
725	p1 = topdomain(localhost);
726	p2 = topdomain(h);
727	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
728		return (1);
729	return (0);
730}
731
732char *
733topdomain(h)
734	char *h;
735{
736	char *p, *maybe = NULL;
737	int dots = 0;
738
739	for (p = h + strlen(h); p >= h; p--) {
740		if (*p == '.') {
741			if (++dots == 2)
742				return (p);
743			maybe = p;
744		}
745	}
746	return (maybe);
747}
748
749void
750usage()
751{
752
753	syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
754	exit(2);
755}
756