Fix dbl-64 asin (sNaN) (bug 20213).
[glibc.git] / hesiod / hesiod.c
blobc2925a0f99e08750c37a8fd4f711597093c25899
1 /* Copyright (C) 1997-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
19 * Copyright (c) 1996,1999 by Internet Software Consortium.
21 * Permission to use, copy, modify, and distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
25 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
26 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
28 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
29 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
30 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
31 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
32 * SOFTWARE.
36 * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
40 * hesiod.c --- the core portion of the hesiod resolver.
42 * This file is derived from the hesiod library from Project Athena;
43 * It has been extensively rewritten by Theodore Ts'o to have a more
44 * thread-safe interface.
47 /* Imports */
49 #include <sys/types.h>
50 #include <netinet/in.h>
51 #include <arpa/nameser.h>
53 #include <errno.h>
54 #include <netdb.h>
55 #include <resolv.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
60 #include "hesiod.h"
61 #include "hesiod_p.h"
63 #define _PATH_HESIOD_CONF "/etc/hesiod.conf"
65 /* Forward */
67 static int parse_config_file(struct hesiod_p *ctx, const char *filename);
68 static char ** get_txt_records(struct hesiod_p *ctx, int class,
69 const char *name);
71 /* Public */
74 * This function is called to initialize a hesiod_p.
76 int
77 hesiod_init(void **context) {
78 struct hesiod_p *ctx;
79 const char *configname;
80 char *cp;
82 ctx = malloc(sizeof(struct hesiod_p));
83 if (ctx == 0)
84 return (-1);
86 ctx->LHS = NULL;
87 ctx->RHS = NULL;
88 /* Set default query classes. */
89 ctx->classes[0] = C_IN;
90 ctx->classes[1] = C_HS;
92 configname = __libc_secure_getenv("HESIOD_CONFIG");
93 if (!configname)
94 configname = _PATH_HESIOD_CONF;
95 if (parse_config_file(ctx, configname) < 0) {
96 goto cleanup;
99 * The default RHS can be overridden by an environment
100 * variable.
102 if ((cp = __libc_secure_getenv("HES_DOMAIN")) != NULL) {
103 free(ctx->RHS);
104 ctx->RHS = malloc(strlen(cp)+2);
105 if (!ctx->RHS)
106 goto cleanup;
107 if (cp[0] == '.')
108 strcpy(ctx->RHS, cp);
109 else {
110 ctx->RHS[0] = '.';
111 strcpy(ctx->RHS + 1, cp);
116 * If there is no default hesiod realm set, we return an
117 * error.
119 if (!ctx->RHS) {
120 __set_errno(ENOEXEC);
121 goto cleanup;
124 *context = ctx;
125 return (0);
127 cleanup:
128 hesiod_end(ctx);
129 return (-1);
133 * This function deallocates the hesiod_p
135 void
136 hesiod_end(void *context) {
137 struct hesiod_p *ctx = (struct hesiod_p *) context;
138 int save_errno = errno;
140 free(ctx->RHS);
141 free(ctx->LHS);
142 free(ctx);
143 __set_errno(save_errno);
147 * This function takes a hesiod (name, type) and returns a DNS
148 * name which is to be resolved.
150 char *
151 hesiod_to_bind(void *context, const char *name, const char *type) {
152 struct hesiod_p *ctx = (struct hesiod_p *) context;
153 char *bindname;
154 char **rhs_list = NULL;
155 const char *RHS, *cp;
156 char *endp;
158 /* Decide what our RHS is, and set cp to the end of the actual name. */
159 if ((cp = strchr(name, '@')) != NULL) {
160 if (strchr(cp + 1, '.'))
161 RHS = cp + 1;
162 else if ((rhs_list = hesiod_resolve(context, cp + 1,
163 "rhs-extension")) != NULL)
164 RHS = *rhs_list;
165 else {
166 __set_errno(ENOENT);
167 return (NULL);
169 } else {
170 RHS = ctx->RHS;
171 cp = name + strlen(name);
175 * Allocate the space we need, including up to three periods and
176 * the terminating NUL.
178 if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) +
179 (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) {
180 if (rhs_list)
181 hesiod_free_list(context, rhs_list);
182 return NULL;
185 /* Now put together the DNS name. */
186 endp = (char *) __mempcpy (bindname, name, cp - name);
187 *endp++ = '.';
188 endp = (char *) __stpcpy (endp, type);
189 if (ctx->LHS) {
190 if (ctx->LHS[0] != '.')
191 *endp++ = '.';
192 endp = __stpcpy (endp, ctx->LHS);
194 if (RHS[0] != '.')
195 *endp++ = '.';
196 strcpy (endp, RHS);
198 if (rhs_list)
199 hesiod_free_list(context, rhs_list);
201 return (bindname);
205 * This is the core function. Given a hesiod (name, type), it
206 * returns an array of strings returned by the resolver.
208 char **
209 hesiod_resolve(void *context, const char *name, const char *type) {
210 struct hesiod_p *ctx = (struct hesiod_p *) context;
211 char *bindname = hesiod_to_bind(context, name, type);
212 char **retvec;
214 if (bindname == NULL)
215 return (NULL);
217 retvec = get_txt_records(ctx, ctx->classes[0], bindname);
219 if (retvec == NULL && (errno == ENOENT || errno == ECONNREFUSED) && ctx->classes[1])
220 retvec = get_txt_records(ctx, ctx->classes[1], bindname);
223 free(bindname);
224 return (retvec);
227 void
228 hesiod_free_list(void *context, char **list) {
229 char **p;
231 for (p = list; *p; p++)
232 free(*p);
233 free(list);
237 * This function parses the /etc/hesiod.conf file
239 static int
240 parse_config_file(struct hesiod_p *ctx, const char *filename) {
241 char buf[MAXDNAME+7];
242 FILE *fp;
245 * Clear the existing configuration variable, just in case
246 * they're set.
248 free(ctx->RHS);
249 free(ctx->LHS);
250 ctx->RHS = ctx->LHS = 0;
251 /* Set default query classes. */
252 ctx->classes[0] = C_IN;
253 ctx->classes[1] = C_HS;
256 * Now open and parse the file...
258 if (!(fp = fopen(filename, "rce")))
259 return (-1);
261 while (fgets(buf, sizeof(buf), fp) != NULL) {
262 char *key, *data, *cp, **cpp;
264 cp = buf;
265 if (*cp == '#' || *cp == '\n' || *cp == '\r')
266 continue;
267 while(*cp == ' ' || *cp == '\t')
268 cp++;
269 key = cp;
270 while(*cp != ' ' && *cp != '\t' && *cp != '=')
271 cp++;
272 *cp++ = '\0';
274 while(*cp == ' ' || *cp == '\t' || *cp == '=')
275 cp++;
276 data = cp;
277 while(*cp != ' ' && *cp != '\n' && *cp != '\r')
278 cp++;
279 *cp++ = '\0';
281 cpp = NULL;
282 if (strcasecmp(key, "lhs") == 0)
283 cpp = &ctx->LHS;
284 else if (strcasecmp(key, "rhs") == 0)
285 cpp = &ctx->RHS;
286 if (cpp) {
287 *cpp = strdup(data);
288 if (!*cpp)
289 goto cleanup;
290 } else if (strcasecmp(key, "classes") == 0) {
291 int n = 0;
292 while (*data && n < 2) {
293 cp = strchrnul(data, ',');
294 if (*cp != '\0')
295 *cp++ = '\0';
296 if (strcasecmp(data, "IN") == 0)
297 ctx->classes[n++] = C_IN;
298 else if (strcasecmp(data, "HS") == 0)
299 ctx->classes[n++] = C_HS;
300 data = cp;
302 if (n == 0) {
303 /* Restore the default. Better than
304 nother at all. */
305 ctx->classes[0] = C_IN;
306 ctx->classes[1] = C_HS;
307 } else if (n == 1
308 || ctx->classes[0] == ctx->classes[1])
309 ctx->classes[1] = 0;
312 fclose(fp);
313 return (0);
315 cleanup:
316 fclose(fp);
317 free(ctx->RHS);
318 free(ctx->LHS);
319 ctx->RHS = ctx->LHS = 0;
320 return (-1);
324 * Given a DNS class and a DNS name, do a lookup for TXT records, and
325 * return a list of them.
327 static char **
328 get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
329 struct {
330 int type; /* RR type */
331 int class; /* RR class */
332 int dlen; /* len of data section */
333 u_char *data; /* pointer to data */
334 } rr;
335 HEADER *hp;
336 u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
337 u_char *cp, *erdata, *eom;
338 char *dst, *edst, **list;
339 int ancount, qdcount;
340 int i, j, n, skip;
343 * Construct the query and send it.
345 n = res_mkquery(QUERY, name, class, T_TXT, NULL, 0,
346 NULL, qbuf, MAX_HESRESP);
347 if (n < 0) {
348 __set_errno(EMSGSIZE);
349 return (NULL);
351 n = res_send(qbuf, n, abuf, MAX_HESRESP);
352 if (n < 0) {
353 __set_errno(ECONNREFUSED);
354 return (NULL);
356 if (n < HFIXEDSZ) {
357 __set_errno(EMSGSIZE);
358 return (NULL);
362 * OK, parse the result.
364 hp = (HEADER *) abuf;
365 ancount = ntohs(hp->ancount);
366 qdcount = ntohs(hp->qdcount);
367 cp = abuf + sizeof(HEADER);
368 eom = abuf + n;
370 /* Skip query, trying to get to the answer section which follows. */
371 for (i = 0; i < qdcount; i++) {
372 skip = dn_skipname(cp, eom);
373 if (skip < 0 || cp + skip + QFIXEDSZ > eom) {
374 __set_errno(EMSGSIZE);
375 return (NULL);
377 cp += skip + QFIXEDSZ;
380 list = malloc((ancount + 1) * sizeof(char *));
381 if (!list)
382 return (NULL);
383 j = 0;
384 for (i = 0; i < ancount; i++) {
385 skip = dn_skipname(cp, eom);
386 if (skip < 0) {
387 __set_errno(EMSGSIZE);
388 goto cleanup;
390 cp += skip;
391 if (cp + 3 * INT16SZ + INT32SZ > eom) {
392 __set_errno(EMSGSIZE);
393 goto cleanup;
395 rr.type = ns_get16(cp);
396 cp += INT16SZ;
397 rr.class = ns_get16(cp);
398 cp += INT16SZ + INT32SZ; /* skip the ttl, too */
399 rr.dlen = ns_get16(cp);
400 cp += INT16SZ;
401 if (rr.dlen == 0 || cp + rr.dlen > eom) {
402 __set_errno(EMSGSIZE);
403 goto cleanup;
405 rr.data = cp;
406 cp += rr.dlen;
407 if (rr.class != class || rr.type != T_TXT)
408 continue;
409 if (!(list[j] = malloc(rr.dlen)))
410 goto cleanup;
411 dst = list[j++];
412 edst = dst + rr.dlen;
413 erdata = rr.data + rr.dlen;
414 cp = rr.data;
415 while (cp < erdata) {
416 n = (unsigned char) *cp++;
417 if (cp + n > eom || dst + n > edst) {
418 __set_errno(EMSGSIZE);
419 goto cleanup;
421 memcpy(dst, cp, n);
422 cp += n;
423 dst += n;
425 if (cp != erdata) {
426 __set_errno(EMSGSIZE);
427 goto cleanup;
429 *dst = '\0';
431 list[j] = NULL;
432 if (j == 0) {
433 __set_errno(ENOENT);
434 goto cleanup;
436 return (list);
438 cleanup:
439 for (i = 0; i < j; i++)
440 free(list[i]);
441 free(list);
442 return (NULL);