dns.c 10.3 KB
Newer Older
Ted Lemon's avatar
Ted Lemon committed
1
2
/* dns.c

3
   Domain Name Service subroutines. */
Ted Lemon's avatar
Ted Lemon committed
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

/*
 * Copyright (C) 1992 by Ted Lemon.
 * Copyright (c) 1997 The Internet Software Consortium.
 * 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. Neither the name of The Internet Software Consortium 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 THE INTERNET SOFTWARE CONSORTIUM 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 THE INTERNET SOFTWARE CONSORTIUM 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.
 *
 * This file is based on software written in 1992 by Ted Lemon for
 * a portable network boot loader.   That original code base has been
 * substantially modified for use in the Internet Software Consortium
 * DHCP suite.
 *
42
43
44
45
46
 * These later modifications were done on behalf of the Internet
 * Software Consortium by Ted Lemon <mellon@fugue.com> in cooperation
 * with Vixie Enterprises.  To learn more about the Internet Software
 * Consortium, see ``http://www.vix.com/isc''.  To learn more about
 * Vixie Enterprises, see ``http://www.vix.com''.
Ted Lemon's avatar
Ted Lemon committed
47
48
49
50
 */

#ifndef lint
static char copyright[] =
51
"$Id: dns.c,v 1.5 1997/11/29 07:51:49 mellon Exp $ Copyright (c) 1997 The Internet Software Consortium.  All rights reserved.\n";
Ted Lemon's avatar
Ted Lemon committed
52
53
54
55
56
#endif /* not lint */

#include "dhcpd.h"
#include "arpa/nameser.h"

57
58
59
60
61
62
int dns_protocol_initialized;
int dns_protocol_fd;

static int addlabel PROTO ((u_int8_t *, char *));
static int skipname PROTO ((u_int8_t *));
static int copy_out_name PROTO ((u_int8_t *, u_int8_t *, char *));
63
64
65
static int nslookup PROTO ((u_int8_t, char *, int, u_int16_t, u_int16_t));
static int zonelookup PROTO ((u_int8_t, char *, int, u_int16_t));
u_int16_t dns_port;
66

Ted Lemon's avatar
Ted Lemon committed
67
68
/* Initialize the DNS protocol. */

69
void dns_startup ()
Ted Lemon's avatar
Ted Lemon committed
70
{
71
	struct servent *srv;
Ted Lemon's avatar
Ted Lemon committed
72
73
74
	struct sockaddr_in from;

	/* Only initialize icmp once. */
75
76
77
	if (dns_protocol_initialized)
		error ("attempted to reinitialize dns protocol");
	dns_protocol_initialized = 1;
Ted Lemon's avatar
Ted Lemon committed
78
79

	/* Get the protocol number (should be 1). */
80
	srv = getservbyname ("domain", "tcp");
81
	if (srv)
82
83
84
		dns_port = srv -> s_port;
	else
		dns_port = htons (53);
Ted Lemon's avatar
Ted Lemon committed
85

86
	/* Get a socket for the DNS protocol. */
87
	dns_protocol_fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
88
	if (dns_protocol_fd < 0)
89
		error ("unable to create dns socket: %m");
Ted Lemon's avatar
Ted Lemon committed
90

91
92
	pick_name_server ();

93
	add_protocol ("dns", dns_protocol_fd, dns_packet, 0);
Ted Lemon's avatar
Ted Lemon committed
94
95
96
97
98
99
100
101
102
}

/* Label manipulation stuff; see RFC1035, page 28 section 4.1.2 and
   page 30, section 4.1.4. */

/* addlabel copies a label into the specified buffer, putting the length of
   the label in the first character, the contents of the label in subsequent
   characters, and returning the length of the conglomeration. */

103
104
105
static int addlabel (buf, label)
	u_int8_t *buf;
	char *label;
Ted Lemon's avatar
Ted Lemon committed
106
{
107
108
109
	*buf = strlen (label);
	memcpy (buf + 1, label, *buf);
	return *buf + 1;
Ted Lemon's avatar
Ted Lemon committed
110
111
112
113
114
}

/* skipname skips over all of the labels in a single domain name,
   returning the length of the domain name. */

115
116
static int skipname (label)
     u_int8_t *label;
Ted Lemon's avatar
Ted Lemon committed
117
{
118
119
120
121
122
	if (*label & INDIR_MASK)
		return 2;
	if (*label == 0)
		return 1;
	return *label + 1 + skipname (label + *label + 1);
Ted Lemon's avatar
Ted Lemon committed
123
124
125
126
127
128
}

