17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate
19*9525b14bSRao Shoaib /*! \file
20*9525b14bSRao Shoaib * \brief
217c478bd9Sstevel@tonic-gate * hesiod.c --- the core portion of the hesiod resolver.
227c478bd9Sstevel@tonic-gate *
237c478bd9Sstevel@tonic-gate * This file is derived from the hesiod library from Project Athena;
247c478bd9Sstevel@tonic-gate * It has been extensively rewritten by Theodore Ts'o to have a more
257c478bd9Sstevel@tonic-gate * thread-safe interface.
26*9525b14bSRao Shoaib * \author
27*9525b14bSRao Shoaib * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /* Imports */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include "port_before.h"
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <netinet/in.h>
367c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <netdb.h>
407c478bd9Sstevel@tonic-gate #include <resolv.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include "port_after.h"
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #include "pathnames.h"
487c478bd9Sstevel@tonic-gate #include "hesiod.h"
497c478bd9Sstevel@tonic-gate #include "hesiod_p.h"
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /* Forward */
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate int hesiod_init(void **context);
547c478bd9Sstevel@tonic-gate void hesiod_end(void *context);
557c478bd9Sstevel@tonic-gate char * hesiod_to_bind(void *context, const char *name,
567c478bd9Sstevel@tonic-gate const char *type);
577c478bd9Sstevel@tonic-gate char ** hesiod_resolve(void *context, const char *name,
587c478bd9Sstevel@tonic-gate const char *type);
597c478bd9Sstevel@tonic-gate void hesiod_free_list(void *context, char **list);
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate static int parse_config_file(struct hesiod_p *ctx, const char *filename);
627c478bd9Sstevel@tonic-gate static char ** get_txt_records(struct hesiod_p *ctx, int class,
637c478bd9Sstevel@tonic-gate const char *name);
647c478bd9Sstevel@tonic-gate static int init(struct hesiod_p *ctx);
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /* Public */
677c478bd9Sstevel@tonic-gate
68*9525b14bSRao Shoaib /*%
697c478bd9Sstevel@tonic-gate * This function is called to initialize a hesiod_p.
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate int
hesiod_init(void ** context)727c478bd9Sstevel@tonic-gate hesiod_init(void **context) {
737c478bd9Sstevel@tonic-gate struct hesiod_p *ctx;
747c478bd9Sstevel@tonic-gate char *cp;
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate ctx = malloc(sizeof(struct hesiod_p));
777c478bd9Sstevel@tonic-gate if (ctx == 0) {
787c478bd9Sstevel@tonic-gate errno = ENOMEM;
797c478bd9Sstevel@tonic-gate return (-1);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate memset(ctx, 0, sizeof (*ctx));
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate if (parse_config_file(ctx, _PATH_HESIOD_CONF) < 0) {
857c478bd9Sstevel@tonic-gate #ifdef DEF_RHS
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * Use compiled in defaults.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate ctx->LHS = malloc(strlen(DEF_LHS) + 1);
907c478bd9Sstevel@tonic-gate ctx->RHS = malloc(strlen(DEF_RHS) + 1);
91*9525b14bSRao Shoaib if (ctx->LHS == NULL || ctx->RHS == NULL) {
927c478bd9Sstevel@tonic-gate errno = ENOMEM;
937c478bd9Sstevel@tonic-gate goto cleanup;
947c478bd9Sstevel@tonic-gate }
95*9525b14bSRao Shoaib strcpy(ctx->LHS, DEF_LHS); /* (checked) */
96*9525b14bSRao Shoaib strcpy(ctx->RHS, DEF_RHS); /* (checked) */
977c478bd9Sstevel@tonic-gate #else
987c478bd9Sstevel@tonic-gate goto cleanup;
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate * The default RHS can be overridden by an environment
1037c478bd9Sstevel@tonic-gate * variable.
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate if ((cp = getenv("HES_DOMAIN")) != NULL) {
1067c478bd9Sstevel@tonic-gate size_t RHSlen = strlen(cp) + 2;
1077c478bd9Sstevel@tonic-gate if (ctx->RHS)
1087c478bd9Sstevel@tonic-gate free(ctx->RHS);
1097c478bd9Sstevel@tonic-gate ctx->RHS = malloc(RHSlen);
1107c478bd9Sstevel@tonic-gate if (!ctx->RHS) {
1117c478bd9Sstevel@tonic-gate errno = ENOMEM;
1127c478bd9Sstevel@tonic-gate goto cleanup;
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate if (cp[0] == '.') {
115*9525b14bSRao Shoaib strcpy(ctx->RHS, cp); /* (checked) */
1167c478bd9Sstevel@tonic-gate } else {
117*9525b14bSRao Shoaib strcpy(ctx->RHS, "."); /* (checked) */
118*9525b14bSRao Shoaib strcat(ctx->RHS, cp); /* (checked) */
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * If there is no default hesiod realm set, we return an
1247c478bd9Sstevel@tonic-gate * error.
1257c478bd9Sstevel@tonic-gate */
1267c478bd9Sstevel@tonic-gate if (!ctx->RHS) {
1277c478bd9Sstevel@tonic-gate errno = ENOEXEC;
1287c478bd9Sstevel@tonic-gate goto cleanup;
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate #if 0
1327c478bd9Sstevel@tonic-gate if (res_ninit(ctx->res) < 0)
1337c478bd9Sstevel@tonic-gate goto cleanup;
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate *context = ctx;
1377c478bd9Sstevel@tonic-gate return (0);
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate cleanup:
1407c478bd9Sstevel@tonic-gate hesiod_end(ctx);
1417c478bd9Sstevel@tonic-gate return (-1);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
144*9525b14bSRao Shoaib /*%
1457c478bd9Sstevel@tonic-gate * This function deallocates the hesiod_p
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate void
hesiod_end(void * context)1487c478bd9Sstevel@tonic-gate hesiod_end(void *context) {
1497c478bd9Sstevel@tonic-gate struct hesiod_p *ctx = (struct hesiod_p *) context;
1507c478bd9Sstevel@tonic-gate int save_errno = errno;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (ctx->res)
1537c478bd9Sstevel@tonic-gate res_nclose(ctx->res);
1547c478bd9Sstevel@tonic-gate if (ctx->RHS)
1557c478bd9Sstevel@tonic-gate free(ctx->RHS);
1567c478bd9Sstevel@tonic-gate if (ctx->LHS)
1577c478bd9Sstevel@tonic-gate free(ctx->LHS);
1587c478bd9Sstevel@tonic-gate if (ctx->res && ctx->free_res)
1597c478bd9Sstevel@tonic-gate (*ctx->free_res)(ctx->res);
1607c478bd9Sstevel@tonic-gate free(ctx);
1617c478bd9Sstevel@tonic-gate errno = save_errno;
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
164*9525b14bSRao Shoaib /*%
1657c478bd9Sstevel@tonic-gate * This function takes a hesiod (name, type) and returns a DNS
1667c478bd9Sstevel@tonic-gate * name which is to be resolved.
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate char *
hesiod_to_bind(void * context,const char * name,const char * type)1697c478bd9Sstevel@tonic-gate hesiod_to_bind(void *context, const char *name, const char *type) {
1707c478bd9Sstevel@tonic-gate struct hesiod_p *ctx = (struct hesiod_p *) context;
1717c478bd9Sstevel@tonic-gate char *bindname;
1727c478bd9Sstevel@tonic-gate char **rhs_list = NULL;
1737c478bd9Sstevel@tonic-gate const char *RHS, *cp;
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate /* Decide what our RHS is, and set cp to the end of the actual name. */
1767c478bd9Sstevel@tonic-gate if ((cp = strchr(name, '@')) != NULL) {
1777c478bd9Sstevel@tonic-gate if (strchr(cp + 1, '.'))
1787c478bd9Sstevel@tonic-gate RHS = cp + 1;
1797c478bd9Sstevel@tonic-gate else if ((rhs_list = hesiod_resolve(context, cp + 1,
1807c478bd9Sstevel@tonic-gate "rhs-extension")) != NULL)
1817c478bd9Sstevel@tonic-gate RHS = *rhs_list;
1827c478bd9Sstevel@tonic-gate else {
1837c478bd9Sstevel@tonic-gate errno = ENOENT;
1847c478bd9Sstevel@tonic-gate return (NULL);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate } else {
1877c478bd9Sstevel@tonic-gate RHS = ctx->RHS;
1887c478bd9Sstevel@tonic-gate cp = name + strlen(name);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate * Allocate the space we need, including up to three periods and
1937c478bd9Sstevel@tonic-gate * the terminating NUL.
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) +
1967c478bd9Sstevel@tonic-gate (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) {
1977c478bd9Sstevel@tonic-gate errno = ENOMEM;
1987c478bd9Sstevel@tonic-gate if (rhs_list)
1997c478bd9Sstevel@tonic-gate hesiod_free_list(context, rhs_list);
2007c478bd9Sstevel@tonic-gate return NULL;
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /* Now put together the DNS name. */
2047c478bd9Sstevel@tonic-gate memcpy(bindname, name, cp - name);
2057c478bd9Sstevel@tonic-gate bindname[cp - name] = '\0';
2067c478bd9Sstevel@tonic-gate strcat(bindname, ".");
2077c478bd9Sstevel@tonic-gate strcat(bindname, type);
2087c478bd9Sstevel@tonic-gate if (ctx->LHS) {
2097c478bd9Sstevel@tonic-gate if (ctx->LHS[0] != '.')
2107c478bd9Sstevel@tonic-gate strcat(bindname, ".");
2117c478bd9Sstevel@tonic-gate strcat(bindname, ctx->LHS);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate if (RHS[0] != '.')
2147c478bd9Sstevel@tonic-gate strcat(bindname, ".");
2157c478bd9Sstevel@tonic-gate strcat(bindname, RHS);
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate if (rhs_list)
2187c478bd9Sstevel@tonic-gate hesiod_free_list(context, rhs_list);
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate return (bindname);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
223*9525b14bSRao Shoaib /*%
2247c478bd9Sstevel@tonic-gate * This is the core function. Given a hesiod (name, type), it
2257c478bd9Sstevel@tonic-gate * returns an array of strings returned by the resolver.
2267c478bd9Sstevel@tonic-gate */
2277c478bd9Sstevel@tonic-gate char **
hesiod_resolve(void * context,const char * name,const char * type)2287c478bd9Sstevel@tonic-gate hesiod_resolve(void *context, const char *name, const char *type) {
2297c478bd9Sstevel@tonic-gate struct hesiod_p *ctx = (struct hesiod_p *) context;
2307c478bd9Sstevel@tonic-gate char *bindname = hesiod_to_bind(context, name, type);
2317c478bd9Sstevel@tonic-gate char **retvec;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate if (bindname == NULL)
2347c478bd9Sstevel@tonic-gate return (NULL);
2357c478bd9Sstevel@tonic-gate if (init(ctx) == -1) {
2367c478bd9Sstevel@tonic-gate free(bindname);
2377c478bd9Sstevel@tonic-gate return (NULL);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate if ((retvec = get_txt_records(ctx, C_IN, bindname))) {
2417c478bd9Sstevel@tonic-gate free(bindname);
2427c478bd9Sstevel@tonic-gate return (retvec);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate if (errno != ENOENT)
2467c478bd9Sstevel@tonic-gate return (NULL);
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate retvec = get_txt_records(ctx, C_HS, bindname);
2497c478bd9Sstevel@tonic-gate free(bindname);
2507c478bd9Sstevel@tonic-gate return (retvec);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate void
hesiod_free_list(void * context,char ** list)2547c478bd9Sstevel@tonic-gate hesiod_free_list(void *context, char **list) {
2557c478bd9Sstevel@tonic-gate char **p;
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate UNUSED(context);
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate for (p = list; *p; p++)
2607c478bd9Sstevel@tonic-gate free(*p);
2617c478bd9Sstevel@tonic-gate free(list);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
264*9525b14bSRao Shoaib /*%
2657c478bd9Sstevel@tonic-gate * This function parses the /etc/hesiod.conf file
2667c478bd9Sstevel@tonic-gate */
2677c478bd9Sstevel@tonic-gate static int
parse_config_file(struct hesiod_p * ctx,const char * filename)2687c478bd9Sstevel@tonic-gate parse_config_file(struct hesiod_p *ctx, const char *filename) {
2697c478bd9Sstevel@tonic-gate char *key, *data, *cp, **cpp;
2707c478bd9Sstevel@tonic-gate char buf[MAXDNAME+7];
2717c478bd9Sstevel@tonic-gate FILE *fp;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * Clear the existing configuration variable, just in case
2757c478bd9Sstevel@tonic-gate * they're set.
2767c478bd9Sstevel@tonic-gate */
2777c478bd9Sstevel@tonic-gate if (ctx->RHS)
2787c478bd9Sstevel@tonic-gate free(ctx->RHS);
2797c478bd9Sstevel@tonic-gate if (ctx->LHS)
2807c478bd9Sstevel@tonic-gate free(ctx->LHS);
2817c478bd9Sstevel@tonic-gate ctx->RHS = ctx->LHS = 0;
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate * Now open and parse the file...
2857c478bd9Sstevel@tonic-gate */
2867c478bd9Sstevel@tonic-gate if (!(fp = fopen(filename, "r")))
2877c478bd9Sstevel@tonic-gate return (-1);
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof(buf), fp) != NULL) {
2907c478bd9Sstevel@tonic-gate cp = buf;
2917c478bd9Sstevel@tonic-gate if (*cp == '#' || *cp == '\n' || *cp == '\r')
2927c478bd9Sstevel@tonic-gate continue;
2937c478bd9Sstevel@tonic-gate while(*cp == ' ' || *cp == '\t')
2947c478bd9Sstevel@tonic-gate cp++;
2957c478bd9Sstevel@tonic-gate key = cp;
2967c478bd9Sstevel@tonic-gate while(*cp != ' ' && *cp != '\t' && *cp != '=')
2977c478bd9Sstevel@tonic-gate cp++;
2987c478bd9Sstevel@tonic-gate *cp++ = '\0';
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate while(*cp == ' ' || *cp == '\t' || *cp == '=')
3017c478bd9Sstevel@tonic-gate cp++;
3027c478bd9Sstevel@tonic-gate data = cp;
3037c478bd9Sstevel@tonic-gate while(*cp != ' ' && *cp != '\n' && *cp != '\r')
3047c478bd9Sstevel@tonic-gate cp++;
3057c478bd9Sstevel@tonic-gate *cp++ = '\0';
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate if (strcmp(key, "lhs") == 0)
3087c478bd9Sstevel@tonic-gate cpp = &ctx->LHS;
3097c478bd9Sstevel@tonic-gate else if (strcmp(key, "rhs") == 0)
3107c478bd9Sstevel@tonic-gate cpp = &ctx->RHS;
3117c478bd9Sstevel@tonic-gate else
3127c478bd9Sstevel@tonic-gate continue;
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate *cpp = malloc(strlen(data) + 1);
3157c478bd9Sstevel@tonic-gate if (!*cpp) {
3167c478bd9Sstevel@tonic-gate errno = ENOMEM;
3177c478bd9Sstevel@tonic-gate goto cleanup;
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate strcpy(*cpp, data);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate fclose(fp);
3227c478bd9Sstevel@tonic-gate return (0);
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate cleanup:
3257c478bd9Sstevel@tonic-gate fclose(fp);
3267c478bd9Sstevel@tonic-gate if (ctx->RHS)
3277c478bd9Sstevel@tonic-gate free(ctx->RHS);
3287c478bd9Sstevel@tonic-gate if (ctx->LHS)
3297c478bd9Sstevel@tonic-gate free(ctx->LHS);
3307c478bd9Sstevel@tonic-gate ctx->RHS = ctx->LHS = 0;
3317c478bd9Sstevel@tonic-gate return (-1);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate
334*9525b14bSRao Shoaib /*%
3357c478bd9Sstevel@tonic-gate * Given a DNS class and a DNS name, do a lookup for TXT records, and
3367c478bd9Sstevel@tonic-gate * return a list of them.
3377c478bd9Sstevel@tonic-gate */
3387c478bd9Sstevel@tonic-gate static char **
get_txt_records(struct hesiod_p * ctx,int class,const char * name)3397c478bd9Sstevel@tonic-gate get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
3407c478bd9Sstevel@tonic-gate struct {
341*9525b14bSRao Shoaib int type; /*%< RR type */
342*9525b14bSRao Shoaib int class; /*%< RR class */
343*9525b14bSRao Shoaib int dlen; /*%< len of data section */
344*9525b14bSRao Shoaib u_char *data; /*%< pointer to data */
3457c478bd9Sstevel@tonic-gate } rr;
3467c478bd9Sstevel@tonic-gate HEADER *hp;
3477c478bd9Sstevel@tonic-gate u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
3487c478bd9Sstevel@tonic-gate u_char *cp, *erdata, *eom;
3497c478bd9Sstevel@tonic-gate char *dst, *edst, **list;
3507c478bd9Sstevel@tonic-gate int ancount, qdcount;
3517c478bd9Sstevel@tonic-gate int i, j, n, skip;
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate * Construct the query and send it.
3557c478bd9Sstevel@tonic-gate */
3567c478bd9Sstevel@tonic-gate n = res_nmkquery(ctx->res, QUERY, name, class, T_TXT, NULL, 0,
3577c478bd9Sstevel@tonic-gate NULL, qbuf, MAX_HESRESP);
3587c478bd9Sstevel@tonic-gate if (n < 0) {
3597c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
3607c478bd9Sstevel@tonic-gate return (NULL);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate n = res_nsend(ctx->res, qbuf, n, abuf, MAX_HESRESP);
3637c478bd9Sstevel@tonic-gate if (n < 0) {
3647c478bd9Sstevel@tonic-gate errno = ECONNREFUSED;
3657c478bd9Sstevel@tonic-gate return (NULL);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate if (n < HFIXEDSZ) {
3687c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
3697c478bd9Sstevel@tonic-gate return (NULL);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate * OK, parse the result.
3747c478bd9Sstevel@tonic-gate */
3757c478bd9Sstevel@tonic-gate hp = (HEADER *) abuf;
3767c478bd9Sstevel@tonic-gate ancount = ntohs(hp->ancount);
3777c478bd9Sstevel@tonic-gate qdcount = ntohs(hp->qdcount);
3787c478bd9Sstevel@tonic-gate cp = abuf + sizeof(HEADER);
3797c478bd9Sstevel@tonic-gate eom = abuf + n;
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate /* Skip query, trying to get to the answer section which follows. */
3827c478bd9Sstevel@tonic-gate for (i = 0; i < qdcount; i++) {
3837c478bd9Sstevel@tonic-gate skip = dn_skipname(cp, eom);
3847c478bd9Sstevel@tonic-gate if (skip < 0 || cp + skip + QFIXEDSZ > eom) {
3857c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
3867c478bd9Sstevel@tonic-gate return (NULL);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate cp += skip + QFIXEDSZ;
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate list = malloc((ancount + 1) * sizeof(char *));
3927c478bd9Sstevel@tonic-gate if (!list) {
3937c478bd9Sstevel@tonic-gate errno = ENOMEM;
3947c478bd9Sstevel@tonic-gate return (NULL);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate j = 0;
3977c478bd9Sstevel@tonic-gate for (i = 0; i < ancount; i++) {
3987c478bd9Sstevel@tonic-gate skip = dn_skipname(cp, eom);
3997c478bd9Sstevel@tonic-gate if (skip < 0) {
4007c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
4017c478bd9Sstevel@tonic-gate goto cleanup;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate cp += skip;
4047c478bd9Sstevel@tonic-gate if (cp + 3 * INT16SZ + INT32SZ > eom) {
4057c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
4067c478bd9Sstevel@tonic-gate goto cleanup;
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate rr.type = ns_get16(cp);
4097c478bd9Sstevel@tonic-gate cp += INT16SZ;
4107c478bd9Sstevel@tonic-gate rr.class = ns_get16(cp);
411*9525b14bSRao Shoaib cp += INT16SZ + INT32SZ; /*%< skip the ttl, too */
4127c478bd9Sstevel@tonic-gate rr.dlen = ns_get16(cp);
4137c478bd9Sstevel@tonic-gate cp += INT16SZ;
4147c478bd9Sstevel@tonic-gate if (cp + rr.dlen > eom) {
4157c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
4167c478bd9Sstevel@tonic-gate goto cleanup;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate rr.data = cp;
4197c478bd9Sstevel@tonic-gate cp += rr.dlen;
4207c478bd9Sstevel@tonic-gate if (rr.class != class || rr.type != T_TXT)
4217c478bd9Sstevel@tonic-gate continue;
4227c478bd9Sstevel@tonic-gate if (!(list[j] = malloc(rr.dlen)))
4237c478bd9Sstevel@tonic-gate goto cleanup;
4247c478bd9Sstevel@tonic-gate dst = list[j++];
4257c478bd9Sstevel@tonic-gate edst = dst + rr.dlen;
4267c478bd9Sstevel@tonic-gate erdata = rr.data + rr.dlen;
4277c478bd9Sstevel@tonic-gate cp = rr.data;
4287c478bd9Sstevel@tonic-gate while (cp < erdata) {
4297c478bd9Sstevel@tonic-gate n = (unsigned char) *cp++;
4307c478bd9Sstevel@tonic-gate if (cp + n > eom || dst + n > edst) {
4317c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
4327c478bd9Sstevel@tonic-gate goto cleanup;
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate memcpy(dst, cp, n);
4357c478bd9Sstevel@tonic-gate cp += n;
4367c478bd9Sstevel@tonic-gate dst += n;
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate if (cp != erdata) {
4397c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
4407c478bd9Sstevel@tonic-gate goto cleanup;
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate *dst = '\0';
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate list[j] = NULL;
4457c478bd9Sstevel@tonic-gate if (j == 0) {
4467c478bd9Sstevel@tonic-gate errno = ENOENT;
4477c478bd9Sstevel@tonic-gate goto cleanup;
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate return (list);
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate cleanup:
4527c478bd9Sstevel@tonic-gate for (i = 0; i < j; i++)
4537c478bd9Sstevel@tonic-gate free(list[i]);
4547c478bd9Sstevel@tonic-gate free(list);
4557c478bd9Sstevel@tonic-gate return (NULL);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate struct __res_state *
__hesiod_res_get(void * context)4597c478bd9Sstevel@tonic-gate __hesiod_res_get(void *context) {
4607c478bd9Sstevel@tonic-gate struct hesiod_p *ctx = context;
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate if (!ctx->res) {
4637c478bd9Sstevel@tonic-gate struct __res_state *res;
4647c478bd9Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res);
4657c478bd9Sstevel@tonic-gate if (res == NULL) {
4667c478bd9Sstevel@tonic-gate errno = ENOMEM;
4677c478bd9Sstevel@tonic-gate return (NULL);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate memset(res, 0, sizeof *res);
4707c478bd9Sstevel@tonic-gate __hesiod_res_set(ctx, res, free);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate return (ctx->res);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate void
__hesiod_res_set(void * context,struct __res_state * res,void (* free_res)(void *))4777c478bd9Sstevel@tonic-gate __hesiod_res_set(void *context, struct __res_state *res,
4787c478bd9Sstevel@tonic-gate void (*free_res)(void *)) {
4797c478bd9Sstevel@tonic-gate struct hesiod_p *ctx = context;
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate if (ctx->res && ctx->free_res) {
4827c478bd9Sstevel@tonic-gate res_nclose(ctx->res);
4837c478bd9Sstevel@tonic-gate (*ctx->free_res)(ctx->res);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate ctx->res = res;
4877c478bd9Sstevel@tonic-gate ctx->free_res = free_res;
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate static int
init(struct hesiod_p * ctx)4917c478bd9Sstevel@tonic-gate init(struct hesiod_p *ctx) {
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate if (!ctx->res && !__hesiod_res_get(ctx))
4947c478bd9Sstevel@tonic-gate return (-1);
4957c478bd9Sstevel@tonic-gate
496*9525b14bSRao Shoaib if (((ctx->res->options & RES_INIT) == 0U) &&
4977c478bd9Sstevel@tonic-gate (res_ninit(ctx->res) == -1))
4987c478bd9Sstevel@tonic-gate return (-1);
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate return (0);
5017c478bd9Sstevel@tonic-gate }
502