xref: /openbsd-src/lib/libc/net/rcmd.c (revision f8ab9db3505998e113a25f7cb6067d1e7112bbb9)
1 /*
2  * Copyright (c) 1995, 1996, 1998 Theo de Raadt.  All rights reserved.
3  * Copyright (c) 1983, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *rcsid = "$OpenBSD: rcmd.c,v 1.51 2005/03/08 18:34:42 deraadt Exp $";
33 #endif /* LIBC_SCCS and not lint */
34 
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 
42 #include <signal.h>
43 #include <fcntl.h>
44 #include <netdb.h>
45 #include <unistd.h>
46 #include <pwd.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <string.h>
51 #include <syslog.h>
52 #include <stdlib.h>
53 #include <netgroup.h>
54 
55 int	__ivaliduser(FILE *, in_addr_t, const char *, const char *);
56 int	__ivaliduser_sa(FILE *, struct sockaddr *, socklen_t,
57 	    const char *, const char *);
58 static int __icheckhost(struct sockaddr *, socklen_t, const char *);
59 static char *__gethostloop(struct sockaddr *, socklen_t);
60 
61 int
62 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
63 	char **ahost;
64 	in_port_t rport;
65 	const char *locuser, *remuser, *cmd;
66 	int *fd2p;
67 {
68 	return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
69 }
70 
71 int
72 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
73 	char **ahost;
74 	in_port_t rport;
75 	const char *locuser, *remuser, *cmd;
76 	int *fd2p;
77 	int af;
78 {
79 	static char hbuf[MAXHOSTNAMELEN];
80 	char pbuf[NI_MAXSERV];
81 	struct addrinfo hints, *res, *r;
82 	int error;
83 	struct sockaddr_storage from;
84 	fd_set *readsp = NULL;
85 	sigset_t oldmask, mask;
86 	pid_t pid;
87 	int s, lport, timo;
88 	char c, *p;
89 	int refused;
90 
91 	/* call rcmdsh() with specified remote shell if appropriate. */
92 	if (!issetugid() && (p = getenv("RSH")) && *p) {
93 		struct servent *sp = getservbyname("shell", "tcp");
94 
95 		if (sp && sp->s_port == rport)
96 			return (rcmdsh(ahost, rport, locuser, remuser,
97 			    cmd, p));
98 	}
99 
100 	/* use rsh(1) if non-root and remote port is shell. */
101 	if (geteuid()) {
102 		struct servent *sp = getservbyname("shell", "tcp");
103 
104 		if (sp && sp->s_port == rport)
105 			return (rcmdsh(ahost, rport, locuser, remuser,
106 			    cmd, NULL));
107 	}
108 
109 	pid = getpid();
110 	snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport));
111 	memset(&hints, 0, sizeof(hints));
112 	hints.ai_family = af;
113 	hints.ai_socktype = SOCK_STREAM;
114 	hints.ai_flags = AI_CANONNAME;
115 	error = getaddrinfo(*ahost, pbuf, &hints, &res);
116 	if (error) {
117 #if 0
118 		warnx("%s: %s", *ahost, gai_strerror(error));
119 #endif
120 		return (-1);
121 	}
122 	if (res->ai_canonname) {
123 		strlcpy(hbuf, res->ai_canonname, sizeof(hbuf));
124 		*ahost = hbuf;
125 	} else
126 		; /*XXX*/
127 
128 	r = res;
129 	refused = 0;
130 	sigemptyset(&mask);
131 	sigaddset(&mask, SIGURG);
132 	oldmask = sigprocmask(SIG_BLOCK, &mask, &oldmask);
133 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
134 		s = rresvport_af(&lport, r->ai_family);
135 		if (s < 0) {
136 			if (errno == EAGAIN)
137 				(void)fprintf(stderr,
138 				    "rcmd: socket: All ports in use\n");
139 			else
140 				(void)fprintf(stderr, "rcmd: socket: %s\n",
141 				    strerror(errno));
142 			if (r->ai_next) {
143 				r = r->ai_next;
144 				continue;
145 			} else {
146 				sigprocmask(SIG_SETMASK, &oldmask, NULL);
147 				freeaddrinfo(res);
148 				return (-1);
149 			}
150 		}
151 		fcntl(s, F_SETOWN, pid);
152 		if (connect(s, r->ai_addr, r->ai_addrlen) >= 0)
153 			break;
154 		(void)close(s);
155 		if (errno == EADDRINUSE) {
156 			lport--;
157 			continue;
158 		}
159 		if (errno == ECONNREFUSED)
160 			refused++;
161 		if (r->ai_next) {
162 			int oerrno = errno;
163 			char hbuf[NI_MAXHOST];
164 			const int niflags = NI_NUMERICHOST;
165 
166 			hbuf[0] = '\0';
167 			if (getnameinfo(r->ai_addr, r->ai_addrlen,
168 			    hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
169 				strlcpy(hbuf, "(invalid)", sizeof hbuf);
170 			(void)fprintf(stderr, "connect to address %s: ", hbuf);
171 			errno = oerrno;
172 			perror(0);
173 			r = r->ai_next;
174 			hbuf[0] = '\0';
175 			if (getnameinfo(r->ai_addr, r->ai_addrlen,
176 			    hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
177 				strlcpy(hbuf, "(invalid)", sizeof hbuf);
178 			(void)fprintf(stderr, "Trying %s...\n", hbuf);
179 			continue;
180 		}
181 		if (refused && timo <= 16) {
182 			(void)sleep(timo);
183 			timo *= 2;
184 			r = res;
185 			refused = 0;
186 			continue;
187 		}
188 		(void)fprintf(stderr, "%s: %s\n", res->ai_canonname,
189 		    strerror(errno));
190 		sigprocmask(SIG_SETMASK, &oldmask, NULL);
191 		freeaddrinfo(res);
192 		return (-1);
193 	}
194 	/* given "af" can be PF_UNSPEC, we need the real af for "s" */
195 	af = r->ai_family;
196 	freeaddrinfo(res);
197 #if 0
198 	/*
199 	 * try to rresvport() to the same port. This will make rresvport()
200 	 * fail it's first bind, resulting in it choosing a random port.
201 	 */
202 	lport--;
203 #endif
204 	if (fd2p == 0) {
205 		write(s, "", 1);
206 		lport = 0;
207 	} else {
208 		char num[8];
209 		int s2 = rresvport_af(&lport, af), s3;
210 		socklen_t len = sizeof(from);
211 		int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask);
212 
213 		if (s2 < 0)
214 			goto bad;
215 		readsp = (fd_set *)malloc(fdssize);
216 		if (readsp == NULL) {
217 			close(s2);
218 			goto bad;
219 		}
220 		listen(s2, 1);
221 		(void)snprintf(num, sizeof(num), "%d", lport);
222 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
223 			(void)fprintf(stderr,
224 			    "rcmd: write (setting up stderr): %s\n",
225 			    strerror(errno));
226 			(void)close(s2);
227 			goto bad;
228 		}
229 again:
230 		bzero(readsp, fdssize);
231 		FD_SET(s, readsp);
232 		FD_SET(s2, readsp);
233 		errno = 0;
234 		if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 ||
235 		    !FD_ISSET(s2, readsp)) {
236 			if (errno != 0)
237 				(void)fprintf(stderr,
238 				    "rcmd: select (setting up stderr): %s\n",
239 				    strerror(errno));
240 			else
241 				(void)fprintf(stderr,
242 				"select: protocol failure in circuit setup\n");
243 			(void)close(s2);
244 			goto bad;
245 		}
246 		s3 = accept(s2, (struct sockaddr *)&from, &len);
247 		if (s3 < 0) {
248 			(void)fprintf(stderr,
249 			    "rcmd: accept: %s\n", strerror(errno));
250 			lport = 0;
251 			close(s2);
252 			goto bad;
253 		}
254 
255 		/*
256 		 * XXX careful for ftp bounce attacks. If discovered, shut them
257 		 * down and check for the real auxiliary channel to connect.
258 		 */
259 		switch (from.ss_family) {
260 		case AF_INET:
261 		case AF_INET6:
262 			if (getnameinfo((struct sockaddr *)&from, len,
263 			    NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 &&
264 			    atoi(num) != 20) {
265 				break;
266 			}
267 			close(s3);
268 			goto again;
269 		default:
270 			break;
271 		}
272 		(void)close(s2);
273 
274 		*fd2p = s3;
275 		switch (from.ss_family) {
276 		case AF_INET:
277 		case AF_INET6:
278 			if (getnameinfo((struct sockaddr *)&from, len,
279 			    NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 ||
280 			    (atoi(num) >= IPPORT_RESERVED ||
281 			     atoi(num) < IPPORT_RESERVED / 2)) {
282 				(void)fprintf(stderr,
283 				    "socket: protocol failure in circuit setup.\n");
284 				goto bad2;
285 			}
286 			break;
287 		default:
288 			break;
289 		}
290 	}
291 	(void)write(s, locuser, strlen(locuser)+1);
292 	(void)write(s, remuser, strlen(remuser)+1);
293 	(void)write(s, cmd, strlen(cmd)+1);
294 	if (read(s, &c, 1) != 1) {
295 		(void)fprintf(stderr,
296 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
297 		goto bad2;
298 	}
299 	if (c != 0) {
300 		while (read(s, &c, 1) == 1) {
301 			(void)write(STDERR_FILENO, &c, 1);
302 			if (c == '\n')
303 				break;
304 		}
305 		goto bad2;
306 	}
307 	sigprocmask(SIG_SETMASK, &oldmask, NULL);
308 	free(readsp);
309 	return (s);
310 bad2:
311 	if (lport)
312 		(void)close(*fd2p);
313 bad:
314 	if (readsp)
315 		free(readsp);
316 	(void)close(s);
317 	sigprocmask(SIG_SETMASK, &oldmask, NULL);
318 	return (-1);
319 }
320 
321 int	__check_rhosts_file = 1;
322 char	*__rcmd_errstr;
323 
324 int
325 ruserok(rhost, superuser, ruser, luser)
326 	const char *rhost, *ruser, *luser;
327 	int superuser;
328 {
329 	struct addrinfo hints, *res, *r;
330 	int error;
331 
332 	memset(&hints, 0, sizeof(hints));
333 	hints.ai_family = PF_UNSPEC;
334 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
335 	error = getaddrinfo(rhost, "0", &hints, &res);
336 	if (error)
337 		return (-1);
338 
339 	for (r = res; r; r = r->ai_next) {
340 		if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
341 		    luser) == 0) {
342 			freeaddrinfo(res);
343 			return (0);
344 		}
345 	}
346 	freeaddrinfo(res);
347 	return (-1);
348 }
349 
350 /*
351  * New .rhosts strategy: We are passed an ip address. We spin through
352  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
353  * has ip addresses, we don't have to trust a nameserver.  When it
354  * contains hostnames, we spin through the list of addresses the nameserver
355  * gives us and look for a match.
356  *
357  * Returns 0 if ok, -1 if not ok.
358  */
359 int
360 iruserok(raddr, superuser, ruser, luser)
361 	u_int32_t raddr;
362 	int superuser;
363 	const char *ruser, *luser;
364 {
365 	struct sockaddr_in sin;
366 
367 	memset(&sin, 0, sizeof(sin));
368 	sin.sin_family = AF_INET;
369 	sin.sin_len = sizeof(struct sockaddr_in);
370 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
371 	return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser,
372 		    luser);
373 }
374 
375 int
376 iruserok_sa(raddr, rlen, superuser, ruser, luser)
377 	const void *raddr;
378 	int rlen;
379 	int superuser;
380 	const char *ruser, *luser;
381 {
382 	struct sockaddr *sa;
383 	register char *cp;
384 	struct stat sbuf;
385 	struct passwd *pwd;
386 	FILE *hostf;
387 	uid_t uid;
388 	int first;
389 	char pbuf[MAXPATHLEN];
390 
391 	sa = (struct sockaddr *)raddr;
392 	first = 1;
393 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
394 again:
395 	if (hostf) {
396 		if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) {
397 			(void)fclose(hostf);
398 			return (0);
399 		}
400 		(void)fclose(hostf);
401 	}
402 	if (first == 1 && (__check_rhosts_file || superuser)) {
403 		first = 0;
404 		if ((pwd = getpwnam(luser)) == NULL)
405 			return (-1);
406 		snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir);
407 
408 		/*
409 		 * Change effective uid while opening .rhosts.  If root and
410 		 * reading an NFS mounted file system, can't read files that
411 		 * are protected read/write owner only.
412 		 */
413 		uid = geteuid();
414 		(void)seteuid(pwd->pw_uid);
415 		hostf = fopen(pbuf, "r");
416 		(void)seteuid(uid);
417 
418 		if (hostf == NULL)
419 			return (-1);
420 		/*
421 		 * If not a regular file, or is owned by someone other than
422 		 * user or root or if writeable by anyone but the owner, quit.
423 		 */
424 		cp = NULL;
425 		if (lstat(pbuf, &sbuf) < 0)
426 			cp = ".rhosts lstat failed";
427 		else if (!S_ISREG(sbuf.st_mode))
428 			cp = ".rhosts not regular file";
429 		else if (fstat(fileno(hostf), &sbuf) < 0)
430 			cp = ".rhosts fstat failed";
431 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
432 			cp = "bad .rhosts owner";
433 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
434 			cp = ".rhosts writable by other than owner";
435 		/* If there were any problems, quit. */
436 		if (cp) {
437 			__rcmd_errstr = cp;
438 			(void)fclose(hostf);
439 			return (-1);
440 		}
441 		goto again;
442 	}
443 	return (-1);
444 }
445 
446 /*
447  * XXX
448  * Don't make static, used by lpd(8).
449  *
450  * Returns 0 if ok, -1 if not ok.
451  */
452 int
453 __ivaliduser(hostf, raddrl, luser, ruser)
454 	FILE *hostf;
455 	in_addr_t raddrl;
456 	const char *luser, *ruser;
457 {
458 	struct sockaddr_in sin;
459 
460 	memset(&sin, 0, sizeof(sin));
461 	sin.sin_family = AF_INET;
462 	sin.sin_len = sizeof(struct sockaddr_in);
463 	memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr));
464 	return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len,
465 		    luser, ruser);
466 }
467 
468 int
469 __ivaliduser_sa(hostf, raddr, salen, luser, ruser)
470 	FILE *hostf;
471 	struct sockaddr *raddr;
472 	socklen_t salen;
473 	const char *luser, *ruser;
474 {
475 	register char *user, *p;
476 	char *buf;
477 	const char *auser, *ahost;
478 	int hostok, userok;
479 	char *rhost = (char *)-1;
480 	char domain[MAXHOSTNAMELEN];
481 	size_t buflen;
482 
483 	getdomainname(domain, sizeof(domain));
484 
485 	while ((buf = fgetln(hostf, &buflen))) {
486 		p = buf;
487 		if (*p == '#')
488 			continue;
489 		while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') {
490 			if (!isprint(*p))
491 				goto bail;
492 			*p = isupper(*p) ? tolower(*p) : *p;
493 			p++;
494 		}
495 		if (p >= buf + buflen)
496 			continue;
497 		if (*p == ' ' || *p == '\t') {
498 			*p++ = '\0';
499 			while (p < buf + buflen && (*p == ' ' || *p == '\t'))
500 				p++;
501 			if (p >= buf + buflen)
502 				continue;
503 			user = p;
504 			while (p < buf + buflen && *p != '\n' && *p != ' ' &&
505 			    *p != '\t') {
506 				if (!isprint(*p))
507 					goto bail;
508 				p++;
509 			}
510 		} else
511 			user = p;
512 		*p = '\0';
513 
514 		if (p == buf)
515 			continue;
516 
517 		auser = *user ? user : luser;
518 		ahost = buf;
519 
520 		if (strlen(ahost) >= MAXHOSTNAMELEN)
521 			continue;
522 
523 		/*
524 		 * innetgr() must lookup a hostname (we do not attempt
525 		 * to change the semantics so that netgroups may have
526 		 * #.#.#.# addresses in the list.)
527 		 */
528 		if (ahost[0] == '+')
529 			switch (ahost[1]) {
530 			case '\0':
531 				hostok = 1;
532 				break;
533 			case '@':
534 				if (rhost == (char *)-1)
535 					rhost = __gethostloop(raddr, salen);
536 				hostok = 0;
537 				if (rhost)
538 					hostok = innetgr(&ahost[2], rhost,
539 					    NULL, domain);
540 				break;
541 			default:
542 				hostok = __icheckhost(raddr, salen, &ahost[1]);
543 				break;
544 			}
545 		else if (ahost[0] == '-')
546 			switch (ahost[1]) {
547 			case '\0':
548 				hostok = -1;
549 				break;
550 			case '@':
551 				if (rhost == (char *)-1)
552 					rhost = __gethostloop(raddr, salen);
553 				hostok = 0;
554 				if (rhost)
555 					hostok = -innetgr(&ahost[2], rhost,
556 					    NULL, domain);
557 				break;
558 			default:
559 				hostok = -__icheckhost(raddr, salen, &ahost[1]);
560 				break;
561 			}
562 		else
563 			hostok = __icheckhost(raddr, salen, ahost);
564 
565 
566 		if (auser[0] == '+')
567 			switch (auser[1]) {
568 			case '\0':
569 				userok = 1;
570 				break;
571 			case '@':
572 				userok = innetgr(&auser[2], NULL, ruser,
573 				    domain);
574 				break;
575 			default:
576 				userok = strcmp(ruser, &auser[1]) ? 0 : 1;
577 				break;
578 			}
579 		else if (auser[0] == '-')
580 			switch (auser[1]) {
581 			case '\0':
582 				userok = -1;
583 				break;
584 			case '@':
585 				userok = -innetgr(&auser[2], NULL, ruser,
586 				    domain);
587 				break;
588 			default:
589 				userok = strcmp(ruser, &auser[1]) ? 0 : -1;
590 				break;
591 			}
592 		else
593 			userok = strcmp(ruser, auser) ? 0 : 1;
594 
595 		/* Check if one component did not match */
596 		if (hostok == 0 || userok == 0)
597 			continue;
598 
599 		/* Check if we got a forbidden pair */
600 		if (userok <= -1 || hostok <= -1)
601 			return (-1);
602 
603 		/* Check if we got a valid pair */
604 		if (hostok >= 1 && userok >= 1)
605 			return (0);
606 	}
607 bail:
608 	return (-1);
609 }
610 
611 /*
612  * Returns "true" if match, 0 if no match.  If we do not find any
613  * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
614  */
615 static int
616 __icheckhost(raddr, salen, lhost)
617 	struct sockaddr *raddr;
618 	socklen_t salen;
619 	const char *lhost;
620 {
621 	struct addrinfo hints, *res, *r;
622 	char h1[NI_MAXHOST], h2[NI_MAXHOST];
623 	int error;
624 	const int niflags = NI_NUMERICHOST;
625 
626 	h1[0] = '\0';
627 	if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
628 	    niflags) != 0)
629 		return (0);
630 
631 	/* Resolve laddr into sockaddr */
632 	memset(&hints, 0, sizeof(hints));
633 	hints.ai_family = raddr->sa_family;
634 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
635 	res = NULL;
636 	error = getaddrinfo(lhost, "0", &hints, &res);
637 	if (error)
638 		return (0);
639 
640 	/*
641 	 * Try string comparisons between raddr and laddr.
642 	 */
643 	for (r = res; r; r = r->ai_next) {
644 		h2[0] = '\0';
645 		if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
646 		    NULL, 0, niflags) != 0)
647 			continue;
648 		if (strcmp(h1, h2) == 0) {
649 			freeaddrinfo(res);
650 			return (1);
651 		}
652 	}
653 
654 	/* No match. */
655 	freeaddrinfo(res);
656 	return (0);
657 }
658 
659 /*
660  * Return the hostname associated with the supplied address.
661  * Do a reverse lookup as well for security. If a loop cannot
662  * be found, pack the result of inet_ntoa() into the string.
663  */
664 static char *
665 __gethostloop(raddr, salen)
666 	struct sockaddr *raddr;
667 	socklen_t salen;
668 {
669 	static char remotehost[NI_MAXHOST];
670 	char h1[NI_MAXHOST], h2[NI_MAXHOST];
671 	struct addrinfo hints, *res, *r;
672 	int error;
673 	const int niflags = NI_NUMERICHOST;
674 
675 	h1[0] = remotehost[0] = '\0';
676 	if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost),
677 	    NULL, 0, NI_NAMEREQD) != 0)
678 		return (NULL);
679 	if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
680 	    niflags) != 0)
681 		return (NULL);
682 
683 	/*
684 	 * Look up the name and check that the supplied
685 	 * address is in the list
686 	 */
687 	memset(&hints, 0, sizeof(hints));
688 	hints.ai_family = raddr->sa_family;
689 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
690 	hints.ai_flags = AI_CANONNAME;
691 	res = NULL;
692 	error = getaddrinfo(remotehost, "0", &hints, &res);
693 	if (error)
694 		return (NULL);
695 
696 	for (r = res; r; r = r->ai_next) {
697 		h2[0] = '\0';
698 		if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
699 		    NULL, 0, niflags) != 0)
700 			continue;
701 		if (strcmp(h1, h2) == 0) {
702 			freeaddrinfo(res);
703 			return (remotehost);
704 		}
705 	}
706 
707 	/*
708 	 * either the DNS adminstrator has made a configuration
709 	 * mistake, or someone has attempted to spoof us
710 	 */
711 	syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s",
712 	    h1, res->ai_canonname ? res->ai_canonname : remotehost);
713 	freeaddrinfo(res);
714 	return (NULL);
715 }
716