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