aboutsummaryrefslogtreecommitdiff
path: root/hesiod/hesiod.c
blob: bfb95633e352d029b0ffcc3436712e9b205c832b (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* Copyright (C) 1997-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/*
 * Copyright (c) 1996,1999 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/*
 * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
 */

/*
 * hesiod.c --- the core portion of the hesiod resolver.
 *
 * This file is derived from the hesiod library from Project Athena;
 * It has been extensively rewritten by Theodore Ts'o to have a more
 * thread-safe interface.
 */

/* Imports */

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>

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

#include "hesiod.h"
#include "hesiod_p.h"

#define _PATH_HESIOD_CONF "/etc/hesiod.conf"

/* Forward */

static int	parse_config_file(struct hesiod_p *ctx, const char *filename);
static char **	get_txt_records(struct hesiod_p *ctx, int class,
				const char *name);

/* Public */

/*
 * This function is called to initialize a hesiod_p.
 */
int
hesiod_init(void **context) {
	struct hesiod_p *ctx;
	const char *configname;
	char *cp;

	ctx = malloc(sizeof(struct hesiod_p));
	if (ctx == 0)
		return (-1);

	ctx->LHS = NULL;
	ctx->RHS = NULL;
	/* Set default query classes. */
	ctx->classes[0] = C_IN;
	ctx->classes[1] = C_HS;

	configname = __libc_secure_getenv("HESIOD_CONFIG");
	if (!configname)
	  configname = _PATH_HESIOD_CONF;
	if (parse_config_file(ctx, configname) < 0) {
		goto cleanup;
	}
	/*
	 * The default RHS can be overridden by an environment
	 * variable.
	 */
	if ((cp = __libc_secure_getenv("HES_DOMAIN")) != NULL) {
		free(ctx->RHS);
		ctx->RHS = malloc(strlen(cp)+2);
		if (!ctx->RHS)
			goto cleanup;
		if (cp[0] == '.')
			strcpy(ctx->RHS, cp);
		else {
			ctx->RHS[0] = '.';
			strcpy(ctx->RHS + 1, cp);
		}
	}

	/*
	 * If there is no default hesiod realm set, we return an
	 * error.
	 */
	if (!ctx->RHS) {
		__set_errno(ENOEXEC);
		goto cleanup;
	}

	*context = ctx;
	return (0);

 cleanup:
	hesiod_end(ctx);
	return (-1);
}

/*
 * This function deallocates the hesiod_p
 */
void
hesiod_end(void *context) {
	struct hesiod_p *ctx = (struct hesiod_p *) context;
	int save_errno = errno;

	free(ctx->RHS);
	free(ctx->LHS);
	free(ctx);
	__set_errno(save_errno);
}

/*
 * This function takes a hesiod (name, type) and returns a DNS
 * name which is to be resolved.
 */
char *
hesiod_to_bind(void *context, const char *name, const char *type) {
	struct hesiod_p *ctx = (struct hesiod_p *) context;
	char *bindname;
	char **rhs_list = NULL;
	const char *RHS, *cp;
	char *endp;

	/* Decide what our RHS is, and set cp to the end of the actual name. */
	if ((cp = strchr(name, '@')) != NULL) {
		if (strchr(cp + 1, '.'))
			RHS = cp + 1;
		else if ((rhs_list = hesiod_resolve(context, cp + 1,
		    "rhs-extension")) != NULL)
			RHS = *rhs_list;
		else {
			__set_errno(ENOENT);
			return (NULL);
		}
	} else {
		RHS = ctx->RHS;
		cp = name + strlen(name);
	}

	/*
	 * Allocate the space we need, including up to three periods and
	 * the terminating NUL.
	 */
	if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) +
	    (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) {
		if (rhs_list)
			hesiod_free_list(context, rhs_list);
		return NULL;
	}

	/* Now put together the DNS name. */
	endp = (char *) __mempcpy (bindname, name, cp - name);
	*endp++ = '.';
	endp = (char *) __stpcpy (endp, type);
	if (ctx->LHS) {
		if (ctx->LHS[0] != '.')
			*endp++ = '.';
		endp = __stpcpy (endp, ctx->LHS);
	}
	if (RHS[0] != '.')
		*endp++ = '.';
	strcpy (endp, RHS);

	if (rhs_list)
		hesiod_free_list(context, rhs_list);

	return (bindname);
}

/*
 * This is the core function.  Given a hesiod (name, type), it
 * returns an array of strings returned by the resolver.
 */
char **
hesiod_resolve(void *context, const char *name, const char *type) {
	struct hesiod_p *ctx = (struct hesiod_p *) context;
	char *bindname = hesiod_to_bind(context, name, type);
	char **retvec;

	if (bindname == NULL)
		return (NULL);

	retvec = get_txt_records(ctx, ctx->classes[0], bindname);

	if (retvec == NULL && (errno == ENOENT || errno == ECONNREFUSED) && ctx->classes[1])
		retvec = get_txt_records(ctx, ctx->classes[1], bindname);


	free(bindname);
	return (retvec);
}

void
hesiod_free_list(void *context, char **list) {
	char **p;

	for (p = list; *p; p++)
		free(*p);
	free(list);
}

/*
 * This function parses the /etc/hesiod.conf file
 */
