summaryrefslogtreecommitdiffstats
path: root/lib/libc/net/hesiod.c
blob: 701244ac8c22aa908ac15b172bf18256f40140d1 (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*	$NetBSD: hesiod.c,v 1.9 1999/02/11 06:16:38 simonb Exp $	*/

/* Copyright (c) 1996 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.
 */

/* Copyright 1996 by the Massachusetts Institute of Technology.
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 */

/* This file is part of the hesiod library.  It implements the core
 * portion of the hesiod resolver.
 *
 * This file is loosely based on an interim version of hesiod.c from
 * the BIND IRS library, which was in turn based on an earlier version
 * of this file.  Extensive changes have been made on each step of the
 * path.
 *
 * This implementation is not truly thread-safe at the moment because
 * it uses res_send() and accesses _res.
 */

#include <sys/cdefs.h>

#if 0
static char *orig_rcsid = "$NetBSD: hesiod.c,v 1.9 1999/02/11 06:16:38 simonb Exp $";
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

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

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

struct hesiod_p {
	char	*lhs;			/* normally ".ns" */
	char	*rhs;			/* AKA the default hesiod domain */
	int	 classes[2];		/* The class search order. */
};

#define	MAX_HESRESP	1024

static int	  read_config_file(struct hesiod_p *, const char *);
static char	**get_txt_records(int, const char *);
static int	  init_context(void);
static void	  translate_errors(void);


/*
 * hesiod_init --
 *	initialize a hesiod_p.
 */
int 
hesiod_init(context)
	void	**context;
{
	struct hesiod_p	*ctx;
	const char	*p, *configname;

	ctx = malloc(sizeof(struct hesiod_p));
	if (ctx) {
		*context = ctx;
		if (!issetugid())
			configname = getenv("HESIOD_CONFIG");
		else
			configname = NULL;
		if (!configname)
			configname = _PATH_HESIOD_CONF;
		if (read_config_file(ctx, configname) >= 0) {
			/*
			 * The default rhs can be overridden by an
			 * environment variable.
			 */
			if (!issetugid())
				p = getenv("HES_DOMAIN");
			else
				p = NULL;
			if (p) {
				if (ctx->rhs)
					free(ctx->rhs);
				ctx->rhs = malloc(strlen(p) + 2);
				if (ctx->rhs) {
					*ctx->rhs = '.';
					strcpy(ctx->rhs + 1,
					    (*p == '.') ? p + 1 : p);
					return 0;
				} else
					errno = ENOMEM;
			} else
				return 0;
		}
	} else
		errno = ENOMEM;

	if (ctx->lhs)
		free(ctx->lhs);
	if (ctx->rhs)
		free(ctx->rhs);
	if (ctx)
		free(ctx);
	return -1;
}

/*
 * hesiod_end --
 *	Deallocates the hesiod_p.
 */
void 
hesiod_end(context)
	void	*context;
{
	struct hesiod_p *ctx = (struct hesiod_p *) context;

	free(ctx->rhs);
	if (ctx->lhs)
		free(ctx->lhs);
	free(ctx);
}

/*
 * hesiod_to_bind --
 * 	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[MAXDNAME], *p, *ret, **rhs_list = NULL;
	const char	*rhs;
	int		 len;

	if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) {
		errno = EMSGSIZE;
		return NULL;
	}

		/*
		 * Find the right right hand side to use, possibly
		 * truncating bindname.
		 */
	p = strchr(bindname, '@');
	if (p) {
		*p++ = 0;
		if (strchr(p, '.'))
			rhs = name + (p - bindname);
		else {
			rhs_list = hesiod_resolve(context, p, "rhs-extension");
			if (rhs_list)
				rhs = *rhs_list;
			else {
				errno = ENOENT;
				return NULL;
			}
		}
	} else
		rhs = ctx->rhs;

		/* See if we have enough room. */
	len = strlen(bindname) + 1 + strlen(type);
	if (ctx->lhs)
		len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
	len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0);
	if (len > sizeof(bindname) - 1) {
		if (rhs_list)
			hesiod_free_list(context, rhs_list);
		errno = EMSGSIZE;
		return NULL;
	}
		/* Put together the rest of the domain. */
	strcat(bindname, ".");
	strcat(bindname, type);
		/* Only append lhs if it isn't empty. */
	if (ctx->lhs && ctx->lhs[0] != '\0' ) {
		if (ctx->lhs[0] != '.')
			strcat(bindname, ".");
		strcat(bindname, ctx->lhs);
	}
	if (rhs[0] != '.')
		strcat(bindname, ".");
	strcat(bindname, rhs);

		/* rhs_list is no longer needed, since we're done with rhs. */
	if (rhs_list)
		hesiod_free_list(context, rhs_list);

		/* Make a copy of the result and return it to the caller. */
	ret = strdup(bindname);
	if (!ret)
		errno = ENOMEM;
	return ret;
}

