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
34 static const char rcsid[] =
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"
69 static char *parse(hp)
72 static char result[MAXHOSTNAMELEN * 2];
79 len = 16 + strlen(hp->h_name);
80 for (i = 0; hp->h_aliases[i]; i++)
81 len += strlen(hp->h_aliases[i]) + 1;
84 if (len > sizeof(result))
87 bzero(result, sizeof(result));
89 bcopy(hp->h_addr, &addr, sizeof(struct in_addr));
90 snprintf(result, sizeof(result), "%s %s", inet_ntoa(addr), hp->h_name);
92 for (i = 0; hp->h_aliases[i]; i++) {
94 strcat(result, hp->h_aliases[i]);
97 return ((char *)&result);
100 #define MAXPACKET 1024
106 extern struct hostent *__dns_getanswer __P((char *, int, char *, int));
108 static CIRCLEQ_HEAD(dns_qhead, circleq_dnsentry) qhead;
110 struct circleq_dnsentry {
113 struct sockaddr_in client_addr;
114 unsigned long ypvers;
118 unsigned short prot_type;
122 CIRCLEQ_ENTRY(circleq_dnsentry) links;
125 static int pending = 0;
127 int yp_init_resolver()
129 CIRCLEQ_INIT(&qhead);
130 if (!(_res.options & RES_INIT) && res_init() == -1) {
131 yp_error("res_init failed");
134 if ((resfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
135 yp_error("couldn't create socket");
138 if (fcntl(resfd, F_SETFL, O_NONBLOCK) == -1) {
139 yp_error("couldn't make resolver socket non-blocking");
145 static struct circleq_dnsentry *yp_malloc_dnsent()
147 register struct circleq_dnsentry *q;
149 q = (struct circleq_dnsentry *)malloc(sizeof(struct circleq_dnsentry));
152 yp_error("failed to malloc() circleq dns entry");
162 static unsigned long yp_send_dns_query(name, type)
173 bzero(buf, sizeof(buf));
175 n = res_mkquery(QUERY,name,C_IN,type,NULL,0,NULL,buf,sizeof(buf));
178 yp_error("res_mkquery failed");
182 hptr = (HEADER *)&buf;
183 id = ntohs(hptr->id);
185 for (ns = 0; ns < _res.nscount; ns++) {
186 rval = sendto(resfd, buf, n, 0,
187 (struct sockaddr *)&_res.nsaddr_list[ns],
188 sizeof(struct sockaddr));
190 yp_error("sendto failed");
198 static struct circleq_dnsentry *yp_find_dnsqent(id, type)
202 register struct circleq_dnsentry *q;
204 for (q = qhead.cqh_first; q != (void *)&qhead; q = q->links.cqe_next) {
220 static void yp_send_dns_reply(q, buf)
221 struct circleq_dnsentry *q;
224 ypresponse result_v1;
225 ypresp_val result_v2;
227 struct sockaddr_in client_addr;
232 * Set up correct reply struct and
233 * XDR filter depending on ypvers.
237 bzero((char *)&result_v2, sizeof(result_v2));
240 result_v2.stat = YP_NOKEY;
242 result_v2.val.valdat_len = strlen(buf);
243 result_v2.val.valdat_val = buf;
244 result_v2.stat = YP_TRUE;
246 result = (char *)&result_v2;
247 xdrfunc = (xdrproc_t)xdr_ypresp_val;
251 * The odds are we will _never_ execute this
252 * particular code, but we include it anyway
253 * for the sake of completeness.
255 bzero((char *)&result_v1, sizeof(result_v1));
256 result_v1.yp_resptype = YPRESP_VAL;
257 # define YPVAL ypresponse_u.yp_resp_valtype
260 result_v1.YPVAL.stat = YP_NOKEY;
262 result_v1.YPVAL.val.valdat_len = strlen(buf);
263 result_v1.YPVAL.val.valdat_val = buf;
264 result_v1.YPVAL.stat = YP_TRUE;
266 result = (char *)&result_v1;
267 xdrfunc = (xdrproc_t)xdr_ypresponse;
270 yp_error("bad YP program version (%lu)!", q->ypvers);
276 yp_error("sending dns reply to %s (%lu)",
277 inet_ntoa(q->client_addr.sin_addr), q->id);
279 * XXX This is disgusting. There's basically one transport
280 * handle for UDP, but we're holding off on replying to a
281 * client until we're ready, by which time we may have received
282 * several other queries from other clients with different
283 * transaction IDs. So to make the delayed response thing work,
284 * we have to save the transaction ID and client address of
285 * each request, then jam them into the transport handle when
286 * we're ready to send a reply. Then after we've send the reply,
287 * we put the old transaction ID and remote address back the
288 * way we found 'em. This is _INCREDIBLY_ non-portable; it's
289 * not even supported by the RPC library.
292 * XXX Don't frob the transaction ID for TCP handles.
294 if (q->prot_type == SOCK_DGRAM)
295 xid = svcudp_set_xid(q->xprt, q->xid);
296 client_addr = q->xprt->xp_raddr;
297 q->xprt->xp_raddr = q->client_addr;
299 if (!svc_sendreply(q->xprt, xdrfunc, result))
300 yp_error("svc_sendreply failed");
303 * Now that we sent the reply,
304 * put the handle back the way it was.
306 if (q->prot_type == SOCK_DGRAM)
307 svcudp_set_xid(q->xprt, xid);
308 q->xprt->xp_raddr = client_addr;
314 * Decrement TTL on all queue entries, possibly nuking
315 * any that have been around too long without being serviced.
319 register struct circleq_dnsentry *q, *n;
322 while(q != (void *)&qhead) {
324 n = q->links.cqe_next;
326 CIRCLEQ_REMOVE(&qhead, q, links);
341 * Data is pending on the DNS socket; check for valid replies
342 * to our queries and dispatch them to waiting clients.
346 register struct circleq_dnsentry *q;
347 char buf[sizeof(HEADER) + MAXPACKET];
348 char retrybuf[MAXHOSTNAMELEN];
349 struct sockaddr_in sin;
353 struct hostent *hent;
356 yp_error("running dns queue");
358 bzero(buf, sizeof(buf));
360 len = sizeof(struct sockaddr_in);
361 rval = recvfrom(resfd, buf, sizeof(buf), 0,
362 (struct sockaddr *)&sin, &len);
365 yp_error("recvfrom failed: %s", strerror(errno));
370 * We may have data left in the socket that represents
371 * replies to earlier queries that we don't care about
372 * anymore. If there are no lookups pending or the packet
373 * ID doesn't match any of the queue IDs, just drop it
376 hptr = (HEADER *)&buf;
378 (q = yp_find_dnsqent(ntohs(hptr->id), BY_DNS_ID)) == NULL) {
384 yp_error("got dns reply from %s", inet_ntoa(sin.sin_addr));
386 hent = __dns_getanswer(buf, rval, q->name, q->type);
389 * If the lookup failed, try appending one of the domains
390 * from resolv.conf. If we have no domains to test, the
394 if ((h_errno == TRY_AGAIN || h_errno == NO_RECOVERY)
395 && q->domain && *q->domain) {
396 snprintf(retrybuf, sizeof(retrybuf), "%s.%s",
397 q->name, *q->domain);
399 yp_error("retrying with: %s", retrybuf);
400 q->id = yp_send_dns_query(retrybuf, q->type);
406 if (q->type == T_PTR) {
407 hent->h_addr = (char *)&q->addr.s_addr;
408 hent->h_length = sizeof(struct in_addr);
412 /* Got an answer ready for a client -- send it off. */
413 yp_send_dns_reply(q, parse(hent));
415 CIRCLEQ_REMOVE(&qhead, q, links);
419 /* Decrement TTLs on other entries while we're here. */
426 * Queue and transmit an asynchronous DNS hostname lookup.
428 ypstat yp_async_lookup_name(rqstp, name)
429 struct svc_req *rqstp;
432 register struct circleq_dnsentry *q;
435 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
436 type = -1; len = sizeof(type);
437 if (getsockopt(rqstp->rq_xprt->xp_sock, SOL_SOCKET,
438 SO_TYPE, &type, &len) == -1) {
439 yp_error("getsockopt failed: %s", strerror(errno));
443 /* Avoid transmitting dupe requests. */
444 if (type == SOCK_DGRAM &&
445 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL)
448 if ((q = yp_malloc_dnsent()) == NULL)
453 q->xprt = rqstp->rq_xprt;
454 q->ypvers = rqstp->rq_vers;
456 if (q->prot_type == SOCK_DGRAM)
457 q->xid = svcudp_get_xid(q->xprt);
458 q->client_addr = q->xprt->xp_raddr;
459 q->domain = _res.dnsrch;
460 q->id = yp_send_dns_query(name, q->type);
463 yp_error("DNS query failed");
468 q->name = strdup(name);
469 CIRCLEQ_INSERT_HEAD(&qhead, q, links);
473 yp_error("queueing async DNS name lookup (%d)", q->id);
480 * Queue and transmit an asynchronous DNS IP address lookup.
482 ypstat yp_async_lookup_addr(rqstp, addr)
483 struct svc_req *rqstp;
486 register struct circleq_dnsentry *q;
487 char buf[MAXHOSTNAMELEN];
491 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
492 type = -1; len = sizeof(type);
493 if (getsockopt(rqstp->rq_xprt->xp_sock, SOL_SOCKET,
494 SO_TYPE, &type, &len) == -1) {
495 yp_error("getsockopt failed: %s", strerror(errno));
499 /* Avoid transmitting dupe requests. */
500 if (type == SOCK_DGRAM &&
501 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL)
504 if ((q = yp_malloc_dnsent()) == NULL)
507 if (sscanf(addr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
510 snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa", d, c, b, a);
513 yp_error("DNS address is: %s", buf);
517 q->xprt = rqstp->rq_xprt;
518 q->ypvers = rqstp->rq_vers;
521 if (q->prot_type == SOCK_DGRAM)
522 q->xid = svcudp_get_xid(q->xprt);
523 q->client_addr = q->xprt->xp_raddr;
524 q->id = yp_send_dns_query(buf, q->type);
527 yp_error("DNS query failed");
532 inet_aton(addr, &q->addr);
533 q->name = strdup(buf);
534 CIRCLEQ_INSERT_HEAD(&qhead, q, links);
538 yp_error("queueing async DNS address lookup (%d)", q->id);