summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ypserv/yp_dnslookup.c
blob: 7b0acbb0a65a1a7dfb74558f50f9006161b7f6b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * Copyright (c) 1995
 *	Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bill Paul.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$Id: yp_dnslookup.c,v 1.5 1996/12/22 15:45:33 wpaul Exp $
 */

/*
 * Do standard and reverse DNS lookups using the resolver library.
 * Take care of all the dirty work here so the main program only has to
 * pass us a pointer to an array of characters.
 *
 * We have to use direct resolver calls here otherwise the YP server
 * could end up looping by calling itself over and over again until
 * it disappeared up its own belly button.
 */

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/fcntl.h>
#include <sys/queue.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>

#include <stdio.h>
#include <ctype.h>
#include <resolv.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <err.h>

#include <rpcsvc/yp.h>
#include "yp_extern.h"

#ifndef lint
static const char rcsid[] = "$Id: yp_dnslookup.c,v 1.5 1996/12/22 15:45:33 wpaul Exp $";
#endif

static char *parse(hp)
	struct hostent *hp;
{
	static char result[MAXHOSTNAMELEN * 2];
	int len,i;
	struct in_addr addr;

	if (hp == NULL)
		return(NULL);

	len = 16 + strlen(hp->h_name);
	for (i = 0; hp->h_aliases[i]; i++)
		len += strlen(hp->h_aliases[i]) + 1;

	bzero(result, sizeof(result));

	bcopy(hp->h_addr, &addr, sizeof(struct in_addr));
	snprintf(result, sizeof(result), "%s %s", inet_ntoa(addr), hp->h_name);

	for (i = 0; hp->h_aliases[i]; i++) {
		strcat(result, " ");
		strcat(result, hp->h_aliases[i]);
	}

	return ((char *)&result);
}

#define MAXPACKET 1024
#define DEF_TTL 50

extern struct hostent *__dns_getanswer __P((char *, int, char *, int));

static CIRCLEQ_HEAD(dns_qhead, circleq_dnsentry) qhead;

struct circleq_dnsentry {
	SVCXPRT *xprt;
	unsigned long xid;
	struct sockaddr_in client_addr;
	unsigned long id;
	unsigned long ttl;
	unsigned long sent;
	unsigned long type;
	char **domain;
	char *name;
	struct in_addr addr;
	CIRCLEQ_ENTRY(circleq_dnsentry) links;
};

static int pending = 0;

