]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - hesiod/hesiod.c
da0db15d8427574cfc50ab3b220889a13bcb4e94
[1ts-debian.git] / hesiod / hesiod.c
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 by the Massachusetts Institute of Technology.
18  *
19  * Permission to use, copy, modify, and distribute this
20  * software and its documentation for any purpose and without
21  * fee is hereby granted, provided that the above copyright
22  * notice appear in all copies and that both that copyright
23  * notice and this permission notice appear in supporting
24  * documentation, and that the name of M.I.T. not be used in
25  * advertising or publicity pertaining to distribution of the
26  * software without specific, written prior permission.
27  * M.I.T. makes no representations about the suitability of
28  * this software for any purpose.  It is provided "as is"
29  * without express or implied warranty.
30  */
31
32 /* This file is part of the hesiod library.  It implements the core
33  * portion of the hesiod resolver.
34  *
35  * This file is loosely based on an interim version of hesiod.c from
36  * the BIND IRS library, which was in turn based on an earlier version
37  * of this file.  Extensive changes have been made on each step of the
38  * path.
39  *
40  * This implementation is not truly thread-safe at the moment because
41  * it uses res_send() and accesses _res.
42  */
43
44 static const char rcsid[] = "$Id: hesiod.c,v 1.18.2.1 1997/01/03 20:48:20 ghudson Exp $";
45
46 #include <sys/types.h>
47 #include <netinet/in.h>
48 #include <arpa/nameser.h>
49 #include <errno.h>
50 #include <netdb.h>
51 #include <resolv.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include "hesiod.h"
57 #include "hesiod_p.h"
58
59 /* A few operating systems don't define these. */
60 #ifndef C_HS
61 #define C_HS    4
62 #endif
63 #ifndef T_TXT
64 #define T_TXT   16
65 #endif
66
67 static int read_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);
70 static int cistrcmp(const char *s1, const char *s2);
71
72 /* This function is called to initialize a hesiod_p. */
73 int hesiod_init(void **context)
74 {
75   struct hesiod_p *ctx;
76   const char *p, *configname;
77
78   ctx = malloc(sizeof(struct hesiod_p));
79   if (ctx)
80     {
81       *context = ctx;
82       configname = getenv("HESIOD_CONFIG");
83       if (!configname)
84         configname = SYSCONFDIR "/hesiod.conf";
85       if (read_config_file(ctx, configname) >= 0)
86         {
87           /* The default rhs can be overridden by an environment variable. */
88           p = getenv("HES_DOMAIN");
89           if (p)
90             {
91               if (ctx->rhs)
92                 free(ctx->rhs);
93               ctx->rhs = malloc(strlen(p) + 2);
94               if (ctx->rhs)
95                 {
96                   *ctx->rhs = '.';
97                   strcpy(ctx->rhs + 1, (*p == '.') ? p + 1 : p);
98                   return 0;
99                 }
100               else
101                 errno = ENOMEM;
102             }
103           else
104             return 0;
105         }
106     }
107   else
108     errno = ENOMEM;
109
110   if (ctx->lhs)
111     free(ctx->lhs);
112   if (ctx->rhs)
113     free(ctx->rhs);
114   if (ctx)
115     free(ctx);
116   return -1;
117 }
118
119 /* This function deallocates the hesiod_p. */
120 void hesiod_end(void *context)
121 {
122   struct hesiod_p *ctx = (struct hesiod_p *) context;
123
124   free(ctx->rhs);
125   if (ctx->lhs)
126     free(ctx->lhs);
127   free(ctx);
128 }
129
130 /* This function takes a hesiod (name, type) and returns a DNS
131  * name which is to be resolved.
132  */
133 char *hesiod_to_bind(void *context, const char *name, const char *type)
134 {
135   struct hesiod_p *ctx = (struct hesiod_p *) context;
136   char bindname[MAXDNAME], *p, *ret, **rhs_list = NULL;
137   const char *rhs;
138   int len;
139         
140   strcpy(bindname, name);
141
142   /* Find the right right hand side to use, possibly truncating bindname. */
143   p = strchr(bindname, '@');
144   if (p)
145     {
146       *p++ = 0;
147       if (strchr(p, '.'))
148         rhs = name + (p - bindname);
149       else
150         {
151           rhs_list = hesiod_resolve(context, p, "rhs-extension");
152           if (rhs_list)
153             rhs = *rhs_list;
154           else
155             {
156               errno = ENOENT;
157               return NULL;
158             }
159         }
160     } else
161       rhs = ctx->rhs;
162
163   /* See if we have enough room. */
164   len = strlen(bindname) + 1 + strlen(type);
165   if (ctx->lhs)
166     len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
167   len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0);
168   if (len > sizeof(bindname) - 1)
169     {
170       if (rhs_list)
171         hesiod_free_list(context, rhs_list);
172       errno = EMSGSIZE;
173       return NULL;
174     }
175
176   /* Put together the rest of the domain. */
177   strcat(bindname, ".");
178   strcat(bindname, type);
179   if (ctx->lhs)
180     {
181       if (ctx->lhs[0] != '.')
182         strcat(bindname, ".");
183       strcat(bindname, ctx->lhs);
184     }
185   if (rhs[0] != '.')
186     strcat(bindname, ".");
187   strcat(bindname, rhs);
188
189   /* rhs_list is no longer needed, since we're done with rhs. */
190   if (rhs_list)
191     hesiod_free_list(context, rhs_list);
192
193   /* Make a copy of the result and return it to the caller. */
194   ret = malloc(strlen(bindname) + 1);
195   if (!ret)
196     {
197       errno = ENOMEM;
198       return NULL;
199     }
200   strcpy(ret, bindname);
201   return ret;
202 }
203
204 /* This is the core function.  Given a hesiod name and type, it
205  * returns an array of strings returned by the resolver.
206  */
207 char **hesiod_resolve(void *context, const char *name, const char *type)
208 {
209   struct hesiod_p *ctx = (struct hesiod_p *) context;
210   char *bindname, **retvec;
211
212   bindname = hesiod_to_bind(context, name, type);
213   if (!bindname)
214     return NULL;
215
216   retvec = get_txt_records(ctx, ctx->classes[0], bindname);
217   if (retvec == NULL && errno == ENOENT && ctx->classes[1])
218     retvec = get_txt_records(ctx, ctx->classes[1], bindname);
219
220   free(bindname);
221   return retvec;
222 }
223
224 void hesiod_free_list(void *context, char **list)
225 {
226   char **p;
227
228   for (p = list; *p; p++)
229     free(*p);
230   free(list);
231 }
232
233 /* This function parses the /etc/hesiod.conf file.  Returns 0 on success,
234  * -1 on failure.  On failure, it might leave values in ctx->lhs or
235  * ctx->rhs which need to be freed by the caller. */
236 static int read_config_file(struct hesiod_p *ctx, const char *filename)
237 {
238   char *key, *data, *p, **which;
239   char buf[MAXDNAME + 7];
240   int n;
241   FILE *fp;
242
243   /* Set default query classes. */
244   ctx->classes[0] = C_IN;
245   ctx->classes[1] = C_HS;
246
247   /* Try to open the configuration file. */
248   fp = fopen(filename, "r");
249   if (!fp)
250     {
251       /* Use compiled in default domain names. */
252       ctx->lhs = malloc(strlen(DEF_LHS) + 1);
253       ctx->rhs = malloc(strlen(DEF_RHS) + 1);
254       if (ctx->lhs && ctx->rhs)
255         {
256           strcpy(ctx->lhs, DEF_LHS);
257           strcpy(ctx->rhs, DEF_RHS);
258           return 0;
259         }
260       else
261         {
262           errno = ENOMEM;
263           return -1;
264         }
265     }
266
267   ctx->lhs = NULL;
268   ctx->rhs = NULL;
269   while (fgets(buf, sizeof(buf), fp) != NULL)
270     {
271       p = buf;
272       if (*p == '#' || *p == '\n' || *p == '\r')
273         continue;
274       while(*p == ' ' || *p == '\t')
275         p++;
276       key = p;
277       while(*p != ' ' && *p != '\t' && *p != '=')
278         p++;
279       *p++ = 0;
280                 
281       while(isspace(*p) || *p == '=')
282         p++;
283       data = p;
284       while(!isspace(*p))
285         p++;
286       *p = 0;
287
288       if (cistrcmp(key, "lhs") == 0 || cistrcmp(key, "rhs") == 0)
289         {
290           which = (strcmp(key, "lhs") == 0) ? &ctx->lhs : &ctx->rhs;
291           *which = malloc(strlen(data) + 1);
292           if (!*which)
293             {
294               errno = ENOMEM;
295               return -1;
296             }
297           strcpy(*which, data);
298         }
299       else if (cistrcmp(key, "classes") == 0)
300         {
301           n = 0;
302           while (*data && n < 2)
303             {
304               p = data;
305               while (*p && *p != ',')
306                 p++;
307               if (*p)
308                 *p++ = 0;
309               if (cistrcmp(data, "IN") == 0)
310                 ctx->classes[n++] = C_IN;
311               else if (cistrcmp(data, "HS") == 0)
312                 ctx->classes[n++] = C_HS;
313               data = p;
314             }
315           while (n < 2)
316             ctx->classes[n++] = 0;
317         }
318     }
319   fclose(fp);
320
321   if (!ctx->rhs || ctx->classes[0] == 0 || ctx->classes[0] == ctx->classes[1])
322     {
323       errno = ENOEXEC;
324       return -1;
325     }
326
327   return 0;
328 }       
329
330 /* Given a DNS class and a DNS name, do a lookup for TXT records, and
331  * return a list of them.
332  */
333 static char **get_txt_records(struct hesiod_p *ctx, int qclass,
334                               const char *name)
335 {
336   HEADER *hp;
337   unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
338   char *dst, **list;
339   int ancount, qdcount, i, j, n, skip, type, class, len;
340
341   /* Make sure the resolver is initialized. */
342   if ((_res.options & RES_INIT) == 0 && res_init() == -1)
343     return NULL;
344
345   /* Construct the query. */
346   n = res_mkquery(QUERY, name, qclass, T_TXT, NULL, 0,
347                   NULL, qbuf, PACKETSZ);
348   if (n < 0)
349       return NULL;
350
351   /* Send the query. */
352   n = res_send(qbuf, n, abuf, MAX_HESRESP);
353   if (n < 0)
354     {
355       errno = ECONNREFUSED;
356       return NULL;
357     }
358
359   /* Parse the header of the result. */
360   hp = (HEADER *) abuf;
361   ancount = ntohs(hp->ancount);
362   qdcount = ntohs(hp->qdcount);
363   p = abuf + sizeof(HEADER);
364   eom = abuf + n;
365
366   /* Skip questions, trying to get to the answer section which follows. */
367   for (i = 0; i < qdcount; i++)
368     {
369       skip = dn_skipname(p, eom);
370       if (skip < 0 || p + skip + QFIXEDSZ > eom)
371         {
372           errno = EMSGSIZE;
373           return NULL;
374         }
375       p += skip + QFIXEDSZ;
376     }
377
378   /* Allocate space for the text record answers. */
379   list = malloc((ancount + 1) * sizeof(char *));
380   if (!list)
381     {
382       errno = ENOMEM;
383       return NULL;
384     }
385
386   /* Parse the answers. */
387   j = 0;
388   for (i = 0; i < ancount; i++)
389     {
390       /* Parse the header of this answer. */
391       skip = dn_skipname(p, eom);
392       if (skip < 0 || p + skip + 10 > eom)
393         break;
394       type = p[skip + 0] << 8 | p[skip + 1];
395       class = p[skip + 2] << 8 | p[skip + 3];
396       len = p[skip + 8] << 8 | p[skip + 9];
397       p += skip + 10;
398       if (p + len > eom)
399         {
400           errno = EMSGSIZE;
401           break;
402         }
403
404       /* Skip entries of the wrong class and type. */
405       if (class != qclass || type != T_TXT)
406         {
407           p += len;
408           continue;
409         }
410
411       /* Allocate space for this answer. */
412       list[j] = malloc(len);
413       if (!list[j])
414         {
415           errno = ENOMEM;
416           break;
417         }
418       dst = list[j++];
419
420       /* Copy answer data into the allocated area. */
421       eor = p + len;
422       while (p < eor)
423         {
424           n = (unsigned char) *p++;
425           if (p + n > eor)
426             {
427               errno = EMSGSIZE;
428               break;
429             }
430           memcpy(dst, p, n);
431           p += n;
432           dst += n;
433         }
434       if (p < eor)
435         {
436           errno = EMSGSIZE;
437           break;
438         }
439       *dst = 0;
440     }
441
442   /* If we didn't terminate the loop normally, something went wrong. */
443   if (i < ancount)
444     {
445       for (i = 0; i < j; i++)
446         free(list[i]);
447       free(list);
448       return NULL;
449     }
450
451   if (j == 0)
452     {
453       errno = ENOENT;
454       free(list);
455       return NULL;
456     }
457
458   list[j] = NULL;
459   return list;
460 }
461
462 static int cistrcmp(const char *s1, const char *s2)
463 {
464   while (*s1 && tolower(*s1) == tolower(*s2))
465     {
466       s1++;
467       s2++;
468     }
469   return tolower(*s1) - tolower(*s2);
470 }