2 * Copyright (c) 1995, 1996
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/usr.sbin/ypserv/yp_dnslookup.c,v 1.16.2.1 2002/02/15 00:47:00 des Exp $
33 * $DragonFly: src/usr.sbin/ypserv/yp_dnslookup.c,v 1.3 2004/03/31 23:20:22 cpressey Exp $
37 * Do standard and reverse DNS lookups using the resolver library.
38 * Take care of all the dirty work here so the main program only has to
39 * pass us a pointer to an array of characters.
41 * We have to use direct resolver calls here otherwise the YP server
42 * could end up looping by calling itself over and over again until
43 * it disappeared up its own belly button.
46 #include <sys/param.h>
47 #include <sys/socket.h>
49 #include <sys/fcntl.h>
50 #include <sys/queue.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <arpa/nameser.h>
64 #include <rpcsvc/yp.h>
65 #include "yp_extern.h"
68 parse(struct hostent *hp)
70 static char result[MAXHOSTNAMELEN * 2];
77 len = 16 + strlen(hp->h_name);
78 for (i = 0; hp->h_aliases[i]; i++)
79 len += strlen(hp->h_aliases[i]) + 1;
82 if (len > sizeof(result))
85 bzero(result, sizeof(result));
87 bcopy(hp->h_addr, &addr, sizeof(struct in_addr));
88 snprintf(result, sizeof(result), "%s %s", inet_ntoa(addr), hp->h_name);
90 for (i = 0; hp->h_aliases[i]; i++) {
92 strcat(result, hp->h_aliases[i]);
95 return ((char *)&result);
98 #define MAXPACKET 1024
104 extern struct hostent *__dns_getanswer(char *, int, char *, int);
106 static CIRCLEQ_HEAD(dns_qhead, circleq_dnsentry) qhead;
108 struct circleq_dnsentry {
111 struct sockaddr_in client_addr;
112 unsigned long ypvers;
116 unsigned short prot_type;
120 CIRCLEQ_ENTRY(circleq_dnsentry) links;
123 static int pending = 0;
126 yp_init_resolver(void)
128 CIRCLEQ_INIT(&qhead);
129 if (!(_res.options & RES_INIT) && res_init() == -1) {
130 yp_error("res_init failed");
133 if ((resfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
134 yp_error("couldn't create socket");
137 if (fcntl(resfd, F_SETFL, O_NONBLOCK) == -1) {
138 yp_error("couldn't make resolver socket non-blocking");
144 static struct circleq_dnsentry *
145 yp_malloc_dnsent(void)
147 struct circleq_dnsentry *q;
149 q = (struct circleq_dnsentry *)malloc(sizeof(struct circleq_dnsentry));
152 yp_error("failed to malloc() circleq dns entry");
163 yp_send_dns_query(char *name, int type)
172 bzero(buf, sizeof(buf));
174 n = res_mkquery(QUERY,name,C_IN,type,NULL,0,NULL,buf,sizeof(buf));
177 yp_error("res_mkquery failed");
181 hptr = (HEADER *)&buf;
182 id = ntohs(hptr->id);
184 for (ns = 0; ns < _res.nscount; ns++) {
185 rval = sendto(resfd, buf, n, 0,
186 (struct sockaddr *)&_res.nsaddr_list[ns],
187 sizeof(struct sockaddr));
189 yp_error("sendto failed");
197 static struct circleq_dnsentry *
198 yp_find_dnsqent(unsigned long id, int type)
200 struct circleq_dnsentry *q;
202 for (q = qhead.cqh_first; q != (void *)&qhead; q = q->links.cqe_next) {
219 yp_send_dns_reply(struct circleq_dnsentry *q, char *buf)
221 ypresponse result_v1;
222 ypresp_val result_v2;
224 struct sockaddr_in client_addr;
229 * Set up correct reply struct and
230 * XDR filter depending on ypvers.
234 bzero((char *)&result_v2, sizeof(result_v2));
237 result_v2.stat = YP_NOKEY;
239 result_v2.val.valdat_len = strlen(buf);
240 result_v2.val.valdat_val = buf;
241 result_v2.stat = YP_TRUE;
243 result = (char *)&result_v2;
244 xdrfunc = (xdrproc_t)xdr_ypresp_val;
248 * The odds are we will _never_ execute this
249 * particular code, but we include it anyway
250 * for the sake of completeness.
252 bzero((char *)&result_v1, sizeof(result_v1));
253 result_v1.yp_resptype = YPRESP_VAL;
254 # define YPVAL ypresponse_u.yp_resp_valtype
257 result_v1.YPVAL.stat = YP_NOKEY;
259 result_v1.YPVAL.val.valdat_len = strlen(buf);
260 result_v1.YPVAL.val.valdat_val = buf;
261 result_v1.YPVAL.stat = YP_TRUE;
263 result = (char *)&result_v1;
264 xdrfunc = (xdrproc_t)xdr_ypresponse;
267 yp_error("bad YP program version (%lu)!", q->ypvers);
273 yp_error("sending dns reply to %s (%lu)",
274 inet_ntoa(q->client_addr.sin_addr), q->id);
276 * XXX This is disgusting. There's basically one transport
277 * handle for UDP, but we're holding off on replying to a
278 * client until we're ready, by which time we may have received
279 * several other queries from other clients with different
280 * transaction IDs. So to make the delayed response thing work,
281 * we have to save the transaction ID and client address of
282 * each request, then jam them into the transport handle when
283 * we're ready to send a reply. Then after we've send the reply,
284 * we put the old transaction ID and remote address back the
285 * way we found 'em. This is _INCREDIBLY_ non-portable; it's
286 * not even supported by the RPC library.
289 * XXX Don't frob the transaction ID for TCP handles.
291 if (q->prot_type == SOCK_DGRAM)
292 xid = svcudp_set_xid(q->xprt, q->xid);
293 client_addr = q->xprt->xp_raddr;
294 q->xprt->xp_raddr = q->client_addr;
296 if (!svc_sendreply(q->xprt, xdrfunc, result))
297 yp_error("svc_sendreply failed");
300 * Now that we sent the reply,
301 * put the handle back the way it was.
303 if (q->prot_type == SOCK_DGRAM)
304 svcudp_set_xid(q->xprt, xid);
305 q->xprt->xp_raddr = client_addr;
311 * Decrement TTL on all queue entries, possibly nuking
312 * any that have been around too long without being serviced.
317 struct circleq_dnsentry *q, *n;
320 while (q != (void *)&qhead) {
322 n = q->links.cqe_next;
324 CIRCLEQ_REMOVE(&qhead, q, links);
339 * Data is pending on the DNS socket; check for valid replies
340 * to our queries and dispatch them to waiting clients.
345 struct circleq_dnsentry *q;
346 char buf[sizeof(HEADER) + MAXPACKET];
347 char retrybuf[MAXHOSTNAMELEN];
348 struct sockaddr_in sin;
352 struct hostent *hent;
355 yp_error("running dns queue");
357 bzero(buf, sizeof(buf));
359 len = sizeof(struct sockaddr_in);
360 rval = recvfrom(resfd, buf, sizeof(buf), 0,
361 (struct sockaddr *)&sin, &len);
364 yp_error("recvfrom failed: %s", strerror(errno));
369 * We may have data left in the socket that represents
370 * replies to earlier queries that we don't care about
371 * anymore. If there are no lookups pending or the packet
372 * ID doesn't match any of the queue IDs, just drop it
375 hptr = (HEADER *)&buf;
377 (q = yp_find_dnsqent(ntohs(hptr->id), BY_DNS_ID)) == NULL) {
383 yp_error("got dns reply from %s", inet_ntoa(sin.sin_addr));
385 hent = __dns_getanswer(buf, rval, q->name, q->type);
388 * If the lookup failed, try appending one of the domains
389 * from resolv.conf. If we have no domains to test, the
393 if ((h_errno == TRY_AGAIN || h_errno == NO_RECOVERY)
394 && q->domain && *q->domain) {
395 snprintf(retrybuf, sizeof(retrybuf), "%s.%s",
396 q->name, *q->domain);
398 yp_error("retrying with: %s", retrybuf);
399 q->id = yp_send_dns_query(retrybuf, q->type);
405 if (q->type == T_PTR) {
406 hent->h_addr = (char *)&q->addr.s_addr;
407 hent->h_length = sizeof(struct in_addr);
411 /* Got an answer ready for a client -- send it off. */
412 yp_send_dns_reply(q, parse(hent));
414 CIRCLEQ_REMOVE(&qhead, q, links);
418 /* Decrement TTLs on other entries while we're here. */
425 * Queue and transmit an asynchronous DNS hostname lookup.
428 yp_async_lookup_name(struct svc_req *rqstp, char *name)
430 struct circleq_dnsentry *q;
433 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
434 type = -1; len = sizeof(type);
435 if (getsockopt(rqstp->rq_xprt->xp_sock, SOL_SOCKET,
436 SO_TYPE, &type, &len) == -1) {
437 yp_error("getsockopt failed: %s", strerror(errno));
441 /* Avoid transmitting dupe requests. */
442 if (type == SOCK_DGRAM &&
443 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL)
446 if ((q = yp_malloc_dnsent()) == NULL)
451 q->xprt = rqstp->rq_xprt;
452 q->ypvers = rqstp->rq_vers;
454 if (q->prot_type == SOCK_DGRAM)
455 q->xid = svcudp_get_xid(q->xprt);
456 q->client_addr = q->xprt->xp_raddr;
457 q->domain = _res.dnsrch;
458 q->id = yp_send_dns_query(name, q->type);
461 yp_error("DNS query failed");
466 q->name = strdup(name);
467 CIRCLEQ_INSERT_HEAD(&qhead, q, links);
471 yp_error("queueing async DNS name lookup (%d)", q->id);
478 * Queue and transmit an asynchronous DNS IP address lookup.
480 ypstat yp_async_lookup_addr(struct svc_req *rqstp, char *addr)
482 struct circleq_dnsentry *q;
483 char buf[MAXHOSTNAMELEN];
487 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
488 type = -1; len = sizeof(type);
489 if (getsockopt(rqstp->rq_xprt->xp_sock, SOL_SOCKET,
490 SO_TYPE, &type, &len) == -1) {
491 yp_error("getsockopt failed: %s", strerror(errno));
495 /* Avoid transmitting dupe requests. */
496 if (type == SOCK_DGRAM &&
497 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL)
500 if ((q = yp_malloc_dnsent()) == NULL)
503 if (sscanf(addr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
506 snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa", d, c, b, a);
509 yp_error("DNS address is: %s", buf);
513 q->xprt = rqstp->rq_xprt;
514 q->ypvers = rqstp->rq_vers;
517 if (q->prot_type == SOCK_DGRAM)
518 q->xid = svcudp_get_xid(q->xprt);
519 q->client_addr = q->xprt->xp_raddr;
520 q->id = yp_send_dns_query(buf, q->type);
523 yp_error("DNS query failed");
528 inet_aton(addr, &q->addr);
529 q->name = strdup(buf);
530 CIRCLEQ_INSERT_HEAD(&qhead, q, links);
534 yp_error("queueing async DNS address lookup (%d)", q->id);