1 /* Copyright (c) 1996 by Internet Software Consortium.
2  *
3  * Permission to use, copy, modify, and distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
8  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
9  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
10  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
11  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
12  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
14  * SOFTWARE.
15  */
16 
17 /* Copyright 1996, 2000 by the Massachusetts Institute of Technology.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * * Redistributions of source code must retain the above copyright
24  *   notice, this list of conditions and the following disclaimer.
25  *
26  * * Redistributions in binary form must reproduce the above copyright
27  *   notice, this list of conditions and the following disclaimer in
28  *   the documentation and/or other materials provided with the
29  *   distribution.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42  * OF THE POSSIBILITY OF SUCH DAMAGE.
43  */
44 
45 /* This file is part of the hesiod library.  It implements the core
46  * portion of the hesiod resolver.
47  *
48  * This file is loosely based on an interim version of hesiod.c from
49  * the BIND IRS library, which was in turn based on an earlier version
50  * of this file.  Extensive changes have been made on each step of the
51  * path.
52  *
53  * This implementation is not truly thread-safe at the moment because
54  * it uses res_send() and accesses _res.
55  */
56 
57 static const char rcsid[] = "$Id: hesiod.c,v 1.30 2002-04-03 21:40:55 ghudson Exp $";
58 
59 #include "config.h"
60 #include <sys/types.h>
61 #include <netinet/in.h>
62 #define BIND_4_COMPAT
63 #define BIND_8_COMPAT
64 #include <arpa/nameser.h>
65 #include <errno.h>
66 #include <netdb.h>
67 #include <resolv.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72 #include <ctype.h>
73 #ifdef HAVE_LIBIDN
74 #include <idna.h>
75 #include <idn-free.h>
76 #endif
77 #include "hesiod.h"
78 
79 /* A few operating systems don't define this. */
80 #ifndef T_TXT
81 #define T_TXT	16
82 #endif
83 
84 /* Defaults if the configuration file is not present. */
85 #define DEF_RHS ".athena.mit.edu"
86 #define DEF_LHS ".ns"
87 
88 /* Maximum size of a Hesiod response from the DNS. */
89 #define MAX_HESRESP 1024
90 
91 /* The contents of a Hesiod context. */
92 struct hesiod_p {
93   char *lhs;			/* normally ".ns" */
94   char *rhs;			/* AKA the default hesiod domain */
95 };
96 
97 char **hesiod__uidresolve(void *context, const char *uidstr);
98 static int read_config_file(struct hesiod_p *ctx, const char *filename);
99 static char **get_txt_records(struct hesiod_p *ctx, const char *name);
100 static int cistrcmp(const char *s1, const char *s2);
101 
102 /* This function is called to initialize a hesiod_p. */
hesiod_init(void ** context)103 int hesiod_init(void **context)
104 {
105   struct hesiod_p *ctx;
106   const char *p, *configname;
107 
108   ctx = malloc(sizeof(struct hesiod_p));
109   if (ctx)
110     {
111       *context = ctx;
112       configname = ((getuid() == geteuid()) && (getgid() == getegid())) ? getenv("HESIOD_CONFIG") : NULL;
113       if (!configname)
114 	configname = SYSCONFDIR "/hesiod.conf";
115       if (read_config_file(ctx, configname) >= 0)
116 	{
117 	  /* The default rhs can be overridden by an environment variable. */
118 	  p = ((getuid() == geteuid()) && (getgid() == getegid())) ? getenv("HES_DOMAIN") : NULL;
119 	  if (p)
120 	    {
121 	      if (ctx->rhs)
122 		free(ctx->rhs);
123 	      ctx->rhs = malloc(strlen(p) + 2);
124 	      if (ctx->rhs)
125 		{
126 		  *ctx->rhs = '.';
127 		  strcpy(ctx->rhs + 1, (*p == '.') ? p + 1 : p);
128 		  return 0;
129 		}
130 	      else
131 		errno = ENOMEM;
132 	    }
133 	  else
134 	    return 0;
135 	}
136     }
137   else
138     errno = ENOMEM;
139 
140   if (ctx->lhs)
141     free(ctx->lhs);
142   if (ctx->rhs)
143     free(ctx->rhs);
144   if (ctx)
145     free(ctx);
146   return -1;
147 }
148 
149 /* This function deallocates the hesiod_p. */
hesiod_end(void * context)150 void hesiod_end(void *context)
151 {
152   struct hesiod_p *ctx = (struct hesiod_p *) context;
153 
154   free(ctx->rhs);
155   if (ctx->lhs)
156     free(ctx->lhs);
157   free(ctx);
158 }
159 
160 /* This function takes a hesiod (name, type) and returns a DNS
161  * name which is to be resolved.
162  */
hesiod_to_bind(void * context,const char * name,const char * type)163 char *hesiod_to_bind(void *context, const char *name, const char *type)
164 {
165   struct hesiod_p *ctx = (struct hesiod_p *) context;
166   char bindname[MAXDNAME], *p, *ret, *idn_ret, **rhs_list = NULL;
167   const char *rhs;
168   int len, rc;
169 
170   if (strlen(name) > sizeof(bindname) - 1)
171     {
172       errno = EMSGSIZE;
173       return NULL;
174     }
175   strcpy(bindname, name);
176 
177   /* Find the right right hand side to use, possibly truncating bindname. */
178   p = strchr(bindname, '@');
179   if (p)
180     {
181       *p++ = 0;
182       if (strchr(p, '.'))
183 	rhs = name + (p - bindname);
184       else
185 	{
186 	  rhs_list = hesiod_resolve(context, p, "rhs-extension");
187 	  if (rhs_list)
188 	    rhs = *rhs_list;
189 	  else
190 	    {
191 	      errno = ENOENT;
192 	      return NULL;
193 	    }
194 	}
195     } else
196       rhs = ctx->rhs;
197 
198   /* See if we have enough room. */
199   len = strlen(bindname) + 1 + strlen(type);
200   if (ctx->lhs)
201     len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
202   len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0);
203   if (len > sizeof(bindname) - 1)
204     {
205       if (rhs_list)
206 	hesiod_free_list(context, rhs_list);
207       errno = EMSGSIZE;
208       return NULL;
209     }
210 
211   /* Put together the rest of the domain. */
212   strcat(bindname, ".");
213   strcat(bindname, type);
214   if (ctx->lhs)
215     {
216       if (ctx->lhs[0] != '.')
217 	strcat(bindname, ".");
218       strcat(bindname, ctx->lhs);
219     }
220   if (rhs[0] != '.')
221     strcat(bindname, ".");
222   strcat(bindname, rhs);
223 
224   /* rhs_list is no longer needed, since we're done with rhs. */
225   if (rhs_list)
226     hesiod_free_list(context, rhs_list);
227 
228   /* Make a copy of the result and return it to the caller. */
229 #ifdef HAVE_LIBIDN
230   rc = idna_to_ascii_lz(bindname, &idn_ret, 0);
231   if (rc != IDNA_SUCCESS)
232     {
233       errno = EINVAL;
234       return NULL;
235     }
236   ret = strdup(idn_ret);
237   idn_free(idn_ret);
238 #else
239   ret = strdup(bindname);
240 #endif
241   if (!ret)
242     {
243       errno = ENOMEM;
244       return NULL;
245     }
246   return ret;
247 }
248 
249 /* This is the core function.  Given a hesiod name and type, it
250  * returns an array of strings returned by the resolver.
251  */
hesiod_resolve(void * context,const char * name,const char * type)252 char **hesiod_resolve(void *context, const char *name, const char *type)
253 {
254   struct hesiod_p *ctx = (struct hesiod_p *) context;
255   char *bindname, **retvec;
256 
257   bindname = hesiod_to_bind(context, name, type);
258   if (!bindname)
259     return NULL;
260 
261   retvec = get_txt_records(ctx, bindname);
262 
263   hesiod_free_string(context, bindname);
264   return retvec;
265 }
266 
hesiod_free_list(void * context,char ** list)267 void hesiod_free_list(void *context, char **list)
268 {
269   char **p;
270 
271   for (p = list; *p; p++)
272     free(*p);
273   free(list);
274 }
275 
hesiod_free_string(void * context,char * str)276 void hesiod_free_string(void *context, char *str)
277 {
278   free(str);
279 }
280 
281 /* This function parses the /etc/hesiod.conf file.  Returns 0 on success,
282  * -1 on failure.  On failure, it might leave values in ctx->lhs or
283  * ctx->rhs which need to be freed by the caller. */
read_config_file(struct hesiod_p * ctx,const char * filename)284 static int read_config_file(struct hesiod_p *ctx, const char *filename)
285 {
286   char *key, *data, *p, **which;
287   char buf[MAXDNAME + 7];
288   FILE *fp;
289 
290   /* Try to open the configuration file. */
291   fp = fopen(filename, "r");
292   if (!fp)
293     {
294       /* Use compiled in default domain names. */
295       ctx->lhs = malloc(strlen(DEF_LHS) + 1);
296       ctx->rhs = malloc(strlen(DEF_RHS) + 1);
297       if (ctx->lhs && ctx->rhs)
298 	{
299 	  strcpy(ctx->lhs, DEF_LHS);
300 	  strcpy(ctx->rhs, DEF_RHS);
301 	  return 0;
302 	}
303       else
304 	{
305 	  errno = ENOMEM;
306 	  return -1;
307 	}
308     }
309 
310   ctx->lhs = NULL;
311   ctx->rhs = NULL;
312   while (fgets(buf, sizeof(buf), fp) != NULL)
313     {
314       p = buf;
315       if (*p == '#' || *p == '\n' || *p == '\r')
316 	continue;
317       while(*p == ' ' || *p == '\t')
318 	p++;
319       key = p;
320       while(*p != ' ' && *p != '\t' && *p != '=')
321 	p++;
322       *p++ = 0;
323 
324       while(isspace((unsigned char)*p) || *p == '=')
325 	p++;
326       data = p;
327       while(!isspace((unsigned char)*p))
328 	p++;
329       *p = 0;
330 
331       if (cistrcmp(key, "lhs") == 0 || cistrcmp(key, "rhs") == 0)
332 	{
333 	  which = (cistrcmp(key, "lhs") == 0) ? &ctx->lhs : &ctx->rhs;
334 	  *which = malloc(strlen(data) + 1);
335 	  if (!*which)
336 	    {
337 	      errno = ENOMEM;
338 	      return -1;
339 	    }
340 	  strcpy(*which, data);
341 	}
342     }
343   fclose(fp);
344 
345   /* Make sure that the rhs is set. */
346   if (!ctx->rhs)
347     {
348       errno = ENOEXEC;
349       return -1;
350     }
351 
352   return 0;
353 }
354 
355 /* Given a DNS class and a DNS name, do a lookup for TXT records, and
356  * return a list of them.
357  */
get_txt_records(struct hesiod_p * ctx,const char * name)358 static char **get_txt_records(struct hesiod_p *ctx, const char *name)
359 {
360   unsigned char qbuf[PACKETSZ], *abuf;
361   char **tmp;
362   int n, i, len;
363 
364   /* Make sure the resolver is initialized. */
365   if ((_res.options & RES_INIT) == 0 && res_init() == -1)
366     return NULL;
367 
368   /* Construct the query. */
369   n = res_mkquery(QUERY, name, C_IN, T_TXT, NULL, 0, NULL, qbuf, PACKETSZ);
370   if (n < 0)
371     {
372       errno = EMSGSIZE;
373       return NULL;
374     }
375 
376   /* Send the query. */
377   abuf = NULL;
378   len = 1024;
379   i = n;
380   do
381     {
382       abuf = realloc(abuf, len);
383       if (abuf == NULL)
384         {
385           n = -1;
386           break;
387         }
388       n = res_send(qbuf, i, abuf, len);
389       if (n < len)
390         {
391           break;
392         }
393       len = n + 1024;
394     } while(1);
395   if (n < (ssize_t) sizeof(HEADER))
396     {
397       errno = ECONNREFUSED;
398       free(abuf);
399       return NULL;
400     }
401 
402   tmp = hesiod_parse_result(ctx, abuf, n);
403 
404   free(abuf);
405 
406   return tmp;
407 }
408 
hesiod_parse_result(void * ctx,const unsigned char * abuf,int alen)409 char **hesiod_parse_result(void *ctx, const unsigned char *abuf, int alen)
410 {
411   HEADER *hp;
412   unsigned const char *p, *eom, *eor;
413   char *dst, **list;
414   int ancount, qdcount, i, j, skip, type, class, len, n;
415 
416   /* Parse the header of the result. */
417   hp = (HEADER *) abuf;
418   ancount = ntohs(hp->ancount);
419   qdcount = ntohs(hp->qdcount);
420   p = abuf + sizeof(HEADER);
421   eom = abuf + alen;
422 
423   /* Skip questions, trying to get to the answer section which follows. */
424   for (i = 0; i < qdcount; i++)
425     {
426       skip = dn_skipname(p, eom);
427       if (skip < 0 || p + skip + QFIXEDSZ > eom)
428 	{
429 	  errno = EMSGSIZE;
430 	  return NULL;
431 	}
432       p += skip + QFIXEDSZ;
433     }
434 
435   /* Allocate space for the text record answers. */
436   list = malloc((ancount + 1) * sizeof(char *));
437   if (!list)
438     {
439       errno = ENOMEM;
440       return NULL;
441     }
442 
443   /* Parse the answers. */
444   j = 0;
445   for (i = 0; i < ancount; i++)
446     {
447       /* Parse the header of this answer. */
448       skip = dn_skipname(p, eom);
449       if (skip < 0 || p + skip + 10 > eom)
450 	break;
451       type = p[skip + 0] << 8 | p[skip + 1];
452       class = p[skip + 2] << 8 | p[skip + 3];
453       len = p[skip + 8] << 8 | p[skip + 9];
454       p += skip + 10;
455       if (p + len > eom)
456 	{
457 	  errno = EMSGSIZE;
458 	  break;
459 	}
460 
461       /* Skip entries of the wrong type. */
462       if (type != T_TXT)
463 	{
464 	  p += len;
465 	  continue;
466 	}
467 
468       /* Allocate space for this answer. */
469       list[j] = malloc(len);
470       if (!list[j])
471 	{
472 	  errno = ENOMEM;
473 	  break;
474 	}
475       dst = list[j++];
476 
477       /* Copy answer data into the allocated area. */
478       eor = p + len;
479       while (p < eor)
480 	{
481 	  n = (unsigned char) *p++;
482 	  if (p + n > eor)
483 	    {
484 	      errno = EMSGSIZE;
485 	      break;
486 	    }
487 	  memcpy(dst, p, n);
488 	  p += n;
489 	  dst += n;
490 	}
491       if (p < eor)
492 	{
493 	  errno = EMSGSIZE;
494 	  break;
495 	}
496       *dst = 0;
497     }
498 
499   /* If we didn't terminate the loop normally, something went wrong. */
500   if (i < ancount)
501     {
502       for (i = 0; i < j; i++)
503 	free(list[i]);
504       free(list);
505       return NULL;
506     }
507 
508   if (j == 0)
509     {
510       errno = ENOENT;
511       free(list);
512       return NULL;
513     }
514 
515   list[j] = NULL;
516   return list;
517 }
518 
cistrcmp(const char * s1,const char * s2)519 static int cistrcmp(const char *s1, const char *s2)
520 {
521   while (*s1 && *s2 && tolower(*s1) == tolower(*s2))
522     {
523       s1++;
524       s2++;
525     }
526   return tolower(*s1) - tolower(*s2);
527 }
528