/*
 * hesiod_resolve --
 *	Given a hesiod name and type, return an array of strings returned
 *	by the resolver.
 */
char **
hesiod_resolve(context, name, type)
	void		*context;
	const char	*name;
	const char	*type;
{
	struct hesiod_p	*ctx = (struct hesiod_p *) context;
	char		*bindname, **retvec;

	bindname = hesiod_to_bind(context, name, type);
	if (!bindname)
		return NULL;

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

	free(bindname);
	return retvec;
}

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

	if (list == NULL)
		return;
	for (p = list; *p; p++)
		free(*p);
	free(list);
}


/* read_config_file --
 *	Parse the /etc/hesiod.conf file.  Returns 0 on success,
 *	-1 on failure.  On failure, it might leave values in ctx->lhs
 *	or ctx->rhs which need to be freed by the caller.
 */
static int 
read_config_file(ctx, filename)
	struct hesiod_p	*ctx;
	const char	*filename;
{
	char	*key, *data, *p, **which;
	char	 buf[MAXDNAME + 7];
	int	 n;
	FILE	*fp;

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

		/* Try to open the configuration file. */
	fp = fopen(filename, "r");
	if (!fp) {
		/* Use compiled in default domain names. */
		ctx->lhs = strdup(DEF_LHS);
		ctx->rhs = strdup(DEF_RHS);
		if (ctx->lhs && ctx->rhs)
			return 0;
		else {
			errno = ENOMEM;
			return -1;
		}
	}
	ctx->lhs = NULL;
	ctx->rhs = NULL;
	while (fgets(buf, sizeof(buf), fp) != NULL) {
		p = buf;
		if (*p == '#' || *p == '\n' || *p == '\r')
			continue;
		while (*p == ' ' || *p == '\t')
			p++;
		key = p;
		while (*p != ' ' && *p != '\t' && *p != '=')
			p++;
		*p++ = 0;

		while (isspace(*p) || *p == '=')
			p++;
		data = p;
		while (!isspace(*p))
			p++;
		*p = 0;

		if (strcasecmp(key, "lhs") == 0 ||
		    strcasecmp(key, "rhs") == 0) {
			which = (strcasecmp(key, "lhs") == 0)
			    ? &ctx->lhs : &ctx->rhs;
			*which = strdup(data);
			if (!*which) {
				errno = ENOMEM;
				return -1;
			}
		} else {
			if (strcasecmp(key, "classes") == 0) {
				n = 0;
				while (*data && n < 2) {
					p = data;
					while (*p && *p != ',')
						p++;
					if (*p)
						*p++ = 0;
					if (strcasecmp(data, "IN") == 0)
						ctx->classes[n++] = C_IN;
					else
						if (strcasecmp(data, "HS") == 0)
							ctx->classes[n++] =
							    C_HS;
					data = p;
				}
				while (n < 2)
					ctx->classes[n++] = 0;
			}
		}
	}
	fclose(fp);

	if (!ctx->rhs || ctx->classes[0] == 0 ||
	    ctx->classes[0] == ctx->classes[1]) {
		errno = ENOEXEC;
		return -1;
	}
	return 0;
}

