xref: /netbsd-src/lib/libc/net/rcmd.c (revision 2c89343c25e923f229efab99a5c73bb129bcacfc)
1 /*	$NetBSD: rcmd.c,v 1.28 1998/11/13 15:46:56 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Matthew R. Green.
5  * Copyright (c) 1983, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)rcmd.c	8.3 (Berkeley) 3/26/94";
41 #else
42 __RCSID("$NetBSD: rcmd.c,v 1.28 1998/11/13 15:46:56 christos Exp $");
43 #endif
44 #endif /* LIBC_SCCS and not lint */
45 
46 #include "namespace.h"
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <sys/stat.h>
50 #include <sys/poll.h>
51 #include <sys/wait.h>
52 
53 #include <netinet/in.h>
54 #include <rpc/rpc.h>
55 #include <arpa/inet.h>
56 #include <netgroup.h>
57 
58 #include <signal.h>
59 #include <fcntl.h>
60 #include <netdb.h>
61 #include <unistd.h>
62 #include <pwd.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <ctype.h>
66 #include <string.h>
67 #include <syslog.h>
68 #include <stdlib.h>
69 #include <paths.h>
70 #include <err.h>
71 
72 #include "pathnames.h"
73 
74 int	orcmd __P((char **, u_int, const char *, const char *, const char *,
75 	    int *));
76 int	__ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
77 static	int rshrcmd __P((char **, u_int32_t, const char *, const char *,
78 	    const char *, int *, const char *));
79 static	int hprcmd __P((struct hostent *, char **, u_int32_t, const char *,
80 	    const char *, const char *, int *));
81 static	int __icheckhost __P((u_int32_t, const char *));
82 static	char *__gethostloop __P((u_int32_t));
83 
84 int
85 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
86 	char **ahost;
87 	u_short rport;
88 	const char *locuser, *remuser, *cmd;
89 	int *fd2p;
90 {
91 	struct hostent *hp;
92 	struct servent *sp;
93 
94 	/*
95 	 * Canonicalise hostname.
96 	 * XXX: Should we really do this?
97 	 */
98 	hp = gethostbyname(*ahost);
99 	if (hp == NULL) {
100 		herror(*ahost);
101 		return (-1);
102 	}
103 	*ahost = hp->h_name;
104 
105 	/*
106 	 * Check if rport is the same as the shell port, and that the fd2p.  If
107 	 * it is not, the program isn't expecting 'rsh' and so we can't use the
108 	 * RCMD_CMD environment.
109 	 */
110 	sp = getservbyname("shell", "tcp");
111 	if (sp != NULL && sp->s_port == rport)
112 		return (rshrcmd(ahost, rport, locuser, remuser, cmd, fd2p,
113 		    getenv("RCMD_CMD")));
114 	else
115 		return (hprcmd(hp, ahost, rport, locuser, remuser, cmd, fd2p));
116 }
117 
118 /* this is simply a wrapper around hprcmd() that handles ahost first */
119 int
120 orcmd(ahost, rport, locuser, remuser, cmd, fd2p)
121 	char **ahost;
122 	u_int rport;
123 	const char *locuser, *remuser, *cmd;
124 	int *fd2p;
125 {
126 	struct hostent *hp;
127 
128 	hp = gethostbyname(*ahost);
129 	if (hp == NULL) {
130 		herror(*ahost);
131 		return (-1);
132 	}
133 	*ahost = hp->h_name;
134 
135 	return (hprcmd(hp, ahost, rport, locuser, remuser, cmd, fd2p));
136 }
137 
138 static int
139 hprcmd(hp, ahost, rport, locuser, remuser, cmd, fd2p)
140 	struct hostent *hp;
141 	char **ahost;
142 	u_int32_t rport;
143 	const char *locuser, *remuser, *cmd;
144 	int *fd2p;
145 {
146 	struct sockaddr_in sin, from;
147 	struct pollfd reads[2];
148 	sigset_t nmask, omask;
149 	pid_t pid;
150 	int s, lport, timo;
151 	int pollr;
152 	char c;
153 
154 	pid = getpid();
155 	sigemptyset(&nmask);
156 	sigaddset(&nmask, SIGURG);
157 	if (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)
158 		return -1;
159 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
160 		s = rresvport(&lport);
161 		if (s < 0) {
162 			if (errno == EAGAIN)
163 				warnx("rcmd: socket: All ports in use");
164 			else
165 				warn("rcmd: socket");
166 			(void)sigprocmask(SIG_SETMASK, &omask, NULL);
167 			return (-1);
168 		}
169 		fcntl(s, F_SETOWN, pid);
170 #ifdef BSD4_4
171 		sin.sin_len = sizeof(struct sockaddr_in);
172 #endif
173 		sin.sin_family = hp->h_addrtype;
174 		sin.sin_port = rport;
175 		memmove(&sin.sin_addr,
176 		    hp->h_addr_list[0], (size_t)hp->h_length);
177 		if (connect(s, (struct sockaddr *)(void *)&sin, sizeof(sin)) >= 0)
178 			break;
179 		(void)close(s);
180 		if (errno == EADDRINUSE) {
181 			lport--;
182 			continue;
183 		}
184 		if (errno == ECONNREFUSED && timo <= 16) {
185 			(void)sleep((unsigned int)timo);
186 			timo *= 2;
187 			continue;
188 		}
189 		if (hp->h_addr_list[1] != NULL) {
190 			int oerrno = errno;
191 
192 			warnx("rcmd: connect to address %s",
193 			    inet_ntoa(sin.sin_addr));
194 			errno = oerrno;
195 			perror(0);
196 			hp->h_addr_list++;
197 			memmove(&sin.sin_addr, hp->h_addr_list[0],
198 			    (size_t)hp->h_length);
199 			(void)fprintf(stderr, "Trying %s...\n",
200 			    inet_ntoa(sin.sin_addr));
201 			continue;
202 		}
203 		(void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
204 		(void)sigprocmask(SIG_SETMASK, &omask, NULL);
205 		return (-1);
206 	}
207 	lport--;
208 	if (fd2p == 0) {
209 		write(s, "", 1);
210 		lport = 0;
211 	} else {
212 		char num[8];
213 		int s2 = rresvport(&lport), s3;
214 		int len = sizeof(from);
215 
216 		if (s2 < 0)
217 			goto bad;
218 		listen(s2, 1);
219 		(void)snprintf(num, sizeof(num), "%d", lport);
220 		if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
221 			warn("rcmd: write (setting up stderr)");
222 			(void)close(s2);
223 			goto bad;
224 		}
225 		reads[0].fd = s;
226 		reads[0].events = POLLIN;
227 		reads[1].fd = s2;
228 		reads[1].events = POLLIN;
229 		errno = 0;
230 		pollr = poll(reads, 2, INFTIM);
231 		if (pollr < 1 || (reads[1].revents & POLLIN) == 0) {
232 			if (errno != 0)
233 				warn("poll: setting up stderr");
234 			else
235 				warnx("poll: protocol failure in circuit setup");
236 			(void)close(s2);
237 			goto bad;
238 		}
239 		s3 = accept(s2, (struct sockaddr *)(void *)&from, &len);
240 		(void)close(s2);
241 		if (s3 < 0) {
242 			warn("rcmd: accept");
243 			lport = 0;
244 			goto bad;
245 		}
246 		*fd2p = s3;
247 		from.sin_port = ntohs(from.sin_port);
248 		if (from.sin_family != AF_INET ||
249 		    from.sin_port >= IPPORT_RESERVED ||
250 		    from.sin_port < IPPORT_RESERVED / 2) {
251 			warnx("rcmd: protocol failure in circuit setup.");
252 			goto bad2;
253 		}
254 	}
255 
256 	(void)write(s, locuser, strlen(locuser)+1);
257 	(void)write(s, remuser, strlen(remuser)+1);
258 	(void)write(s, cmd, strlen(cmd)+1);
259 	if (read(s, &c, 1) != 1) {
260 		warn("%s", *ahost);
261 		goto bad2;
262 	}
263 	if (c != 0) {
264 		while (read(s, &c, 1) == 1) {
265 			(void)write(STDERR_FILENO, &c, 1);
266 			if (c == '\n')
267 				break;
268 		}
269 		goto bad2;
270 	}
271 	(void)sigprocmask(SIG_SETMASK, &omask, NULL);
272 	return (s);
273 bad2:
274 	if (lport)
275 		(void)close(*fd2p);
276 bad:
277 	(void)close(s);
278 	(void)sigprocmask(SIG_SETMASK, &omask, NULL);
279 	return (-1);
280 }
281 
282 /*
283  * based on code written by Chris Siebenmann <cks@utcc.utoronto.ca>
284  */
285 /* ARGSUSED */
286 static int
287 rshrcmd(ahost, rport, locuser, remuser, cmd, fd2p, rshcmd)
288 	char  	**ahost;
289 	u_int32_t	rport;
290 	const	char *locuser, *remuser, *cmd;
291 	int	*fd2p;
292 	const	char *rshcmd;
293 {
294 	pid_t pid;
295 	int sp[2], ep[2];
296 	char *p;
297 	struct passwd *pw;
298 
299 	/* What rsh/shell to use. */
300 	if (rshcmd == NULL)
301 		rshcmd = _PATH_BIN_RCMD;
302 
303 	/* locuser must exist on this host. */
304 	if ((pw = getpwnam(locuser)) == NULL) {
305 		warnx("rshrcmd: unknown user: %s", locuser);
306 		return(-1);
307 	}
308 
309 	/* get a socketpair we'll use for stdin and stdout. */
310 	if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sp) < 0) {
311 		warn("rshrcmd: socketpair");
312 		return (-1);
313 	}
314 	/* we will use this for the fd2 pointer */
315 	if (fd2p) {
316 		if (socketpair(AF_LOCAL, SOCK_STREAM, 0, ep) < 0) {
317 			warn("rshrcmd: socketpair");
318 			return (-1);
319 		}
320 		*fd2p = ep[0];
321 	}
322 
323 	pid = fork();
324 	if (pid < 0) {
325 		warn("rshrcmd: fork");
326 		return (-1);
327 	}
328 	if (pid == 0) {
329 		/*
330 		 * child
331 		 * - we use sp[1] to be stdin/stdout, and close sp[0]
332 		 * - with fd2p, we use ep[1] for stderr, and close ep[0]
333 		 */
334 		(void)close(sp[0]);
335 		if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) {
336 			warn("rshrcmd: dup2");
337 			_exit(1);
338 		}
339 		if (fd2p) {
340 			if (dup2(ep[1], 2) < 0) {
341 				warn("rshrcmd: dup2");
342 				_exit(1);
343 			}
344 			(void)close(ep[0]);
345 			(void)close(ep[1]);
346 		} else if (dup2(0, 2) < 0) {
347 			warn("rshrcmd: dup2");
348 			_exit(1);
349 		}
350 		/* fork again to lose parent. */
351 		pid = fork();
352 		if (pid < 0) {
353 			warn("rshrcmd: second fork");
354 			_exit(1);
355 		}
356 		if (pid > 0)
357 			_exit(0);
358 
359 		/* Orphan.  Become local user for rshprog. */
360 		if (setuid(pw->pw_uid)) {
361 			warn("rshrcmd: setuid(%u)", pw->pw_uid);
362 			_exit(1);
363 		}
364 
365 		/*
366 		 * If we are rcmd'ing to "localhost" as the same user as we are,
367 		 * then avoid running remote shell for efficiency.
368 		 */
369 		if (strcmp(*ahost, "localhost") == 0 &&
370 		    strcmp(locuser, remuser) == 0) {
371 			if (pw->pw_shell[0] == '\0')
372 				rshcmd = _PATH_BSHELL;
373 			else
374 				rshcmd = pw->pw_shell;
375 			p = strrchr(rshcmd, '/');
376 			execlp(rshcmd, p ? p + 1 : rshcmd, "-c", cmd, NULL);
377 		} else {
378 			p = strrchr(rshcmd, '/');
379 			execlp(rshcmd, p ? p + 1 : rshcmd, *ahost, "-l",
380 			    remuser, cmd, NULL);
381 		}
382 		warn("rshrcmd: exec %s", rshcmd);
383 		_exit(1);
384 	}
385 	/* Parent */
386 	(void)close(sp[1]);
387 	if (fd2p)
388 		(void)close(ep[1]);
389 
390 	(void)waitpid(pid, NULL, 0);
391 	return (sp[0]);
392 }
393 
394 int
395 rresvport(alport)
396 	int *alport;
397 {
398 	struct sockaddr_in sin;
399 	int s;
400 
401 #ifdef BSD4_4
402 	sin.sin_len = sizeof(struct sockaddr_in);
403 #endif
404 	sin.sin_family = AF_INET;
405 	sin.sin_addr.s_addr = INADDR_ANY;
406 	s = socket(AF_INET, SOCK_STREAM, 0);
407 	if (s < 0)
408 		return (-1);
409 #if 0
410 	for (;;) {
411 		sin.sin_port = htons((u_short)*alport);
412 		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
413 			return (s);
414 		if (errno != EADDRINUSE) {
415 			(void)close(s);
416 			return (-1);
417 		}
418 		(*alport)--;
419 		if (*alport == IPPORT_RESERVED/2) {
420 			(void)close(s);
421 			errno = EAGAIN;		/* close */
422 			return (-1);
423 		}
424 	}
425 #else
426 	sin.sin_port = 0;
427 	if (bindresvport(s, &sin) < 0) {
428 		int sverr = errno;
429 
430 		(void)close(s);
431 		errno = sverr;
432 		return (-1);
433 	}
434 	*alport = (int)ntohs(sin.sin_port);
435 	return (s);
436 #endif
437 }
438 
439 int	__check_rhosts_file = 1;
440 char	*__rcmd_errstr;
441 
442 int
443 ruserok(rhost, superuser, ruser, luser)
444 	const char *rhost, *ruser, *luser;
445 	int superuser;
446 {
447 	struct hostent *hp;
448 	char **ap;
449 	int i;
450 #define MAXADDRS	35
451 	u_int32_t addrs[MAXADDRS + 1];
452 
453 	if ((hp = gethostbyname(rhost)) == NULL)
454 		return (-1);
455 	for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i)
456 		memmove(&addrs[i], *ap, sizeof(addrs[i]));
457 	addrs[i] = 0;
458 
459 	for (i = 0; i < MAXADDRS && addrs[i]; i++)
460 		if (iruserok(addrs[i], superuser, ruser, luser) == 0)
461 			return (0);
462 	return (-1);
463 }
464 
465 /*
466  * New .rhosts strategy: We are passed an ip address. We spin through
467  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
468  * has ip addresses, we don't have to trust a nameserver.  When it
469  * contains hostnames, we spin through the list of addresses the nameserver
470  * gives us and look for a match.
471  *
472  * Returns 0 if ok, -1 if not ok.
473  */
474 int
475 iruserok(raddr, superuser, ruser, luser)
476 	u_int32_t raddr;
477 	int superuser;
478 	const char *ruser, *luser;
479 {
480 	register char *cp;
481 	struct stat sbuf;
482 	struct passwd *pwd;
483 	FILE *hostf;
484 	uid_t uid;
485 	gid_t gid;
486 	int first;
487 	char pbuf[MAXPATHLEN];
488 
489 	first = 1;
490 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
491 again:
492 	if (hostf) {
493 		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
494 			(void)fclose(hostf);
495 			return (0);
496 		}
497 		(void)fclose(hostf);
498 	}
499 	if (first == 1 && (__check_rhosts_file || superuser)) {
500 		first = 0;
501 		if ((pwd = getpwnam(luser)) == NULL)
502 			return (-1);
503 		(void)strncpy(pbuf, pwd->pw_dir, sizeof(pbuf) - 1);
504 		(void)strncat(pbuf, "/.rhosts", sizeof(pbuf) - strlen(pbuf) - 1);
505 
506 		/*
507 		 * Change effective uid while opening .rhosts.  If root and
508 		 * reading an NFS mounted file system, can't read files that
509 		 * are protected read/write owner only.
510 		 */
511 		uid = geteuid();
512 		gid = getegid();
513 		(void)setegid(pwd->pw_gid);
514 		initgroups(pwd->pw_name, pwd->pw_gid);
515 		(void)seteuid(pwd->pw_uid);
516 		hostf = fopen(pbuf, "r");
517 		(void)seteuid(uid);
518 		(void)setegid(gid);
519 
520 		if (hostf == NULL)
521 			return (-1);
522 		/*
523 		 * If not a regular file, or is owned by someone other than
524 		 * user or root or if writeable by anyone but the owner, quit.
525 		 */
526 		cp = NULL;
527 		if (lstat(pbuf, &sbuf) < 0)
528 			cp = ".rhosts lstat failed";
529 		else if (!S_ISREG(sbuf.st_mode))
530 			cp = ".rhosts not regular file";
531 		else if (fstat(fileno(hostf), &sbuf) < 0)
532 			cp = ".rhosts fstat failed";
533 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
534 			cp = "bad .rhosts owner";
535 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
536 			cp = ".rhosts writeable by other than owner";
537 		/* If there were any problems, quit. */
538 		if (cp) {
539 			__rcmd_errstr = cp;
540 			(void)fclose(hostf);
541 			return (-1);
542 		}
543 		goto again;
544 	}
545 	return (-1);
546 }
547 
548 /*
549  * XXX
550  * Don't make static, used by lpd(8).
551  *
552  * Returns 0 if ok, -1 if not ok.
553  */
554 int
555 __ivaliduser(hostf, raddr, luser, ruser)
556 	FILE *hostf;
557 	u_int32_t raddr;
558 	const char *luser, *ruser;
559 {
560 	register char *user, *p;
561 	int ch;
562 	char buf[MAXHOSTNAMELEN + 128];		/* host + login */
563 	const char *auser, *ahost;
564 	int hostok, userok;
565 	char *rhost = NULL;
566 	int firsttime = 1;
567 	char domain[MAXHOSTNAMELEN];
568 
569 	getdomainname(domain, sizeof(domain));
570 
571 	while (fgets(buf, sizeof(buf), hostf)) {
572 		p = buf;
573 		/* Skip lines that are too long. */
574 		if (strchr(p, '\n') == NULL) {
575 			while ((ch = getc(hostf)) != '\n' && ch != EOF)
576 				;
577 			continue;
578 		}
579 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
580 			*p = isupper(*p) ? tolower(*p) : *p;
581 			p++;
582 		}
583 		if (*p == ' ' || *p == '\t') {
584 			*p++ = '\0';
585 			while (*p == ' ' || *p == '\t')
586 				p++;
587 			user = p;
588 			while (*p != '\n' && *p != ' ' &&
589 			    *p != '\t' && *p != '\0')
590 				p++;
591 		} else
592 			user = p;
593 		*p = '\0';
594 
595 		if (p == buf)
596 			continue;
597 
598 		auser = *user ? user : luser;
599 		ahost = buf;
600 
601 		if (ahost[0] == '+')
602 			switch (ahost[1]) {
603 			case '\0':
604 				hostok = 1;
605 				break;
606 
607 			case '@':
608 				if (firsttime) {
609 					rhost = __gethostloop(raddr);
610 					firsttime = 0;
611 				}
612 				if (rhost)
613 					hostok = innetgr(&ahost[2], rhost,
614 					    NULL, domain);
615 				else
616 					hostok = 0;
617 				break;
618 
619 			default:
620 				hostok = __icheckhost(raddr, &ahost[1]);
621 				break;
622 			}
623 		else if (ahost[0] == '-')
624 			switch (ahost[1]) {
625 			case '\0':
626 				hostok = -1;
627 				break;
628 
629 			case '@':
630 				if (firsttime) {
631 					rhost = __gethostloop(raddr);
632 					firsttime = 0;
633 				}
634 				if (rhost)
635 					hostok = -innetgr(&ahost[2], rhost,
636 					    NULL, domain);
637 				else
638 					hostok = 0;
639 				break;
640 
641 			default:
642 				hostok = -__icheckhost(raddr, &ahost[1]);
643 				break;
644 			}
645 		else
646 			hostok = __icheckhost(raddr, ahost);
647 
648 
649 		if (auser[0] == '+')
650 			switch (auser[1]) {
651 			case '\0':
652 				userok = 1;
653 				break;
654 
655 			case '@':
656 				userok = innetgr(&auser[2], NULL, ruser,
657 				    domain);
658 				break;
659 
660 			default:
661 				userok = strcmp(ruser, &auser[1]) == 0;
662 				break;
663 			}
664 		else if (auser[0] == '-')
665 			switch (auser[1]) {
666 			case '\0':
667 				userok = -1;
668 				break;
669 
670 			case '@':
671 				userok = -innetgr(&auser[2], NULL, ruser,
672 				    domain);
673 				break;
674 
675 			default:
676 				userok =
677 				    -(strcmp(ruser, &auser[1]) == 0 ? 1 : 0);
678 				break;
679 			}
680 		else
681 			userok = strcmp(ruser, auser) == 0;
682 
683 		/* Check if one component did not match */
684 		if (hostok == 0 || userok == 0)
685 			continue;
686 
687 		/* Check if we got a forbidden pair */
688 		if (userok == -1 || hostok == -1)
689 			return -1;
690 
691 		/* Check if we got a valid pair */
692 		if (hostok == 1 && userok == 1)
693 			return 0;
694 	}
695 	return -1;
696 }
697 
698 /*
699  * Returns "true" if match, 0 if no match.
700  */
701 static int
702 __icheckhost(raddr, lhost)
703 	u_int32_t raddr;
704 	const char *lhost;
705 {
706 	struct hostent *hp;
707 	struct in_addr laddr;
708 	char **pp;
709 
710 	/* Try for raw ip address first. */
711 	if (isdigit(*lhost) && inet_aton(lhost, &laddr) != 0)
712 		return (raddr == laddr.s_addr);
713 
714 	/* Better be a hostname. */
715 	if ((hp = gethostbyname(lhost)) == NULL)
716 		return (0);
717 
718 	/* Spin through ip addresses. */
719 	for (pp = hp->h_addr_list; *pp; ++pp)
720 		if (!memcmp(&raddr, *pp, sizeof(u_int32_t)))
721 			return (1);
722 
723 	/* No match. */
724 	return (0);
725 }
726 
727 /*
728  * Return the hostname associated with the supplied address.
729  * Do a reverse lookup as well for security. If a loop cannot
730  * be found, pack the result of inet_ntoa() into the string.
731  */
732 static char *
733 __gethostloop(raddr)
734 	u_int32_t raddr;
735 {
736 	static char remotehost[MAXHOSTNAMELEN];
737 	struct hostent *hp;
738 	struct in_addr in;
739 
740 	hp = gethostbyaddr((char *)(void *)&raddr, sizeof(raddr), AF_INET);
741 	if (hp == NULL)
742 		return (NULL);
743 
744 	/*
745 	 * Look up the name and check that the supplied
746 	 * address is in the list
747 	 */
748 	strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
749 	remotehost[sizeof(remotehost) - 1] = '\0';
750 	hp = gethostbyname(remotehost);
751 	if (hp == NULL)
752 		return (NULL);
753 
754 	for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++)
755 		if (!memcmp(hp->h_addr_list[0], &raddr, sizeof(raddr)))
756 			return (remotehost);
757 
758 	/*
759 	 * either the DNS adminstrator has made a configuration
760 	 * mistake, or someone has attempted to spoof us
761 	 */
762 	in.s_addr = raddr;
763 	syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s",
764 	    inet_ntoa(in), hp->h_name);
765 	return (NULL);
766 }
767