static int
parse_config_file(struct hesiod_p *ctx, const char *filename) {
	char buf[MAXDNAME+7];
	FILE *fp;

	/*
	 * Clear the existing configuration variable, just in case
	 * they're set.
	 */
	free(ctx->RHS);
	free(ctx->LHS);
	ctx->RHS = ctx->LHS = 0;
	/* Set default query classes. */
	ctx->classes[0] = C_IN;
	ctx->classes[1] = C_HS;

	/*
	 * Now open and parse the file...
	 */
	if (!(fp = fopen(filename, "rce")))
		return (-1);

	while (fgets(buf, sizeof(buf), fp) != NULL) {
		char *key, *data, *cp, **cpp;

		cp = buf;
		if (*cp == '#' || *cp == '\n' || *cp == '\r')
			continue;
		while(*cp == ' ' || *cp == '\t')
			cp++;
		key = cp;
		while(*cp != ' ' && *cp != '\t' && *cp != '=')
			cp++;
		*cp++ = '\0';

		while(*cp == ' ' || *cp == '\t' || *cp == '=')
			cp++;
		data = cp;
		while(*cp != ' ' && *cp != '\n' && *cp != '\r')
			cp++;
		*cp++ = '\0';

		cpp = NULL;
		if (strcasecmp(key, "lhs") == 0)
			cpp = &ctx->LHS;
		else if (strcasecmp(key, "rhs") == 0)
			cpp = &ctx->RHS;
		if (cpp) {
			*cpp = strdup(data);
			if (!*cpp)
				goto cleanup;
		} else if (strcasecmp(key, "classes") == 0) {
			int n = 0;
			while (*data && n < 2) {
				cp = strchrnul(data, ',');
				if (*cp != '\0')
					*cp++ = '\0';
				if (strcasecmp(data, "IN") == 0)
					ctx->classes[n++] = C_IN;
				else if (strcasecmp(data, "HS") == 0)
					ctx->classes[n++] = C_HS;
				data = cp;
			}
			if (n == 0) {
				/* Restore the default.  Better than
				   nother at all.  */
				ctx->classes[0] = C_IN;
				ctx->classes[1] = C_HS;
			} else if (n == 1
				   || ctx->classes[0] == ctx->classes[1])
				ctx->classes[1] = 0;
		}
	}
	fclose(fp);
	return (0);

 cleanup:
	fclose(fp);
	free(ctx->RHS);
	free(ctx->LHS);
	ctx->RHS = ctx->LHS = 0;
	return (-1);
}

/*
 * Given a DNS class and a DNS name, do a lookup for TXT records, and
 * return a list of them.
 */
static char **
get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
	struct {
		int type;		/* RR type */
		int class;		/* RR class */
		int dlen;		/* len of data section */
		u_char *data;		/* pointer to data */
	} rr;
	HEADER *hp;
	u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
	u_char *cp, *erdata, *eom;
	char *dst, *edst, **list;
	int ancount, qdcount;
	int i, j, n, skip;

	/*
	 * Construct the query and send it.
	 */
	n = res_mkquery(QUERY, name, class, T_TXT, NULL, 0,
			 NULL, qbuf, MAX_HESRESP);
	if (n < 0) {
		__set_errno(EMSGSIZE);
		return (NULL);
	}
	n = res_send(qbuf, n, abuf, MAX_HESRESP);
	if (n < 0) {
		__set_errno(ECONNREFUSED);
		return (NULL);
	}
	if (n < HFIXEDSZ) {
		__set_errno(EMSGSIZE);
		return (NULL);
	}

	/*
	 * OK, parse the result.
	 */
	hp = (HEADER *) abuf;
	ancount = ntohs(hp->ancount);
	qdcount = ntohs(hp->qdcount);
	cp = abuf + sizeof(HEADER);
	eom = abuf + n;

	/* Skip query, trying to get to the answer section which follows. */
	for (i = 0; i < qdcount; i++) {
		skip = dn_skipname(cp, eom);
		if (skip < 0 || cp + skip + QFIXEDSZ > eom) {
			__set_errno(EMSGSIZE);
			return (NULL);
		}
		cp += skip + QFIXEDSZ;
	}

	list = malloc((ancount + 1) * sizeof(char *));
	if (!list)
		return (NULL);
	j = 0;
	for (i = 0; i < ancount; i++) {
		skip = dn_skipname(cp, eom);
		if (skip < 0) {
			__set_errno(EMSGSIZE);
			goto cleanup;
		}
		cp += skip;
		if (cp + 3 * INT16SZ + INT32SZ > eom) {
			__set_errno(EMSGSIZE);
			goto cleanup;
		}
		rr.type = ns_get16(cp);
		cp += INT16SZ;
		rr.class = ns_get16(cp);
		cp += INT16SZ + INT32SZ;	/* skip the ttl, too */
		rr.dlen = ns_get16(cp);
		cp += INT16SZ;
		if (rr.dlen == 0 || cp + rr.dlen > eom) {
			__set_errno(EMSGSIZE);
			goto cleanup;
		}
		rr.data = cp;
		cp += rr.dlen;
		if (rr.class != class || rr.type != T_TXT)
			continue;
		if (!(list[j] = malloc(rr.dlen)))
			goto cleanup;
		dst = list[j++];
		edst = dst + rr.dlen;
		erdata = rr.data + rr.dlen;
		cp = rr.data;
		while (cp < erdata) {
			n = (unsigned char) *cp++;
			if (cp + n > eom || dst + n > edst) {
				__set_errno(EMSGSIZE);
				goto cleanup;
			}
			memcpy(dst, cp, n);
			cp += n;
			dst += n;
		}
		if (cp != erdata) {
			__set_errno(EMSGSIZE);
			goto cleanup;
		}
		*dst = '\0';
	}
	list[j] = NULL;
	if (j == 0) {
		__set_errno(ENOENT);
		goto cleanup;
	}
	return (list);

 cleanup:
	for (i = 0; i < j; i++)
		free(list[i]);
	free(list);
	return (NULL);
}