/contrib/bind9/lib/isccfg/aclconf.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 494 lines · 360 code · 55 blank · 79 comment · 139 complexity · cc557448abbc00b0b703f8c17570b02d MD5 · raw file

  1. /*
  2. * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2002 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. #include <config.h>
  19. #include <isc/mem.h>
  20. #include <isc/string.h> /* Required for HP/UX (and others?) */
  21. #include <isc/util.h>
  22. #include <isccfg/namedconf.h>
  23. #include <isccfg/aclconf.h>
  24. #include <dns/acl.h>
  25. #include <dns/iptable.h>
  26. #include <dns/fixedname.h>
  27. #include <dns/log.h>
  28. #define LOOP_MAGIC ISC_MAGIC('L','O','O','P')
  29. isc_result_t
  30. cfg_aclconfctx_create(isc_mem_t *mctx, cfg_aclconfctx_t **ret) {
  31. isc_result_t result;
  32. cfg_aclconfctx_t *actx;
  33. REQUIRE(mctx != NULL);
  34. REQUIRE(ret != NULL && *ret == NULL);
  35. actx = isc_mem_get(mctx, sizeof(*actx));
  36. if (actx == NULL)
  37. return (ISC_R_NOMEMORY);
  38. result = isc_refcount_init(&actx->references, 1);
  39. if (result != ISC_R_SUCCESS)
  40. goto cleanup;
  41. actx->mctx = NULL;
  42. isc_mem_attach(mctx, &actx->mctx);
  43. ISC_LIST_INIT(actx->named_acl_cache);
  44. *ret = actx;
  45. return (ISC_R_SUCCESS);
  46. cleanup:
  47. isc_mem_put(mctx, actx, sizeof(*actx));
  48. return (result);
  49. }
  50. void
  51. cfg_aclconfctx_attach(cfg_aclconfctx_t *src, cfg_aclconfctx_t **dest) {
  52. REQUIRE(src != NULL);
  53. REQUIRE(dest != NULL && *dest == NULL);
  54. isc_refcount_increment(&src->references, NULL);
  55. *dest = src;
  56. }
  57. void
  58. cfg_aclconfctx_detach(cfg_aclconfctx_t **actxp) {
  59. cfg_aclconfctx_t *actx;
  60. dns_acl_t *dacl, *next;
  61. unsigned int refs;
  62. REQUIRE(actxp != NULL && *actxp != NULL);
  63. actx = *actxp;
  64. isc_refcount_decrement(&actx->references, &refs);
  65. if (refs == 0) {
  66. for (dacl = ISC_LIST_HEAD(actx->named_acl_cache);
  67. dacl != NULL;
  68. dacl = next)
  69. {
  70. next = ISC_LIST_NEXT(dacl, nextincache);
  71. ISC_LIST_UNLINK(actx->named_acl_cache, dacl,
  72. nextincache);
  73. dns_acl_detach(&dacl);
  74. }
  75. isc_mem_putanddetach(&actx->mctx, actx, sizeof(*actx));
  76. }
  77. *actxp = NULL;
  78. }
  79. /*
  80. * Find the definition of the named acl whose name is "name".
  81. */
  82. static isc_result_t
  83. get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
  84. isc_result_t result;
  85. const cfg_obj_t *acls = NULL;
  86. const cfg_listelt_t *elt;
  87. result = cfg_map_get(cctx, "acl", &acls);
  88. if (result != ISC_R_SUCCESS)
  89. return (result);
  90. for (elt = cfg_list_first(acls);
  91. elt != NULL;
  92. elt = cfg_list_next(elt)) {
  93. const cfg_obj_t *acl = cfg_listelt_value(elt);
  94. const char *aclname = cfg_obj_asstring(cfg_tuple_get(acl, "name"));
  95. if (strcasecmp(aclname, name) == 0) {
  96. if (ret != NULL) {
  97. *ret = cfg_tuple_get(acl, "value");
  98. }
  99. return (ISC_R_SUCCESS);
  100. }
  101. }
  102. return (ISC_R_NOTFOUND);
  103. }
  104. static isc_result_t
  105. convert_named_acl(const cfg_obj_t *nameobj, const cfg_obj_t *cctx,
  106. isc_log_t *lctx, cfg_aclconfctx_t *ctx,
  107. isc_mem_t *mctx, unsigned int nest_level,
  108. dns_acl_t **target)
  109. {
  110. isc_result_t result;
  111. const cfg_obj_t *cacl = NULL;
  112. dns_acl_t *dacl;
  113. dns_acl_t loop;
  114. const char *aclname = cfg_obj_asstring(nameobj);
  115. /* Look for an already-converted version. */
  116. for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
  117. dacl != NULL;
  118. dacl = ISC_LIST_NEXT(dacl, nextincache))
  119. {
  120. if (strcasecmp(aclname, dacl->name) == 0) {
  121. if (ISC_MAGIC_VALID(dacl, LOOP_MAGIC)) {
  122. cfg_obj_log(nameobj, lctx, ISC_LOG_ERROR,
  123. "acl loop detected: %s", aclname);
  124. return (ISC_R_FAILURE);
  125. }
  126. dns_acl_attach(dacl, target);
  127. return (ISC_R_SUCCESS);
  128. }
  129. }
  130. /* Not yet converted. Convert now. */
  131. result = get_acl_def(cctx, aclname, &cacl);
  132. if (result != ISC_R_SUCCESS) {
  133. cfg_obj_log(nameobj, lctx, ISC_LOG_WARNING,
  134. "undefined ACL '%s'", aclname);
  135. return (result);
  136. }
  137. /*
  138. * Add a loop detection element.
  139. */
  140. memset(&loop, 0, sizeof(loop));
  141. ISC_LINK_INIT(&loop, nextincache);
  142. DE_CONST(aclname, loop.name);
  143. loop.magic = LOOP_MAGIC;
  144. ISC_LIST_APPEND(ctx->named_acl_cache, &loop, nextincache);
  145. result = cfg_acl_fromconfig(cacl, cctx, lctx, ctx, mctx,
  146. nest_level, &dacl);
  147. ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
  148. loop.magic = 0;
  149. loop.name = NULL;
  150. if (result != ISC_R_SUCCESS)
  151. return (result);
  152. dacl->name = isc_mem_strdup(dacl->mctx, aclname);
  153. if (dacl->name == NULL)
  154. return (ISC_R_NOMEMORY);
  155. ISC_LIST_APPEND(ctx->named_acl_cache, dacl, nextincache);
  156. dns_acl_attach(dacl, target);
  157. return (ISC_R_SUCCESS);
  158. }
  159. static isc_result_t
  160. convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
  161. dns_name_t *dnsname)
  162. {
  163. isc_result_t result;
  164. isc_buffer_t buf;
  165. dns_fixedname_t fixname;
  166. unsigned int keylen;
  167. const char *txtname = cfg_obj_asstring(keyobj);
  168. keylen = strlen(txtname);
  169. isc_buffer_init(&buf, txtname, keylen);
  170. isc_buffer_add(&buf, keylen);
  171. dns_fixedname_init(&fixname);
  172. result = dns_name_fromtext(dns_fixedname_name(&fixname), &buf,
  173. dns_rootname, 0, NULL);
  174. if (result != ISC_R_SUCCESS) {
  175. cfg_obj_log(keyobj, lctx, ISC_LOG_WARNING,
  176. "key name '%s' is not a valid domain name",
  177. txtname);
  178. return (result);
  179. }
  180. return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
  181. }
  182. /*
  183. * Recursively pre-parse an ACL definition to find the total number
  184. * of non-IP-prefix elements (localhost, localnets, key) in all nested
  185. * ACLs, so that the parent will have enough space allocated for the
  186. * elements table after all the nested ACLs have been merged in to the
  187. * parent.
  188. */
  189. static int
  190. count_acl_elements(const cfg_obj_t *caml, const cfg_obj_t *cctx,
  191. isc_boolean_t *has_negative)
  192. {
  193. const cfg_listelt_t *elt;
  194. const cfg_obj_t *cacl = NULL;
  195. isc_result_t result;
  196. int n = 0;
  197. if (has_negative != NULL)
  198. *has_negative = ISC_FALSE;
  199. for (elt = cfg_list_first(caml);
  200. elt != NULL;
  201. elt = cfg_list_next(elt)) {
  202. const cfg_obj_t *ce = cfg_listelt_value(elt);
  203. /* negated element; just get the value. */
  204. if (cfg_obj_istuple(ce)) {
  205. ce = cfg_tuple_get(ce, "value");
  206. if (has_negative != NULL)
  207. *has_negative = ISC_TRUE;
  208. }
  209. if (cfg_obj_istype(ce, &cfg_type_keyref)) {
  210. n++;
  211. } else if (cfg_obj_islist(ce)) {
  212. isc_boolean_t negative;
  213. n += count_acl_elements(ce, cctx, &negative);
  214. if (negative)
  215. n++;
  216. } else if (cfg_obj_isstring(ce)) {
  217. const char *name = cfg_obj_asstring(ce);
  218. if (strcasecmp(name, "localhost") == 0 ||
  219. strcasecmp(name, "localnets") == 0) {
  220. n++;
  221. } else if (strcasecmp(name, "any") != 0 &&
  222. strcasecmp(name, "none") != 0) {
  223. result = get_acl_def(cctx, name, &cacl);
  224. if (result == ISC_R_SUCCESS)
  225. n += count_acl_elements(cacl, cctx,
  226. NULL) + 1;
  227. }
  228. }
  229. }
  230. return n;
  231. }
  232. isc_result_t
  233. cfg_acl_fromconfig(const cfg_obj_t *caml,
  234. const cfg_obj_t *cctx,
  235. isc_log_t *lctx,
  236. cfg_aclconfctx_t *ctx,
  237. isc_mem_t *mctx,
  238. unsigned int nest_level,
  239. dns_acl_t **target)
  240. {
  241. isc_result_t result;
  242. dns_acl_t *dacl = NULL, *inneracl = NULL;
  243. dns_aclelement_t *de;
  244. const cfg_listelt_t *elt;
  245. dns_iptable_t *iptab;
  246. int new_nest_level = 0;
  247. if (nest_level != 0)
  248. new_nest_level = nest_level - 1;
  249. REQUIRE(target != NULL);
  250. REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
  251. if (*target != NULL) {
  252. /*
  253. * If target already points to an ACL, then we're being
  254. * called recursively to configure a nested ACL. The
  255. * nested ACL's contents should just be absorbed into its
  256. * parent ACL.
  257. */
  258. dns_acl_attach(*target, &dacl);
  259. dns_acl_detach(target);
  260. } else {
  261. /*
  262. * Need to allocate a new ACL structure. Count the items
  263. * in the ACL definition that will require space in the
  264. * elements table. (Note that if nest_level is nonzero,
  265. * *everything* goes in the elements table.)
  266. */
  267. int nelem;
  268. if (nest_level == 0)
  269. nelem = count_acl_elements(caml, cctx, NULL);
  270. else
  271. nelem = cfg_list_length(caml, ISC_FALSE);
  272. result = dns_acl_create(mctx, nelem, &dacl);
  273. if (result != ISC_R_SUCCESS)
  274. return (result);
  275. }
  276. de = dacl->elements;
  277. for (elt = cfg_list_first(caml);
  278. elt != NULL;
  279. elt = cfg_list_next(elt)) {
  280. const cfg_obj_t *ce = cfg_listelt_value(elt);
  281. isc_boolean_t neg;
  282. if (cfg_obj_istuple(ce)) {
  283. /* This must be a negated element. */
  284. ce = cfg_tuple_get(ce, "value");
  285. neg = ISC_TRUE;
  286. dacl->has_negatives = ISC_TRUE;
  287. } else
  288. neg = ISC_FALSE;
  289. /*
  290. * If nest_level is nonzero, then every element is
  291. * to be stored as a separate, nested ACL rather than
  292. * merged into the main iptable.
  293. */
  294. iptab = dacl->iptable;
  295. if (nest_level != 0) {
  296. result = dns_acl_create(mctx,
  297. cfg_list_length(ce, ISC_FALSE),
  298. &de->nestedacl);
  299. if (result != ISC_R_SUCCESS)
  300. goto cleanup;
  301. iptab = de->nestedacl->iptable;
  302. }
  303. if (cfg_obj_isnetprefix(ce)) {
  304. /* Network prefix */
  305. isc_netaddr_t addr;
  306. unsigned int bitlen;
  307. cfg_obj_asnetprefix(ce, &addr, &bitlen);
  308. /*
  309. * If nesting ACLs (nest_level != 0), we negate
  310. * the nestedacl element, not the iptable entry.
  311. */
  312. result = dns_iptable_addprefix(iptab, &addr, bitlen,
  313. ISC_TF(nest_level != 0 || !neg));
  314. if (result != ISC_R_SUCCESS)
  315. goto cleanup;
  316. if (nest_level > 0) {
  317. de->type = dns_aclelementtype_nestedacl;
  318. de->negative = neg;
  319. } else
  320. continue;
  321. } else if (cfg_obj_islist(ce)) {
  322. /*
  323. * If we're nesting ACLs, put the nested
  324. * ACL onto the elements list; otherwise
  325. * merge it into *this* ACL. We nest ACLs
  326. * in two cases: 1) sortlist, 2) if the
  327. * nested ACL contains negated members.
  328. */
  329. if (inneracl != NULL)
  330. dns_acl_detach(&inneracl);
  331. result = cfg_acl_fromconfig(ce, cctx, lctx,
  332. ctx, mctx, new_nest_level,
  333. &inneracl);
  334. if (result != ISC_R_SUCCESS)
  335. goto cleanup;
  336. nested_acl:
  337. if (nest_level > 0 || inneracl->has_negatives) {
  338. de->type = dns_aclelementtype_nestedacl;
  339. de->negative = neg;
  340. if (de->nestedacl != NULL)
  341. dns_acl_detach(&de->nestedacl);
  342. dns_acl_attach(inneracl,
  343. &de->nestedacl);
  344. dns_acl_detach(&inneracl);
  345. /* Fall through. */
  346. } else {
  347. dns_acl_merge(dacl, inneracl,
  348. ISC_TF(!neg));
  349. de += inneracl->length; /* elements added */
  350. dns_acl_detach(&inneracl);
  351. continue;
  352. }
  353. } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
  354. /* Key name. */
  355. de->type = dns_aclelementtype_keyname;
  356. de->negative = neg;
  357. dns_name_init(&de->keyname, NULL);
  358. result = convert_keyname(ce, lctx, mctx,
  359. &de->keyname);
  360. if (result != ISC_R_SUCCESS)
  361. goto cleanup;
  362. } else if (cfg_obj_isstring(ce)) {
  363. /* ACL name. */
  364. const char *name = cfg_obj_asstring(ce);
  365. if (strcasecmp(name, "any") == 0) {
  366. /* Iptable entry with zero bit length. */
  367. result = dns_iptable_addprefix(iptab, NULL, 0,
  368. ISC_TF(nest_level != 0 || !neg));
  369. if (result != ISC_R_SUCCESS)
  370. goto cleanup;
  371. if (nest_level != 0) {
  372. de->type = dns_aclelementtype_nestedacl;
  373. de->negative = neg;
  374. } else
  375. continue;
  376. } else if (strcasecmp(name, "none") == 0) {
  377. /* none == !any */
  378. /*
  379. * We don't unconditional set
  380. * dacl->has_negatives and
  381. * de->negative to true so we can handle
  382. * "!none;".
  383. */
  384. result = dns_iptable_addprefix(iptab, NULL, 0,
  385. ISC_TF(nest_level != 0 || neg));
  386. if (result != ISC_R_SUCCESS)
  387. goto cleanup;
  388. if (!neg)
  389. dacl->has_negatives = !neg;
  390. if (nest_level != 0) {
  391. de->type = dns_aclelementtype_nestedacl;
  392. de->negative = !neg;
  393. } else
  394. continue;
  395. } else if (strcasecmp(name, "localhost") == 0) {
  396. de->type = dns_aclelementtype_localhost;
  397. de->negative = neg;
  398. } else if (strcasecmp(name, "localnets") == 0) {
  399. de->type = dns_aclelementtype_localnets;
  400. de->negative = neg;
  401. } else {
  402. if (inneracl != NULL)
  403. dns_acl_detach(&inneracl);
  404. result = convert_named_acl(ce, cctx, lctx, ctx,
  405. mctx, new_nest_level,
  406. &inneracl);
  407. if (result != ISC_R_SUCCESS)
  408. goto cleanup;
  409. goto nested_acl;
  410. }
  411. } else {
  412. cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
  413. "address match list contains "
  414. "unsupported element type");
  415. result = ISC_R_FAILURE;
  416. goto cleanup;
  417. }
  418. /*
  419. * This should only be reached for localhost, localnets
  420. * and keyname elements, and nested ACLs if nest_level is
  421. * nonzero (i.e., in sortlists).
  422. */
  423. if (de->nestedacl != NULL &&
  424. de->type != dns_aclelementtype_nestedacl)
  425. dns_acl_detach(&de->nestedacl);
  426. dacl->node_count++;
  427. de->node_num = dacl->node_count;
  428. dacl->length++;
  429. de++;
  430. INSIST(dacl->length <= dacl->alloc);
  431. }
  432. dns_acl_attach(dacl, target);
  433. result = ISC_R_SUCCESS;
  434. cleanup:
  435. if (inneracl != NULL)
  436. dns_acl_detach(&inneracl);
  437. dns_acl_detach(&dacl);
  438. return (result);
  439. }