/*
 * get_txt_records --
 *	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(qclass, name)
	int		 qclass;
	const char	*name;
{
	HEADER		*hp;
	unsigned char	 qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
	char		*dst, **list;
	int		 ancount, qdcount, i, j, n, skip, type, class, len;

		/* Make sure the resolver is initialized. */
	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
		return NULL;

		/* Construct the query. */
	n = res_mkquery(QUERY, name, qclass, T_TXT, NULL, 0,
	    NULL, qbuf, PACKETSZ);
	if (n < 0)
		return NULL;

		/* Send the query. */
	n = res_send(qbuf, n, abuf, MAX_HESRESP);
	if (n < 0) {
		errno = ECONNREFUSED;
		return NULL;
	}
		/* Parse the header of the result. */
	hp = (HEADER *) (void *) abuf;
	ancount = ntohs(hp->ancount);
	qdcount = ntohs(hp->qdcount);
	p = abuf + sizeof(HEADER);
	eom = abuf + n;

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

		/* Allocate space for the text record answers. */
	list = malloc((ancount + 1) * sizeof(char *));
	if (!list) {
		errno = ENOMEM;
		return NULL;
	}
		/* Parse the answers. */
	j = 0;
	for (i = 0; i < ancount; i++) {
		/* Parse the header of this answer. */
		skip = dn_skipname(p, eom);
		if (skip < 0 || p + skip + 10 > eom)
			break;
		type = p[skip + 0] << 8 | p[skip + 1];
		class = p[skip + 2] << 8 | p[skip + 3];
		len = p[skip + 8] << 8 | p[skip + 9];
		p += skip + 10;
		if (p + len > eom) {
			errno = EMSGSIZE;
			break;
		}
		/* Skip entries of the wrong class and type. */
		if (class != qclass || type != T_TXT) {
			p += len;
			continue;
		}
		/* Allocate space for this answer. */
		list[j] = malloc((size_t)len);
		if (!list[j]) {
			errno = ENOMEM;
			break;
		}
		dst = list[j++];

		/* Copy answer data into the allocated area. */
		eor = p + len;
		while (p < eor) {
			n = (unsigned char) *p++;
			if (p + n > eor) {
				errno = EMSGSIZE;
				break;
			}
			memcpy(dst, p, (size_t)n);
			p += n;
			dst += n;
		}
		if (p < eor) {
			errno = EMSGSIZE;
			break;
		}
		*dst = 0;
	}

		/*
		 * If we didn't terminate the loop normally, something
		 * went wrong.
		 */
	if (i < ancount) {
		for (i = 0; i < j; i++)
			free(list[i]);
		free(list);
		return NULL;
	}
	if (j == 0) {
		errno = ENOENT;
		free(list);
		return NULL;
	}
	list[j] = NULL;
	return list;
}

		/*
		 *	COMPATIBILITY FUNCTIONS
		 */

static int	  inited = 0;
static void	 *context;
static int	  errval = HES_ER_UNINIT;

int
hes_init()
{
	init_context();
	return errval;
}

char *
hes_to_bind(name, type)
	const char	*name;
	const char	*type;
{
	static	char	*bindname;
	if (init_context() < 0)
		return NULL;
	if (bindname)
		free(bindname);
	bindname = hesiod_to_bind(context, name, type);
	if (!bindname)
		translate_errors();
	return bindname;
}

char **
hes_resolve(name, type)
	const char	*name;
	const char	*type;
{
	static char	**list;

	if (init_context() < 0)
		return NULL;

	/*
	 * In the old Hesiod interface, the caller was responsible for
	 * freeing the returned strings but not the vector of strings itself.
	 */
	if (list)
		free(list);

	list = hesiod_resolve(context, name, type);
	if (!list)
		translate_errors();
	return list;
}

int
hes_error()
{
	return errval;
}

void
hes_free(hp)
	char **hp;
{
	hesiod_free_list(context, hp);
}

static int
init_context()
{
	if (!inited) {
		inited = 1;
		if (hesiod_init(&context) < 0) {
			errval = HES_ER_CONFIG;
			return -1;
		}
		errval = HES_ER_OK;
	}
	return 0;
}

static void
translate_errors()
{
	switch (errno) {
	case ENOENT:
		errval = HES_ER_NOTFOUND;
		break;
	case ECONNREFUSED:
	case EMSGSIZE:
		errval = HES_ER_NET;
		break;
	case ENOMEM:
	default:
		/* Not a good match, but the best we can do. */
		errval = HES_ER_CONFIG;
		break;
	}
}
OpenPOWER on IntegriCloud