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