xref: /openbsd-src/lib/libc/net/rcmd.c (revision 16582178b67d833dad7c5fb5cc634cd91f2e52df)
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.33 2000/01/27 05:18:47 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 			sigsetmask(oldmask);
144 			freeaddrinfo(res);
145 			return (-1);
146 		}
147 		fcntl(s, F_SETOWN, pid);
148 		if (connect(s, r->ai_addr, r->ai_addrlen) >= 0)
149 			break;
150 		(void)close(s);
151 		if (errno == EADDRINUSE) {
152 			lport--;
153 			continue;
154 		}
155 		if (errno == ECONNREFUSED && timo <= 16) {
156 			(void)sleep(timo);
157 			timo *= 2;
158 			continue;
159 		}
160 		if (r->ai_next) {
161 			int oerrno = errno;
162 			char hbuf[NI_MAXHOST];
163 #ifdef NI_WITHSCOPEID
164 			const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
165 #else
166 			const int niflags = NI_NUMERICHOST;
167 #endif
168 
169 			hbuf[0] = '\0';
170 			if (getnameinfo(r->ai_addr, r->ai_addrlen,
171 			    hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
172 				strcpy(hbuf, "(invalid)");
173 			(void)fprintf(stderr, "connect to address %s: ", hbuf);
174 			errno = oerrno;
175 			perror(0);
176 			r = r->ai_next;
177 			hbuf[0] = '\0';
178 			if (getnameinfo(r->ai_addr, r->ai_addrlen,
179 			    hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
180 				strcpy(hbuf, "(invalid)");
181 			(void)fprintf(stderr, "Trying %s...\n", hbuf);
182 			continue;
183 		}
184 		(void)fprintf(stderr, "%s: %s\n", res->ai_canonname,
185 		    strerror(errno));
186 		sigsetmask(oldmask);
187 		freeaddrinfo(res);
188 		return (-1);
189 	}
190 	/* given "af" can be PF_UNSPEC, we need the real af for "s" */
191 	af = r->ai_family;
192 	freeaddrinfo(res);
193 #if 0
194 	/*
195 	 * try to rresvport() to the same port. This will make rresvport()
196 	 * fail it's first bind, resulting in it choosing a random port.
197 	 */
198 	lport--;
199 #endif
200 	if (fd2p == 0) {
201 		write(s, "", 1);
202 		lport = 0;
203 	} else {
204 		char num[8];
205 		int s2 = rresvport_af(&lport, af), s3;
206 		int len = sizeof(from);
207 		int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask);
208 
209 		if (s2 < 0)
210 			goto bad;
211 		readsp = (fd_set *)malloc(fdssize);
212 		if (readsp == NULL)
213 			goto bad;
214 		listen(s2, 1);
215 		(void)snprintf(num, sizeof(num), "%d", lport);
216 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
217 			(void)fprintf(stderr,
218 			    "rcmd: write (setting up stderr): %s\n",
219 			    strerror(errno));
220 			(void)close(s2);
221 			goto bad;
222 		}
223 again:
224 		bzero(readsp, fdssize);
225 		FD_SET(s, readsp);
226 		FD_SET(s2, readsp);
227 		errno = 0;
228 		if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 ||
229 		    !FD_ISSET(s2, readsp)) {
230 			if (errno != 0)
231 				(void)fprintf(stderr,
232 				    "rcmd: select (setting up stderr): %s\n",
233 				    strerror(errno));
234 			else
235 				(void)fprintf(stderr,
236 				"select: protocol failure in circuit setup\n");
237 			(void)close(s2);
238 			goto bad;
239 		}
240 		s3 = accept(s2, (struct sockaddr *)&from, &len);
241 		/*
242 		 * XXX careful for ftp bounce attacks. If discovered, shut them
243 		 * down and check for the real auxiliary channel to connect.
244 		 */
245 		switch (from.ss_family) {
246 		case AF_INET:
247 		case AF_INET6:
248 			if (getnameinfo((struct sockaddr *)&from, len,
249 			    NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 &&
250 			    atoi(num) != 20) {
251 				break;
252 			}
253 			close(s3);
254 			goto again;
255 		default:
256 			break;
257 		}
258 		(void)close(s2);
259 		if (s3 < 0) {
260 			(void)fprintf(stderr,
261 			    "rcmd: accept: %s\n", strerror(errno));
262 			lport = 0;
263 			goto bad;
264 		}
265 		*fd2p = s3;
266 		switch (from.ss_family) {
267 		case AF_INET:
268 		case AF_INET6:
269 			if (getnameinfo((struct sockaddr *)&from, len,
270 			    NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 ||
271 			    (atoi(num) >= IPPORT_RESERVED ||
272 			     atoi(num) < IPPORT_RESERVED / 2)) {
273 				(void)fprintf(stderr,
274 				    "socket: protocol failure in circuit setup.\n");
275 				goto bad2;
276 			}
277 			break;
278 		default:
279 			break;
280 		}
281 	}
282 	(void)write(s, locuser, strlen(locuser)+1);
283 	(void)write(s, remuser, strlen(remuser)+1);
284 	(void)write(s, cmd, strlen(cmd)+1);
285 	if (read(s, &c, 1) != 1) {
286 		(void)fprintf(stderr,
287 		    "rcmd: %s: %s\n", *ahost, strerror(errno));
288 		goto bad2;
289 	}
290 	if (c != 0) {
291 		while (read(s, &c, 1) == 1) {
292 			(void)write(STDERR_FILENO, &c, 1);
293 			if (c == '\n')
294 				break;
295 		}
296 		goto bad2;
297 	}
298 	sigsetmask(oldmask);
299 	free(readsp);
300 	return (s);
301 bad2:
302 	if (lport)
303 		(void)close(*fd2p);
304 bad:
305 	if (readsp)
306 		free(readsp);
307 	(void)close(s);
308 	sigsetmask(oldmask);
309 	return (-1);
310 }
311 
312 int	__check_rhosts_file = 1;
313 char	*__rcmd_errstr;
314 
315 int
316 ruserok(rhost, superuser, ruser, luser)
317 	const char *rhost, *ruser, *luser;
318 	int superuser;
319 {
320 	struct addrinfo hints, *res, *r;
321 	int error;
322 
323 	memset(&hints, 0, sizeof(hints));
324 	hints.ai_family = PF_UNSPEC;
325 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
326 	error = getaddrinfo(rhost, "0", &hints, &res);
327 	if (error)
328 		return (-1);
329 
330 	for (r = res; r; r = r->ai_next) {
331 		if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
332 		    luser) == 0) {
333 			freeaddrinfo(res);
334 			return (0);
335 		}
336 	}
337 	freeaddrinfo(res);
338 	return (-1);
339 }
340 
341 /*
342  * New .rhosts strategy: We are passed an ip address. We spin through
343  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
344  * has ip addresses, we don't have to trust a nameserver.  When it
345  * contains hostnames, we spin through the list of addresses the nameserver
346  * gives us and look for a match.
347  *
348  * Returns 0 if ok, -1 if not ok.
349  */
350 int
351 iruserok(raddr, superuser, ruser, luser)
352 	u_int32_t raddr;
353 	int superuser;
354 	const char *ruser, *luser;
355 {
356 	struct sockaddr_in sin;
357 
358 	memset(&sin, 0, sizeof(sin));
359 	sin.sin_family = AF_INET;
360 	sin.sin_len = sizeof(struct sockaddr_in);
361 	memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr));
362 	return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser,
363 		    luser);
364 }
365 
366 int
367 iruserok_sa(raddr, rlen, superuser, ruser, luser)
368 	const void *raddr;
369 	int rlen;
370 	int superuser;
371 	const char *ruser, *luser;
372 {
373 	struct sockaddr *sa;
374 	register char *cp;
375 	struct stat sbuf;
376 	struct passwd *pwd;
377 	FILE *hostf;
378 	uid_t uid;
379 	int first;
380 	char pbuf[MAXPATHLEN];
381 
382 	sa = (struct sockaddr *)raddr;
383 #ifdef lint
384 	rlen = rlen;
385 #endif
386 	first = 1;
387 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
388 again:
389 	if (hostf) {
390 		if (__ivaliduser_sa(hostf, sa, luser, ruser) == 0) {
391 			(void)fclose(hostf);
392 			return (0);
393 		}
394 		(void)fclose(hostf);
395 	}
396 	if (first == 1 && (__check_rhosts_file || superuser)) {
397 		first = 0;
398 		if ((pwd = getpwnam(luser)) == NULL)
399 			return (-1);
400 		(void)strcpy(pbuf, pwd->pw_dir);
401 		(void)strcat(pbuf, "/.rhosts");
402 
403 		/*
404 		 * Change effective uid while opening .rhosts.  If root and
405 		 * reading an NFS mounted file system, can't read files that
406 		 * are protected read/write owner only.
407 		 */
408 		uid = geteuid();
409 		(void)seteuid(pwd->pw_uid);
410 		hostf = fopen(pbuf, "r");
411 		(void)seteuid(uid);
412 
413 		if (hostf == NULL)
414 			return (-1);
415 		/*
416 		 * If not a regular file, or is owned by someone other than
417 		 * user or root or if writeable by anyone but the owner, quit.
418 		 */
419 		cp = NULL;
420 		if (lstat(pbuf, &sbuf) < 0)
421 			cp = ".rhosts lstat failed";
422 		else if (!S_ISREG(sbuf.st_mode))
423 			cp = ".rhosts not regular file";
424 		else if (fstat(fileno(hostf), &sbuf) < 0)
425 			cp = ".rhosts fstat failed";
426 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
427 			cp = "bad .rhosts owner";
428 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
429 			cp = ".rhosts writeable by other than owner";
430 		/* If there were any problems, quit. */
431 		if (cp) {
432 			__rcmd_errstr = cp;
433 			(void)fclose(hostf);
434 			return (-1);
435 		}
436 		goto again;
437 	}
438 	return (-1);
439 }
440 
441 /*
442  * XXX
443  * Don't make static, used by lpd(8).
444  *
445  * Returns 0 if ok, -1 if not ok.
446  */
447 int
448 __ivaliduser(hostf, raddrl, luser, ruser)
449 	FILE *hostf;
450 	in_addr_t raddrl;
451 	const char *luser, *ruser;
452 {
453 	struct sockaddr_in sin;
454 
455 	memset(&sin, 0, sizeof(sin));
456 	sin.sin_family = AF_INET;
457 	sin.sin_len = sizeof(struct sockaddr_in);
458 	memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr));
459 	return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, luser, ruser);
460 }
461 
462 int
463 __ivaliduser_sa(hostf, raddr, luser, ruser)
464 	FILE *hostf;
465 	struct sockaddr *raddr;
466 	const char *luser, *ruser;
467 {
468 	register char *user, *p;
469 	char *buf;
470 	const char *auser, *ahost;
471 	int hostok, userok;
472 	char *rhost = (char *)-1;
473 	char domain[MAXHOSTNAMELEN];
474 	size_t buflen;
475 
476 	getdomainname(domain, sizeof(domain));
477 
478 	while ((buf = fgetln(hostf, &buflen))) {
479 		p = buf;
480 		if (*p == '#')
481 			continue;
482 		while (*p != '\n' && *p != ' ' && *p != '\t' && p < buf + buflen) {
483 			if (!isprint(*p))
484 				goto bail;
485 			*p = isupper(*p) ? tolower(*p) : *p;
486 			p++;
487 		}
488 		if (p >= buf + buflen)
489 			continue;
490 		if (*p == ' ' || *p == '\t') {
491 			*p++ = '\0';
492 			while ((*p == ' ' || *p == '\t') && p < buf + buflen)
493 				p++;
494 			if (p >= buf + buflen)
495 				continue;
496 			user = p;
497 			while (*p != '\n' && *p != ' ' &&
498 			    *p != '\t' && p < buf + buflen) {
499 				if (!isprint(*p))
500 					goto bail;
501 				p++;
502 			}
503 		} else
504 			user = p;
505 		*p = '\0';
506 
507 		if (p == buf)
508 			continue;
509 
510 		auser = *user ? user : luser;
511 		ahost = buf;
512 
513 		if (strlen(ahost) >= MAXHOSTNAMELEN)
514 			continue;
515 
516 		/*
517 		 * innetgr() must lookup a hostname (we do not attempt
518 		 * to change the semantics so that netgroups may have
519 		 * #.#.#.# addresses in the list.)
520 		 */
521 		if (ahost[0] == '+')
522 			switch (ahost[1]) {
523 			case '\0':
524 				hostok = 1;
525 				break;
526 			case '@':
527 				if (rhost == (char *)-1)
528 					rhost = __gethostloop(raddr);
529 				hostok = 0;
530 				if (rhost)
531 					hostok = innetgr(&ahost[2], rhost,
532 					    NULL, domain);
533 				break;
534 			default:
535 				hostok = __icheckhost(raddr, &ahost[1]);
536 				break;
537 			}
538 		else if (ahost[0] == '-')
539 			switch (ahost[1]) {
540 			case '\0':
541 				hostok = -1;
542 				break;
543 			case '@':
544 				if (rhost == (char *)-1)
545 					rhost = __gethostloop(raddr);
546 				hostok = 0;
547 				if (rhost)
548 					hostok = -innetgr(&ahost[2], rhost,
549 					    NULL, domain);
550 				break;
551 			default:
552 				hostok = -__icheckhost(raddr, &ahost[1]);
553 				break;
554 			}
555 		else
556 			hostok = __icheckhost(raddr, ahost);
557 
558 
559 		if (auser[0] == '+')
560 			switch (auser[1]) {
561 			case '\0':
562 				userok = 1;
563 				break;
564 			case '@':
565 				userok = innetgr(&auser[2], NULL, ruser,
566 				    domain);
567 				break;
568 			default:
569 				userok = strcmp(ruser, &auser[1]) ? 0 : 1;
570 				break;
571 			}
572 		else if (auser[0] == '-')
573 			switch (auser[1]) {
574 			case '\0':
575 				userok = -1;
576 				break;
577 			case '@':
578 				userok = -innetgr(&auser[2], NULL, ruser,
579 				    domain);
580 				break;
581 			default:
582 				userok = strcmp(ruser, &auser[1]) ? 0 : -1;
583 				break;
584 			}
585 		else
586 			userok = strcmp(ruser, auser) ? 0 : 1;
587 
588 		/* Check if one component did not match */
589 		if (hostok == 0 || userok == 0)
590 			continue;
591 
592 		/* Check if we got a forbidden pair */
593 		if (userok <= -1 || hostok <= -1)
594 			return (-1);
595 
596 		/* Check if we got a valid pair */
597 		if (hostok >= 1 && userok >= 1)
598 			return (0);
599 	}
600 bail:
601 	return (-1);
602 }
603 
604 /*
605  * Returns "true" if match, 0 if no match.  If we do not find any
606  * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
607  *
608  * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
609  * if af == AF_INET6.
610  */
611 static int
612 __icheckhost(raddr, lhost)
613 	struct sockaddr *raddr;
614 	const char *lhost;
615 {
616 	struct addrinfo hints, *res, *r;
617 	char h1[NI_MAXHOST], h2[NI_MAXHOST];
618 	int error;
619 #ifdef NI_WITHSCOPEID
620 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
621 #else
622 	const int niflags = NI_NUMERICHOST;
623 #endif
624 
625 	h1[0] = '\0';
626 	if (getnameinfo(raddr, raddr->sa_len, h1, sizeof(h1), NULL, 0,
627 	    niflags) != 0)
628 		return (0);
629 
630 	/* Resolve laddr into sockaddr */
631 	memset(&hints, 0, sizeof(hints));
632 	hints.ai_family = raddr->sa_family;
633 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
634 	res = NULL;
635 	error = getaddrinfo(lhost, "0", &hints, &res);
636 	if (error)
637 		return (0);
638 
639 	/*
640 	 * Try string comparisons between raddr and laddr.
641 	 */
642 	for (r = res; r; r = r->ai_next) {
643 		h2[0] = '\0';
644 		if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
645 		    NULL, 0, niflags) != 0)
646 			continue;
647 		if (strcmp(h1, h2) == 0) {
648 			freeaddrinfo(res);
649 			return (1);
650 		}
651 	}
652 
653 	/* No match. */
654 	freeaddrinfo(res);
655 	return (0);
656 }
657 
658 /*
659  * Return the hostname associated with the supplied address.
660  * Do a reverse lookup as well for security. If a loop cannot
661  * be found, pack the result of inet_ntoa() into the string.
662  *
663  * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
664  * if af == AF_INET6.
665  */
666 static char *
667 __gethostloop(raddr)
668 	struct sockaddr *raddr;
669 {
670 	static char remotehost[NI_MAXHOST];
671 	char h1[NI_MAXHOST], h2[NI_MAXHOST];
672 	struct addrinfo hints, *res, *r;
673 	int error;
674 #ifdef NI_WITHSCOPEID
675 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
676 #else
677 	const int niflags = NI_NUMERICHOST;
678 #endif
679 
680 	h1[0] = remotehost[0] = '\0';
681 	if (getnameinfo(raddr, raddr->sa_len, remotehost, sizeof(remotehost),
682 	    NULL, 0, NI_NAMEREQD) != 0)
683 		return (NULL);
684 	if (getnameinfo(raddr, raddr->sa_len, h1, sizeof(h1), NULL, 0,
685 	    niflags) != 0)
686 		return (NULL);
687 
688 	/*
689 	 * Look up the name and check that the supplied
690 	 * address is in the list
691 	 */
692 	memset(&hints, 0, sizeof(hints));
693 	hints.ai_family = raddr->sa_family;
694 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
695 	hints.ai_flags = AI_CANONNAME;
696 	res = NULL;
697 	error = getaddrinfo(remotehost, "0", &hints, &res);
698 	if (error)
699 		return (NULL);
700 
701 	for (r = res; r; r = r->ai_next) {
702 		h2[0] = '\0';
703 		if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
704 		    NULL, 0, niflags) != 0)
705 			continue;
706 		if (strcmp(h1, h2) == 0) {
707 			freeaddrinfo(res);
708 			return (remotehost);
709 		}
710 	}
711 
712 	/*
713 	 * either the DNS adminstrator has made a configuration
714 	 * mistake, or someone has attempted to spoof us
715 	 */
716 	syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s",
717 	    h1, res->ai_canonname ? res->ai_canonname : remotehost);
718 	freeaddrinfo(res);
719 	return (NULL);
720 }
721