xref: /openbsd-src/lib/libc/net/rcmd.c (revision f5a2605fbb9ea2062a6ad323f319b5c8936b2b0c)
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. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  *	This product includes software developed by Theo de Raadt.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *rcsid = "$OpenBSD: rcmd.c,v 1.34 2000/01/30 05:17:49 itojun Exp $";
38 #endif /* LIBC_SCCS and not lint */
39 
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 
47 #include <signal.h>
48 #include <fcntl.h>
49 #include <netdb.h>
50 #include <unistd.h>
51 #include <pwd.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include <ctype.h>
55 #include <string.h>
56 #include <syslog.h>
57 #include <stdlib.h>
58 #include <netgroup.h>
59 
60 int	__ivaliduser __P((FILE *, in_addr_t, const char *, const char *));
61 int	__ivaliduser_sa __P((FILE *, struct sockaddr *, const char *, const char *));
62 static int __icheckhost __P((struct sockaddr *, const char *));
63 static char *__gethostloop __P((struct sockaddr *));
64 
65 int
66 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
67 	char **ahost;
68 	in_port_t rport;
69 	const char *locuser, *remuser, *cmd;
70 	int *fd2p;
71 {
72 	return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
73 }
74 
75 int
76 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af)
77 	char **ahost;
78 	in_port_t rport;
79 	const char *locuser, *remuser, *cmd;
80 	int *fd2p;
81 	int af;
82 {
83 	static char hbuf[MAXHOSTNAMELEN];
84 	char pbuf[NI_MAXSERV];
85 	struct addrinfo hints, *res, *r;
86 	int error;
87 	struct sockaddr_storage from;
88 	fd_set *readsp = NULL;
89 	int oldmask;
90 	pid_t pid;
91 	int s, lport, timo;
92 	char c, *p;
93 
94 	/* call rcmdsh() with specified remote shell if appropriate. */
95 	if (!issetugid() && (p = getenv("RSH"))) {
96 		struct servent *sp = getservbyname("shell", "tcp");
97 
98 		if (sp && sp->s_port == rport)
99 			return (rcmdsh(ahost, rport, locuser, remuser,
100 			    cmd, p));
101 	}
102 
103 	/* use rsh(1) if non-root and remote port is shell. */
104 	if (geteuid()) {
105 		struct servent *sp = getservbyname("shell", "tcp");
106 
107 		if (sp && sp->s_port == rport)
108 			return (rcmdsh(ahost, rport, locuser, remuser,
109 			    cmd, NULL));
110 	}
111 
112 	pid = getpid();
113 	snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport));
114 	memset(&hints, 0, sizeof(hints));
115 	hints.ai_family = af;
116 	hints.ai_socktype = SOCK_STREAM;
117 	hints.ai_flags = AI_CANONNAME;
118 	error = getaddrinfo(*ahost, pbuf, &hints, &res);
119 	if (error) {
120 #if 0
121 		warnx("%s: %s", *ahost, gai_strerror(error));
122 #endif
123 		return (-1);
124 	}
125 	if (res->ai_canonname) {
126 		strncpy(hbuf, res->ai_canonname, sizeof(hbuf) - 1);
127 		hbuf[sizeof(hbuf) - 1] = '\0';
128 		*ahost = hbuf;
129 	} else
130 		; /*XXX*/
131 
132 	r = res;
133 	oldmask = sigblock(sigmask(SIGURG));
134 	for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
135 		s = rresvport_af(&lport, r->ai_family);
136 		if (s < 0) {
137 			if (errno == EAGAIN)
138 				(void)fprintf(stderr,
139 				    "rcmd: socket: All ports in use\n");
140 			else
141 				(void)fprintf(stderr, "rcmd: socket: %s\n",
142 				    strerror(errno));
143 			if (r->ai_next) {
144 				r = r->ai_next;
145 				continue;
146 			} else {
147 				sigsetmask(oldmask);
148 				freeaddrinfo(res);
149 				return (-1);
150 			}
151 		}
152 		fcntl(s, F_SETOWN, pid);
153 		if (connect(s, r->ai_addr, r->ai_addrlen) >= 0)
154 			break;
155 		(void)close(s);
156 		if (errno == EADDRINUSE) {
157 			lport--;
158 			continue;
159 		}
160 		if (errno == ECONNREFUSED && timo <= 16) {
161 			(void)sleep(timo);
162 			timo *= 2;
163 			continue;
164 		}
165 		if (r->ai_next) {
166 			int oerrno = errno;
167 			char hbuf[NI_MAXHOST];
168 #ifdef NI_WITHSCOPEID
169 			const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
170 #else
171 			const int niflags = NI_NUMERICHOST;
172 #endif
173 
174 			hbuf[0] = '\0';
175 			if (getnameinfo(r->ai_addr, r->ai_addrlen,
176 			    hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
177 				strcpy(hbuf, "(invalid)");
178 			(void)fprintf(stderr, "connect to address %s: ", hbuf);
179 			errno = oerrno;
180 			perror(0);
181 			r = r->ai_next;
182 			hbuf[0] = '\0';
183 			if (getnameinfo(r->ai_addr, r->ai_addrlen,
184 			    hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
185 				strcpy(hbuf, "(invalid)");
186 			(void)fprintf(stderr, "Trying %s...\n", hbuf);
187 			continue;
188 		}
189 		(void)fprintf(stderr, "%s: %s\n", res->ai_canonname,
190 		    strerror(errno));
191 		sigsetmask(oldmask);
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 		int 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 	sigsetmask(oldmask);
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 	sigsetmask(oldmask);
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 #ifdef lint
389 	rlen = rlen;
390 #endif
391 	first = 1;
392 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
393 again:
394 	if (hostf) {
395 		if (__ivaliduser_sa(hostf, sa, luser, ruser) == 0) {
396 			(void)fclose(hostf);
397 			return (0);
398 		}
399 		(void)fclose(hostf);
400 	}
401 	if (first == 1 && (__check_rhosts_file || superuser)) {
402 		first = 0;
403 		if ((pwd = getpwnam(luser)) == NULL)
404 			return (-1);
405 		(void)strcpy(pbuf, pwd->pw_dir);
406 		(void)strcat(pbuf, "/.rhosts");
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 writeable 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, luser, ruser);
465 }
466 
467 int
468 __ivaliduser_sa(hostf, raddr, luser, ruser)
469 	FILE *hostf;
470 	struct sockaddr *raddr;
471 	const char *luser, *ruser;
472 {
473 	register char *user, *p;
474 	char *buf;
475 	const char *auser, *ahost;
476 	int hostok, userok;
477 	char *rhost = (char *)-1;
478 	char domain[MAXHOSTNAMELEN];
479 	size_t buflen;
480 
481 	getdomainname(domain, sizeof(domain));
482 
483 	while ((buf = fgetln(hostf, &buflen))) {
484 		p = buf;
485 		if (*p == '#')
486 			continue;
487 		while (*p != '\n' && *p != ' ' && *p != '\t' && p < buf + buflen) {
488 			if (!isprint(*p))
489 				goto bail;
490 			*p = isupper(*p) ? tolower(*p) : *p;
491 			p++;
492 		}
493 		if (p >= buf + buflen)
494 			continue;
495 		if (*p == ' ' || *p == '\t') {
496 			*p++ = '\0';
497 			while ((*p == ' ' || *p == '\t') && p < buf + buflen)
498 				p++;
499 			if (p >= buf + buflen)
500 				continue;
501 			user = p;
502 			while (*p != '\n' && *p != ' ' &&
503 			    *p != '\t' && p < buf + buflen) {
504 				if (!isprint(*p))
505 					goto bail;
506 				p++;
507 			}
508 		} else
509 			user = p;
510 		*p = '\0';
511 
512 		if (p == buf)
513 			continue;
514 
515 		auser = *user ? user : luser;
516 		ahost = buf;
517 
518 		if (strlen(ahost) >= MAXHOSTNAMELEN)
519 			continue;
520 
521 		/*
522 		 * innetgr() must lookup a hostname (we do not attempt
523 		 * to change the semantics so that netgroups may have
524 		 * #.#.#.# addresses in the list.)
525 		 */
526 		if (ahost[0] == '+')
527 			switch (ahost[1]) {
528 			case '\0':
529 				hostok = 1;
530 				break;
531 			case '@':
532 				if (rhost == (char *)-1)
533 					rhost = __gethostloop(raddr);
534 				hostok = 0;
535 				if (rhost)
536 					hostok = innetgr(&ahost[2], rhost,
537 					    NULL, domain);
538 				break;
539 			default:
540 				hostok = __icheckhost(raddr, &ahost[1]);
541 				break;
542 			}
543 		else if (ahost[0] == '-')
544 			switch (ahost[1]) {
545 			case '\0':
546 				hostok = -1;
547 				break;
548 			case '@':
549 				if (rhost == (char *)-1)
550 					rhost = __gethostloop(raddr);
551 				hostok = 0;
552 				if (rhost)
553 					hostok = -innetgr(&ahost[2], rhost,
554 					    NULL, domain);
555 				break;
556 			default:
557 				hostok = -__icheckhost(raddr, &ahost[1]);
558 				break;
559 			}
560 		else
561 			hostok = __icheckhost(raddr, ahost);
562 
563 
564 		if (auser[0] == '+')
565 			switch (auser[1]) {
566 			case '\0':
567 				userok = 1;
568 				break;
569 			case '@':
570 				userok = innetgr(&auser[2], NULL, ruser,
571 				    domain);
572 				break;
573 			default:
574 				userok = strcmp(ruser, &auser[1]) ? 0 : 1;
575 				break;
576 			}
577 		else if (auser[0] == '-')
578 			switch (auser[1]) {
579 			case '\0':
580 				userok = -1;
581 				break;
582 			case '@':
583 				userok = -innetgr(&auser[2], NULL, ruser,
584 				    domain);
585 				break;
586 			default:
587 				userok = strcmp(ruser, &auser[1]) ? 0 : -1;
588 				break;
589 			}
590 		else
591 			userok = strcmp(ruser, auser) ? 0 : 1;
592 
593 		/* Check if one component did not match */
594 		if (hostok == 0 || userok == 0)
595 			continue;
596 
597 		/* Check if we got a forbidden pair */
598 		if (userok <= -1 || hostok <= -1)
599 			return (-1);
600 
601 		/* Check if we got a valid pair */
602 		if (hostok >= 1 && userok >= 1)
603 			return (0);
604 	}
605 bail:
606 	return (-1);
607 }
608 
609 /*
610  * Returns "true" if match, 0 if no match.  If we do not find any
611  * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
612  *
613  * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
614  * if af == AF_INET6.
615  */
616 static int
617 __icheckhost(raddr, lhost)
618 	struct sockaddr *raddr;
619 	const char *lhost;
620 {
621 	struct addrinfo hints, *res, *r;
622 	char h1[NI_MAXHOST], h2[NI_MAXHOST];
623 	int error;
624 #ifdef NI_WITHSCOPEID
625 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
626 #else
627 	const int niflags = NI_NUMERICHOST;
628 #endif
629 
630 	h1[0] = '\0';
631 	if (getnameinfo(raddr, raddr->sa_len, h1, sizeof(h1), NULL, 0,
632 	    niflags) != 0)
633 		return (0);
634 
635 	/* Resolve laddr into sockaddr */
636 	memset(&hints, 0, sizeof(hints));
637 	hints.ai_family = raddr->sa_family;
638 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
639 	res = NULL;
640 	error = getaddrinfo(lhost, "0", &hints, &res);
641 	if (error)
642 		return (0);
643 
644 	/*
645 	 * Try string comparisons between raddr and laddr.
646 	 */
647 	for (r = res; r; r = r->ai_next) {
648 		h2[0] = '\0';
649 		if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
650 		    NULL, 0, niflags) != 0)
651 			continue;
652 		if (strcmp(h1, h2) == 0) {
653 			freeaddrinfo(res);
654 			return (1);
655 		}
656 	}
657 
658 	/* No match. */
659 	freeaddrinfo(res);
660 	return (0);
661 }
662 
663 /*
664  * Return the hostname associated with the supplied address.
665  * Do a reverse lookup as well for security. If a loop cannot
666  * be found, pack the result of inet_ntoa() into the string.
667  *
668  * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
669  * if af == AF_INET6.
670  */
671 static char *
672 __gethostloop(raddr)
673 	struct sockaddr *raddr;
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, raddr->sa_len, remotehost, sizeof(remotehost),
687 	    NULL, 0, NI_NAMEREQD) != 0)
688 		return (NULL);
689 	if (getnameinfo(raddr, raddr->sa_len, 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