int yp_init_resolver()
{
	CIRCLEQ_INIT(&qhead);
	if (!(_res.options & RES_INIT) && res_init() == -1) {
		yp_error("res_init failed");
		return(1);
	}
	if ((resfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		yp_error("couldn't create socket");
		return(1);
	}
	if (fcntl(resfd, F_SETFL, O_NONBLOCK) == -1) {
		yp_error("couldn't make resolver socket non-blocking");
		return(1);
	}
	return(0);
}

int yp_dnsq_pending()
{
	return(pending);
}

static struct circleq_dnsentry *yp_malloc_dnsent()
{
	register struct circleq_dnsentry *q;

	q = (struct circleq_dnsentry *)malloc(sizeof(struct circleq_dnsentry));

	if (q == NULL) {
		yp_error("failed to malloc() circleq dns entry: %s",
							strerror(errno));
		return(NULL);
	}

	return(q);
}

/*
 * Transmit a query.
 */
static unsigned long yp_send_dns_query(name, type)
	char *name;
	int type;
{
	char buf[MAXPACKET];
	int n;
	HEADER *hptr;
	int ns;
	int rval;
	unsigned long id;

	bzero(buf, sizeof(buf));

	n = res_mkquery(QUERY,name,C_IN,type,NULL,0,NULL,buf,sizeof(buf));

	if (n <= 0) {
		yp_error("res_mkquery failed");
		return(0);
	}

	hptr = (HEADER *)&buf;
	id = ntohs(hptr->id);

	for (ns = 0; ns < _res.nscount; ns++) {
		rval = sendto(resfd, buf, n, 0,
			(struct sockaddr *)&_res.nsaddr_list[ns],
				sizeof(struct sockaddr));
		if (rval == -1) {
			yp_error("sendto failed");
			return(0);
		}
	}

	return(id);
}

static struct circleq_dnsentry *yp_find_dnsqent(id)
	unsigned long id;
{
	register struct circleq_dnsentry *q;

	for (q = qhead.cqh_first; q != (void *)&qhead; q = q->links.cqe_next) {
		if (q->id == id)
			return(q);
	}
	return (NULL);
}

static void yp_send_dns_reply(q, buf)
	struct circleq_dnsentry *q;
	char *buf;
{
	ypresp_val result;
	unsigned long xid;
	struct sockaddr_in client_addr;

	bzero((char *)&result, sizeof(result));

	if (buf == NULL)
		result.stat = YP_NOKEY;
	else {
		result.val.valdat_len = strlen(buf);
		result.val.valdat_val = buf;
		result.stat = YP_TRUE;
	}

	if (debug)
		yp_error("Sending dns reply to %s (%lu)",
					inet_ntoa(q->client_addr.sin_addr),
					q->id);

	/*
	 * XXX This is disgusting. There's basically one transport
	 * handle for UDP, but we're holding off on replying to a
	 * client until we're ready, by which time we may have received
	 * several other queries from other clients with different
	 * transaction IDs. So to make the delayed response thing work,
	 * we have to save the transaction ID and client address of
	 * each request, then jam them into the transport handle when
	 * we're ready to send a reply. Then after we've send the reply,
	 * we put the old transaction ID and remote address back the
	 * way we found 'em. This is _INCREDIBLY_ non-portable; it's
	 * not even supported by the RPC library.
	 */
	xid = svcudp_set_xid(q->xprt, q->xid);
	client_addr = q->xprt->xp_raddr;
	q->xprt->xp_raddr = q->client_addr;
	if (!svc_sendreply(q->xprt, xdr_ypresp_val, (char *)&result))
		yp_error("svc_sendreply failed");
	svcudp_set_xid(q->xprt, xid);
	q->xprt->xp_raddr = client_addr;
	return;
}

void yp_prune_dnsq()
{
	register struct circleq_dnsentry *q;

	for (q = qhead.cqh_first; q != (void *)&qhead; q = q->links.cqe_next) {
		q->ttl--;
		if (!q->ttl) {
			CIRCLEQ_REMOVE(&qhead, q, links);
			free(q->name);
			free(q);
			pending--;
		}
	}

	if (pending < 0)
		pending = 0;

	return;
}

void yp_run_dnsq()
{
	register struct circleq_dnsentry *q;
	char buf[sizeof(HEADER) + MAXPACKET];
	struct sockaddr_in sin;
	int rval;
	int len;
	HEADER *hptr;
	struct hostent *hent;

	if (debug)
		yp_error("Running dns queue");

	bzero(buf, sizeof(buf));

	len = sizeof(struct sockaddr_in);
	rval = recvfrom(resfd, buf, sizeof(buf), 0,
			(struct sockaddr *)&sin, &len);

	if (rval == -1) {
		yp_error("recvfrom failed: %s", strerror(errno));
		return;
	}

	hptr = (HEADER *)&buf;
	if ((q = yp_find_dnsqent(ntohs(hptr->id))) == NULL) {
		/* bogus id -- ignore */
		return;
	}

	if (debug)
		yp_error("Got dns reply from %s", inet_ntoa(sin.sin_addr));

	hent = __dns_getanswer(buf, rval, q->name, q->type);

	if (hent == NULL) {
		char retrybuf[MAXHOSTNAMELEN];

		if (q->domain && *q->domain) {
			snprintf(retrybuf, sizeof(retrybuf), "%s.%s",
						q->name, *q->domain);
			if (debug)
				yp_error("Retrying with: %s", retrybuf);
			q->id = yp_send_dns_query(retrybuf, q->type);
			q->ttl = DEF_TTL;
			q->domain++;
			return;
		}
	}

	if (q->type == T_PTR) {
		hent->h_addr = (char *)&q->addr.s_addr;
		hent->h_length = sizeof(struct in_addr);
	}
	yp_send_dns_reply(q, parse(hent));

	pending--;
	CIRCLEQ_REMOVE(&qhead, q, links);
	free(q->name);
	free(q);

	yp_prune_dnsq();

	return;
}

ypstat yp_async_lookup_name(xprt, name)
	SVCXPRT	*xprt;
	char *name;
{
	register struct circleq_dnsentry *q;

	if ((q = yp_malloc_dnsent()) == NULL)
		return(YP_YPERR);

	q->type = T_A;
	q->ttl = DEF_TTL;
	q->sent = 1;
	q->xprt = xprt;
	q->xid = svcudp_get_xid(xprt);
	q->client_addr = xprt->xp_raddr;
	if (!strchr(name, '.'))
		q->domain = _res.dnsrch;
	else
		q->domain = NULL;
	q->id = yp_send_dns_query(name, q->type);

	if (q->id == 0) {
		yp_error("DNS query failed");
		free(q);
		return(YP_YPERR);
	}

	q->name = strdup(name);
	CIRCLEQ_INSERT_HEAD(&qhead, q, links);
	pending++;

	if (debug)
		yp_error("Queueing async DNS name lookup (%d)", q->id);

	return(YP_TRUE);
}

ypstat yp_async_lookup_addr(xprt, addr)
	SVCXPRT *xprt;
	char *addr;
{
	register struct circleq_dnsentry *q;
	char buf[MAXHOSTNAMELEN];
	int a, b, c, d;

	if ((q = yp_malloc_dnsent()) == NULL)
		return(YP_YPERR);

	if (sscanf(addr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
		return(YP_NOKEY);

	snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
					d, c, b, a, addr);

	if (debug)
		yp_error("DNS address is: %s", buf);

	q->type = T_PTR;
	q->ttl = DEF_TTL;
	q->sent = 1;
	q->xprt = xprt;
	q->domain = NULL;
	q->xid = svcudp_get_xid(xprt);
	q->client_addr = xprt->xp_raddr;
	q->id = yp_send_dns_query(buf, q->type);

	if (q->id == 0) {
		yp_error("DNS query failed");
		free(q);
		return(YP_YPERR);
	}

	inet_aton(addr, &q->addr);
	q->name = strdup(buf);
	CIRCLEQ_INSERT_HEAD(&qhead, q, links);
	pending++;

	if (debug)
		yp_error("Queueing async DNS address lookup (%d)", q->id);

	return(YP_TRUE);
}
OpenPOWER on IntegriCloud