/* copy_out_name copies out the name appearing at the specified location
   into a string, stored as fields seperated by dots rather than lengths
   and labels.   The length of the label-formatted name is returned. */

129
130
131
132
static int copy_out_name (base, name, buf)
     u_int8_t *base;
     u_int8_t *name;
     char *buf;
Ted Lemon's avatar
Ted Lemon committed
133
{
134
135
136
137
138
139
140
141
142
143
144
145
	if (*name & INDIR_MASK) {
		int offset = (*name & ~INDIR_MASK) + (*name + 1);
		return copy_out_name (base, base + offset, buf);
	}
	if (!*name) {
		*buf = 0;
		return 1;
	}
	memcpy (buf, name + 1, *name);
	*(buf + *name) = '.';
	return (*name + 1
		+ copy_out_name (base, name + *name + 1, buf + *name + 1));
Ted Lemon's avatar
Ted Lemon committed
146
147
148
149
150
151
152
153
}

/* ns_inaddr_lookup constructs a PTR lookup query for an internet address -
   e.g., 1.200.9.192.in-addr.arpa.   If the specified timeout period passes
   before the query is satisfied, or if the query fails, the callback is
   called with a null pointer.   Otherwise, the callback is called with the
   address of the string returned by the name server. */

154
int ns_inaddr_lookup (id, inaddr)
155
156
	u_int16_t id;
	struct iaddr inaddr;
Ted Lemon's avatar
Ted Lemon committed
157
{
158
159
160
161
162
163
164
165
166
	unsigned char namebuf [512];
	unsigned char *s = namebuf;
	unsigned char *label;
	int i;
	unsigned char c;

	for (i = 3; i >= 0; --i) {
		label = s++;
		*label = 1;
167
		c = inaddr.iabuf [i];
168
169
170
171
172
173
174
175
176
		if (c > 100) {
			++*label;
			*s++ = '0' + c / 100;
		}
		if (c > 10) {
			++*label;
			*s++ = '0' + ((c / 10) % 10);
		}
		*s++ = '0' + (c % 10);
Ted Lemon's avatar
Ted Lemon committed
177
	}
178
179
180
	s += addlabel (s, "in-addr");
	s += addlabel (s, "arpa");
	*s++ = 0;
181
182
/*	return nslookup (id, namebuf, s - namebuf, T_PTR, C_IN); */
	return zonelookup (id, namebuf, s - namebuf, C_IN);
Ted Lemon's avatar
Ted Lemon committed
183
184
}

185
/* Construct and transmit a name server query. */
Ted Lemon's avatar
Ted Lemon committed
186

187
static int nslookup (id, qname, namelen, qtype, qclass)
188
189
190
191
192
	u_int8_t id;
	char *qname;
	int namelen;
	u_int16_t qtype;
	u_int16_t qclass;
