xref: /freebsd/usr.sbin/ypserv/yp_dnslookup.c (revision b5ebd8bb4e90b810c834aabb78c87054ab84db8e)
1*df57947fSPedro F. Giffuni /*-
2*df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni  *
4adc4fa33SBill Paul  * Copyright (c) 1995, 1996
5778c7b1cSBill Paul  *	Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
6778c7b1cSBill Paul  *
7778c7b1cSBill Paul  * Redistribution and use in source and binary forms, with or without
8778c7b1cSBill Paul  * modification, are permitted provided that the following conditions
9778c7b1cSBill Paul  * are met:
10778c7b1cSBill Paul  * 1. Redistributions of source code must retain the above copyright
11778c7b1cSBill Paul  *    notice, this list of conditions and the following disclaimer.
12778c7b1cSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
13778c7b1cSBill Paul  *    notice, this list of conditions and the following disclaimer in the
14778c7b1cSBill Paul  *    documentation and/or other materials provided with the distribution.
15778c7b1cSBill Paul  * 3. All advertising materials mentioning features or use of this software
16778c7b1cSBill Paul  *    must display the following acknowledgement:
17778c7b1cSBill Paul  *	This product includes software developed by Bill Paul.
18778c7b1cSBill Paul  * 4. Neither the name of the University nor the names of its contributors
19778c7b1cSBill Paul  *    may be used to endorse or promote products derived from this software
20778c7b1cSBill Paul  *    without specific prior written permission.
21778c7b1cSBill Paul  *
22778c7b1cSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23778c7b1cSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24778c7b1cSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25778c7b1cSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26778c7b1cSBill Paul  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27778c7b1cSBill Paul  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28778c7b1cSBill Paul  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29778c7b1cSBill Paul  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30778c7b1cSBill Paul  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31778c7b1cSBill Paul  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32778c7b1cSBill Paul  * SUCH DAMAGE.
33778c7b1cSBill Paul  */
34778c7b1cSBill Paul 
35b728350eSDavid E. O'Brien #include <sys/cdefs.h>
36778c7b1cSBill Paul /*
37778c7b1cSBill Paul  * Do standard and reverse DNS lookups using the resolver library.
38778c7b1cSBill Paul  * Take care of all the dirty work here so the main program only has to
39778c7b1cSBill Paul  * pass us a pointer to an array of characters.
40778c7b1cSBill Paul  *
41778c7b1cSBill Paul  * We have to use direct resolver calls here otherwise the YP server
42778c7b1cSBill Paul  * could end up looping by calling itself over and over again until
43778c7b1cSBill Paul  * it disappeared up its own belly button.
44778c7b1cSBill Paul  */
45778c7b1cSBill Paul 
46778c7b1cSBill Paul #include <sys/param.h>
47778c7b1cSBill Paul #include <sys/socket.h>
48180807d2SBill Paul #include <sys/time.h>
49180807d2SBill Paul #include <sys/fcntl.h>
50180807d2SBill Paul #include <sys/queue.h>
51180807d2SBill Paul #include <netinet/in.h>
52778c7b1cSBill Paul #include <arpa/inet.h>
53180807d2SBill Paul #include <arpa/nameser.h>
54180807d2SBill Paul 
55180807d2SBill Paul #include <ctype.h>
5698834523SPhilippe Charnier #include <errno.h>
57180807d2SBill Paul #include <netdb.h>
5898834523SPhilippe Charnier #include <stdio.h>
59180807d2SBill Paul #include <stdlib.h>
60180807d2SBill Paul #include <string.h>
6198834523SPhilippe Charnier #include <resolv.h>
6298834523SPhilippe Charnier #include <unistd.h>
63180807d2SBill Paul 
64180807d2SBill Paul #include <rpcsvc/yp.h>
65778c7b1cSBill Paul #include "yp_extern.h"
66778c7b1cSBill Paul 
67dc584ddbSDag-Erling Smørgrav static char *
parse(struct hostent * hp)68dc584ddbSDag-Erling Smørgrav parse(struct hostent *hp)
69778c7b1cSBill Paul {
70778c7b1cSBill Paul 	static char result[MAXHOSTNAMELEN * 2];
714b937b70SPhilippe Charnier 	int i;
724b937b70SPhilippe Charnier 	size_t len;
734e5a7758SHajimu UMEMOTO 	char addr[46];
74778c7b1cSBill Paul 
75180807d2SBill Paul 	if (hp == NULL)
76180807d2SBill Paul 		return(NULL);
77180807d2SBill Paul 
784e5a7758SHajimu UMEMOTO 	if (inet_ntop(hp->h_addrtype, hp->h_addr, addr, sizeof(addr)) == NULL)
794e5a7758SHajimu UMEMOTO 		return(NULL);
804e5a7758SHajimu UMEMOTO 
814e5a7758SHajimu UMEMOTO 	len = strlen(addr) + 1 + strlen(hp->h_name);
82778c7b1cSBill Paul 	for (i = 0; hp->h_aliases[i]; i++)
83778c7b1cSBill Paul 		len += strlen(hp->h_aliases[i]) + 1;
8467d3ec9aSWarner Losh 	len++;
8567d3ec9aSWarner Losh 
8667d3ec9aSWarner Losh 	if (len > sizeof(result))
8767d3ec9aSWarner Losh 		return(NULL);
88778c7b1cSBill Paul 
89778c7b1cSBill Paul 	bzero(result, sizeof(result));
904e5a7758SHajimu UMEMOTO 	snprintf(result, sizeof(result), "%s %s", addr, hp->h_name);
91778c7b1cSBill Paul 	for (i = 0; hp->h_aliases[i]; i++) {
92778c7b1cSBill Paul 		strcat(result, " ");
93778c7b1cSBill Paul 		strcat(result, hp->h_aliases[i]);
94778c7b1cSBill Paul 	}
95778c7b1cSBill Paul 
96778c7b1cSBill Paul 	return ((char *)&result);
97778c7b1cSBill Paul }
98778c7b1cSBill Paul 
994e5a7758SHajimu UMEMOTO #define MAXPACKET (64*1024)
100180807d2SBill Paul #define DEF_TTL 50
101180807d2SBill Paul 
1029c171de0SBill Paul #define BY_DNS_ID 1
1039c171de0SBill Paul #define BY_RPC_XID 2
1049c171de0SBill Paul 
105ed4d1c46SDag-Erling Smørgrav extern struct hostent *__dns_getanswer(char *, int, char *, int);
106180807d2SBill Paul 
107c5e5cd90SPoul-Henning Kamp static TAILQ_HEAD(dns_qhead, circleq_dnsentry) qhead;
108180807d2SBill Paul 
109180807d2SBill Paul struct circleq_dnsentry {
110180807d2SBill Paul 	SVCXPRT *xprt;
111180807d2SBill Paul 	unsigned long xid;
112180807d2SBill Paul 	struct sockaddr_in client_addr;
113adc4fa33SBill Paul 	unsigned long ypvers;
114180807d2SBill Paul 	unsigned long id;
115180807d2SBill Paul 	unsigned long ttl;
116180807d2SBill Paul 	unsigned long type;
117926f037aSBill Paul 	unsigned short prot_type;
118180807d2SBill Paul 	char **domain;
119180807d2SBill Paul 	char *name;
1204e5a7758SHajimu UMEMOTO 	int addrtype;
1214e5a7758SHajimu UMEMOTO 	int addrlen;
1224e5a7758SHajimu UMEMOTO 	uint32_t addr[4];	/* IPv4 or IPv6 */
123c5e5cd90SPoul-Henning Kamp 	TAILQ_ENTRY(circleq_dnsentry) links;
124180807d2SBill Paul };
125180807d2SBill Paul 
126180807d2SBill Paul static int pending = 0;
127180807d2SBill Paul 
128dc584ddbSDag-Erling Smørgrav int
yp_init_resolver(void)129dc584ddbSDag-Erling Smørgrav yp_init_resolver(void)
130778c7b1cSBill Paul {
131c5e5cd90SPoul-Henning Kamp 	TAILQ_INIT(&qhead);
132180807d2SBill Paul 	if (!(_res.options & RES_INIT) && res_init() == -1) {
133180807d2SBill Paul 		yp_error("res_init failed");
134180807d2SBill Paul 		return(1);
135180807d2SBill Paul 	}
136180807d2SBill Paul 	if ((resfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
137180807d2SBill Paul 		yp_error("couldn't create socket");
138180807d2SBill Paul 		return(1);
139180807d2SBill Paul 	}
140180807d2SBill Paul 	if (fcntl(resfd, F_SETFL, O_NONBLOCK) == -1) {
141180807d2SBill Paul 		yp_error("couldn't make resolver socket non-blocking");
142180807d2SBill Paul 		return(1);
143180807d2SBill Paul 	}
144180807d2SBill Paul 	return(0);
145778c7b1cSBill Paul }
146778c7b1cSBill Paul 
147dc584ddbSDag-Erling Smørgrav static struct
yp_malloc_dnsent(void)148dc584ddbSDag-Erling Smørgrav circleq_dnsentry *yp_malloc_dnsent(void)
149180807d2SBill Paul {
150180807d2SBill Paul 	register struct circleq_dnsentry *q;
151778c7b1cSBill Paul 
152650dae44SMarcelo Araujo 	q = malloc(sizeof(struct circleq_dnsentry));
153180807d2SBill Paul 
154180807d2SBill Paul 	if (q == NULL) {
15598834523SPhilippe Charnier 		yp_error("failed to malloc() circleq dns entry");
156180807d2SBill Paul 		return(NULL);
157180807d2SBill Paul 	}
158180807d2SBill Paul 
159180807d2SBill Paul 	return(q);
160180807d2SBill Paul }
161180807d2SBill Paul 
162180807d2SBill Paul /*
163180807d2SBill Paul  * Transmit a query.
164180807d2SBill Paul  */
165dc584ddbSDag-Erling Smørgrav static unsigned long
yp_send_dns_query(char * name,int type)166dc584ddbSDag-Erling Smørgrav yp_send_dns_query(char *name, int type)
167180807d2SBill Paul {
168180807d2SBill Paul 	char buf[MAXPACKET];
169180807d2SBill Paul 	int n;
170180807d2SBill Paul 	HEADER *hptr;
171180807d2SBill Paul 	int ns;
172180807d2SBill Paul 	int rval;
173180807d2SBill Paul 	unsigned long id;
174180807d2SBill Paul 
175180807d2SBill Paul 	bzero(buf, sizeof(buf));
176180807d2SBill Paul 
177180807d2SBill Paul 	n = res_mkquery(QUERY,name,C_IN,type,NULL,0,NULL,buf,sizeof(buf));
178180807d2SBill Paul 
179180807d2SBill Paul 	if (n <= 0) {
18015b23bddSMark Murray 		yp_error("res_mkquery failed for %s type %d", name, type);
181180807d2SBill Paul 		return(0);
182180807d2SBill Paul 	}
183180807d2SBill Paul 
184180807d2SBill Paul 	hptr = (HEADER *)&buf;
185180807d2SBill Paul 	id = ntohs(hptr->id);
186180807d2SBill Paul 
187180807d2SBill Paul 	for (ns = 0; ns < _res.nscount; ns++) {
188180807d2SBill Paul 		rval = sendto(resfd, buf, n, 0,
189180807d2SBill Paul 			(struct sockaddr *)&_res.nsaddr_list[ns],
190180807d2SBill Paul 				sizeof(struct sockaddr));
191180807d2SBill Paul 		if (rval == -1) {
192180807d2SBill Paul 			yp_error("sendto failed");
193180807d2SBill Paul 			return(0);
194180807d2SBill Paul 		}
195180807d2SBill Paul 	}
196180807d2SBill Paul 
197180807d2SBill Paul 	return(id);
198180807d2SBill Paul }
199180807d2SBill Paul 
200dc584ddbSDag-Erling Smørgrav static struct circleq_dnsentry *
yp_find_dnsqent(unsigned long id,int type)201dc584ddbSDag-Erling Smørgrav yp_find_dnsqent(unsigned long id, int type)
202180807d2SBill Paul {
203180807d2SBill Paul 	register struct circleq_dnsentry *q;
204180807d2SBill Paul 
205c5e5cd90SPoul-Henning Kamp 	TAILQ_FOREACH(q, &qhead, links) {
2069c171de0SBill Paul 		switch (type) {
2079c171de0SBill Paul 		case BY_RPC_XID:
2089c171de0SBill Paul 			if (id == q->xid)
2099c171de0SBill Paul 				return(q);
2109c171de0SBill Paul 			break;
2119c171de0SBill Paul 		case BY_DNS_ID:
2129c171de0SBill Paul 		default:
213adc4fa33SBill Paul 			if (id == q->id)
214180807d2SBill Paul 				return(q);
2159c171de0SBill Paul 			break;
2169c171de0SBill Paul 		}
217180807d2SBill Paul 	}
218180807d2SBill Paul 	return (NULL);
219180807d2SBill Paul }
220180807d2SBill Paul 
221dc584ddbSDag-Erling Smørgrav static void
yp_send_dns_reply(struct circleq_dnsentry * q,char * buf)222dc584ddbSDag-Erling Smørgrav yp_send_dns_reply(struct circleq_dnsentry *q, char *buf)
223180807d2SBill Paul {
224adc4fa33SBill Paul 	ypresponse result_v1;
225adc4fa33SBill Paul 	ypresp_val result_v2;
226180807d2SBill Paul 	unsigned long xid;
227180807d2SBill Paul 	struct sockaddr_in client_addr;
228adc4fa33SBill Paul 	xdrproc_t xdrfunc;
229adc4fa33SBill Paul 	char *result;
230180807d2SBill Paul 
231adc4fa33SBill Paul 	/*
232adc4fa33SBill Paul 	 * Set up correct reply struct and
233adc4fa33SBill Paul 	 * XDR filter depending on ypvers.
234adc4fa33SBill Paul 	 */
235adc4fa33SBill Paul 	switch (q->ypvers) {
236adc4fa33SBill Paul 	case YPVERS:
237adc4fa33SBill Paul 		bzero((char *)&result_v2, sizeof(result_v2));
238180807d2SBill Paul 
239180807d2SBill Paul 		if (buf == NULL)
240adc4fa33SBill Paul 			result_v2.stat = YP_NOKEY;
241180807d2SBill Paul 		else {
242adc4fa33SBill Paul 			result_v2.val.valdat_len = strlen(buf);
243adc4fa33SBill Paul 			result_v2.val.valdat_val = buf;
244adc4fa33SBill Paul 			result_v2.stat = YP_TRUE;
245adc4fa33SBill Paul 		}
246adc4fa33SBill Paul 		result = (char *)&result_v2;
247adc4fa33SBill Paul 		xdrfunc = (xdrproc_t)xdr_ypresp_val;
248adc4fa33SBill Paul 		break;
249adc4fa33SBill Paul 	case YPOLDVERS:
250adc4fa33SBill Paul 		/*
251adc4fa33SBill Paul 		 * The odds are we will _never_ execute this
252adc4fa33SBill Paul 		 * particular code, but we include it anyway
253adc4fa33SBill Paul 		 * for the sake of completeness.
254adc4fa33SBill Paul 		 */
255adc4fa33SBill Paul 		bzero((char *)&result_v1, sizeof(result_v1));
256adc4fa33SBill Paul 		result_v1.yp_resptype = YPRESP_VAL;
257adc4fa33SBill Paul 
258dc584ddbSDag-Erling Smørgrav #define YPVAL ypresponse_u.yp_resp_valtype
259adc4fa33SBill Paul 		if (buf == NULL)
260adc4fa33SBill Paul 			result_v1.YPVAL.stat = YP_NOKEY;
261adc4fa33SBill Paul 		else {
262adc4fa33SBill Paul 			result_v1.YPVAL.val.valdat_len = strlen(buf);
263adc4fa33SBill Paul 			result_v1.YPVAL.val.valdat_val = buf;
264adc4fa33SBill Paul 			result_v1.YPVAL.stat = YP_TRUE;
265adc4fa33SBill Paul 		}
266adc4fa33SBill Paul 		result = (char *)&result_v1;
267adc4fa33SBill Paul 		xdrfunc = (xdrproc_t)xdr_ypresponse;
268adc4fa33SBill Paul 		break;
269adc4fa33SBill Paul 	default:
27098834523SPhilippe Charnier 		yp_error("bad YP program version (%lu)!", q->ypvers);
271adc4fa33SBill Paul 			return;
272adc4fa33SBill Paul 		break;
273180807d2SBill Paul 	}
274180807d2SBill Paul 
275180807d2SBill Paul 	if (debug)
27698834523SPhilippe Charnier 		yp_error("sending dns reply to %s (%lu)",
277adc4fa33SBill Paul 			inet_ntoa(q->client_addr.sin_addr), q->id);
278180807d2SBill Paul 	/*
279180807d2SBill Paul 	 * XXX This is disgusting. There's basically one transport
280180807d2SBill Paul 	 * handle for UDP, but we're holding off on replying to a
281180807d2SBill Paul 	 * client until we're ready, by which time we may have received
282180807d2SBill Paul 	 * several other queries from other clients with different
283180807d2SBill Paul 	 * transaction IDs. So to make the delayed response thing work,
284180807d2SBill Paul 	 * we have to save the transaction ID and client address of
285180807d2SBill Paul 	 * each request, then jam them into the transport handle when
286180807d2SBill Paul 	 * we're ready to send a reply. Then after we've send the reply,
287180807d2SBill Paul 	 * we put the old transaction ID and remote address back the
288180807d2SBill Paul 	 * way we found 'em. This is _INCREDIBLY_ non-portable; it's
289180807d2SBill Paul 	 * not even supported by the RPC library.
290180807d2SBill Paul 	 */
291926f037aSBill Paul 	/*
292adc4fa33SBill Paul 	 * XXX Don't frob the transaction ID for TCP handles.
293926f037aSBill Paul 	 */
294926f037aSBill Paul 	if (q->prot_type == SOCK_DGRAM)
295180807d2SBill Paul 		xid = svcudp_set_xid(q->xprt, q->xid);
296180807d2SBill Paul 	client_addr = q->xprt->xp_raddr;
297180807d2SBill Paul 	q->xprt->xp_raddr = q->client_addr;
298adc4fa33SBill Paul 
299adc4fa33SBill Paul 	if (!svc_sendreply(q->xprt, xdrfunc, result))
300180807d2SBill Paul 		yp_error("svc_sendreply failed");
301adc4fa33SBill Paul 
302adc4fa33SBill Paul 	/*
303adc4fa33SBill Paul 	 * Now that we sent the reply,
304adc4fa33SBill Paul 	 * put the handle back the way it was.
305adc4fa33SBill Paul 	 */
306926f037aSBill Paul 	if (q->prot_type == SOCK_DGRAM)
307180807d2SBill Paul 		svcudp_set_xid(q->xprt, xid);
308180807d2SBill Paul 	q->xprt->xp_raddr = client_addr;
309180807d2SBill Paul }
310180807d2SBill Paul 
311adc4fa33SBill Paul /*
312adc4fa33SBill Paul  * Decrement TTL on all queue entries, possibly nuking
313adc4fa33SBill Paul  * any that have been around too long without being serviced.
314adc4fa33SBill Paul  */
315dc584ddbSDag-Erling Smørgrav void
yp_prune_dnsq(void)316dc584ddbSDag-Erling Smørgrav yp_prune_dnsq(void)
317180807d2SBill Paul {
3187deb24a6SBill Paul 	register struct circleq_dnsentry *q, *n;
319180807d2SBill Paul 
320c5e5cd90SPoul-Henning Kamp 	q = TAILQ_FIRST(&qhead);
321c5e5cd90SPoul-Henning Kamp 	while (q != NULL) {
322180807d2SBill Paul 		q->ttl--;
323c5e5cd90SPoul-Henning Kamp 		n = TAILQ_NEXT(q, links);
324180807d2SBill Paul 		if (!q->ttl) {
325c5e5cd90SPoul-Henning Kamp 			TAILQ_REMOVE(&qhead, q, links);
326180807d2SBill Paul 			free(q->name);
327180807d2SBill Paul 			free(q);
328180807d2SBill Paul 			pending--;
329180807d2SBill Paul 		}
3307deb24a6SBill Paul 		q = n;
331180807d2SBill Paul 	}
332180807d2SBill Paul 
333180807d2SBill Paul 	if (pending < 0)
334180807d2SBill Paul 		pending = 0;
335180807d2SBill Paul }
336180807d2SBill Paul 
337adc4fa33SBill Paul /*
338adc4fa33SBill Paul  * Data is pending on the DNS socket; check for valid replies
339adc4fa33SBill Paul  * to our queries and dispatch them to waiting clients.
340adc4fa33SBill Paul  */
341dc584ddbSDag-Erling Smørgrav void
yp_run_dnsq(void)342dc584ddbSDag-Erling Smørgrav yp_run_dnsq(void)
343180807d2SBill Paul {
344180807d2SBill Paul 	register struct circleq_dnsentry *q;
345180807d2SBill Paul 	char buf[sizeof(HEADER) + MAXPACKET];
346180807d2SBill Paul 	struct sockaddr_in sin;
347595e5323SStefan Farfeleder 	socklen_t len;
348180807d2SBill Paul 	int rval;
349180807d2SBill Paul 	HEADER *hptr;
350180807d2SBill Paul 	struct hostent *hent;
351180807d2SBill Paul 
352180807d2SBill Paul 	if (debug)
35398834523SPhilippe Charnier 		yp_error("running dns queue");
354180807d2SBill Paul 
355180807d2SBill Paul 	bzero(buf, sizeof(buf));
356180807d2SBill Paul 
357180807d2SBill Paul 	len = sizeof(struct sockaddr_in);
358180807d2SBill Paul 	rval = recvfrom(resfd, buf, sizeof(buf), 0,
359180807d2SBill Paul 			(struct sockaddr *)&sin, &len);
360180807d2SBill Paul 
361180807d2SBill Paul 	if (rval == -1) {
362180807d2SBill Paul 		yp_error("recvfrom failed: %s", strerror(errno));
363180807d2SBill Paul 		return;
364180807d2SBill Paul 	}
365180807d2SBill Paul 
366adc4fa33SBill Paul 	/*
367adc4fa33SBill Paul 	 * We may have data left in the socket that represents
368adc4fa33SBill Paul 	 * replies to earlier queries that we don't care about
369adc4fa33SBill Paul 	 * anymore. If there are no lookups pending or the packet
370adc4fa33SBill Paul 	 * ID doesn't match any of the queue IDs, just drop it
371adc4fa33SBill Paul 	 * on the floor.
372adc4fa33SBill Paul 	 */
373180807d2SBill Paul 	hptr = (HEADER *)&buf;
3749c171de0SBill Paul 	if (!pending ||
3759c171de0SBill Paul 		(q = yp_find_dnsqent(ntohs(hptr->id), BY_DNS_ID)) == NULL) {
376adc4fa33SBill Paul 		/* ignore */
377180807d2SBill Paul 		return;
378180807d2SBill Paul 	}
379180807d2SBill Paul 
380180807d2SBill Paul 	if (debug)
38198834523SPhilippe Charnier 		yp_error("got dns reply from %s", inet_ntoa(sin.sin_addr));
382180807d2SBill Paul 
383180807d2SBill Paul 	hent = __dns_getanswer(buf, rval, q->name, q->type);
384180807d2SBill Paul 
38515b23bddSMark Murray 	if (hent != NULL) {
386180807d2SBill Paul 		if (q->type == T_PTR) {
3874e5a7758SHajimu UMEMOTO 			hent->h_addr = (char *)q->addr;
3884e5a7758SHajimu UMEMOTO 			hent->h_addrtype = q->addrtype;
3894e5a7758SHajimu UMEMOTO 			hent->h_length = q->addrlen;
390180807d2SBill Paul 		}
391926f037aSBill Paul 	}
392926f037aSBill Paul 
393adc4fa33SBill Paul 	/* Got an answer ready for a client -- send it off. */
394180807d2SBill Paul 	yp_send_dns_reply(q, parse(hent));
395180807d2SBill Paul 	pending--;
396c5e5cd90SPoul-Henning Kamp 	TAILQ_REMOVE(&qhead, q, links);
397180807d2SBill Paul 	free(q->name);
398180807d2SBill Paul 	free(q);
399180807d2SBill Paul 
400adc4fa33SBill Paul 	/* Decrement TTLs on other entries while we're here. */
401180807d2SBill Paul 	yp_prune_dnsq();
402180807d2SBill Paul }
403180807d2SBill Paul 
404adc4fa33SBill Paul /*
405adc4fa33SBill Paul  * Queue and transmit an asynchronous DNS hostname lookup.
406adc4fa33SBill Paul  */
407dc584ddbSDag-Erling Smørgrav ypstat
yp_async_lookup_name(struct svc_req * rqstp,char * name,int af)4084e5a7758SHajimu UMEMOTO yp_async_lookup_name(struct svc_req *rqstp, char *name, int af)
409180807d2SBill Paul {
410180807d2SBill Paul 	register struct circleq_dnsentry *q;
411595e5323SStefan Farfeleder 	socklen_t len;
412595e5323SStefan Farfeleder 	int type;
413180807d2SBill Paul 
4149c171de0SBill Paul 	/* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
415595e5323SStefan Farfeleder 	type = -1;
416595e5323SStefan Farfeleder 	len = sizeof(type);
4178360efbdSAlfred Perlstein 	if (getsockopt(rqstp->rq_xprt->xp_fd, SOL_SOCKET,
4189c171de0SBill Paul 					SO_TYPE, &type, &len) == -1) {
4199c171de0SBill Paul 		yp_error("getsockopt failed: %s", strerror(errno));
4209c171de0SBill Paul 		return(YP_YPERR);
4219c171de0SBill Paul 	}
4229c171de0SBill Paul 
4239c171de0SBill Paul 	/* Avoid transmitting dupe requests. */
4249c171de0SBill Paul 	if (type == SOCK_DGRAM &&
4259c171de0SBill Paul 	    yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL)
4269c171de0SBill Paul 		return(YP_TRUE);
4279c171de0SBill Paul 
428180807d2SBill Paul 	if ((q = yp_malloc_dnsent()) == NULL)
429180807d2SBill Paul 		return(YP_YPERR);
430180807d2SBill Paul 
4314e5a7758SHajimu UMEMOTO 	q->type = (af == AF_INET) ? T_A : T_AAAA;
432180807d2SBill Paul 	q->ttl = DEF_TTL;
433adc4fa33SBill Paul 	q->xprt = rqstp->rq_xprt;
434adc4fa33SBill Paul 	q->ypvers = rqstp->rq_vers;
435926f037aSBill Paul 	q->prot_type = type;
436926f037aSBill Paul 	if (q->prot_type == SOCK_DGRAM)
437adc4fa33SBill Paul 		q->xid = svcudp_get_xid(q->xprt);
438adc4fa33SBill Paul 	q->client_addr = q->xprt->xp_raddr;
439180807d2SBill Paul 	q->domain = _res.dnsrch;
440180807d2SBill Paul 	q->id = yp_send_dns_query(name, q->type);
441180807d2SBill Paul 
442180807d2SBill Paul 	if (q->id == 0) {
443180807d2SBill Paul 		yp_error("DNS query failed");
444180807d2SBill Paul 		free(q);
445180807d2SBill Paul 		return(YP_YPERR);
446180807d2SBill Paul 	}
447180807d2SBill Paul 
448180807d2SBill Paul 	q->name = strdup(name);
449c5e5cd90SPoul-Henning Kamp 	TAILQ_INSERT_HEAD(&qhead, q, links);
450180807d2SBill Paul 	pending++;
451180807d2SBill Paul 
452180807d2SBill Paul 	if (debug)
45347d9f3f4SHiroki Sato 		yp_error("queueing async DNS name lookup (%lu)", q->id);
454180807d2SBill Paul 
4557deb24a6SBill Paul 	yp_prune_dnsq();
456180807d2SBill Paul 	return(YP_TRUE);
457180807d2SBill Paul }
458180807d2SBill Paul 
459adc4fa33SBill Paul /*
460adc4fa33SBill Paul  * Queue and transmit an asynchronous DNS IP address lookup.
461adc4fa33SBill Paul  */
462dc584ddbSDag-Erling Smørgrav ypstat
yp_async_lookup_addr(struct svc_req * rqstp,char * addr,int af)4634e5a7758SHajimu UMEMOTO yp_async_lookup_addr(struct svc_req *rqstp, char *addr, int af)
464180807d2SBill Paul {
465180807d2SBill Paul 	register struct circleq_dnsentry *q;
4664e5a7758SHajimu UMEMOTO 	char buf[MAXHOSTNAMELEN], *qp;
4674e5a7758SHajimu UMEMOTO 	uint32_t abuf[4];	/* IPv4 or IPv6 */
4684e5a7758SHajimu UMEMOTO 	u_char *uaddr = (u_char *)abuf;
469595e5323SStefan Farfeleder 	socklen_t len;
4704e5a7758SHajimu UMEMOTO 	int type, n;
471180807d2SBill Paul 
4729c171de0SBill Paul 	/* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
473595e5323SStefan Farfeleder 	type = -1;
474595e5323SStefan Farfeleder 	len = sizeof(type);
4758360efbdSAlfred Perlstein 	if (getsockopt(rqstp->rq_xprt->xp_fd, SOL_SOCKET,
4769c171de0SBill Paul 					SO_TYPE, &type, &len) == -1) {
4779c171de0SBill Paul 		yp_error("getsockopt failed: %s", strerror(errno));
4789c171de0SBill Paul 		return(YP_YPERR);
4799c171de0SBill Paul 	}
4809c171de0SBill Paul 
4819c171de0SBill Paul 	/* Avoid transmitting dupe requests. */
4829c171de0SBill Paul 	if (type == SOCK_DGRAM &&
4839c171de0SBill Paul 	    yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL)
4849c171de0SBill Paul 		return(YP_TRUE);
4859c171de0SBill Paul 
4864e5a7758SHajimu UMEMOTO 	switch (af) {
4874e5a7758SHajimu UMEMOTO 	case AF_INET:
4884e5a7758SHajimu UMEMOTO 		if (inet_aton(addr, (struct in_addr *)uaddr) != 1)
489180807d2SBill Paul 			return(YP_NOKEY);
4904e5a7758SHajimu UMEMOTO 		snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa",
4914e5a7758SHajimu UMEMOTO 		    (uaddr[3] & 0xff), (uaddr[2] & 0xff),
4924e5a7758SHajimu UMEMOTO 		    (uaddr[1] & 0xff), (uaddr[0] & 0xff));
4934e5a7758SHajimu UMEMOTO 		len = INADDRSZ;
4944e5a7758SHajimu UMEMOTO 		break;
4954e5a7758SHajimu UMEMOTO 	case AF_INET6:
4964e5a7758SHajimu UMEMOTO 		if (inet_pton(af, addr, uaddr) != 1)
4974e5a7758SHajimu UMEMOTO 			return(YP_NOKEY);
4984e5a7758SHajimu UMEMOTO 		qp = buf;
4994e5a7758SHajimu UMEMOTO 		for (n = IN6ADDRSZ - 1; n >= 0; n--) {
5004e5a7758SHajimu UMEMOTO 			qp += (size_t)sprintf(qp, "%x.%x.", uaddr[n] & 0xf,
5014e5a7758SHajimu UMEMOTO 			    (uaddr[n] >> 4) & 0xf);
5024e5a7758SHajimu UMEMOTO 		}
5034e5a7758SHajimu UMEMOTO 		strlcat(buf, "ip6.arpa", sizeof(buf));
5044e5a7758SHajimu UMEMOTO 		len = IN6ADDRSZ;
5054e5a7758SHajimu UMEMOTO 		break;
5064e5a7758SHajimu UMEMOTO 	default:
5074e5a7758SHajimu UMEMOTO 		return(YP_YPERR);
5084e5a7758SHajimu UMEMOTO 	}
509180807d2SBill Paul 
5105fb70b0cSDon Lewis 	if ((q = yp_malloc_dnsent()) == NULL)
5115fb70b0cSDon Lewis 		return(YP_YPERR);
5125fb70b0cSDon Lewis 
513180807d2SBill Paul 	if (debug)
514180807d2SBill Paul 		yp_error("DNS address is: %s", buf);
515180807d2SBill Paul 
516180807d2SBill Paul 	q->type = T_PTR;
517180807d2SBill Paul 	q->ttl = DEF_TTL;
518adc4fa33SBill Paul 	q->xprt = rqstp->rq_xprt;
519adc4fa33SBill Paul 	q->ypvers = rqstp->rq_vers;
520180807d2SBill Paul 	q->domain = NULL;
521926f037aSBill Paul 	q->prot_type = type;
522926f037aSBill Paul 	if (q->prot_type == SOCK_DGRAM)
523adc4fa33SBill Paul 		q->xid = svcudp_get_xid(q->xprt);
524adc4fa33SBill Paul 	q->client_addr = q->xprt->xp_raddr;
525180807d2SBill Paul 	q->id = yp_send_dns_query(buf, q->type);
526180807d2SBill Paul 
527180807d2SBill Paul 	if (q->id == 0) {
528180807d2SBill Paul 		yp_error("DNS query failed");
529180807d2SBill Paul 		free(q);
530180807d2SBill Paul 		return(YP_YPERR);
531180807d2SBill Paul 	}
532180807d2SBill Paul 
5334e5a7758SHajimu UMEMOTO 	memcpy(q->addr, uaddr, len);
5344e5a7758SHajimu UMEMOTO 	q->addrlen = len;
5354e5a7758SHajimu UMEMOTO 	q->addrtype = af;
536180807d2SBill Paul 	q->name = strdup(buf);
537c5e5cd90SPoul-Henning Kamp 	TAILQ_INSERT_HEAD(&qhead, q, links);
538180807d2SBill Paul 	pending++;
539180807d2SBill Paul 
540180807d2SBill Paul 	if (debug)
54147d9f3f4SHiroki Sato 		yp_error("queueing async DNS address lookup (%lu)", q->id);
542180807d2SBill Paul 
5437deb24a6SBill Paul 	yp_prune_dnsq();
544180807d2SBill Paul 	return(YP_TRUE);
545778c7b1cSBill Paul }
546