hesiod.c 10.0 KB

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