2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995, 1996
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * Do standard and reverse DNS lookups using the resolver library.
40 * Take care of all the dirty work here so the main program only has to
41 * pass us a pointer to an array of characters.
43 * We have to use direct resolver calls here otherwise the YP server
44 * could end up looping by calling itself over and over again until
45 * it disappeared up its own belly button.
48 #include <sys/param.h>
49 #include <sys/socket.h>
51 #include <sys/fcntl.h>
52 #include <sys/queue.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <arpa/nameser.h>
66 #include <rpcsvc/yp.h>
67 #include "yp_extern.h"
70 parse(struct hostent *hp)
72 static char result[MAXHOSTNAMELEN * 2];
80 if (inet_ntop(hp->h_addrtype, hp->h_addr, addr, sizeof(addr)) == NULL)
83 len = strlen(addr) + 1 + strlen(hp->h_name);
84 for (i = 0; hp->h_aliases[i]; i++)
85 len += strlen(hp->h_aliases[i]) + 1;
88 if (len > sizeof(result))
91 bzero(result, sizeof(result));
92 snprintf(result, sizeof(result), "%s %s", addr, hp->h_name);
93 for (i = 0; hp->h_aliases[i]; i++) {
95 strcat(result, hp->h_aliases[i]);
98 return ((char *)&result);
101 #define MAXPACKET (64*1024)
107 extern struct hostent *__dns_getanswer(char *, int, char *, int);
109 static TAILQ_HEAD(dns_qhead, circleq_dnsentry) qhead;
111 struct circleq_dnsentry {
114 struct sockaddr_in client_addr;
115 unsigned long ypvers;
119 unsigned short prot_type;
124 uint32_t addr[4]; /* IPv4 or IPv6 */
125 TAILQ_ENTRY(circleq_dnsentry) links;
128 static int pending = 0;
131 yp_init_resolver(void)
134 if (!(_res.options & RES_INIT) && res_init() == -1) {
135 yp_error("res_init failed");
138 if ((resfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
139 yp_error("couldn't create socket");
142 if (fcntl(resfd, F_SETFL, O_NONBLOCK) == -1) {
143 yp_error("couldn't make resolver socket non-blocking");
150 circleq_dnsentry *yp_malloc_dnsent(void)
152 register struct circleq_dnsentry *q;
154 q = malloc(sizeof(struct circleq_dnsentry));
157 yp_error("failed to malloc() circleq dns entry");
168 yp_send_dns_query(char *name, int type)
177 bzero(buf, sizeof(buf));
179 n = res_mkquery(QUERY,name,C_IN,type,NULL,0,NULL,buf,sizeof(buf));
182 yp_error("res_mkquery failed for %s type %d", name, type);
186 hptr = (HEADER *)&buf;
187 id = ntohs(hptr->id);
189 for (ns = 0; ns < _res.nscount; ns++) {
190 rval = sendto(resfd, buf, n, 0,
191 (struct sockaddr *)&_res.nsaddr_list[ns],
192 sizeof(struct sockaddr));
194 yp_error("sendto failed");
202 static struct circleq_dnsentry *
203 yp_find_dnsqent(unsigned long id, int type)
205 register struct circleq_dnsentry *q;
207 TAILQ_FOREACH(q, &qhead, links) {
224 yp_send_dns_reply(struct circleq_dnsentry *q, char *buf)
226 ypresponse result_v1;
227 ypresp_val result_v2;
229 struct sockaddr_in client_addr;
234 * Set up correct reply struct and
235 * XDR filter depending on ypvers.
239 bzero((char *)&result_v2, sizeof(result_v2));
242 result_v2.stat = YP_NOKEY;
244 result_v2.val.valdat_len = strlen(buf);
245 result_v2.val.valdat_val = buf;
246 result_v2.stat = YP_TRUE;
248 result = (char *)&result_v2;
249 xdrfunc = (xdrproc_t)xdr_ypresp_val;
253 * The odds are we will _never_ execute this
254 * particular code, but we include it anyway
255 * for the sake of completeness.
257 bzero((char *)&result_v1, sizeof(result_v1));
258 result_v1.yp_resptype = YPRESP_VAL;
260 #define YPVAL ypresponse_u.yp_resp_valtype
262 result_v1.YPVAL.stat = YP_NOKEY;
264 result_v1.YPVAL.val.valdat_len = strlen(buf);
265 result_v1.YPVAL.val.valdat_val = buf;
266 result_v1.YPVAL.stat = YP_TRUE;
268 result = (char *)&result_v1;
269 xdrfunc = (xdrproc_t)xdr_ypresponse;
272 yp_error("bad YP program version (%lu)!", q->ypvers);
278 yp_error("sending dns reply to %s (%lu)",
279 inet_ntoa(q->client_addr.sin_addr), q->id);
281 * XXX This is disgusting. There's basically one transport
282 * handle for UDP, but we're holding off on replying to a
283 * client until we're ready, by which time we may have received
284 * several other queries from other clients with different
285 * transaction IDs. So to make the delayed response thing work,
286 * we have to save the transaction ID and client address of
287 * each request, then jam them into the transport handle when
288 * we're ready to send a reply. Then after we've send the reply,
289 * we put the old transaction ID and remote address back the
290 * way we found 'em. This is _INCREDIBLY_ non-portable; it's
291 * not even supported by the RPC library.
294 * XXX Don't frob the transaction ID for TCP handles.
296 if (q->prot_type == SOCK_DGRAM)
297 xid = svcudp_set_xid(q->xprt, q->xid);
298 client_addr = q->xprt->xp_raddr;
299 q->xprt->xp_raddr = q->client_addr;
301 if (!svc_sendreply(q->xprt, xdrfunc, result))
302 yp_error("svc_sendreply failed");
305 * Now that we sent the reply,
306 * put the handle back the way it was.
308 if (q->prot_type == SOCK_DGRAM)
309 svcudp_set_xid(q->xprt, xid);
310 q->xprt->xp_raddr = client_addr;
316 * Decrement TTL on all queue entries, possibly nuking
317 * any that have been around too long without being serviced.
322 register struct circleq_dnsentry *q, *n;
324 q = TAILQ_FIRST(&qhead);
327 n = TAILQ_NEXT(q, links);
329 TAILQ_REMOVE(&qhead, q, links);
344 * Data is pending on the DNS socket; check for valid replies
345 * to our queries and dispatch them to waiting clients.
350 register struct circleq_dnsentry *q;
351 char buf[sizeof(HEADER) + MAXPACKET];
352 struct sockaddr_in sin;
356 struct hostent *hent;
359 yp_error("running dns queue");
361 bzero(buf, sizeof(buf));
363 len = sizeof(struct sockaddr_in);
364 rval = recvfrom(resfd, buf, sizeof(buf), 0,
365 (struct sockaddr *)&sin, &len);
368 yp_error("recvfrom failed: %s", strerror(errno));
373 * We may have data left in the socket that represents
374 * replies to earlier queries that we don't care about
375 * anymore. If there are no lookups pending or the packet
376 * ID doesn't match any of the queue IDs, just drop it
379 hptr = (HEADER *)&buf;
381 (q = yp_find_dnsqent(ntohs(hptr->id), BY_DNS_ID)) == NULL) {
387 yp_error("got dns reply from %s", inet_ntoa(sin.sin_addr));
389 hent = __dns_getanswer(buf, rval, q->name, q->type);
392 if (q->type == T_PTR) {
393 hent->h_addr = (char *)q->addr;
394 hent->h_addrtype = q->addrtype;
395 hent->h_length = q->addrlen;
399 /* Got an answer ready for a client -- send it off. */
400 yp_send_dns_reply(q, parse(hent));
402 TAILQ_REMOVE(&qhead, q, links);
406 /* Decrement TTLs on other entries while we're here. */
413 * Queue and transmit an asynchronous DNS hostname lookup.
416 yp_async_lookup_name(struct svc_req *rqstp, char *name, int af)
418 register struct circleq_dnsentry *q;
422 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
425 if (getsockopt(rqstp->rq_xprt->xp_fd, SOL_SOCKET,
426 SO_TYPE, &type, &len) == -1) {
427 yp_error("getsockopt failed: %s", strerror(errno));
431 /* Avoid transmitting dupe requests. */
432 if (type == SOCK_DGRAM &&
433 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL)
436 if ((q = yp_malloc_dnsent()) == NULL)
439 q->type = (af == AF_INET) ? T_A : T_AAAA;
441 q->xprt = rqstp->rq_xprt;
442 q->ypvers = rqstp->rq_vers;
444 if (q->prot_type == SOCK_DGRAM)
445 q->xid = svcudp_get_xid(q->xprt);
446 q->client_addr = q->xprt->xp_raddr;
447 q->domain = _res.dnsrch;
448 q->id = yp_send_dns_query(name, q->type);
451 yp_error("DNS query failed");
456 q->name = strdup(name);
457 TAILQ_INSERT_HEAD(&qhead, q, links);
461 yp_error("queueing async DNS name lookup (%lu)", q->id);
468 * Queue and transmit an asynchronous DNS IP address lookup.
471 yp_async_lookup_addr(struct svc_req *rqstp, char *addr, int af)
473 register struct circleq_dnsentry *q;
474 char buf[MAXHOSTNAMELEN], *qp;
475 uint32_t abuf[4]; /* IPv4 or IPv6 */
476 u_char *uaddr = (u_char *)abuf;
480 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
483 if (getsockopt(rqstp->rq_xprt->xp_fd, SOL_SOCKET,
484 SO_TYPE, &type, &len) == -1) {
485 yp_error("getsockopt failed: %s", strerror(errno));
489 /* Avoid transmitting dupe requests. */
490 if (type == SOCK_DGRAM &&
491 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL)
496 if (inet_aton(addr, (struct in_addr *)uaddr) != 1)
498 snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa",
499 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
500 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
504 if (inet_pton(af, addr, uaddr) != 1)
507 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
508 qp += (size_t)sprintf(qp, "%x.%x.", uaddr[n] & 0xf,
509 (uaddr[n] >> 4) & 0xf);
511 strlcat(buf, "ip6.arpa", sizeof(buf));
518 if ((q = yp_malloc_dnsent()) == NULL)
522 yp_error("DNS address is: %s", buf);
526 q->xprt = rqstp->rq_xprt;
527 q->ypvers = rqstp->rq_vers;
530 if (q->prot_type == SOCK_DGRAM)
531 q->xid = svcudp_get_xid(q->xprt);
532 q->client_addr = q->xprt->xp_raddr;
533 q->id = yp_send_dns_query(buf, q->type);
536 yp_error("DNS query failed");
541 memcpy(q->addr, uaddr, len);
544 q->name = strdup(buf);
545 TAILQ_INSERT_HEAD(&qhead, q, links);
549 yp_error("queueing async DNS address lookup (%lu)", q->id);