Ted Lemon's avatar
Ted Lemon committed
193
{
194
195
	HEADER *hdr;
	unsigned char query [512];
196
	u_int8_t *s;
197
	int len;
198
199
	int i, status;
	struct sockaddr_in *server = pick_name_server ();
200
	
201
202
203
	if (!server)
		return 0;

204
205
	/* Construct a header... */
	hdr = (HEADER *)query;
206
	memset (hdr, 0, sizeof *hdr);
207
208
209
210
211
212
	hdr -> id = htons (id);
	hdr -> rd = 1;
	hdr -> opcode = QUERY;
	hdr -> qdcount = htons (1);

	/* Copy in the name we're looking up. */
213
	s = (u_int8_t *)(hdr + 1);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
	memcpy (s, qname, namelen);
	s += namelen;
	
	/* Set the query type. */
	putUShort (s, qtype);
	s += sizeof (u_int16_t);

	/* Set the query class. */
	putUShort (s, qclass);
	s += sizeof (u_int16_t);

	/* Send the query. */
	status = sendto (dns_protocol_fd, query, s - query, 0,
			 (struct sockaddr *)server, sizeof *server);

	/* If the send failed, report the failure. */
	if (status < 0)
		return 0;
	return 1;
Ted Lemon's avatar
Ted Lemon committed
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
/* Construct a query for the SOA for a specified name.
   Try every possible SOA name starting from the name specified and going
   to the root name - e.g., for

   	215.5.5.192.in-addr.arpa, look for SOAs matching:

	215.5.5.5.192.in-addr.arpa
	5.5.192.in-addr.arpa
	5.192.in-addr.arpa
	192.in-addr.arpa
	in-addr.arpa
	arpa */

static int zonelookup (id, qname, namelen, qclass)
	u_int8_t id;
	char *qname;
	int namelen;
	u_int16_t qclass;
{
	HEADER *hdr;
	unsigned char query [512];
	u_int8_t *s, *nptr;
	int len;
	int i, status, count;
	struct sockaddr_in *server = pick_name_server ();
	
261
262
263
	if (!server)
		return 0;

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
	/* Construct a header... */
	hdr = (HEADER *)query;
	memset (hdr, 0, sizeof *hdr);
	hdr -> id = htons (id);
	hdr -> rd = 1;
	hdr -> opcode = QUERY;

	/* Copy in the name we're looking up. */
	s = (u_int8_t *)(hdr + 1);
	memcpy (s, qname, namelen);
	s += namelen;
	
	/* Set the query type. */
	putUShort (s, T_SOA);
	s += sizeof (u_int16_t);

	/* Set the query class. */
	putUShort (s, qclass);
	s += sizeof (u_int16_t);
	count = 1;

	/* Now query up the hierarchy. */
	nptr = (u_int8_t *)(hdr + 1);
	while (*(nptr += *nptr + 1)) {
		/* Store a compressed reference from the full name. */
		putUShort (s, ntohs (htons (0xC000) |
				     htons (nptr - &query [0])));
		s += sizeof (u_int16_t);

		/* Store the query type. */
		putUShort (s, T_SOA);
		s += sizeof (u_int16_t);

		putUShort (s, qclass);
		s += sizeof (u_int16_t);

		/* Increment the query count... */
		++count;
break;
	}
	hdr -> qdcount = htons (count);

dump_raw (query, s - query);
	/* Send the query. */
	status = sendto (dns_protocol_fd, query, s - query, 0,
			 (struct sockaddr *)server, sizeof *server);

	/* If the send failed, report the failure. */
	if (status < 0)
		return 0;
	return 1;
}

317
318
/* Process a reply from a name server. */

319
void dns_packet (protocol)
320
	struct protocol *protocol;
Ted Lemon's avatar
Ted Lemon committed
321
{
322
323
324
325
326
327
328
	HEADER *ns_header;
	struct sockaddr_in from;
	int fl;
	unsigned char buf [4096];
	unsigned char nbuf [512];
	unsigned char *base;
	unsigned char *dptr;
329
330
331
332
333
334
	u_int16_t type;
	u_int16_t class;
	TIME ttl;
	u_int16_t rdlength;
	int len, status;
	int i;
335
336

	len = sizeof from;
337
	status = recvfrom (protocol -> fd, buf, sizeof buf, 0,
338
339
340
341
342
			  (struct sockaddr *)&from, &len);
	if (status < 0) {
		warn ("icmp_echoreply: %m");
		return;
	}
Ted Lemon's avatar
Ted Lemon committed
343

344
345
	ns_header = (HEADER *)buf;
	base = (unsigned char *)(ns_header + 1);
Ted Lemon's avatar
Ted Lemon committed
346

347
#if 0
Ted Lemon's avatar
Ted Lemon committed
348
	/* Ignore invalid packets... */
349
350
351
352
353
	if (ntohs (ns_header -> id) > ns_query_max) {
		printf ("Out-of-range NS message; id = %d\n",
			ntohs (ns_header -> id));
		return;
	}
354
#endif
Ted Lemon's avatar
Ted Lemon committed
355
356

	/* Parse the response... */
357
	dptr = base;
Ted Lemon's avatar
Ted Lemon committed
358

359
360
361
362
363
364
	/* Skip over the queries... */
	for (i = 0; i < ntohs (ns_header -> qdcount); i++) {
		dptr += skipname (dptr);
		/* Skip over the query type and query class. */
		dptr += 2 * sizeof (u_int16_t);
	}
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
	/* Process the answers... */
	for (i = 0; i < ntohs (ns_header -> ancount); i++) {
		/* Skip over the name we looked up. */
		dptr += skipname (dptr);

		/* Get the type. */
		type = getUShort (dptr);
		dptr += sizeof type;

		/* Get the class. */
		class = getUShort (dptr);
		dptr += sizeof class;

		/* Get the time-to-live. */
		ttl = getULong (dptr);
		dptr += sizeof ttl;

		/* Get the length of the reply. */
		rdlength = getUShort (dptr);
		dptr += sizeof rdlength;

		switch (type) {
		      case T_A:
			note ("A record; value is %d.%d.%d.%d",
			      dptr [0], dptr [1], dptr [2], dptr [3]);
			break;

		      case T_CNAME:
		      case T_PTR:
			copy_out_name (base, dptr, nbuf);
			note ("Domain name; value is %s\n", nbuf);
			return;

		      default:
			note ("unhandled type: %x", type);
		}
402
	}
Ted Lemon's avatar
Ted Lemon committed
403
}