PageRenderTime 92ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/bind9/bin/named/server.c

https://bitbucket.org/freebsd/freebsd-head/
C | 7648 lines | 5910 code | 887 blank | 851 comment | 1790 complexity | 0d51a127a90e8c0853cc6539ac9d6bce MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 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: server.c,v 1.599.8.19 2012/02/22 00:33:32 each Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <ctype.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <isc/app.h>
  27. #include <isc/base64.h>
  28. #include <isc/dir.h>
  29. #include <isc/entropy.h>
  30. #include <isc/file.h>
  31. #include <isc/hash.h>
  32. #include <isc/httpd.h>
  33. #include <isc/lex.h>
  34. #include <isc/parseint.h>
  35. #include <isc/portset.h>
  36. #include <isc/print.h>
  37. #include <isc/resource.h>
  38. #include <isc/sha2.h>
  39. #include <isc/socket.h>
  40. #include <isc/stat.h>
  41. #include <isc/stats.h>
  42. #include <isc/stdio.h>
  43. #include <isc/string.h>
  44. #include <isc/task.h>
  45. #include <isc/timer.h>
  46. #include <isc/util.h>
  47. #include <isc/xml.h>
  48. #include <isccfg/namedconf.h>
  49. #include <bind9/check.h>
  50. #include <dns/acache.h>
  51. #include <dns/adb.h>
  52. #include <dns/cache.h>
  53. #include <dns/db.h>
  54. #include <dns/dispatch.h>
  55. #include <dns/dlz.h>
  56. #include <dns/dns64.h>
  57. #include <dns/forward.h>
  58. #include <dns/journal.h>
  59. #include <dns/keytable.h>
  60. #include <dns/keyvalues.h>
  61. #include <dns/lib.h>
  62. #include <dns/master.h>
  63. #include <dns/masterdump.h>
  64. #include <dns/order.h>
  65. #include <dns/peer.h>
  66. #include <dns/portlist.h>
  67. #include <dns/rbt.h>
  68. #include <dns/rdataclass.h>
  69. #include <dns/rdataset.h>
  70. #include <dns/rdatastruct.h>
  71. #include <dns/resolver.h>
  72. #include <dns/rootns.h>
  73. #include <dns/secalg.h>
  74. #include <dns/stats.h>
  75. #include <dns/tkey.h>
  76. #include <dns/tsig.h>
  77. #include <dns/view.h>
  78. #include <dns/zone.h>
  79. #include <dns/zt.h>
  80. #include <dst/dst.h>
  81. #include <dst/result.h>
  82. #include <named/client.h>
  83. #include <named/config.h>
  84. #include <named/control.h>
  85. #include <named/interfacemgr.h>
  86. #include <named/log.h>
  87. #include <named/logconf.h>
  88. #include <named/lwresd.h>
  89. #include <named/main.h>
  90. #include <named/os.h>
  91. #include <named/server.h>
  92. #include <named/statschannel.h>
  93. #include <named/tkeyconf.h>
  94. #include <named/tsigconf.h>
  95. #include <named/zoneconf.h>
  96. #ifdef HAVE_LIBSCF
  97. #include <named/ns_smf_globals.h>
  98. #include <stdlib.h>
  99. #endif
  100. #ifndef PATH_MAX
  101. #define PATH_MAX 1024
  102. #endif
  103. /*%
  104. * Check an operation for failure. Assumes that the function
  105. * using it has a 'result' variable and a 'cleanup' label.
  106. */
  107. #define CHECK(op) \
  108. do { result = (op); \
  109. if (result != ISC_R_SUCCESS) goto cleanup; \
  110. } while (0)
  111. #define CHECKM(op, msg) \
  112. do { result = (op); \
  113. if (result != ISC_R_SUCCESS) { \
  114. isc_log_write(ns_g_lctx, \
  115. NS_LOGCATEGORY_GENERAL, \
  116. NS_LOGMODULE_SERVER, \
  117. ISC_LOG_ERROR, \
  118. "%s: %s", msg, \
  119. isc_result_totext(result)); \
  120. goto cleanup; \
  121. } \
  122. } while (0) \
  123. #define CHECKMF(op, msg, file) \
  124. do { result = (op); \
  125. if (result != ISC_R_SUCCESS) { \
  126. isc_log_write(ns_g_lctx, \
  127. NS_LOGCATEGORY_GENERAL, \
  128. NS_LOGMODULE_SERVER, \
  129. ISC_LOG_ERROR, \
  130. "%s '%s': %s", msg, file, \
  131. isc_result_totext(result)); \
  132. goto cleanup; \
  133. } \
  134. } while (0) \
  135. #define CHECKFATAL(op, msg) \
  136. do { result = (op); \
  137. if (result != ISC_R_SUCCESS) \
  138. fatal(msg, result); \
  139. } while (0) \
  140. /*%
  141. * Maximum ADB size for views that share a cache. Use this limit to suppress
  142. * the total of memory footprint, which should be the main reason for sharing
  143. * a cache. Only effective when a finite max-cache-size is specified.
  144. * This is currently defined to be 8MB.
  145. */
  146. #define MAX_ADB_SIZE_FOR_CACHESHARE 8388608
  147. struct ns_dispatch {
  148. isc_sockaddr_t addr;
  149. unsigned int dispatchgen;
  150. dns_dispatch_t *dispatch;
  151. ISC_LINK(struct ns_dispatch) link;
  152. };
  153. struct ns_cache {
  154. dns_cache_t *cache;
  155. dns_view_t *primaryview;
  156. isc_boolean_t needflush;
  157. isc_boolean_t adbsizeadjusted;
  158. ISC_LINK(ns_cache_t) link;
  159. };
  160. struct dumpcontext {
  161. isc_mem_t *mctx;
  162. isc_boolean_t dumpcache;
  163. isc_boolean_t dumpzones;
  164. FILE *fp;
  165. ISC_LIST(struct viewlistentry) viewlist;
  166. struct viewlistentry *view;
  167. struct zonelistentry *zone;
  168. dns_dumpctx_t *mdctx;
  169. dns_db_t *db;
  170. dns_db_t *cache;
  171. isc_task_t *task;
  172. dns_dbversion_t *version;
  173. };
  174. struct viewlistentry {
  175. dns_view_t *view;
  176. ISC_LINK(struct viewlistentry) link;
  177. ISC_LIST(struct zonelistentry) zonelist;
  178. };
  179. struct zonelistentry {
  180. dns_zone_t *zone;
  181. ISC_LINK(struct zonelistentry) link;
  182. };
  183. /*%
  184. * Configuration context to retain for each view that allows
  185. * new zones to be added at runtime.
  186. */
  187. struct cfg_context {
  188. isc_mem_t * mctx;
  189. cfg_parser_t * parser;
  190. cfg_obj_t * config;
  191. cfg_parser_t * nzparser;
  192. cfg_obj_t * nzconfig;
  193. cfg_aclconfctx_t * actx;
  194. };
  195. /*
  196. * These zones should not leak onto the Internet.
  197. */
  198. static const struct {
  199. const char *zone;
  200. isc_boolean_t rfc1918;
  201. } empty_zones[] = {
  202. /* RFC 1918 */
  203. { "10.IN-ADDR.ARPA", ISC_TRUE },
  204. { "16.172.IN-ADDR.ARPA", ISC_TRUE },
  205. { "17.172.IN-ADDR.ARPA", ISC_TRUE },
  206. { "18.172.IN-ADDR.ARPA", ISC_TRUE },
  207. { "19.172.IN-ADDR.ARPA", ISC_TRUE },
  208. { "20.172.IN-ADDR.ARPA", ISC_TRUE },
  209. { "21.172.IN-ADDR.ARPA", ISC_TRUE },
  210. { "22.172.IN-ADDR.ARPA", ISC_TRUE },
  211. { "23.172.IN-ADDR.ARPA", ISC_TRUE },
  212. { "24.172.IN-ADDR.ARPA", ISC_TRUE },
  213. { "25.172.IN-ADDR.ARPA", ISC_TRUE },
  214. { "26.172.IN-ADDR.ARPA", ISC_TRUE },
  215. { "27.172.IN-ADDR.ARPA", ISC_TRUE },
  216. { "28.172.IN-ADDR.ARPA", ISC_TRUE },
  217. { "29.172.IN-ADDR.ARPA", ISC_TRUE },
  218. { "30.172.IN-ADDR.ARPA", ISC_TRUE },
  219. { "31.172.IN-ADDR.ARPA", ISC_TRUE },
  220. { "168.192.IN-ADDR.ARPA", ISC_TRUE },
  221. /* RFC 5735 and RFC 5737 */
  222. { "0.IN-ADDR.ARPA", ISC_FALSE }, /* THIS NETWORK */
  223. { "127.IN-ADDR.ARPA", ISC_FALSE }, /* LOOPBACK */
  224. { "254.169.IN-ADDR.ARPA", ISC_FALSE }, /* LINK LOCAL */
  225. { "2.0.192.IN-ADDR.ARPA", ISC_FALSE }, /* TEST NET */
  226. { "100.51.198.IN-ADDR.ARPA", ISC_FALSE }, /* TEST NET 2 */
  227. { "113.0.203.IN-ADDR.ARPA", ISC_FALSE }, /* TEST NET 3 */
  228. { "255.255.255.255.IN-ADDR.ARPA", ISC_FALSE }, /* BROADCAST */
  229. /* Local IPv6 Unicast Addresses */
  230. { "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA", ISC_FALSE },
  231. { "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA", ISC_FALSE },
  232. /* LOCALLY ASSIGNED LOCAL ADDRESS SCOPE */
  233. { "D.F.IP6.ARPA", ISC_FALSE },
  234. { "8.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
  235. { "9.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
  236. { "A.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
  237. { "B.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
  238. /* Example Prefix, RFC 3849. */
  239. { "8.B.D.0.1.0.0.2.IP6.ARPA", ISC_FALSE },
  240. { NULL, ISC_FALSE }
  241. };
  242. ISC_PLATFORM_NORETURN_PRE static void
  243. fatal(const char *msg, isc_result_t result) ISC_PLATFORM_NORETURN_POST;
  244. static void
  245. ns_server_reload(isc_task_t *task, isc_event_t *event);
  246. static isc_result_t
  247. ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
  248. cfg_aclconfctx_t *actx,
  249. isc_mem_t *mctx, ns_listenelt_t **target);
  250. static isc_result_t
  251. ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
  252. cfg_aclconfctx_t *actx,
  253. isc_mem_t *mctx, ns_listenlist_t **target);
  254. static isc_result_t
  255. configure_forward(const cfg_obj_t *config, dns_view_t *view, dns_name_t *origin,
  256. const cfg_obj_t *forwarders, const cfg_obj_t *forwardtype);
  257. static isc_result_t
  258. configure_alternates(const cfg_obj_t *config, dns_view_t *view,
  259. const cfg_obj_t *alternates);
  260. static isc_result_t
  261. configure_zone(const cfg_obj_t *config, const cfg_obj_t *zconfig,
  262. const cfg_obj_t *vconfig, isc_mem_t *mctx, dns_view_t *view,
  263. cfg_aclconfctx_t *aclconf, isc_boolean_t added);
  264. static isc_result_t
  265. add_keydata_zone(dns_view_t *view, const char *directory, isc_mem_t *mctx);
  266. static void
  267. end_reserved_dispatches(ns_server_t *server, isc_boolean_t all);
  268. static void
  269. newzone_cfgctx_destroy(void **cfgp);
  270. /*%
  271. * Configure a single view ACL at '*aclp'. Get its configuration from
  272. * 'vconfig' (for per-view configuration) and maybe from 'config'
  273. */
  274. static isc_result_t
  275. configure_view_acl(const cfg_obj_t *vconfig, const cfg_obj_t *config,
  276. const char *aclname, const char *acltuplename,
  277. cfg_aclconfctx_t *actx, isc_mem_t *mctx, dns_acl_t **aclp)
  278. {
  279. isc_result_t result;
  280. const cfg_obj_t *maps[3];
  281. const cfg_obj_t *aclobj = NULL;
  282. int i = 0;
  283. if (*aclp != NULL)
  284. dns_acl_detach(aclp);
  285. if (vconfig != NULL)
  286. maps[i++] = cfg_tuple_get(vconfig, "options");
  287. if (config != NULL) {
  288. const cfg_obj_t *options = NULL;
  289. (void)cfg_map_get(config, "options", &options);
  290. if (options != NULL)
  291. maps[i++] = options;
  292. }
  293. maps[i] = NULL;
  294. (void)ns_config_get(maps, aclname, &aclobj);
  295. if (aclobj == NULL)
  296. /*
  297. * No value available. *aclp == NULL.
  298. */
  299. return (ISC_R_SUCCESS);
  300. if (acltuplename != NULL) {
  301. /*
  302. * If the ACL is given in an optional tuple, retrieve it.
  303. * The parser should have ensured that a valid object be
  304. * returned.
  305. */
  306. aclobj = cfg_tuple_get(aclobj, acltuplename);
  307. }
  308. result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
  309. actx, mctx, 0, aclp);
  310. return (result);
  311. }
  312. /*%
  313. * Configure a sortlist at '*aclp'. Essentially the same as
  314. * configure_view_acl() except it calls cfg_acl_fromconfig with a
  315. * nest_level value of 2.
  316. */
  317. static isc_result_t
  318. configure_view_sortlist(const cfg_obj_t *vconfig, const cfg_obj_t *config,
  319. cfg_aclconfctx_t *actx, isc_mem_t *mctx,
  320. dns_acl_t **aclp)
  321. {
  322. isc_result_t result;
  323. const cfg_obj_t *maps[3];
  324. const cfg_obj_t *aclobj = NULL;
  325. int i = 0;
  326. if (*aclp != NULL)
  327. dns_acl_detach(aclp);
  328. if (vconfig != NULL)
  329. maps[i++] = cfg_tuple_get(vconfig, "options");
  330. if (config != NULL) {
  331. const cfg_obj_t *options = NULL;
  332. (void)cfg_map_get(config, "options", &options);
  333. if (options != NULL)
  334. maps[i++] = options;
  335. }
  336. maps[i] = NULL;
  337. (void)ns_config_get(maps, "sortlist", &aclobj);
  338. if (aclobj == NULL)
  339. return (ISC_R_SUCCESS);
  340. /*
  341. * Use a nest level of 3 for the "top level" of the sortlist;
  342. * this means each entry in the top three levels will be stored
  343. * as lists of separate, nested ACLs, rather than merged together
  344. * into IP tables as is usually done with ACLs.
  345. */
  346. result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
  347. actx, mctx, 3, aclp);
  348. return (result);
  349. }
  350. static isc_result_t
  351. configure_view_nametable(const cfg_obj_t *vconfig, const cfg_obj_t *config,
  352. const char *confname, const char *conftuplename,
  353. isc_mem_t *mctx, dns_rbt_t **rbtp)
  354. {
  355. isc_result_t result;
  356. const cfg_obj_t *maps[3];
  357. const cfg_obj_t *obj = NULL;
  358. const cfg_listelt_t *element;
  359. int i = 0;
  360. dns_fixedname_t fixed;
  361. dns_name_t *name;
  362. isc_buffer_t b;
  363. const char *str;
  364. const cfg_obj_t *nameobj;
  365. if (*rbtp != NULL)
  366. dns_rbt_destroy(rbtp);
  367. if (vconfig != NULL)
  368. maps[i++] = cfg_tuple_get(vconfig, "options");
  369. if (config != NULL) {
  370. const cfg_obj_t *options = NULL;
  371. (void)cfg_map_get(config, "options", &options);
  372. if (options != NULL)
  373. maps[i++] = options;
  374. }
  375. maps[i] = NULL;
  376. (void)ns_config_get(maps, confname, &obj);
  377. if (obj == NULL)
  378. /*
  379. * No value available. *rbtp == NULL.
  380. */
  381. return (ISC_R_SUCCESS);
  382. if (conftuplename != NULL) {
  383. obj = cfg_tuple_get(obj, conftuplename);
  384. if (cfg_obj_isvoid(obj))
  385. return (ISC_R_SUCCESS);
  386. }
  387. result = dns_rbt_create(mctx, NULL, NULL, rbtp);
  388. if (result != ISC_R_SUCCESS)
  389. return (result);
  390. dns_fixedname_init(&fixed);
  391. name = dns_fixedname_name(&fixed);
  392. for (element = cfg_list_first(obj);
  393. element != NULL;
  394. element = cfg_list_next(element)) {
  395. nameobj = cfg_listelt_value(element);
  396. str = cfg_obj_asstring(nameobj);
  397. isc_buffer_init(&b, str, strlen(str));
  398. isc_buffer_add(&b, strlen(str));
  399. CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
  400. /*
  401. * We don't need the node data, but need to set dummy data to
  402. * avoid a partial match with an empty node. For example, if
  403. * we have foo.example.com and bar.example.com, we'd get a match
  404. * for baz.example.com, which is not the expected result.
  405. * We simply use (void *)1 as the dummy data.
  406. */
  407. result = dns_rbt_addname(*rbtp, name, (void *)1);
  408. if (result != ISC_R_SUCCESS) {
  409. cfg_obj_log(nameobj, ns_g_lctx, ISC_LOG_ERROR,
  410. "failed to add %s for %s: %s",
  411. str, confname, isc_result_totext(result));
  412. goto cleanup;
  413. }
  414. }
  415. return (result);
  416. cleanup:
  417. dns_rbt_destroy(rbtp);
  418. return (result);
  419. }
  420. static isc_result_t
  421. dstkey_fromconfig(const cfg_obj_t *vconfig, const cfg_obj_t *key,
  422. isc_boolean_t managed, dst_key_t **target, isc_mem_t *mctx)
  423. {
  424. dns_rdataclass_t viewclass;
  425. dns_rdata_dnskey_t keystruct;
  426. isc_uint32_t flags, proto, alg;
  427. const char *keystr, *keynamestr;
  428. unsigned char keydata[4096];
  429. isc_buffer_t keydatabuf;
  430. unsigned char rrdata[4096];
  431. isc_buffer_t rrdatabuf;
  432. isc_region_t r;
  433. dns_fixedname_t fkeyname;
  434. dns_name_t *keyname;
  435. isc_buffer_t namebuf;
  436. isc_result_t result;
  437. dst_key_t *dstkey = NULL;
  438. INSIST(target != NULL && *target == NULL);
  439. flags = cfg_obj_asuint32(cfg_tuple_get(key, "flags"));
  440. proto = cfg_obj_asuint32(cfg_tuple_get(key, "protocol"));
  441. alg = cfg_obj_asuint32(cfg_tuple_get(key, "algorithm"));
  442. keyname = dns_fixedname_name(&fkeyname);
  443. keynamestr = cfg_obj_asstring(cfg_tuple_get(key, "name"));
  444. if (managed) {
  445. const char *initmethod;
  446. initmethod = cfg_obj_asstring(cfg_tuple_get(key, "init"));
  447. if (strcasecmp(initmethod, "initial-key") != 0) {
  448. cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
  449. "managed key '%s': "
  450. "invalid initialization method '%s'",
  451. keynamestr, initmethod);
  452. result = ISC_R_FAILURE;
  453. goto cleanup;
  454. }
  455. }
  456. if (vconfig == NULL)
  457. viewclass = dns_rdataclass_in;
  458. else {
  459. const cfg_obj_t *classobj = cfg_tuple_get(vconfig, "class");
  460. CHECK(ns_config_getclass(classobj, dns_rdataclass_in,
  461. &viewclass));
  462. }
  463. keystruct.common.rdclass = viewclass;
  464. keystruct.common.rdtype = dns_rdatatype_dnskey;
  465. /*
  466. * The key data in keystruct is not dynamically allocated.
  467. */
  468. keystruct.mctx = NULL;
  469. ISC_LINK_INIT(&keystruct.common, link);
  470. if (flags > 0xffff)
  471. CHECKM(ISC_R_RANGE, "key flags");
  472. if (proto > 0xff)
  473. CHECKM(ISC_R_RANGE, "key protocol");
  474. if (alg > 0xff)
  475. CHECKM(ISC_R_RANGE, "key algorithm");
  476. keystruct.flags = (isc_uint16_t)flags;
  477. keystruct.protocol = (isc_uint8_t)proto;
  478. keystruct.algorithm = (isc_uint8_t)alg;
  479. isc_buffer_init(&keydatabuf, keydata, sizeof(keydata));
  480. isc_buffer_init(&rrdatabuf, rrdata, sizeof(rrdata));
  481. keystr = cfg_obj_asstring(cfg_tuple_get(key, "key"));
  482. CHECK(isc_base64_decodestring(keystr, &keydatabuf));
  483. isc_buffer_usedregion(&keydatabuf, &r);
  484. keystruct.datalen = r.length;
  485. keystruct.data = r.base;
  486. if ((keystruct.algorithm == DST_ALG_RSASHA1 ||
  487. keystruct.algorithm == DST_ALG_RSAMD5) &&
  488. r.length > 1 && r.base[0] == 1 && r.base[1] == 3)
  489. cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
  490. "%s key '%s' has a weak exponent",
  491. managed ? "managed" : "trusted",
  492. keynamestr);
  493. CHECK(dns_rdata_fromstruct(NULL,
  494. keystruct.common.rdclass,
  495. keystruct.common.rdtype,
  496. &keystruct, &rrdatabuf));
  497. dns_fixedname_init(&fkeyname);
  498. isc_buffer_init(&namebuf, keynamestr, strlen(keynamestr));
  499. isc_buffer_add(&namebuf, strlen(keynamestr));
  500. CHECK(dns_name_fromtext(keyname, &namebuf, dns_rootname, 0, NULL));
  501. CHECK(dst_key_fromdns(keyname, viewclass, &rrdatabuf,
  502. mctx, &dstkey));
  503. *target = dstkey;
  504. return (ISC_R_SUCCESS);
  505. cleanup:
  506. if (result == DST_R_NOCRYPTO) {
  507. cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
  508. "ignoring %s key for '%s': no crypto support",
  509. managed ? "managed" : "trusted",
  510. keynamestr);
  511. } else if (result == DST_R_UNSUPPORTEDALG) {
  512. cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
  513. "skipping %s key for '%s': %s",
  514. managed ? "managed" : "trusted",
  515. keynamestr, isc_result_totext(result));
  516. } else {
  517. cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
  518. "configuring %s key for '%s': %s",
  519. managed ? "managed" : "trusted",
  520. keynamestr, isc_result_totext(result));
  521. result = ISC_R_FAILURE;
  522. }
  523. if (dstkey != NULL)
  524. dst_key_free(&dstkey);
  525. return (result);
  526. }
  527. static isc_result_t
  528. load_view_keys(const cfg_obj_t *keys, const cfg_obj_t *vconfig,
  529. dns_view_t *view, isc_boolean_t managed,
  530. dns_name_t *keyname, isc_mem_t *mctx)
  531. {
  532. const cfg_listelt_t *elt, *elt2;
  533. const cfg_obj_t *key, *keylist;
  534. dst_key_t *dstkey = NULL;
  535. isc_result_t result;
  536. dns_keytable_t *secroots = NULL;
  537. CHECK(dns_view_getsecroots(view, &secroots));
  538. for (elt = cfg_list_first(keys);
  539. elt != NULL;
  540. elt = cfg_list_next(elt)) {
  541. keylist = cfg_listelt_value(elt);
  542. for (elt2 = cfg_list_first(keylist);
  543. elt2 != NULL;
  544. elt2 = cfg_list_next(elt2)) {
  545. key = cfg_listelt_value(elt2);
  546. result = dstkey_fromconfig(vconfig, key, managed,
  547. &dstkey, mctx);
  548. if (result == DST_R_UNSUPPORTEDALG) {
  549. result = ISC_R_SUCCESS;
  550. continue;
  551. }
  552. if (result != ISC_R_SUCCESS)
  553. goto cleanup;
  554. /*
  555. * If keyname was specified, we only add that key.
  556. */
  557. if (keyname != NULL &&
  558. !dns_name_equal(keyname, dst_key_name(dstkey)))
  559. {
  560. dst_key_free(&dstkey);
  561. continue;
  562. }
  563. CHECK(dns_keytable_add(secroots, managed, &dstkey));
  564. }
  565. }
  566. cleanup:
  567. if (dstkey != NULL)
  568. dst_key_free(&dstkey);
  569. if (secroots != NULL)
  570. dns_keytable_detach(&secroots);
  571. if (result == DST_R_NOCRYPTO)
  572. result = ISC_R_SUCCESS;
  573. return (result);
  574. }
  575. /*%
  576. * Configure DNSSEC keys for a view.
  577. *
  578. * The per-view configuration values and the server-global defaults are read
  579. * from 'vconfig' and 'config'.
  580. */
  581. static isc_result_t
  582. configure_view_dnsseckeys(dns_view_t *view, const cfg_obj_t *vconfig,
  583. const cfg_obj_t *config, const cfg_obj_t *bindkeys,
  584. isc_boolean_t auto_dlv, isc_boolean_t auto_root,
  585. isc_mem_t *mctx)
  586. {
  587. isc_result_t result = ISC_R_SUCCESS;
  588. const cfg_obj_t *view_keys = NULL;
  589. const cfg_obj_t *global_keys = NULL;
  590. const cfg_obj_t *view_managed_keys = NULL;
  591. const cfg_obj_t *global_managed_keys = NULL;
  592. const cfg_obj_t *maps[4];
  593. const cfg_obj_t *voptions = NULL;
  594. const cfg_obj_t *options = NULL;
  595. const cfg_obj_t *obj = NULL;
  596. const char *directory;
  597. int i = 0;
  598. /* We don't need trust anchors for the _bind view */
  599. if (strcmp(view->name, "_bind") == 0 &&
  600. view->rdclass == dns_rdataclass_chaos) {
  601. return (ISC_R_SUCCESS);
  602. }
  603. if (vconfig != NULL) {
  604. voptions = cfg_tuple_get(vconfig, "options");
  605. if (voptions != NULL) {
  606. (void) cfg_map_get(voptions, "trusted-keys",
  607. &view_keys);
  608. (void) cfg_map_get(voptions, "managed-keys",
  609. &view_managed_keys);
  610. maps[i++] = voptions;
  611. }
  612. }
  613. if (config != NULL) {
  614. (void)cfg_map_get(config, "trusted-keys", &global_keys);
  615. (void)cfg_map_get(config, "managed-keys", &global_managed_keys);
  616. (void)cfg_map_get(config, "options", &options);
  617. if (options != NULL) {
  618. maps[i++] = options;
  619. }
  620. }
  621. maps[i++] = ns_g_defaults;
  622. maps[i] = NULL;
  623. result = dns_view_initsecroots(view, mctx);
  624. if (result != ISC_R_SUCCESS) {
  625. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  626. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  627. "couldn't create keytable");
  628. return (ISC_R_UNEXPECTED);
  629. }
  630. if (auto_dlv && view->rdclass == dns_rdataclass_in) {
  631. const cfg_obj_t *builtin_keys = NULL;
  632. const cfg_obj_t *builtin_managed_keys = NULL;
  633. isc_log_write(ns_g_lctx, DNS_LOGCATEGORY_SECURITY,
  634. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  635. "using built-in DLV key for view %s",
  636. view->name);
  637. /*
  638. * If bind.keys exists, it overrides the managed-keys
  639. * clause hard-coded in ns_g_config.
  640. */
  641. if (bindkeys != NULL) {
  642. (void)cfg_map_get(bindkeys, "trusted-keys",
  643. &builtin_keys);
  644. (void)cfg_map_get(bindkeys, "managed-keys",
  645. &builtin_managed_keys);
  646. } else {
  647. (void)cfg_map_get(ns_g_config, "trusted-keys",
  648. &builtin_keys);
  649. (void)cfg_map_get(ns_g_config, "managed-keys",
  650. &builtin_managed_keys);
  651. }
  652. if (builtin_keys != NULL)
  653. CHECK(load_view_keys(builtin_keys, vconfig, view,
  654. ISC_FALSE, view->dlv, mctx));
  655. if (builtin_managed_keys != NULL)
  656. CHECK(load_view_keys(builtin_managed_keys, vconfig,
  657. view, ISC_TRUE, view->dlv, mctx));
  658. }
  659. if (auto_root && view->rdclass == dns_rdataclass_in) {
  660. const cfg_obj_t *builtin_keys = NULL;
  661. const cfg_obj_t *builtin_managed_keys = NULL;
  662. isc_log_write(ns_g_lctx, DNS_LOGCATEGORY_SECURITY,
  663. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  664. "using built-in root key for view %s",
  665. view->name);
  666. /*
  667. * If bind.keys exists, it overrides the managed-keys
  668. * clause hard-coded in ns_g_config.
  669. */
  670. if (bindkeys != NULL) {
  671. (void)cfg_map_get(bindkeys, "trusted-keys",
  672. &builtin_keys);
  673. (void)cfg_map_get(bindkeys, "managed-keys",
  674. &builtin_managed_keys);
  675. } else {
  676. (void)cfg_map_get(ns_g_config, "trusted-keys",
  677. &builtin_keys);
  678. (void)cfg_map_get(ns_g_config, "managed-keys",
  679. &builtin_managed_keys);
  680. }
  681. if (builtin_keys != NULL)
  682. CHECK(load_view_keys(builtin_keys, vconfig, view,
  683. ISC_FALSE, dns_rootname, mctx));
  684. if (builtin_managed_keys != NULL)
  685. CHECK(load_view_keys(builtin_managed_keys, vconfig,
  686. view, ISC_TRUE, dns_rootname,
  687. mctx));
  688. }
  689. CHECK(load_view_keys(view_keys, vconfig, view, ISC_FALSE,
  690. NULL, mctx));
  691. CHECK(load_view_keys(view_managed_keys, vconfig, view, ISC_TRUE,
  692. NULL, mctx));
  693. if (view->rdclass == dns_rdataclass_in) {
  694. CHECK(load_view_keys(global_keys, vconfig, view, ISC_FALSE,
  695. NULL, mctx));
  696. CHECK(load_view_keys(global_managed_keys, vconfig, view,
  697. ISC_TRUE, NULL, mctx));
  698. }
  699. /*
  700. * Add key zone for managed-keys.
  701. */
  702. obj = NULL;
  703. (void)ns_config_get(maps, "managed-keys-directory", &obj);
  704. directory = obj != NULL ? cfg_obj_asstring(obj) : NULL;
  705. CHECK(add_keydata_zone(view, directory, ns_g_mctx));
  706. cleanup:
  707. return (result);
  708. }
  709. static isc_result_t
  710. mustbesecure(const cfg_obj_t *mbs, dns_resolver_t *resolver) {
  711. const cfg_listelt_t *element;
  712. const cfg_obj_t *obj;
  713. const char *str;
  714. dns_fixedname_t fixed;
  715. dns_name_t *name;
  716. isc_boolean_t value;
  717. isc_result_t result;
  718. isc_buffer_t b;
  719. dns_fixedname_init(&fixed);
  720. name = dns_fixedname_name(&fixed);
  721. for (element = cfg_list_first(mbs);
  722. element != NULL;
  723. element = cfg_list_next(element))
  724. {
  725. obj = cfg_listelt_value(element);
  726. str = cfg_obj_asstring(cfg_tuple_get(obj, "name"));
  727. isc_buffer_init(&b, str, strlen(str));
  728. isc_buffer_add(&b, strlen(str));
  729. CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
  730. value = cfg_obj_asboolean(cfg_tuple_get(obj, "value"));
  731. CHECK(dns_resolver_setmustbesecure(resolver, name, value));
  732. }
  733. result = ISC_R_SUCCESS;
  734. cleanup:
  735. return (result);
  736. }
  737. /*%
  738. * Get a dispatch appropriate for the resolver of a given view.
  739. */
  740. static isc_result_t
  741. get_view_querysource_dispatch(const cfg_obj_t **maps,
  742. int af, dns_dispatch_t **dispatchp,
  743. isc_boolean_t is_firstview)
  744. {
  745. isc_result_t result = ISC_R_FAILURE;
  746. dns_dispatch_t *disp;
  747. isc_sockaddr_t sa;
  748. unsigned int attrs, attrmask;
  749. const cfg_obj_t *obj = NULL;
  750. unsigned int maxdispatchbuffers;
  751. switch (af) {
  752. case AF_INET:
  753. result = ns_config_get(maps, "query-source", &obj);
  754. INSIST(result == ISC_R_SUCCESS);
  755. break;
  756. case AF_INET6:
  757. result = ns_config_get(maps, "query-source-v6", &obj);
  758. INSIST(result == ISC_R_SUCCESS);
  759. break;
  760. default:
  761. INSIST(0);
  762. }
  763. sa = *(cfg_obj_assockaddr(obj));
  764. INSIST(isc_sockaddr_pf(&sa) == af);
  765. /*
  766. * If we don't support this address family, we're done!
  767. */
  768. switch (af) {
  769. case AF_INET:
  770. result = isc_net_probeipv4();
  771. break;
  772. case AF_INET6:
  773. result = isc_net_probeipv6();
  774. break;
  775. default:
  776. INSIST(0);
  777. }
  778. if (result != ISC_R_SUCCESS)
  779. return (ISC_R_SUCCESS);
  780. /*
  781. * Try to find a dispatcher that we can share.
  782. */
  783. attrs = 0;
  784. attrs |= DNS_DISPATCHATTR_UDP;
  785. switch (af) {
  786. case AF_INET:
  787. attrs |= DNS_DISPATCHATTR_IPV4;
  788. break;
  789. case AF_INET6:
  790. attrs |= DNS_DISPATCHATTR_IPV6;
  791. break;
  792. }
  793. if (isc_sockaddr_getport(&sa) == 0) {
  794. attrs |= DNS_DISPATCHATTR_EXCLUSIVE;
  795. maxdispatchbuffers = 4096;
  796. } else {
  797. INSIST(obj != NULL);
  798. if (is_firstview) {
  799. cfg_obj_log(obj, ns_g_lctx, ISC_LOG_INFO,
  800. "using specific query-source port "
  801. "suppresses port randomization and can be "
  802. "insecure.");
  803. }
  804. maxdispatchbuffers = 1000;
  805. }
  806. attrmask = 0;
  807. attrmask |= DNS_DISPATCHATTR_UDP;
  808. attrmask |= DNS_DISPATCHATTR_TCP;
  809. attrmask |= DNS_DISPATCHATTR_IPV4;
  810. attrmask |= DNS_DISPATCHATTR_IPV6;
  811. disp = NULL;
  812. result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
  813. ns_g_taskmgr, &sa, 4096,
  814. maxdispatchbuffers, 32768, 16411, 16433,
  815. attrs, attrmask, &disp);
  816. if (result != ISC_R_SUCCESS) {
  817. isc_sockaddr_t any;
  818. char buf[ISC_SOCKADDR_FORMATSIZE];
  819. switch (af) {
  820. case AF_INET:
  821. isc_sockaddr_any(&any);
  822. break;
  823. case AF_INET6:
  824. isc_sockaddr_any6(&any);
  825. break;
  826. }
  827. if (isc_sockaddr_equal(&sa, &any))
  828. return (ISC_R_SUCCESS);
  829. isc_sockaddr_format(&sa, buf, sizeof(buf));
  830. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  831. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  832. "could not get query source dispatcher (%s)",
  833. buf);
  834. return (result);
  835. }
  836. *dispatchp = disp;
  837. return (ISC_R_SUCCESS);
  838. }
  839. static isc_result_t
  840. configure_order(dns_order_t *order, const cfg_obj_t *ent) {
  841. dns_rdataclass_t rdclass;
  842. dns_rdatatype_t rdtype;
  843. const cfg_obj_t *obj;
  844. dns_fixedname_t fixed;
  845. unsigned int mode = 0;
  846. const char *str;
  847. isc_buffer_t b;
  848. isc_result_t result;
  849. isc_boolean_t addroot;
  850. result = ns_config_getclass(cfg_tuple_get(ent, "class"),
  851. dns_rdataclass_any, &rdclass);
  852. if (result != ISC_R_SUCCESS)
  853. return (result);
  854. result = ns_config_gettype(cfg_tuple_get(ent, "type"),
  855. dns_rdatatype_any, &rdtype);
  856. if (result != ISC_R_SUCCESS)
  857. return (result);
  858. obj = cfg_tuple_get(ent, "name");
  859. if (cfg_obj_isstring(obj))
  860. str = cfg_obj_asstring(obj);
  861. else
  862. str = "*";
  863. addroot = ISC_TF(strcmp(str, "*") == 0);
  864. isc_buffer_init(&b, str, strlen(str));
  865. isc_buffer_add(&b, strlen(str));
  866. dns_fixedname_init(&fixed);
  867. result = dns_name_fromtext(dns_fixedname_name(&fixed), &b,
  868. dns_rootname, 0, NULL);
  869. if (result != ISC_R_SUCCESS)
  870. return (result);
  871. obj = cfg_tuple_get(ent, "ordering");
  872. INSIST(cfg_obj_isstring(obj));
  873. str = cfg_obj_asstring(obj);
  874. if (!strcasecmp(str, "fixed"))
  875. mode = DNS_RDATASETATTR_FIXEDORDER;
  876. else if (!strcasecmp(str, "random"))
  877. mode = DNS_RDATASETATTR_RANDOMIZE;
  878. else if (!strcasecmp(str, "cyclic"))
  879. mode = 0;
  880. else
  881. INSIST(0);
  882. /*
  883. * "*" should match everything including the root (BIND 8 compat).
  884. * As dns_name_matcheswildcard(".", "*.") returns FALSE add a
  885. * explicit entry for "." when the name is "*".
  886. */
  887. if (addroot) {
  888. result = dns_order_add(order, dns_rootname,
  889. rdtype, rdclass, mode);
  890. if (result != ISC_R_SUCCESS)
  891. return (result);
  892. }
  893. return (dns_order_add(order, dns_fixedname_name(&fixed),
  894. rdtype, rdclass, mode));
  895. }
  896. static isc_result_t
  897. configure_peer(const cfg_obj_t *cpeer, isc_mem_t *mctx, dns_peer_t **peerp) {
  898. isc_netaddr_t na;
  899. dns_peer_t *peer;
  900. const cfg_obj_t *obj;
  901. const char *str;
  902. isc_result_t result;
  903. unsigned int prefixlen;
  904. cfg_obj_asnetprefix(cfg_map_getname(cpeer), &na, &prefixlen);
  905. peer = NULL;
  906. result = dns_peer_newprefix(mctx, &na, prefixlen, &peer);
  907. if (result != ISC_R_SUCCESS)
  908. return (result);
  909. obj = NULL;
  910. (void)cfg_map_get(cpeer, "bogus", &obj);
  911. if (obj != NULL)
  912. CHECK(dns_peer_setbogus(peer, cfg_obj_asboolean(obj)));
  913. obj = NULL;
  914. (void)cfg_map_get(cpeer, "provide-ixfr", &obj);
  915. if (obj != NULL)
  916. CHECK(dns_peer_setprovideixfr(peer, cfg_obj_asboolean(obj)));
  917. obj = NULL;
  918. (void)cfg_map_get(cpeer, "request-ixfr", &obj);
  919. if (obj != NULL)
  920. CHECK(dns_peer_setrequestixfr(peer, cfg_obj_asboolean(obj)));
  921. obj = NULL;
  922. (void)cfg_map_get(cpeer, "request-nsid", &obj);
  923. if (obj != NULL)
  924. CHECK(dns_peer_setrequestnsid(peer, cfg_obj_asboolean(obj)));
  925. obj = NULL;
  926. (void)cfg_map_get(cpeer, "edns", &obj);
  927. if (obj != NULL)
  928. CHECK(dns_peer_setsupportedns(peer, cfg_obj_asboolean(obj)));
  929. obj = NULL;
  930. (void)cfg_map_get(cpeer, "edns-udp-size", &obj);
  931. if (obj != NULL) {
  932. isc_uint32_t udpsize = cfg_obj_asuint32(obj);
  933. if (udpsize < 512)
  934. udpsize = 512;
  935. if (udpsize > 4096)
  936. udpsize = 4096;
  937. CHECK(dns_peer_setudpsize(peer, (isc_uint16_t)udpsize));
  938. }
  939. obj = NULL;
  940. (void)cfg_map_get(cpeer, "max-udp-size", &obj);
  941. if (obj != NULL) {
  942. isc_uint32_t udpsize = cfg_obj_asuint32(obj);
  943. if (udpsize < 512)
  944. udpsize = 512;
  945. if (udpsize > 4096)
  946. udpsize = 4096;
  947. CHECK(dns_peer_setmaxudp(peer, (isc_uint16_t)udpsize));
  948. }
  949. obj = NULL;
  950. (void)cfg_map_get(cpeer, "transfers", &obj);
  951. if (obj != NULL)
  952. CHECK(dns_peer_settransfers(peer, cfg_obj_asuint32(obj)));
  953. obj = NULL;
  954. (void)cfg_map_get(cpeer, "transfer-format", &obj);
  955. if (obj != NULL) {
  956. str = cfg_obj_asstring(obj);
  957. if (strcasecmp(str, "many-answers") == 0)
  958. CHECK(dns_peer_settransferformat(peer,
  959. dns_many_answers));
  960. else if (strcasecmp(str, "one-answer") == 0)
  961. CHECK(dns_peer_settransferformat(peer,
  962. dns_one_answer));
  963. else
  964. INSIST(0);
  965. }
  966. obj = NULL;
  967. (void)cfg_map_get(cpeer, "keys", &obj);
  968. if (obj != NULL) {
  969. result = dns_peer_setkeybycharp(peer, cfg_obj_asstring(obj));
  970. if (result != ISC_R_SUCCESS)
  971. goto cleanup;
  972. }
  973. obj = NULL;
  974. if (na.family == AF_INET)
  975. (void)cfg_map_get(cpeer, "transfer-source", &obj);
  976. else
  977. (void)cfg_map_get(cpeer, "transfer-source-v6", &obj);
  978. if (obj != NULL) {
  979. result = dns_peer_settransfersource(peer,
  980. cfg_obj_assockaddr(obj));
  981. if (result != ISC_R_SUCCESS)
  982. goto cleanup;
  983. ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
  984. }
  985. obj = NULL;
  986. if (na.family == AF_INET)
  987. (void)cfg_map_get(cpeer, "notify-source", &obj);
  988. else
  989. (void)cfg_map_get(cpeer, "notify-source-v6", &obj);
  990. if (obj != NULL) {
  991. result = dns_peer_setnotifysource(peer,
  992. cfg_obj_assockaddr(obj));
  993. if (result != ISC_R_SUCCESS)
  994. goto cleanup;
  995. ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
  996. }
  997. obj = NULL;
  998. if (na.family == AF_INET)
  999. (void)cfg_map_get(cpeer, "query-source", &obj);
  1000. else
  1001. (void)cfg_map_get(cpeer, "query-source-v6", &obj);
  1002. if (obj != NULL) {
  1003. result = dns_peer_setquerysource(peer,
  1004. cfg_obj_assockaddr(obj));
  1005. if (result != ISC_R_SUCCESS)
  1006. goto cleanup;
  1007. ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
  1008. }
  1009. *peerp = peer;
  1010. return (ISC_R_SUCCESS);
  1011. cleanup:
  1012. dns_peer_detach(&peer);
  1013. return (result);
  1014. }
  1015. static isc_result_t
  1016. disable_algorithms(const cfg_obj_t *disabled, dns_resolver_t *resolver) {
  1017. isc_result_t result;
  1018. const cfg_obj_t *algorithms;
  1019. const cfg_listelt_t *element;
  1020. const char *str;
  1021. dns_fixedname_t fixed;
  1022. dns_name_t *name;
  1023. isc_buffer_t b;
  1024. dns_fixedname_init(&fixed);
  1025. name = dns_fixedname_name(&fixed);
  1026. str = cfg_obj_asstring(cfg_tuple_get(disabled, "name"));
  1027. isc_buffer_init(&b, str, strlen(str));
  1028. isc_buffer_add(&b, strlen(str));
  1029. CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
  1030. algorithms = cfg_tuple_get(disabled, "algorithms");
  1031. for (element = cfg_list_first(algorithms);
  1032. element != NULL;
  1033. element = cfg_list_next(element))
  1034. {
  1035. isc_textregion_t r;
  1036. dns_secalg_t alg;
  1037. DE_CONST(cfg_obj_asstring(cfg_listelt_value(element)), r.base);
  1038. r.length = strlen(r.base);
  1039. result = dns_secalg_fromtext(&alg, &r);
  1040. if (result != ISC_R_SUCCESS) {
  1041. isc_uint8_t ui;
  1042. result = isc_parse_uint8(&ui, r.base, 10);
  1043. alg = ui;
  1044. }
  1045. if (result != ISC_R_SUCCESS) {
  1046. cfg_obj_log(cfg_listelt_value(element),
  1047. ns_g_lctx, ISC_LOG_ERROR,
  1048. "invalid algorithm");
  1049. CHECK(result);
  1050. }
  1051. CHECK(dns_resolver_disable_algorithm(resolver, name, alg));
  1052. }
  1053. cleanup:
  1054. return (result);
  1055. }
  1056. static isc_boolean_t
  1057. on_disable_list(const cfg_obj_t *disablelist, dns_name_t *zonename) {
  1058. const cfg_listelt_t *element;
  1059. dns_fixedname_t fixed;
  1060. dns_name_t *name;
  1061. isc_result_t result;
  1062. const cfg_obj_t *value;
  1063. const char *str;
  1064. isc_buffer_t b;
  1065. dns_fixedname_init(&fixed);
  1066. name = dns_fixedname_name(&fixed);
  1067. for (element = cfg_list_first(disablelist);
  1068. element != NULL;
  1069. element = cfg_list_next(element))
  1070. {
  1071. value = cfg_listelt_value(element);
  1072. str = cfg_obj_asstring(value);
  1073. isc_buffer_init(&b, str, strlen(str));
  1074. isc_buffer_add(&b, strlen(str));
  1075. result = dns_name_fromtext(name, &b, dns_rootname,
  1076. 0, NULL);
  1077. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  1078. if (dns_name_equal(name, zonename))
  1079. return (ISC_TRUE);
  1080. }
  1081. return (ISC_FALSE);
  1082. }
  1083. static void
  1084. check_dbtype(dns_zone_t **zonep, unsigned int dbtypec, const char **dbargv,
  1085. isc_mem_t *mctx)
  1086. {
  1087. char **argv = NULL;
  1088. unsigned int i;
  1089. isc_result_t result;
  1090. result = dns_zone_getdbtype(*zonep, &argv, mctx);
  1091. if (result != ISC_R_SUCCESS) {
  1092. dns_zone_detach(zonep);
  1093. return;
  1094. }
  1095. /*
  1096. * Check that all the arguments match.
  1097. */
  1098. for (i = 0; i < dbtypec; i++)
  1099. if (argv[i] == NULL || strcmp(argv[i], dbargv[i]) != 0) {
  1100. dns_zone_detach(zonep);
  1101. break;
  1102. }
  1103. /*
  1104. * Check that there are not extra arguments.
  1105. */
  1106. if (i == dbtypec && argv[i] != NULL)
  1107. dns_zone_detach(zonep);
  1108. isc_mem_free(mctx, argv);
  1109. }
  1110. static isc_result_t
  1111. setquerystats(dns_zone_t *zone, isc_mem_t *mctx, isc_boolean_t on) {
  1112. isc_result_t result;
  1113. isc_stats_t *zoneqrystats;
  1114. zoneqrystats = NULL;
  1115. if (on) {
  1116. result = isc_stats_create(mctx, &zoneqrystats,
  1117. dns_nsstatscounter_max);
  1118. if (result != ISC_R_SUCCESS)
  1119. return (result);
  1120. }
  1121. dns_zone_setrequeststats(zone, zoneqrystats);
  1122. if (zoneqrystats != NULL)
  1123. isc_stats_detach(&zoneqrystats);
  1124. return (ISC_R_SUCCESS);
  1125. }
  1126. static ns_cache_t *
  1127. cachelist_find(ns_cachelist_t *cachelist, const char *cachename) {
  1128. ns_cache_t *nsc;
  1129. for (nsc = ISC_LIST_HEAD(*cachelist);
  1130. nsc != NULL;
  1131. nsc = ISC_LIST_NEXT(nsc, link)) {
  1132. if (strcmp(dns_cache_getname(nsc->cache), cachename) == 0)
  1133. return (nsc);
  1134. }
  1135. return (NULL);
  1136. }
  1137. static isc_boolean_t
  1138. cache_reusable(dns_view_t *originview, dns_view_t *view,
  1139. isc_boolean_t new_zero_no_soattl)
  1140. {
  1141. if (originview->checknames != view->checknames ||
  1142. dns_resolver_getzeronosoattl(originview->resolver) !=
  1143. new_zero_no_soattl ||
  1144. originview->acceptexpired != view->acceptexpired ||
  1145. originview->enablevalidation != view->enablevalidation ||
  1146. originview->maxcachettl != view->maxcachettl ||
  1147. originview->maxncachettl != view->maxncachettl) {
  1148. return (ISC_FALSE);
  1149. }
  1150. return (ISC_TRUE);
  1151. }
  1152. static isc_boolean_t
  1153. cache_sharable(dns_view_t *originview, dns_view_t *view,
  1154. isc_boolean_t new_zero_no_soattl,
  1155. unsigned int new_cleaning_interval,
  1156. isc_uint32_t new_max_cache_size)
  1157. {
  1158. /*
  1159. * If the cache cannot even reused for the same view, it cannot be
  1160. * shared with other views.
  1161. */
  1162. if (!cache_reusable(originview, view, new_zero_no_soattl))
  1163. return (ISC_FALSE);
  1164. /*
  1165. * Check other cache related parameters that must be consistent among
  1166. * the sharing views.
  1167. */
  1168. if (dns_cache_getcleaninginterval(originview->cache) !=
  1169. new_cleaning_interval ||
  1170. dns_cache_getcachesize(originview->cache) != new_max_cache_size) {
  1171. return (ISC_FALSE);
  1172. }
  1173. return (ISC_TRUE);
  1174. }
  1175. /*
  1176. * Callback from DLZ configure when the driver sets up a writeable zone
  1177. */
  1178. static isc_result_t
  1179. dlzconfigure_callback(dns_view_t *view, dns_zone_t *zone) {
  1180. dns_name_t *origin = dns_zone_getorigin(zone);
  1181. dns_rdataclass_t zclass = view->rdclass;
  1182. isc_result_t result;
  1183. result = dns_zonemgr_managezone(ns_g_server->zonemgr, zone);
  1184. if (result != ISC_R_SUCCESS)
  1185. return result;
  1186. dns_zone_setstats(zone, ns_g_server->zonestats);
  1187. return ns_zone_configure_writeable_dlz(view->dlzdatabase,
  1188. zone, zclass, origin);
  1189. }
  1190. static isc_result_t
  1191. dns64_reverse(dns_view_t *view, isc_mem_t *mctx, isc_netaddr_t *na,
  1192. unsigned int prefixlen, const char *server,
  1193. const char *contact)
  1194. {
  1195. char *cp;
  1196. char reverse[48+sizeof("ip6.arpa.")];
  1197. const char *dns64_dbtype[4] = { "_dns64", "dns64", ".", "." };
  1198. const char *sep = ": view ";
  1199. const char *viewname = view->name;
  1200. const unsigned char *s6;
  1201. dns_fixedname_t fixed;
  1202. dns_name_t *name;
  1203. dns_zone_t *zone = NULL;
  1204. int dns64_dbtypec = 4;
  1205. isc_buffer_t b;
  1206. isc_result_t result;
  1207. REQUIRE(prefixlen == 32 || prefixlen == 40 || prefixlen == 48 ||
  1208. prefixlen == 56 || prefixlen == 64 || prefixlen == 96);
  1209. if (!strcmp(viewname, "_default")) {
  1210. sep = "";
  1211. viewname = "";
  1212. }
  1213. /*
  1214. * Construct the reverse name of the zone.
  1215. */
  1216. cp = reverse;
  1217. s6 = na->type.in6.s6_addr;
  1218. while (prefixlen > 0) {
  1219. prefixlen -= 8;
  1220. sprintf(cp, "%x.%x.", s6[prefixlen/8] & 0xf,
  1221. (s6[prefixlen/8] >> 4) & 0xf);
  1222. cp += 4;
  1223. }
  1224. strcat(cp, "ip6.arpa.");
  1225. /*
  1226. * Create the actual zone.
  1227. */
  1228. if (server != NULL)
  1229. dns64_dbtype[2] = server;
  1230. if (contact != NULL)
  1231. dns64_dbtype[3] = contact;
  1232. dns_fixedname_init(&fixed);
  1233. name = dns_fixedname_name(&fixed);
  1234. isc_buffer_init(&b, reverse, strlen(reverse));
  1235. isc_buffer_add(&b, strlen(reverse));
  1236. CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
  1237. CHECK(dns_zone_create(&zone, mctx));
  1238. CHECK(dns_zone_setorigin(zone, name));
  1239. dns_zone_setview(zone, view);
  1240. CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
  1241. dns_zone_setclass(zone, view->rdclass);
  1242. dns_zone_settype(zone, dns_zone_master);
  1243. dns_zone_setstats(zone, ns_g_server->zonestats);
  1244. CHECK(dns_zone_setdbtype(zone, dns64_dbtypec, dns64_dbtype));
  1245. if (view->queryacl != NULL)
  1246. dns_zone_setqueryacl(zone, view->queryacl);
  1247. if (view->queryonacl != NULL)
  1248. dns_zone_setqueryonacl(zone, view->queryonacl);
  1249. dns_zone_setdialup(zone, dns_dialuptype_no);
  1250. dns_zone_setnotifytype(zone, dns_notifytype_no);
  1251. dns_zone_setoption(zone, DNS_ZONEOPT_NOCHECKNS, ISC_TRUE);
  1252. CHECK(setquerystats(zone, mctx, ISC_FALSE)); /* XXXMPA */
  1253. CHECK(dns_view_addzone(view, zone));
  1254. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  1255. ISC_LOG_INFO, "dns64 reverse zone%s%s: %s", sep,
  1256. viewname, reverse);
  1257. cleanup:
  1258. if (zone != NULL)
  1259. dns_zone_detach(&zone);
  1260. return (result);
  1261. }
  1262. static isc_result_t
  1263. configure_rpz(dns_view_t *view, const cfg_listelt_t *element) {
  1264. const cfg_obj_t *rpz_obj, *policy_obj;
  1265. const char *str;
  1266. dns_fixedname_t fixed;
  1267. dns_name_t *origin;
  1268. dns_rpz_zone_t *old, *new;
  1269. dns_zone_t *zone = NULL;
  1270. isc_result_t result;
  1271. unsigned int l1, l2;
  1272. new = isc_mem_get(view->mctx, sizeof(*new));
  1273. if (new == NULL) {
  1274. result = ISC_R_NOMEMORY;
  1275. goto cleanup;
  1276. }
  1277. memset(new, 0, sizeof(*new));
  1278. dns_name_init(&new->nsdname, NULL);
  1279. dns_name_init(&new->origin, NULL);
  1280. dns_name_init(&new->cname, NULL);
  1281. ISC_LIST_INITANDAPPEND(view->rpz_zones, new, link);
  1282. rpz_obj = cfg_listelt_value(element);
  1283. policy_obj = cfg_tuple_get(rpz_obj, "policy");
  1284. if (cfg_obj_isvoid(policy_obj)) {
  1285. new->policy = DNS_RPZ_POLICY_GIVEN;
  1286. } else {
  1287. str = cfg_obj_asstring(policy_obj);
  1288. new->policy = dns_rpz_str2policy(str);
  1289. INSIST(new->policy != DNS_RPZ_POLICY_ERROR);
  1290. }
  1291. dns_fixedname_init(&fixed);
  1292. origin = dns_fixedname_name(&fixed);
  1293. str = cfg_obj_asstring(cfg_tuple_get(rpz_obj, "name"));
  1294. result = dns_name_fromstring(origin, str, DNS_NAME_DOWNCASE, NULL);
  1295. if (result != ISC_R_SUCCESS) {
  1296. cfg_obj_log(rpz_obj, ns_g_lctx, DNS_RPZ_ERROR_LEVEL,
  1297. "invalid zone '%s'", str);
  1298. goto cleanup;
  1299. }
  1300. result = dns_name_fromstring2(&new->nsdname, DNS_RPZ_NSDNAME_ZONE,
  1301. origin, DNS_NAME_DOWNCASE, view->mctx);
  1302. if (result != ISC_R_SUCCESS) {
  1303. cfg_obj_log(rpz_obj, ns_g_lctx, DNS_RPZ_ERROR_LEVEL,
  1304. "invalid zone '%s'", str);
  1305. goto cleanup;
  1306. }
  1307. /*
  1308. * The origin is part of 'nsdname' so we don't need to keep it
  1309. * seperately.
  1310. */
  1311. l1 = dns_name_countlabels(&new->nsdname);
  1312. l2 = dns_name_countlabels(origin);
  1313. dns_name_getlabelsequence(&new->nsdname, l1 - l2, l2, &new->origin);
  1314. /*
  1315. * Are we configured to with the reponse policy zone?
  1316. */
  1317. result = dns_view_findzone(view, &new->origin, &zone);
  1318. if (result != ISC_R_SUCCESS) {
  1319. cfg_obj_log(rpz_obj, ns_g_lctx, DNS_RPZ_ERROR_LEVEL,
  1320. "unknown zone '%s'", str);
  1321. goto cleanup;
  1322. }
  1323. if (dns_zone_gettype(zone) != dns_zone_master &&
  1324. dns_zone_gettype(zone) != dns_zone_slave) {
  1325. cfg_obj_log(rpz_obj, ns_g_lctx, DNS_RPZ_ERROR_LEVEL,
  1326. "zone '%s' is neither master nor slave", str);
  1327. dns_zone_detach(&zone);
  1328. result = DNS_R_NOTMASTER;
  1329. goto cleanup;
  1330. }
  1331. dns_zone_detach(&zone);
  1332. for (old = ISC_LIST_HEAD(view->rpz_zones);
  1333. old != new;
  1334. old = ISC_LIST_NEXT(old, link)) {
  1335. ++new->num;
  1336. if (dns_name_equal(&old->origin, &new->origin)) {
  1337. cfg_obj_log(rpz_obj, ns_g_lctx, DNS_RPZ_ERROR_LEVEL,
  1338. "duplicate '%s'", str);
  1339. result = DNS_R_DUPLICATE;
  1340. goto cleanup;
  1341. }
  1342. }
  1343. if (new->policy == DNS_RPZ_POLICY_CNAME) {
  1344. str = cfg_obj_asstring(cfg_tuple_get(rpz_obj, "cname"));
  1345. result = dns_name_fromstring(&new->cname, str, 0, view->mctx);
  1346. if (result != ISC_R_SUCCESS) {
  1347. cfg_obj_log(rpz_obj, ns_g_lctx, DNS_RPZ_ERROR_LEVEL,
  1348. "invalid cname '%s'", str);
  1349. goto cleanup;
  1350. }
  1351. }
  1352. return (ISC_R_SUCCESS);
  1353. cleanup:
  1354. dns_rpz_view_destroy(view);
  1355. return (result);
  1356. }
  1357. /*
  1358. * Configure 'view' according to 'vconfig', taking defaults from 'config'
  1359. * where values are missing in 'vconfig'.
  1360. *
  1361. * When configuring the default view, 'vconfig' will be NULL and the
  1362. * global defaults in 'config' used exclusively.
  1363. */
  1364. static isc_result_t
  1365. configure_view(dns_view_t *view, cfg_obj_t *config, cfg_obj_t *vconfig,
  1366. ns_cachelist_t *cachelist, const cfg_obj_t *bindkeys,
  1367. isc_mem_t *mctx, cfg_aclconfctx_t *actx,
  1368. isc_boolean_t need_hints)
  1369. {
  1370. const cfg_obj_t *maps[4];
  1371. const cfg_obj_t *cfgmaps[3];
  1372. const cfg_obj_t *optionmaps[3];
  1373. const cfg_obj_t *options = NULL;
  1374. const cfg_obj_t *voptions = NULL;
  1375. const cfg_obj_t *forwardtype;
  1376. const cfg_obj_t *forwarders;
  1377. const cfg_obj_t *alternates;
  1378. const cfg_obj_t *zonelist;
  1379. const cfg_obj_t *dlz;
  1380. unsigned int dlzargc;
  1381. char **dlzargv;
  1382. const cfg_obj_t *disabled;
  1383. const cfg_obj_t *obj;
  1384. const cfg_listelt_t *element;
  1385. in_port_t port;
  1386. dns_cache_t *cache = NULL;
  1387. isc_result_t result;
  1388. isc_uint32_t max_adb_size;
  1389. unsigned int cleaning_interval;
  1390. isc_uint32_t max_cache_size;
  1391. isc_uint32_t max_acache_size;
  1392. isc_uint32_t lame_ttl;
  1393. dns_tsig_keyring_t *ring = NULL;
  1394. dns_view_t *pview = NULL; /* Production view */
  1395. isc_mem_t *cmctx = NULL, *hmctx = NULL;
  1396. dns_dispatch_t *dispatch4 = NULL;
  1397. dns_dispatch_t *dispatch6 = NULL;
  1398. isc_boolean_t reused_cache = ISC_FALSE;
  1399. isc_boolean_t shared_cache = ISC_FALSE;
  1400. int i = 0, j = 0, k = 0;
  1401. const char *str;
  1402. const char *cachename = NULL;
  1403. dns_order_t *order = NULL;
  1404. isc_uint32_t udpsize;
  1405. unsigned int resopts = 0;
  1406. dns_zone_t *zone = NULL;
  1407. isc_uint32_t max_clients_per_query;
  1408. const char *sep = ": view ";
  1409. const char *viewname = view->name;
  1410. const char *forview = " for view ";
  1411. isc_boolean_t rfc1918;
  1412. isc_boolean_t empty_zones_enable;
  1413. const cfg_obj_t *disablelist = NULL;
  1414. isc_stats_t *resstats = NULL;
  1415. dns_stats_t *resquerystats = NULL;
  1416. isc_boolean_t auto_dlv = ISC_FALSE;
  1417. isc_boolean_t auto_root = ISC_FALSE;
  1418. ns_cache_t *nsc;
  1419. isc_boolean_t zero_no_soattl;
  1420. dns_acl_t *clients = NULL, *mapped = NULL, *excluded = NULL;
  1421. unsigned int query_timeout;
  1422. struct cfg_context *nzctx;
  1423. REQUIRE(DNS_VIEW_VALID(view));
  1424. if (config != NULL)
  1425. (void)cfg_map_get(config, "options", &options);
  1426. /*
  1427. * maps: view options, options, defaults
  1428. * cfgmaps: view options, config
  1429. * optionmaps: view options, options
  1430. */
  1431. if (vconfig != NULL) {
  1432. voptions = cfg_tuple_get(vconfig, "options");
  1433. maps[i++] = voptions;
  1434. optionmaps[j++] = voptions;
  1435. cfgmaps[k++] = voptions;
  1436. }
  1437. if (options != NULL) {
  1438. maps[i++] = options;
  1439. optionmaps[j++] = options;
  1440. }
  1441. maps[i++] = ns_g_defaults;
  1442. maps[i] = NULL;
  1443. optionmaps[j] = NULL;
  1444. if (config != NULL)
  1445. cfgmaps[k++] = config;
  1446. cfgmaps[k] = NULL;
  1447. if (!strcmp(viewname, "_default")) {
  1448. sep = "";
  1449. viewname = "";
  1450. forview = "";
  1451. POST(forview);
  1452. }
  1453. /*
  1454. * Set the view's port number for outgoing queries.
  1455. */
  1456. CHECKM(ns_config_getport(config, &port), "port");
  1457. dns_view_setdstport(view, port);
  1458. /*
  1459. * Create additional cache for this view and zones under the view
  1460. * if explicitly enabled.
  1461. * XXX950 default to on.
  1462. */
  1463. obj = NULL;
  1464. (void)ns_config_get(maps, "acache-enable", &obj);
  1465. if (obj != NULL && cfg_obj_asboolean(obj)) {
  1466. cmctx = NULL;
  1467. CHECK(isc_mem_create(0, 0, &cmctx));
  1468. CHECK(dns_acache_create(&view->acache, cmctx, ns_g_taskmgr,
  1469. ns_g_timermgr));
  1470. isc_mem_setname(cmctx, "acache", NULL);
  1471. isc_mem_detach(&cmctx);
  1472. }
  1473. if (view->acache != NULL) {
  1474. obj = NULL;
  1475. result = ns_config_get(maps, "acache-cleaning-interval", &obj);
  1476. INSIST(result == ISC_R_SUCCESS);
  1477. dns_acache_setcleaninginterval(view->acache,
  1478. cfg_obj_asuint32(obj) * 60);
  1479. obj = NULL;
  1480. result = ns_config_get(maps, "max-acache-size", &obj);
  1481. INSIST(result == ISC_R_SUCCESS);
  1482. if (cfg_obj_isstring(obj)) {
  1483. str = cfg_obj_asstring(obj);
  1484. INSIST(strcasecmp(str, "unlimited") == 0);
  1485. max_acache_size = ISC_UINT32_MAX;
  1486. } else {
  1487. isc_resourcevalue_t value;
  1488. value = cfg_obj_asuint64(obj);
  1489. if (value > ISC_UINT32_MAX) {
  1490. cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
  1491. "'max-acache-size "
  1492. "%" ISC_PRINT_QUADFORMAT
  1493. "d' is too large",
  1494. value);
  1495. result = ISC_R_RANGE;
  1496. goto cleanup;
  1497. }
  1498. max_acache_size = (isc_uint32_t)value;
  1499. }
  1500. dns_acache_setcachesize(view->acache, max_acache_size);
  1501. }
  1502. CHECK(configure_view_acl(vconfig, config, "allow-query", NULL, actx,
  1503. ns_g_mctx, &view->queryacl));
  1504. if (view->queryacl == NULL) {
  1505. CHECK(configure_view_acl(NULL, ns_g_config, "allow-query",
  1506. NULL, actx, ns_g_mctx,
  1507. &view->queryacl));
  1508. }
  1509. /*
  1510. * Configure the zones.
  1511. */
  1512. zonelist = NULL;
  1513. if (voptions != NULL)
  1514. (void)cfg_map_get(voptions, "zone", &zonelist);
  1515. else
  1516. (void)cfg_map_get(config, "zone", &zonelist);
  1517. /*
  1518. * Load zone configuration
  1519. */
  1520. for (element = cfg_list_first(zonelist);
  1521. element != NULL;
  1522. element = cfg_list_next(element))
  1523. {
  1524. const cfg_obj_t *zconfig = cfg_listelt_value(element);
  1525. CHECK(configure_zone(config, zconfig, vconfig, mctx, view,
  1526. actx, ISC_FALSE));
  1527. }
  1528. /*
  1529. * If we're allowing added zones, then load zone configuration
  1530. * from the newzone file for zones that were added during previous
  1531. * runs.
  1532. */
  1533. nzctx = view->new_zone_config;
  1534. if (nzctx != NULL && nzctx->nzconfig != NULL) {
  1535. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  1536. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  1537. "loading additional zones for view '%s'",
  1538. view->name);
  1539. zonelist = NULL;
  1540. cfg_map_get(nzctx->nzconfig, "zone", &zonelist);
  1541. for (element = cfg_list_first(zonelist);
  1542. element != NULL;
  1543. element = cfg_list_next(element))
  1544. {
  1545. const cfg_obj_t *zconfig = cfg_listelt_value(element);
  1546. CHECK(configure_zone(config, zconfig, vconfig,
  1547. mctx, view, actx,
  1548. ISC_TRUE));
  1549. }
  1550. }
  1551. /*
  1552. * Create Dynamically Loadable Zone driver.
  1553. */
  1554. dlz = NULL;
  1555. if (voptions != NULL)
  1556. (void)cfg_map_get(voptions, "dlz", &dlz);
  1557. else
  1558. (void)cfg_map_get(config, "dlz", &dlz);
  1559. obj = NULL;
  1560. if (dlz != NULL) {
  1561. (void)cfg_map_get(cfg_tuple_get(dlz, "options"),
  1562. "database", &obj);
  1563. if (obj != NULL) {
  1564. char *s = isc_mem_strdup(mctx, cfg_obj_asstring(obj));
  1565. if (s == NULL) {
  1566. result = ISC_R_NOMEMORY;
  1567. goto cleanup;
  1568. }
  1569. result = dns_dlzstrtoargv(mctx, s, &dlzargc, &dlzargv);
  1570. if (result != ISC_R_SUCCESS) {
  1571. isc_mem_free(mctx, s);
  1572. goto cleanup;
  1573. }
  1574. obj = cfg_tuple_get(dlz, "name");
  1575. result = dns_dlzcreate(mctx, cfg_obj_asstring(obj),
  1576. dlzargv[0], dlzargc, dlzargv,
  1577. &view->dlzdatabase);
  1578. isc_mem_free(mctx, s);
  1579. isc_mem_put(mctx, dlzargv, dlzargc * sizeof(*dlzargv));
  1580. if (result != ISC_R_SUCCESS)
  1581. goto cleanup;
  1582. /*
  1583. * If the dlz backend supports configuration,
  1584. * then call its configure method now.
  1585. */
  1586. result = dns_dlzconfigure(view, dlzconfigure_callback);
  1587. if (result != ISC_R_SUCCESS)
  1588. goto cleanup;
  1589. }
  1590. }
  1591. /*
  1592. * Obtain configuration parameters that affect the decision of whether
  1593. * we can reuse/share an existing cache.
  1594. */
  1595. obj = NULL;
  1596. result = ns_config_get(maps, "cleaning-interval", &obj);
  1597. INSIST(result == ISC_R_SUCCESS);
  1598. cleaning_interval = cfg_obj_asuint32(obj) * 60;
  1599. obj = NULL;
  1600. result = ns_config_get(maps, "max-cache-size", &obj);
  1601. INSIST(result == ISC_R_SUCCESS);
  1602. if (cfg_obj_isstring(obj)) {
  1603. str = cfg_obj_asstring(obj);
  1604. INSIST(strcasecmp(str, "unlimited") == 0);
  1605. max_cache_size = ISC_UINT32_MAX;
  1606. } else {
  1607. isc_resourcevalue_t value;
  1608. value = cfg_obj_asuint64(obj);
  1609. if (value > ISC_UINT32_MAX) {
  1610. cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
  1611. "'max-cache-size "
  1612. "%" ISC_PRINT_QUADFORMAT "d' is too large",
  1613. value);
  1614. result = ISC_R_RANGE;
  1615. goto cleanup;
  1616. }
  1617. max_cache_size = (isc_uint32_t)value;
  1618. }
  1619. /* Check-names. */
  1620. obj = NULL;
  1621. result = ns_checknames_get(maps, "response", &obj);
  1622. INSIST(result == ISC_R_SUCCESS);
  1623. str = cfg_obj_asstring(obj);
  1624. if (strcasecmp(str, "fail") == 0) {
  1625. resopts |= DNS_RESOLVER_CHECKNAMES |
  1626. DNS_RESOLVER_CHECKNAMESFAIL;
  1627. view->checknames = ISC_TRUE;
  1628. } else if (strcasecmp(str, "warn") == 0) {
  1629. resopts |= DNS_RESOLVER_CHECKNAMES;
  1630. view->checknames = ISC_FALSE;
  1631. } else if (strcasecmp(str, "ignore") == 0) {
  1632. view->checknames = ISC_FALSE;
  1633. } else
  1634. INSIST(0);
  1635. obj = NULL;
  1636. result = ns_config_get(maps, "zero-no-soa-ttl-cache", &obj);
  1637. INSIST(result == ISC_R_SUCCESS);
  1638. zero_no_soattl = cfg_obj_asboolean(obj);
  1639. obj = NULL;
  1640. result = ns_config_get(maps, "dns64", &obj);
  1641. if (result == ISC_R_SUCCESS && strcmp(view->name, "_bind") &&
  1642. strcmp(view->name, "_meta")) {
  1643. const cfg_listelt_t *element;
  1644. isc_netaddr_t na, suffix, *sp;
  1645. unsigned int prefixlen;
  1646. const char *server, *contact;
  1647. const cfg_obj_t *myobj;
  1648. myobj = NULL;
  1649. result = ns_config_get(maps, "dns64-server", &myobj);
  1650. if (result == ISC_R_SUCCESS)
  1651. server = cfg_obj_asstring(myobj);
  1652. else
  1653. server = NULL;
  1654. myobj = NULL;
  1655. result = ns_config_get(maps, "dns64-contact", &myobj);
  1656. if (result == ISC_R_SUCCESS)
  1657. contact = cfg_obj_asstring(myobj);
  1658. else
  1659. contact = NULL;
  1660. for (element = cfg_list_first(obj);
  1661. element != NULL;
  1662. element = cfg_list_next(element))
  1663. {
  1664. const cfg_obj_t *map = cfg_listelt_value(element);
  1665. dns_dns64_t *dns64 = NULL;
  1666. unsigned int dns64options = 0;
  1667. cfg_obj_asnetprefix(cfg_map_getname(map), &na,
  1668. &prefixlen);
  1669. obj = NULL;
  1670. (void)cfg_map_get(map, "suffix", &obj);
  1671. if (obj != NULL) {
  1672. sp = &suffix;
  1673. isc_netaddr_fromsockaddr(sp,
  1674. cfg_obj_assockaddr(obj));
  1675. } else
  1676. sp = NULL;
  1677. clients = mapped = excluded = NULL;
  1678. obj = NULL;
  1679. (void)cfg_map_get(map, "clients", &obj);
  1680. if (obj != NULL) {
  1681. result = cfg_acl_fromconfig(obj, config,
  1682. ns_g_lctx, actx,
  1683. mctx, 0, &clients);
  1684. if (result != ISC_R_SUCCESS)
  1685. goto cleanup;
  1686. }
  1687. obj = NULL;
  1688. (void)cfg_map_get(map, "mapped", &obj);
  1689. if (obj != NULL) {
  1690. result = cfg_acl_fromconfig(obj, config,
  1691. ns_g_lctx, actx,
  1692. mctx, 0, &mapped);
  1693. if (result != ISC_R_SUCCESS)
  1694. goto cleanup;
  1695. }
  1696. obj = NULL;
  1697. (void)cfg_map_get(map, "exclude", &obj);
  1698. if (obj != NULL) {
  1699. result = cfg_acl_fromconfig(obj, config,
  1700. ns_g_lctx, actx,
  1701. mctx, 0, &excluded);
  1702. if (result != ISC_R_SUCCESS)
  1703. goto cleanup;
  1704. }
  1705. obj = NULL;
  1706. (void)cfg_map_get(map, "recursive-only", &obj);
  1707. if (obj != NULL && cfg_obj_asboolean(obj))
  1708. dns64options |= DNS_DNS64_RECURSIVE_ONLY;
  1709. obj = NULL;
  1710. (void)cfg_map_get(map, "break-dnssec", &obj);
  1711. if (obj != NULL && cfg_obj_asboolean(obj))
  1712. dns64options |= DNS_DNS64_BREAK_DNSSEC;
  1713. result = dns_dns64_create(mctx, &na, prefixlen, sp,
  1714. clients, mapped, excluded,
  1715. dns64options, &dns64);
  1716. if (result != ISC_R_SUCCESS)
  1717. goto cleanup;
  1718. dns_dns64_append(&view->dns64, dns64);
  1719. view->dns64cnt++;
  1720. result = dns64_reverse(view, mctx, &na, prefixlen,
  1721. server, contact);
  1722. if (result != ISC_R_SUCCESS)
  1723. goto cleanup;
  1724. if (clients != NULL)
  1725. dns_acl_detach(&clients);
  1726. if (mapped != NULL)
  1727. dns_acl_detach(&mapped);
  1728. if (excluded != NULL)
  1729. dns_acl_detach(&excluded);
  1730. }
  1731. }
  1732. obj = NULL;
  1733. result = ns_config_get(maps, "dnssec-accept-expired", &obj);
  1734. INSIST(result == ISC_R_SUCCESS);
  1735. view->acceptexpired = cfg_obj_asboolean(obj);
  1736. obj = NULL;
  1737. result = ns_config_get(maps, "dnssec-validation", &obj);
  1738. INSIST(result == ISC_R_SUCCESS);
  1739. if (cfg_obj_isboolean(obj)) {
  1740. view->enablevalidation = cfg_obj_asboolean(obj);
  1741. } else {
  1742. /* If dnssec-validation is not boolean, it must be "auto" */
  1743. view->enablevalidation = ISC_TRUE;
  1744. auto_root = ISC_TRUE;
  1745. }
  1746. obj = NULL;
  1747. result = ns_config_get(maps, "max-cache-ttl", &obj);
  1748. INSIST(result == ISC_R_SUCCESS);
  1749. view->maxcachettl = cfg_obj_asuint32(obj);
  1750. obj = NULL;
  1751. result = ns_config_get(maps, "max-ncache-ttl", &obj);
  1752. INSIST(result == ISC_R_SUCCESS);
  1753. view->maxncachettl = cfg_obj_asuint32(obj);
  1754. if (view->maxncachettl > 7 * 24 * 3600)
  1755. view->maxncachettl = 7 * 24 * 3600;
  1756. /*
  1757. * Configure the view's cache.
  1758. *
  1759. * First, check to see if there are any attach-cache options. If yes,
  1760. * attempt to lookup an existing cache at attach it to the view. If
  1761. * there is not one, then try to reuse an existing cache if possible;
  1762. * otherwise create a new cache.
  1763. *
  1764. * Note that the ADB is not preserved or shared in either case.
  1765. *
  1766. * When a matching view is found, the associated statistics are also
  1767. * retrieved and reused.
  1768. *
  1769. * XXX Determining when it is safe to reuse or share a cache is tricky.
  1770. * When the view's configuration changes, the cached data may become
  1771. * invalid because it reflects our old view of the world. We check
  1772. * some of the configuration parameters that could invalidate the cache
  1773. * or otherwise make it unsharable, but there are other configuration
  1774. * options that should be checked. For example, if a view uses a
  1775. * forwarder, changes in the forwarder configuration may invalidate
  1776. * the cache. At the moment, it's the administrator's responsibility to
  1777. * ensure these configuration options don't invalidate reusing/sharing.
  1778. */
  1779. obj = NULL;
  1780. result = ns_config_get(maps, "attach-cache", &obj);
  1781. if (result == ISC_R_SUCCESS)
  1782. cachename = cfg_obj_asstring(obj);
  1783. else
  1784. cachename = view->name;
  1785. cache = NULL;
  1786. nsc = cachelist_find(cachelist, cachename);
  1787. if (nsc != NULL) {
  1788. if (!cache_sharable(nsc->primaryview, view, zero_no_soattl,
  1789. cleaning_interval, max_cache_size)) {
  1790. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  1791. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  1792. "views %s and %s can't share the cache "
  1793. "due to configuration parameter mismatch",
  1794. nsc->primaryview->name, view->name);
  1795. result = ISC_R_FAILURE;
  1796. goto cleanup;
  1797. }
  1798. dns_cache_attach(nsc->cache, &cache);
  1799. shared_cache = ISC_TRUE;
  1800. } else {
  1801. if (strcmp(cachename, view->name) == 0) {
  1802. result = dns_viewlist_find(&ns_g_server->viewlist,
  1803. cachename, view->rdclass,
  1804. &pview);
  1805. if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
  1806. goto cleanup;
  1807. if (pview != NULL) {
  1808. if (!cache_reusable(pview, view,
  1809. zero_no_soattl)) {
  1810. isc_log_write(ns_g_lctx,
  1811. NS_LOGCATEGORY_GENERAL,
  1812. NS_LOGMODULE_SERVER,
  1813. ISC_LOG_DEBUG(1),
  1814. "cache cannot be reused "
  1815. "for view %s due to "
  1816. "configuration parameter "
  1817. "mismatch", view->name);
  1818. } else {
  1819. INSIST(pview->cache != NULL);
  1820. isc_log_write(ns_g_lctx,
  1821. NS_LOGCATEGORY_GENERAL,
  1822. NS_LOGMODULE_SERVER,
  1823. ISC_LOG_DEBUG(3),
  1824. "reusing existing cache");
  1825. reused_cache = ISC_TRUE;
  1826. dns_cache_attach(pview->cache, &cache);
  1827. }
  1828. dns_view_getresstats(pview, &resstats);
  1829. dns_view_getresquerystats(pview,
  1830. &resquerystats);
  1831. dns_view_detach(&pview);
  1832. }
  1833. }
  1834. if (cache == NULL) {
  1835. /*
  1836. * Create a cache with the desired name. This normally
  1837. * equals the view name, but may also be a forward
  1838. * reference to a view that share the cache with this
  1839. * view but is not yet configured. If it is not the
  1840. * view name but not a forward reference either, then it
  1841. * is simply a named cache that is not shared.
  1842. *
  1843. * We use two separate memory contexts for the
  1844. * cache, for the main cache memory and the heap
  1845. * memory.
  1846. */
  1847. CHECK(isc_mem_create(0, 0, &cmctx));
  1848. isc_mem_setname(cmctx, "cache", NULL);
  1849. CHECK(isc_mem_create(0, 0, &hmctx));
  1850. isc_mem_setname(hmctx, "cache_heap", NULL);
  1851. CHECK(dns_cache_create3(cmctx, hmctx, ns_g_taskmgr,
  1852. ns_g_timermgr, view->rdclass,
  1853. cachename, "rbt", 0, NULL,
  1854. &cache));
  1855. isc_mem_detach(&cmctx);
  1856. isc_mem_detach(&hmctx);
  1857. }
  1858. nsc = isc_mem_get(mctx, sizeof(*nsc));
  1859. if (nsc == NULL) {
  1860. result = ISC_R_NOMEMORY;
  1861. goto cleanup;
  1862. }
  1863. nsc->cache = NULL;
  1864. dns_cache_attach(cache, &nsc->cache);
  1865. nsc->primaryview = view;
  1866. nsc->needflush = ISC_FALSE;
  1867. nsc->adbsizeadjusted = ISC_FALSE;
  1868. ISC_LINK_INIT(nsc, link);
  1869. ISC_LIST_APPEND(*cachelist, nsc, link);
  1870. }
  1871. dns_view_setcache2(view, cache, shared_cache);
  1872. /*
  1873. * cache-file cannot be inherited if views are present, but this
  1874. * should be caught by the configuration checking stage.
  1875. */
  1876. obj = NULL;
  1877. result = ns_config_get(maps, "cache-file", &obj);
  1878. if (result == ISC_R_SUCCESS && strcmp(view->name, "_bind") != 0) {
  1879. CHECK(dns_cache_setfilename(cache, cfg_obj_asstring(obj)));
  1880. if (!reused_cache && !shared_cache)
  1881. CHECK(dns_cache_load(cache));
  1882. }
  1883. dns_cache_setcleaninginterval(cache, cleaning_interval);
  1884. dns_cache_setcachesize(cache, max_cache_size);
  1885. dns_cache_detach(&cache);
  1886. /*
  1887. * Resolver.
  1888. *
  1889. * XXXRTH Hardwired number of tasks.
  1890. */
  1891. CHECK(get_view_querysource_dispatch(maps, AF_INET, &dispatch4,
  1892. ISC_TF(ISC_LIST_PREV(view, link)
  1893. == NULL)));
  1894. CHECK(get_view_querysource_dispatch(maps, AF_INET6, &dispatch6,
  1895. ISC_TF(ISC_LIST_PREV(view, link)
  1896. == NULL)));
  1897. if (dispatch4 == NULL && dispatch6 == NULL) {
  1898. UNEXPECTED_ERROR(__FILE__, __LINE__,
  1899. "unable to obtain neither an IPv4 nor"
  1900. " an IPv6 dispatch");
  1901. result = ISC_R_UNEXPECTED;
  1902. goto cleanup;
  1903. }
  1904. CHECK(dns_view_createresolver(view, ns_g_taskmgr, 31,
  1905. ns_g_socketmgr, ns_g_timermgr,
  1906. resopts, ns_g_dispatchmgr,
  1907. dispatch4, dispatch6));
  1908. if (resstats == NULL) {
  1909. CHECK(isc_stats_create(mctx, &resstats,
  1910. dns_resstatscounter_max));
  1911. }
  1912. dns_view_setresstats(view, resstats);
  1913. if (resquerystats == NULL)
  1914. CHECK(dns_rdatatypestats_create(mctx, &resquerystats));
  1915. dns_view_setresquerystats(view, resquerystats);
  1916. /*
  1917. * Set the ADB cache size to 1/8th of the max-cache-size or
  1918. * MAX_ADB_SIZE_FOR_CACHESHARE when the cache is shared.
  1919. */
  1920. max_adb_size = 0;
  1921. if (max_cache_size != 0) {
  1922. max_adb_size = max_cache_size / 8;
  1923. if (max_adb_size == 0)
  1924. max_adb_size = 1; /* Force minimum. */
  1925. if (view != nsc->primaryview &&
  1926. max_adb_size > MAX_ADB_SIZE_FOR_CACHESHARE) {
  1927. max_adb_size = MAX_ADB_SIZE_FOR_CACHESHARE;
  1928. if (!nsc->adbsizeadjusted) {
  1929. dns_adb_setadbsize(nsc->primaryview->adb,
  1930. MAX_ADB_SIZE_FOR_CACHESHARE);
  1931. nsc->adbsizeadjusted = ISC_TRUE;
  1932. }
  1933. }
  1934. }
  1935. dns_adb_setadbsize(view->adb, max_adb_size);
  1936. /*
  1937. * Set resolver's lame-ttl.
  1938. */
  1939. obj = NULL;
  1940. result = ns_config_get(maps, "lame-ttl", &obj);
  1941. INSIST(result == ISC_R_SUCCESS);
  1942. lame_ttl = cfg_obj_asuint32(obj);
  1943. if (lame_ttl > 1800)
  1944. lame_ttl = 1800;
  1945. dns_resolver_setlamettl(view->resolver, lame_ttl);
  1946. /*
  1947. * Set the resolver's query timeout.
  1948. */
  1949. obj = NULL;
  1950. result = ns_config_get(maps, "resolver-query-timeout", &obj);
  1951. INSIST(result == ISC_R_SUCCESS);
  1952. query_timeout = cfg_obj_asuint32(obj);
  1953. dns_resolver_settimeout(view->resolver, query_timeout);
  1954. /* Specify whether to use 0-TTL for negative response for SOA query */
  1955. dns_resolver_setzeronosoattl(view->resolver, zero_no_soattl);
  1956. /*
  1957. * Set the resolver's EDNS UDP size.
  1958. */
  1959. obj = NULL;
  1960. result = ns_config_get(maps, "edns-udp-size", &obj);
  1961. INSIST(result == ISC_R_SUCCESS);
  1962. udpsize = cfg_obj_asuint32(obj);
  1963. if (udpsize < 512)
  1964. udpsize = 512;
  1965. if (udpsize > 4096)
  1966. udpsize = 4096;
  1967. dns_resolver_setudpsize(view->resolver, (isc_uint16_t)udpsize);
  1968. /*
  1969. * Set the maximum UDP response size.
  1970. */
  1971. obj = NULL;
  1972. result = ns_config_get(maps, "max-udp-size", &obj);
  1973. INSIST(result == ISC_R_SUCCESS);
  1974. udpsize = cfg_obj_asuint32(obj);
  1975. if (udpsize < 512)
  1976. udpsize = 512;
  1977. if (udpsize > 4096)
  1978. udpsize = 4096;
  1979. view->maxudp = udpsize;
  1980. /*
  1981. * Set supported DNSSEC algorithms.
  1982. */
  1983. dns_resolver_reset_algorithms(view->resolver);
  1984. disabled = NULL;
  1985. (void)ns_config_get(maps, "disable-algorithms", &disabled);
  1986. if (disabled != NULL) {
  1987. for (element = cfg_list_first(disabled);
  1988. element != NULL;
  1989. element = cfg_list_next(element))
  1990. CHECK(disable_algorithms(cfg_listelt_value(element),
  1991. view->resolver));
  1992. }
  1993. /*
  1994. * A global or view "forwarders" option, if present,
  1995. * creates an entry for "." in the forwarding table.
  1996. */
  1997. forwardtype = NULL;
  1998. forwarders = NULL;
  1999. (void)ns_config_get(maps, "forward", &forwardtype);
  2000. (void)ns_config_get(maps, "forwarders", &forwarders);
  2001. if (forwarders != NULL)
  2002. CHECK(configure_forward(config, view, dns_rootname,
  2003. forwarders, forwardtype));
  2004. /*
  2005. * Dual Stack Servers.
  2006. */
  2007. alternates = NULL;
  2008. (void)ns_config_get(maps, "dual-stack-servers", &alternates);
  2009. if (alternates != NULL)
  2010. CHECK(configure_alternates(config, view, alternates));
  2011. /*
  2012. * We have default hints for class IN if we need them.
  2013. */
  2014. if (view->rdclass == dns_rdataclass_in && view->hints == NULL)
  2015. dns_view_sethints(view, ns_g_server->in_roothints);
  2016. /*
  2017. * If we still have no hints, this is a non-IN view with no
  2018. * "hints zone" configured. Issue a warning, except if this
  2019. * is a root server. Root servers never need to consult
  2020. * their hints, so it's no point requiring users to configure
  2021. * them.
  2022. */
  2023. if (view->hints == NULL) {
  2024. dns_zone_t *rootzone = NULL;
  2025. (void)dns_view_findzone(view, dns_rootname, &rootzone);
  2026. if (rootzone != NULL) {
  2027. dns_zone_detach(&rootzone);
  2028. need_hints = ISC_FALSE;
  2029. }
  2030. if (need_hints)
  2031. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  2032. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  2033. "no root hints for view '%s'",
  2034. view->name);
  2035. }
  2036. /*
  2037. * Configure the view's TSIG keys.
  2038. */
  2039. CHECK(ns_tsigkeyring_fromconfig(config, vconfig, view->mctx, &ring));
  2040. if (ns_g_server->sessionkey != NULL) {
  2041. CHECK(dns_tsigkeyring_add(ring, ns_g_server->session_keyname,
  2042. ns_g_server->sessionkey));
  2043. }
  2044. dns_view_setkeyring(view, ring);
  2045. dns_tsigkeyring_detach(&ring);
  2046. /*
  2047. * See if we can re-use a dynamic key ring.
  2048. */
  2049. result = dns_viewlist_find(&ns_g_server->viewlist, view->name,
  2050. view->rdclass, &pview);
  2051. if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
  2052. goto cleanup;
  2053. if (pview != NULL) {
  2054. dns_view_getdynamickeyring(pview, &ring);
  2055. if (ring != NULL)
  2056. dns_view_setdynamickeyring(view, ring);
  2057. dns_tsigkeyring_detach(&ring);
  2058. dns_view_detach(&pview);
  2059. } else
  2060. dns_view_restorekeyring(view);
  2061. /*
  2062. * Configure the view's peer list.
  2063. */
  2064. {
  2065. const cfg_obj_t *peers = NULL;
  2066. const cfg_listelt_t *element;
  2067. dns_peerlist_t *newpeers = NULL;
  2068. (void)ns_config_get(cfgmaps, "server", &peers);
  2069. CHECK(dns_peerlist_new(mctx, &newpeers));
  2070. for (element = cfg_list_first(peers);
  2071. element != NULL;
  2072. element = cfg_list_next(element))
  2073. {
  2074. const cfg_obj_t *cpeer = cfg_listelt_value(element);
  2075. dns_peer_t *peer;
  2076. CHECK(configure_peer(cpeer, mctx, &peer));
  2077. dns_peerlist_addpeer(newpeers, peer);
  2078. dns_peer_detach(&peer);
  2079. }
  2080. dns_peerlist_detach(&view->peers);
  2081. view->peers = newpeers; /* Transfer ownership. */
  2082. }
  2083. /*
  2084. * Configure the views rrset-order.
  2085. */
  2086. {
  2087. const cfg_obj_t *rrsetorder = NULL;
  2088. const cfg_listelt_t *element;
  2089. (void)ns_config_get(maps, "rrset-order", &rrsetorder);
  2090. CHECK(dns_order_create(mctx, &order));
  2091. for (element = cfg_list_first(rrsetorder);
  2092. element != NULL;
  2093. element = cfg_list_next(element))
  2094. {
  2095. const cfg_obj_t *ent = cfg_listelt_value(element);
  2096. CHECK(configure_order(order, ent));
  2097. }
  2098. if (view->order != NULL)
  2099. dns_order_detach(&view->order);
  2100. dns_order_attach(order, &view->order);
  2101. dns_order_detach(&order);
  2102. }
  2103. /*
  2104. * Copy the aclenv object.
  2105. */
  2106. dns_aclenv_copy(&view->aclenv, &ns_g_server->aclenv);
  2107. /*
  2108. * Configure the "match-clients" and "match-destinations" ACL.
  2109. */
  2110. CHECK(configure_view_acl(vconfig, config, "match-clients", NULL, actx,
  2111. ns_g_mctx, &view->matchclients));
  2112. CHECK(configure_view_acl(vconfig, config, "match-destinations", NULL,
  2113. actx, ns_g_mctx, &view->matchdestinations));
  2114. /*
  2115. * Configure the "match-recursive-only" option.
  2116. */
  2117. obj = NULL;
  2118. (void)ns_config_get(maps, "match-recursive-only", &obj);
  2119. if (obj != NULL && cfg_obj_asboolean(obj))
  2120. view->matchrecursiveonly = ISC_TRUE;
  2121. else
  2122. view->matchrecursiveonly = ISC_FALSE;
  2123. /*
  2124. * Configure other configurable data.
  2125. */
  2126. obj = NULL;
  2127. result = ns_config_get(maps, "recursion", &obj);
  2128. INSIST(result == ISC_R_SUCCESS);
  2129. view->recursion = cfg_obj_asboolean(obj);
  2130. obj = NULL;
  2131. result = ns_config_get(maps, "auth-nxdomain", &obj);
  2132. INSIST(result == ISC_R_SUCCESS);
  2133. view->auth_nxdomain = cfg_obj_asboolean(obj);
  2134. obj = NULL;
  2135. result = ns_config_get(maps, "minimal-responses", &obj);
  2136. INSIST(result == ISC_R_SUCCESS);
  2137. view->minimalresponses = cfg_obj_asboolean(obj);
  2138. obj = NULL;
  2139. result = ns_config_get(maps, "transfer-format", &obj);
  2140. INSIST(result == ISC_R_SUCCESS);
  2141. str = cfg_obj_asstring(obj);
  2142. if (strcasecmp(str, "many-answers") == 0)
  2143. view->transfer_format = dns_many_answers;
  2144. else if (strcasecmp(str, "one-answer") == 0)
  2145. view->transfer_format = dns_one_answer;
  2146. else
  2147. INSIST(0);
  2148. /*
  2149. * Set sources where additional data and CNAME/DNAME
  2150. * targets for authoritative answers may be found.
  2151. */
  2152. obj = NULL;
  2153. result = ns_config_get(maps, "additional-from-auth", &obj);
  2154. INSIST(result == ISC_R_SUCCESS);
  2155. view->additionalfromauth = cfg_obj_asboolean(obj);
  2156. if (view->recursion && ! view->additionalfromauth) {
  2157. cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
  2158. "'additional-from-auth no' is only supported "
  2159. "with 'recursion no'");
  2160. view->additionalfromauth = ISC_TRUE;
  2161. }
  2162. obj = NULL;
  2163. result = ns_config_get(maps, "additional-from-cache", &obj);
  2164. INSIST(result == ISC_R_SUCCESS);
  2165. view->additionalfromcache = cfg_obj_asboolean(obj);
  2166. if (view->recursion && ! view->additionalfromcache) {
  2167. cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
  2168. "'additional-from-cache no' is only supported "
  2169. "with 'recursion no'");
  2170. view->additionalfromcache = ISC_TRUE;
  2171. }
  2172. /*
  2173. * Set "allow-query-cache", "allow-query-cache-on",
  2174. * "allow-recursion", and "allow-recursion-on" acls if
  2175. * configured in named.conf.
  2176. */
  2177. CHECK(configure_view_acl(vconfig, config, "allow-query-cache", NULL,
  2178. actx, ns_g_mctx, &view->cacheacl));
  2179. CHECK(configure_view_acl(vconfig, config, "allow-query-cache-on", NULL,
  2180. actx, ns_g_mctx, &view->cacheonacl));
  2181. if (view->cacheonacl == NULL)
  2182. CHECK(configure_view_acl(NULL, ns_g_config,
  2183. "allow-query-cache-on", NULL, actx,
  2184. ns_g_mctx, &view->cacheonacl));
  2185. if (strcmp(view->name, "_bind") != 0) {
  2186. CHECK(configure_view_acl(vconfig, config, "allow-recursion",
  2187. NULL, actx, ns_g_mctx,
  2188. &view->recursionacl));
  2189. CHECK(configure_view_acl(vconfig, config, "allow-recursion-on",
  2190. NULL, actx, ns_g_mctx,
  2191. &view->recursiononacl));
  2192. }
  2193. /*
  2194. * "allow-query-cache" inherits from "allow-recursion" if set,
  2195. * otherwise from "allow-query" if set.
  2196. * "allow-recursion" inherits from "allow-query-cache" if set,
  2197. * otherwise from "allow-query" if set.
  2198. */
  2199. if (view->cacheacl == NULL && view->recursionacl != NULL)
  2200. dns_acl_attach(view->recursionacl, &view->cacheacl);
  2201. /*
  2202. * XXXEACH: This call to configure_view_acl() is redundant. We
  2203. * are leaving it as it is because we are making a minimal change
  2204. * for a patch release. In the future this should be changed to
  2205. * dns_acl_attach(view->queryacl, &view->cacheacl).
  2206. */
  2207. if (view->cacheacl == NULL && view->recursion)
  2208. CHECK(configure_view_acl(vconfig, config, "allow-query", NULL,
  2209. actx, ns_g_mctx, &view->cacheacl));
  2210. if (view->recursion &&
  2211. view->recursionacl == NULL && view->cacheacl != NULL)
  2212. dns_acl_attach(view->cacheacl, &view->recursionacl);
  2213. /*
  2214. * Set default "allow-recursion", "allow-recursion-on" and
  2215. * "allow-query-cache" acls.
  2216. */
  2217. if (view->recursionacl == NULL && view->recursion)
  2218. CHECK(configure_view_acl(NULL, ns_g_config,
  2219. "allow-recursion", NULL,
  2220. actx, ns_g_mctx,
  2221. &view->recursionacl));
  2222. if (view->recursiononacl == NULL && view->recursion)
  2223. CHECK(configure_view_acl(NULL, ns_g_config,
  2224. "allow-recursion-on", NULL,
  2225. actx, ns_g_mctx,
  2226. &view->recursiononacl));
  2227. if (view->cacheacl == NULL) {
  2228. if (view->recursion)
  2229. CHECK(configure_view_acl(NULL, ns_g_config,
  2230. "allow-query-cache", NULL,
  2231. actx, ns_g_mctx,
  2232. &view->cacheacl));
  2233. else
  2234. CHECK(dns_acl_none(mctx, &view->cacheacl));
  2235. }
  2236. /*
  2237. * Filter setting on addresses in the answer section.
  2238. */
  2239. CHECK(configure_view_acl(vconfig, config, "deny-answer-addresses",
  2240. "acl", actx, ns_g_mctx, &view->denyansweracl));
  2241. CHECK(configure_view_nametable(vconfig, config, "deny-answer-addresses",
  2242. "except-from", ns_g_mctx,
  2243. &view->answeracl_exclude));
  2244. /*
  2245. * Filter setting on names (CNAME/DNAME targets) in the answer section.
  2246. */
  2247. CHECK(configure_view_nametable(vconfig, config, "deny-answer-aliases",
  2248. "name", ns_g_mctx,
  2249. &view->denyanswernames));
  2250. CHECK(configure_view_nametable(vconfig, config, "deny-answer-aliases",
  2251. "except-from", ns_g_mctx,
  2252. &view->answernames_exclude));
  2253. /*
  2254. * Configure sortlist, if set
  2255. */
  2256. CHECK(configure_view_sortlist(vconfig, config, actx, ns_g_mctx,
  2257. &view->sortlist));
  2258. /*
  2259. * Configure default allow-transfer, allow-notify, allow-update
  2260. * and allow-update-forwarding ACLs, if set, so they can be
  2261. * inherited by zones.
  2262. */
  2263. if (view->notifyacl == NULL)
  2264. CHECK(configure_view_acl(NULL, ns_g_config,
  2265. "allow-notify", NULL, actx,
  2266. ns_g_mctx, &view->notifyacl));
  2267. if (view->transferacl == NULL)
  2268. CHECK(configure_view_acl(NULL, ns_g_config,
  2269. "allow-transfer", NULL, actx,
  2270. ns_g_mctx, &view->transferacl));
  2271. if (view->updateacl == NULL)
  2272. CHECK(configure_view_acl(NULL, ns_g_config,
  2273. "allow-update", NULL, actx,
  2274. ns_g_mctx, &view->updateacl));
  2275. if (view->upfwdacl == NULL)
  2276. CHECK(configure_view_acl(NULL, ns_g_config,
  2277. "allow-update-forwarding", NULL, actx,
  2278. ns_g_mctx, &view->upfwdacl));
  2279. obj = NULL;
  2280. result = ns_config_get(maps, "request-ixfr", &obj);
  2281. INSIST(result == ISC_R_SUCCESS);
  2282. view->requestixfr = cfg_obj_asboolean(obj);
  2283. obj = NULL;
  2284. result = ns_config_get(maps, "provide-ixfr", &obj);
  2285. INSIST(result == ISC_R_SUCCESS);
  2286. view->provideixfr = cfg_obj_asboolean(obj);
  2287. obj = NULL;
  2288. result = ns_config_get(maps, "request-nsid", &obj);
  2289. INSIST(result == ISC_R_SUCCESS);
  2290. view->requestnsid = cfg_obj_asboolean(obj);
  2291. obj = NULL;
  2292. result = ns_config_get(maps, "max-clients-per-query", &obj);
  2293. INSIST(result == ISC_R_SUCCESS);
  2294. max_clients_per_query = cfg_obj_asuint32(obj);
  2295. obj = NULL;
  2296. result = ns_config_get(maps, "clients-per-query", &obj);
  2297. INSIST(result == ISC_R_SUCCESS);
  2298. dns_resolver_setclientsperquery(view->resolver,
  2299. cfg_obj_asuint32(obj),
  2300. max_clients_per_query);
  2301. #ifdef ALLOW_FILTER_AAAA_ON_V4
  2302. obj = NULL;
  2303. result = ns_config_get(maps, "filter-aaaa-on-v4", &obj);
  2304. INSIST(result == ISC_R_SUCCESS);
  2305. if (cfg_obj_isboolean(obj)) {
  2306. if (cfg_obj_asboolean(obj))
  2307. view->v4_aaaa = dns_v4_aaaa_filter;
  2308. else
  2309. view->v4_aaaa = dns_v4_aaaa_ok;
  2310. } else {
  2311. const char *v4_aaaastr = cfg_obj_asstring(obj);
  2312. if (strcasecmp(v4_aaaastr, "break-dnssec") == 0)
  2313. view->v4_aaaa = dns_v4_aaaa_break_dnssec;
  2314. else
  2315. INSIST(0);
  2316. }
  2317. CHECK(configure_view_acl(vconfig, config, "filter-aaaa", NULL,
  2318. actx, ns_g_mctx, &view->v4_aaaa_acl));
  2319. #endif
  2320. obj = NULL;
  2321. result = ns_config_get(maps, "dnssec-enable", &obj);
  2322. INSIST(result == ISC_R_SUCCESS);
  2323. view->enablednssec = cfg_obj_asboolean(obj);
  2324. obj = NULL;
  2325. result = ns_config_get(optionmaps, "dnssec-lookaside", &obj);
  2326. if (result == ISC_R_SUCCESS) {
  2327. /* If set to "auto", use the version from the defaults */
  2328. const cfg_obj_t *dlvobj;
  2329. const char *dom;
  2330. dlvobj = cfg_listelt_value(cfg_list_first(obj));
  2331. dom = cfg_obj_asstring(cfg_tuple_get(dlvobj, "domain"));
  2332. if (cfg_obj_isvoid(cfg_tuple_get(dlvobj, "trust-anchor"))) {
  2333. /* If "no", skip; if "auto", use global default */
  2334. if (!strcasecmp(dom, "no"))
  2335. result = ISC_R_NOTFOUND;
  2336. else if (!strcasecmp(dom, "auto")) {
  2337. auto_dlv = ISC_TRUE;
  2338. obj = NULL;
  2339. result = cfg_map_get(ns_g_defaults,
  2340. "dnssec-lookaside", &obj);
  2341. }
  2342. }
  2343. }
  2344. if (result == ISC_R_SUCCESS) {
  2345. for (element = cfg_list_first(obj);
  2346. element != NULL;
  2347. element = cfg_list_next(element))
  2348. {
  2349. const char *str;
  2350. isc_buffer_t b;
  2351. dns_name_t *dlv;
  2352. obj = cfg_listelt_value(element);
  2353. str = cfg_obj_asstring(cfg_tuple_get(obj,
  2354. "trust-anchor"));
  2355. isc_buffer_init(&b, str, strlen(str));
  2356. isc_buffer_add(&b, strlen(str));
  2357. dlv = dns_fixedname_name(&view->dlv_fixed);
  2358. CHECK(dns_name_fromtext(dlv, &b, dns_rootname,
  2359. DNS_NAME_DOWNCASE, NULL));
  2360. view->dlv = dns_fixedname_name(&view->dlv_fixed);
  2361. }
  2362. } else
  2363. view->dlv = NULL;
  2364. /*
  2365. * For now, there is only one kind of trusted keys, the
  2366. * "security roots".
  2367. */
  2368. CHECK(configure_view_dnsseckeys(view, vconfig, config, bindkeys,
  2369. auto_dlv, auto_root, mctx));
  2370. dns_resolver_resetmustbesecure(view->resolver);
  2371. obj = NULL;
  2372. result = ns_config_get(maps, "dnssec-must-be-secure", &obj);
  2373. if (result == ISC_R_SUCCESS)
  2374. CHECK(mustbesecure(obj, view->resolver));
  2375. obj = NULL;
  2376. result = ns_config_get(maps, "preferred-glue", &obj);
  2377. if (result == ISC_R_SUCCESS) {
  2378. str = cfg_obj_asstring(obj);
  2379. if (strcasecmp(str, "a") == 0)
  2380. view->preferred_glue = dns_rdatatype_a;
  2381. else if (strcasecmp(str, "aaaa") == 0)
  2382. view->preferred_glue = dns_rdatatype_aaaa;
  2383. else
  2384. view->preferred_glue = 0;
  2385. } else
  2386. view->preferred_glue = 0;
  2387. obj = NULL;
  2388. result = ns_config_get(maps, "root-delegation-only", &obj);
  2389. if (result == ISC_R_SUCCESS) {
  2390. dns_view_setrootdelonly(view, ISC_TRUE);
  2391. if (!cfg_obj_isvoid(obj)) {
  2392. dns_fixedname_t fixed;
  2393. dns_name_t *name;
  2394. isc_buffer_t b;
  2395. const char *str;
  2396. const cfg_obj_t *exclude;
  2397. dns_fixedname_init(&fixed);
  2398. name = dns_fixedname_name(&fixed);
  2399. for (element = cfg_list_first(obj);
  2400. element != NULL;
  2401. element = cfg_list_next(element)) {
  2402. exclude = cfg_listelt_value(element);
  2403. str = cfg_obj_asstring(exclude);
  2404. isc_buffer_init(&b, str, strlen(str));
  2405. isc_buffer_add(&b, strlen(str));
  2406. CHECK(dns_name_fromtext(name, &b, dns_rootname,
  2407. 0, NULL));
  2408. CHECK(dns_view_excludedelegationonly(view,
  2409. name));
  2410. }
  2411. }
  2412. } else
  2413. dns_view_setrootdelonly(view, ISC_FALSE);
  2414. /*
  2415. * Setup automatic empty zones. If recursion is off then
  2416. * they are disabled by default.
  2417. */
  2418. obj = NULL;
  2419. (void)ns_config_get(maps, "empty-zones-enable", &obj);
  2420. (void)ns_config_get(maps, "disable-empty-zone", &disablelist);
  2421. if (obj == NULL && disablelist == NULL &&
  2422. view->rdclass == dns_rdataclass_in) {
  2423. rfc1918 = ISC_FALSE;
  2424. empty_zones_enable = view->recursion;
  2425. } else if (view->rdclass == dns_rdataclass_in) {
  2426. rfc1918 = ISC_TRUE;
  2427. if (obj != NULL)
  2428. empty_zones_enable = cfg_obj_asboolean(obj);
  2429. else
  2430. empty_zones_enable = view->recursion;
  2431. } else {
  2432. rfc1918 = ISC_FALSE;
  2433. empty_zones_enable = ISC_FALSE;
  2434. }
  2435. if (empty_zones_enable && !lwresd_g_useresolvconf) {
  2436. const char *empty;
  2437. int empty_zone = 0;
  2438. dns_fixedname_t fixed;
  2439. dns_name_t *name;
  2440. isc_buffer_t buffer;
  2441. const char *str;
  2442. char server[DNS_NAME_FORMATSIZE + 1];
  2443. char contact[DNS_NAME_FORMATSIZE + 1];
  2444. isc_boolean_t logit;
  2445. const char *empty_dbtype[4] =
  2446. { "_builtin", "empty", NULL, NULL };
  2447. int empty_dbtypec = 4;
  2448. isc_boolean_t zonestats_on;
  2449. dns_fixedname_init(&fixed);
  2450. name = dns_fixedname_name(&fixed);
  2451. obj = NULL;
  2452. result = ns_config_get(maps, "empty-server", &obj);
  2453. if (result == ISC_R_SUCCESS) {
  2454. str = cfg_obj_asstring(obj);
  2455. isc_buffer_init(&buffer, str, strlen(str));
  2456. isc_buffer_add(&buffer, strlen(str));
  2457. CHECK(dns_name_fromtext(name, &buffer, dns_rootname, 0,
  2458. NULL));
  2459. isc_buffer_init(&buffer, server, sizeof(server) - 1);
  2460. CHECK(dns_name_totext(name, ISC_FALSE, &buffer));
  2461. server[isc_buffer_usedlength(&buffer)] = 0;
  2462. empty_dbtype[2] = server;
  2463. } else
  2464. empty_dbtype[2] = "@";
  2465. obj = NULL;
  2466. result = ns_config_get(maps, "empty-contact", &obj);
  2467. if (result == ISC_R_SUCCESS) {
  2468. str = cfg_obj_asstring(obj);
  2469. isc_buffer_init(&buffer, str, strlen(str));
  2470. isc_buffer_add(&buffer, strlen(str));
  2471. CHECK(dns_name_fromtext(name, &buffer, dns_rootname, 0,
  2472. NULL));
  2473. isc_buffer_init(&buffer, contact, sizeof(contact) - 1);
  2474. CHECK(dns_name_totext(name, ISC_FALSE, &buffer));
  2475. contact[isc_buffer_usedlength(&buffer)] = 0;
  2476. empty_dbtype[3] = contact;
  2477. } else
  2478. empty_dbtype[3] = ".";
  2479. obj = NULL;
  2480. result = ns_config_get(maps, "zone-statistics", &obj);
  2481. INSIST(result == ISC_R_SUCCESS);
  2482. zonestats_on = cfg_obj_asboolean(obj);
  2483. logit = ISC_TRUE;
  2484. for (empty = empty_zones[empty_zone].zone;
  2485. empty != NULL;
  2486. empty = empty_zones[++empty_zone].zone)
  2487. {
  2488. dns_forwarders_t *forwarders = NULL;
  2489. dns_view_t *pview = NULL;
  2490. isc_buffer_init(&buffer, empty, strlen(empty));
  2491. isc_buffer_add(&buffer, strlen(empty));
  2492. /*
  2493. * Look for zone on drop list.
  2494. */
  2495. CHECK(dns_name_fromtext(name, &buffer, dns_rootname, 0,
  2496. NULL));
  2497. if (disablelist != NULL &&
  2498. on_disable_list(disablelist, name))
  2499. continue;
  2500. /*
  2501. * This zone already exists.
  2502. */
  2503. (void)dns_view_findzone(view, name, &zone);
  2504. if (zone != NULL) {
  2505. CHECK(setquerystats(zone, mctx, zonestats_on));
  2506. dns_zone_detach(&zone);
  2507. continue;
  2508. }
  2509. /*
  2510. * If we would forward this name don't add a
  2511. * empty zone for it.
  2512. */
  2513. result = dns_fwdtable_find(view->fwdtable, name,
  2514. &forwarders);
  2515. if (result == ISC_R_SUCCESS &&
  2516. forwarders->fwdpolicy == dns_fwdpolicy_only)
  2517. continue;
  2518. if (!rfc1918 && empty_zones[empty_zone].rfc1918) {
  2519. if (logit) {
  2520. isc_log_write(ns_g_lctx,
  2521. NS_LOGCATEGORY_GENERAL,
  2522. NS_LOGMODULE_SERVER,
  2523. ISC_LOG_WARNING,
  2524. "Warning%s%s: "
  2525. "'empty-zones-enable/"
  2526. "disable-empty-zone' "
  2527. "not set: disabling "
  2528. "RFC 1918 empty zones",
  2529. sep, viewname);
  2530. logit = ISC_FALSE;
  2531. }
  2532. continue;
  2533. }
  2534. /*
  2535. * See if we can re-use a existing zone.
  2536. */
  2537. result = dns_viewlist_find(&ns_g_server->viewlist,
  2538. view->name, view->rdclass,
  2539. &pview);
  2540. if (result != ISC_R_NOTFOUND &&
  2541. result != ISC_R_SUCCESS)
  2542. goto cleanup;
  2543. if (pview != NULL) {
  2544. (void)dns_view_findzone(pview, name, &zone);
  2545. dns_view_detach(&pview);
  2546. if (zone != NULL)
  2547. check_dbtype(&zone, empty_dbtypec,
  2548. empty_dbtype, mctx);
  2549. if (zone != NULL) {
  2550. dns_zone_setview(zone, view);
  2551. CHECK(dns_view_addzone(view, zone));
  2552. CHECK(setquerystats(zone, mctx,
  2553. zonestats_on));
  2554. dns_zone_detach(&zone);
  2555. continue;
  2556. }
  2557. }
  2558. CHECK(dns_zone_create(&zone, mctx));
  2559. CHECK(dns_zone_setorigin(zone, name));
  2560. dns_zone_setview(zone, view);
  2561. CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr,
  2562. zone));
  2563. dns_zone_setclass(zone, view->rdclass);
  2564. dns_zone_settype(zone, dns_zone_master);
  2565. dns_zone_setstats(zone, ns_g_server->zonestats);
  2566. CHECK(dns_zone_setdbtype(zone, empty_dbtypec,
  2567. empty_dbtype));
  2568. if (view->queryacl != NULL)
  2569. dns_zone_setqueryacl(zone, view->queryacl);
  2570. if (view->queryonacl != NULL)
  2571. dns_zone_setqueryonacl(zone, view->queryonacl);
  2572. dns_zone_setdialup(zone, dns_dialuptype_no);
  2573. dns_zone_setnotifytype(zone, dns_notifytype_no);
  2574. dns_zone_setoption(zone, DNS_ZONEOPT_NOCHECKNS,
  2575. ISC_TRUE);
  2576. CHECK(setquerystats(zone, mctx, zonestats_on));
  2577. CHECK(dns_view_addzone(view, zone));
  2578. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  2579. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  2580. "automatic empty zone%s%s: %s",
  2581. sep, viewname, empty);
  2582. dns_zone_detach(&zone);
  2583. }
  2584. }
  2585. /*
  2586. * Make the list of response policy zone names for views that
  2587. * are used for real lookups and so care about hints.
  2588. */
  2589. zonelist = NULL;
  2590. if (view->rdclass == dns_rdataclass_in && need_hints) {
  2591. obj = NULL;
  2592. result = ns_config_get(maps, "response-policy", &obj);
  2593. if (result == ISC_R_SUCCESS)
  2594. cfg_map_get(obj, "zone", &zonelist);
  2595. }
  2596. if (zonelist != NULL) {
  2597. for (element = cfg_list_first(zonelist);
  2598. element != NULL;
  2599. element = cfg_list_next(element)) {
  2600. result = configure_rpz(view, element);
  2601. if (result != ISC_R_SUCCESS)
  2602. goto cleanup;
  2603. dns_rpz_set_need(ISC_TRUE);
  2604. }
  2605. }
  2606. result = ISC_R_SUCCESS;
  2607. cleanup:
  2608. if (clients != NULL)
  2609. dns_acl_detach(&clients);
  2610. if (mapped != NULL)
  2611. dns_acl_detach(&mapped);
  2612. if (excluded != NULL)
  2613. dns_acl_detach(&excluded);
  2614. if (ring != NULL)
  2615. dns_tsigkeyring_detach(&ring);
  2616. if (zone != NULL)
  2617. dns_zone_detach(&zone);
  2618. if (dispatch4 != NULL)
  2619. dns_dispatch_detach(&dispatch4);
  2620. if (dispatch6 != NULL)
  2621. dns_dispatch_detach(&dispatch6);
  2622. if (resstats != NULL)
  2623. isc_stats_detach(&resstats);
  2624. if (resquerystats != NULL)
  2625. dns_stats_detach(&resquerystats);
  2626. if (order != NULL)
  2627. dns_order_detach(&order);
  2628. if (cmctx != NULL)
  2629. isc_mem_detach(&cmctx);
  2630. if (hmctx != NULL)
  2631. isc_mem_detach(&hmctx);
  2632. if (cache != NULL)
  2633. dns_cache_detach(&cache);
  2634. return (result);
  2635. }
  2636. static isc_result_t
  2637. configure_hints(dns_view_t *view, const char *filename) {
  2638. isc_result_t result;
  2639. dns_db_t *db;
  2640. db = NULL;
  2641. result = dns_rootns_create(view->mctx, view->rdclass, filename, &db);
  2642. if (result == ISC_R_SUCCESS) {
  2643. dns_view_sethints(view, db);
  2644. dns_db_detach(&db);
  2645. }
  2646. return (result);
  2647. }
  2648. static isc_result_t
  2649. configure_alternates(const cfg_obj_t *config, dns_view_t *view,
  2650. const cfg_obj_t *alternates)
  2651. {
  2652. const cfg_obj_t *portobj;
  2653. const cfg_obj_t *addresses;
  2654. const cfg_listelt_t *element;
  2655. isc_result_t result = ISC_R_SUCCESS;
  2656. in_port_t port;
  2657. /*
  2658. * Determine which port to send requests to.
  2659. */
  2660. if (ns_g_lwresdonly && ns_g_port != 0)
  2661. port = ns_g_port;
  2662. else
  2663. CHECKM(ns_config_getport(config, &port), "port");
  2664. if (alternates != NULL) {
  2665. portobj = cfg_tuple_get(alternates, "port");
  2666. if (cfg_obj_isuint32(portobj)) {
  2667. isc_uint32_t val = cfg_obj_asuint32(portobj);
  2668. if (val > ISC_UINT16_MAX) {
  2669. cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
  2670. "port '%u' out of range", val);
  2671. return (ISC_R_RANGE);
  2672. }
  2673. port = (in_port_t) val;
  2674. }
  2675. }
  2676. addresses = NULL;
  2677. if (alternates != NULL)
  2678. addresses = cfg_tuple_get(alternates, "addresses");
  2679. for (element = cfg_list_first(addresses);
  2680. element != NULL;
  2681. element = cfg_list_next(element))
  2682. {
  2683. const cfg_obj_t *alternate = cfg_listelt_value(element);
  2684. isc_sockaddr_t sa;
  2685. if (!cfg_obj_issockaddr(alternate)) {
  2686. dns_fixedname_t fixed;
  2687. dns_name_t *name;
  2688. const char *str = cfg_obj_asstring(cfg_tuple_get(
  2689. alternate, "name"));
  2690. isc_buffer_t buffer;
  2691. in_port_t myport = port;
  2692. isc_buffer_init(&buffer, str, strlen(str));
  2693. isc_buffer_add(&buffer, strlen(str));
  2694. dns_fixedname_init(&fixed);
  2695. name = dns_fixedname_name(&fixed);
  2696. CHECK(dns_name_fromtext(name, &buffer, dns_rootname, 0,
  2697. NULL));
  2698. portobj = cfg_tuple_get(alternate, "port");
  2699. if (cfg_obj_isuint32(portobj)) {
  2700. isc_uint32_t val = cfg_obj_asuint32(portobj);
  2701. if (val > ISC_UINT16_MAX) {
  2702. cfg_obj_log(portobj, ns_g_lctx,
  2703. ISC_LOG_ERROR,
  2704. "port '%u' out of range",
  2705. val);
  2706. return (ISC_R_RANGE);
  2707. }
  2708. myport = (in_port_t) val;
  2709. }
  2710. CHECK(dns_resolver_addalternate(view->resolver, NULL,
  2711. name, myport));
  2712. continue;
  2713. }
  2714. sa = *cfg_obj_assockaddr(alternate);
  2715. if (isc_sockaddr_getport(&sa) == 0)
  2716. isc_sockaddr_setport(&sa, port);
  2717. CHECK(dns_resolver_addalternate(view->resolver, &sa,
  2718. NULL, 0));
  2719. }
  2720. cleanup:
  2721. return (result);
  2722. }
  2723. static isc_result_t
  2724. configure_forward(const cfg_obj_t *config, dns_view_t *view, dns_name_t *origin,
  2725. const cfg_obj_t *forwarders, const cfg_obj_t *forwardtype)
  2726. {
  2727. const cfg_obj_t *portobj;
  2728. const cfg_obj_t *faddresses;
  2729. const cfg_listelt_t *element;
  2730. dns_fwdpolicy_t fwdpolicy = dns_fwdpolicy_none;
  2731. isc_sockaddrlist_t addresses;
  2732. isc_sockaddr_t *sa;
  2733. isc_result_t result;
  2734. in_port_t port;
  2735. ISC_LIST_INIT(addresses);
  2736. /*
  2737. * Determine which port to send forwarded requests to.
  2738. */
  2739. if (ns_g_lwresdonly && ns_g_port != 0)
  2740. port = ns_g_port;
  2741. else
  2742. CHECKM(ns_config_getport(config, &port), "port");
  2743. if (forwarders != NULL) {
  2744. portobj = cfg_tuple_get(forwarders, "port");
  2745. if (cfg_obj_isuint32(portobj)) {
  2746. isc_uint32_t val = cfg_obj_asuint32(portobj);
  2747. if (val > ISC_UINT16_MAX) {
  2748. cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
  2749. "port '%u' out of range", val);
  2750. return (ISC_R_RANGE);
  2751. }
  2752. port = (in_port_t) val;
  2753. }
  2754. }
  2755. faddresses = NULL;
  2756. if (forwarders != NULL)
  2757. faddresses = cfg_tuple_get(forwarders, "addresses");
  2758. for (element = cfg_list_first(faddresses);
  2759. element != NULL;
  2760. element = cfg_list_next(element))
  2761. {
  2762. const cfg_obj_t *forwarder = cfg_listelt_value(element);
  2763. sa = isc_mem_get(view->mctx, sizeof(isc_sockaddr_t));
  2764. if (sa == NULL) {
  2765. result = ISC_R_NOMEMORY;
  2766. goto cleanup;
  2767. }
  2768. *sa = *cfg_obj_assockaddr(forwarder);
  2769. if (isc_sockaddr_getport(sa) == 0)
  2770. isc_sockaddr_setport(sa, port);
  2771. ISC_LINK_INIT(sa, link);
  2772. ISC_LIST_APPEND(addresses, sa, link);
  2773. }
  2774. if (ISC_LIST_EMPTY(addresses)) {
  2775. if (forwardtype != NULL)
  2776. cfg_obj_log(forwarders, ns_g_lctx, ISC_LOG_WARNING,
  2777. "no forwarders seen; disabling "
  2778. "forwarding");
  2779. fwdpolicy = dns_fwdpolicy_none;
  2780. } else {
  2781. if (forwardtype == NULL)
  2782. fwdpolicy = dns_fwdpolicy_first;
  2783. else {
  2784. const char *forwardstr = cfg_obj_asstring(forwardtype);
  2785. if (strcasecmp(forwardstr, "first") == 0)
  2786. fwdpolicy = dns_fwdpolicy_first;
  2787. else if (strcasecmp(forwardstr, "only") == 0)
  2788. fwdpolicy = dns_fwdpolicy_only;
  2789. else
  2790. INSIST(0);
  2791. }
  2792. }
  2793. result = dns_fwdtable_add(view->fwdtable, origin, &addresses,
  2794. fwdpolicy);
  2795. if (result != ISC_R_SUCCESS) {
  2796. char namebuf[DNS_NAME_FORMATSIZE];
  2797. dns_name_format(origin, namebuf, sizeof(namebuf));
  2798. cfg_obj_log(forwarders, ns_g_lctx, ISC_LOG_WARNING,
  2799. "could not set up forwarding for domain '%s': %s",
  2800. namebuf, isc_result_totext(result));
  2801. goto cleanup;
  2802. }
  2803. result = ISC_R_SUCCESS;
  2804. cleanup:
  2805. while (!ISC_LIST_EMPTY(addresses)) {
  2806. sa = ISC_LIST_HEAD(addresses);
  2807. ISC_LIST_UNLINK(addresses, sa, link);
  2808. isc_mem_put(view->mctx, sa, sizeof(isc_sockaddr_t));
  2809. }
  2810. return (result);
  2811. }
  2812. static isc_result_t
  2813. get_viewinfo(const cfg_obj_t *vconfig, const char **namep,
  2814. dns_rdataclass_t *classp)
  2815. {
  2816. isc_result_t result = ISC_R_SUCCESS;
  2817. const char *viewname;
  2818. dns_rdataclass_t viewclass;
  2819. REQUIRE(namep != NULL && *namep == NULL);
  2820. REQUIRE(classp != NULL);
  2821. if (vconfig != NULL) {
  2822. const cfg_obj_t *classobj = NULL;
  2823. viewname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
  2824. classobj = cfg_tuple_get(vconfig, "class");
  2825. result = ns_config_getclass(classobj, dns_rdataclass_in,
  2826. &viewclass);
  2827. } else {
  2828. viewname = "_default";
  2829. viewclass = dns_rdataclass_in;
  2830. }
  2831. *namep = viewname;
  2832. *classp = viewclass;
  2833. return (result);
  2834. }
  2835. /*
  2836. * Find a view based on its configuration info and attach to it.
  2837. *
  2838. * If 'vconfig' is NULL, attach to the default view.
  2839. */
  2840. static isc_result_t
  2841. find_view(const cfg_obj_t *vconfig, dns_viewlist_t *viewlist,
  2842. dns_view_t **viewp)
  2843. {
  2844. isc_result_t result;
  2845. const char *viewname = NULL;
  2846. dns_rdataclass_t viewclass;
  2847. dns_view_t *view = NULL;
  2848. result = get_viewinfo(vconfig, &viewname, &viewclass);
  2849. if (result != ISC_R_SUCCESS)
  2850. return (result);
  2851. result = dns_viewlist_find(viewlist, viewname, viewclass, &view);
  2852. if (result != ISC_R_SUCCESS)
  2853. return (result);
  2854. *viewp = view;
  2855. return (ISC_R_SUCCESS);
  2856. }
  2857. /*
  2858. * Create a new view and add it to the list.
  2859. *
  2860. * If 'vconfig' is NULL, create the default view.
  2861. *
  2862. * The view created is attached to '*viewp'.
  2863. */
  2864. static isc_result_t
  2865. create_view(const cfg_obj_t *vconfig, dns_viewlist_t *viewlist,
  2866. dns_view_t **viewp)
  2867. {
  2868. isc_result_t result;
  2869. const char *viewname = NULL;
  2870. dns_rdataclass_t viewclass;
  2871. dns_view_t *view = NULL;
  2872. result = get_viewinfo(vconfig, &viewname, &viewclass);
  2873. if (result != ISC_R_SUCCESS)
  2874. return (result);
  2875. result = dns_viewlist_find(viewlist, viewname, viewclass, &view);
  2876. if (result == ISC_R_SUCCESS)
  2877. return (ISC_R_EXISTS);
  2878. if (result != ISC_R_NOTFOUND)
  2879. return (result);
  2880. INSIST(view == NULL);
  2881. result = dns_view_create(ns_g_mctx, viewclass, viewname, &view);
  2882. if (result != ISC_R_SUCCESS)
  2883. return (result);
  2884. ISC_LIST_APPEND(*viewlist, view, link);
  2885. dns_view_attach(view, viewp);
  2886. return (ISC_R_SUCCESS);
  2887. }
  2888. /*
  2889. * Configure or reconfigure a zone.
  2890. */
  2891. static isc_result_t
  2892. configure_zone(const cfg_obj_t *config, const cfg_obj_t *zconfig,
  2893. const cfg_obj_t *vconfig, isc_mem_t *mctx, dns_view_t *view,
  2894. cfg_aclconfctx_t *aclconf, isc_boolean_t added)
  2895. {
  2896. dns_view_t *pview = NULL; /* Production view */
  2897. dns_zone_t *zone = NULL; /* New or reused zone */
  2898. dns_zone_t *dupzone = NULL;
  2899. const cfg_obj_t *options = NULL;
  2900. const cfg_obj_t *zoptions = NULL;
  2901. const cfg_obj_t *typeobj = NULL;
  2902. const cfg_obj_t *forwarders = NULL;
  2903. const cfg_obj_t *forwardtype = NULL;
  2904. const cfg_obj_t *only = NULL;
  2905. isc_result_t result;
  2906. isc_result_t tresult;
  2907. isc_buffer_t buffer;
  2908. dns_fixedname_t fixorigin;
  2909. dns_name_t *origin;
  2910. const char *zname;
  2911. dns_rdataclass_t zclass;
  2912. const char *ztypestr;
  2913. options = NULL;
  2914. (void)cfg_map_get(config, "options", &options);
  2915. zoptions = cfg_tuple_get(zconfig, "options");
  2916. /*
  2917. * Get the zone origin as a dns_name_t.
  2918. */
  2919. zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
  2920. isc_buffer_init(&buffer, zname, strlen(zname));
  2921. isc_buffer_add(&buffer, strlen(zname));
  2922. dns_fixedname_init(&fixorigin);
  2923. CHECK(dns_name_fromtext(dns_fixedname_name(&fixorigin),
  2924. &buffer, dns_rootname, 0, NULL));
  2925. origin = dns_fixedname_name(&fixorigin);
  2926. CHECK(ns_config_getclass(cfg_tuple_get(zconfig, "class"),
  2927. view->rdclass, &zclass));
  2928. if (zclass != view->rdclass) {
  2929. const char *vname = NULL;
  2930. if (vconfig != NULL)
  2931. vname = cfg_obj_asstring(cfg_tuple_get(vconfig,
  2932. "name"));
  2933. else
  2934. vname = "<default view>";
  2935. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  2936. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  2937. "zone '%s': wrong class for view '%s'",
  2938. zname, vname);
  2939. result = ISC_R_FAILURE;
  2940. goto cleanup;
  2941. }
  2942. (void)cfg_map_get(zoptions, "type", &typeobj);
  2943. if (typeobj == NULL) {
  2944. cfg_obj_log(zconfig, ns_g_lctx, ISC_LOG_ERROR,
  2945. "zone '%s' 'type' not specified", zname);
  2946. return (ISC_R_FAILURE);
  2947. }
  2948. ztypestr = cfg_obj_asstring(typeobj);
  2949. /*
  2950. * "hints zones" aren't zones. If we've got one,
  2951. * configure it and return.
  2952. */
  2953. if (strcasecmp(ztypestr, "hint") == 0) {
  2954. const cfg_obj_t *fileobj = NULL;
  2955. if (cfg_map_get(zoptions, "file", &fileobj) != ISC_R_SUCCESS) {
  2956. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  2957. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  2958. "zone '%s': 'file' not specified",
  2959. zname);
  2960. result = ISC_R_FAILURE;
  2961. goto cleanup;
  2962. }
  2963. if (dns_name_equal(origin, dns_rootname)) {
  2964. const char *hintsfile = cfg_obj_asstring(fileobj);
  2965. result = configure_hints(view, hintsfile);
  2966. if (result != ISC_R_SUCCESS) {
  2967. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  2968. NS_LOGMODULE_SERVER,
  2969. ISC_LOG_ERROR,
  2970. "could not configure root hints "
  2971. "from '%s': %s", hintsfile,
  2972. isc_result_totext(result));
  2973. goto cleanup;
  2974. }
  2975. /*
  2976. * Hint zones may also refer to delegation only points.
  2977. */
  2978. only = NULL;
  2979. tresult = cfg_map_get(zoptions, "delegation-only",
  2980. &only);
  2981. if (tresult == ISC_R_SUCCESS && cfg_obj_asboolean(only))
  2982. CHECK(dns_view_adddelegationonly(view, origin));
  2983. } else {
  2984. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  2985. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  2986. "ignoring non-root hint zone '%s'",
  2987. zname);
  2988. result = ISC_R_SUCCESS;
  2989. }
  2990. /* Skip ordinary zone processing. */
  2991. goto cleanup;
  2992. }
  2993. /*
  2994. * "forward zones" aren't zones either. Translate this syntax into
  2995. * the appropriate selective forwarding configuration and return.
  2996. */
  2997. if (strcasecmp(ztypestr, "forward") == 0) {
  2998. forwardtype = NULL;
  2999. forwarders = NULL;
  3000. (void)cfg_map_get(zoptions, "forward", &forwardtype);
  3001. (void)cfg_map_get(zoptions, "forwarders", &forwarders);
  3002. result = configure_forward(config, view, origin, forwarders,
  3003. forwardtype);
  3004. goto cleanup;
  3005. }
  3006. /*
  3007. * "delegation-only zones" aren't zones either.
  3008. */
  3009. if (strcasecmp(ztypestr, "delegation-only") == 0) {
  3010. result = dns_view_adddelegationonly(view, origin);
  3011. goto cleanup;
  3012. }
  3013. /*
  3014. * Check for duplicates in the new zone table.
  3015. */
  3016. result = dns_view_findzone(view, origin, &dupzone);
  3017. if (result == ISC_R_SUCCESS) {
  3018. /*
  3019. * We already have this zone!
  3020. */
  3021. cfg_obj_log(zconfig, ns_g_lctx, ISC_LOG_ERROR,
  3022. "zone '%s' already exists", zname);
  3023. dns_zone_detach(&dupzone);
  3024. result = ISC_R_EXISTS;
  3025. goto cleanup;
  3026. }
  3027. INSIST(dupzone == NULL);
  3028. /*
  3029. * See if we can reuse an existing zone. This is
  3030. * only possible if all of these are true:
  3031. * - The zone's view exists
  3032. * - A zone with the right name exists in the view
  3033. * - The zone is compatible with the config
  3034. * options (e.g., an existing master zone cannot
  3035. * be reused if the options specify a slave zone)
  3036. */
  3037. result = dns_viewlist_find(&ns_g_server->viewlist,
  3038. view->name, view->rdclass,
  3039. &pview);
  3040. if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
  3041. goto cleanup;
  3042. if (pview != NULL)
  3043. result = dns_view_findzone(pview, origin, &zone);
  3044. if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
  3045. goto cleanup;
  3046. if (zone != NULL && !ns_zone_reusable(zone, zconfig))
  3047. dns_zone_detach(&zone);
  3048. if (zone != NULL) {
  3049. /*
  3050. * We found a reusable zone. Make it use the
  3051. * new view.
  3052. */
  3053. dns_zone_setview(zone, view);
  3054. if (view->acache != NULL)
  3055. dns_zone_setacache(zone, view->acache);
  3056. } else {
  3057. /*
  3058. * We cannot reuse an existing zone, we have
  3059. * to create a new one.
  3060. */
  3061. CHECK(dns_zone_create(&zone, mctx));
  3062. CHECK(dns_zone_setorigin(zone, origin));
  3063. dns_zone_setview(zone, view);
  3064. if (view->acache != NULL)
  3065. dns_zone_setacache(zone, view->acache);
  3066. CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
  3067. dns_zone_setstats(zone, ns_g_server->zonestats);
  3068. }
  3069. /*
  3070. * If the zone contains a 'forwarders' statement, configure
  3071. * selective forwarding.
  3072. */
  3073. forwarders = NULL;
  3074. if (cfg_map_get(zoptions, "forwarders", &forwarders) == ISC_R_SUCCESS)
  3075. {
  3076. forwardtype = NULL;
  3077. (void)cfg_map_get(zoptions, "forward", &forwardtype);
  3078. CHECK(configure_forward(config, view, origin, forwarders,
  3079. forwardtype));
  3080. }
  3081. /*
  3082. * Stub and forward zones may also refer to delegation only points.
  3083. */
  3084. only = NULL;
  3085. if (cfg_map_get(zoptions, "delegation-only", &only) == ISC_R_SUCCESS)
  3086. {
  3087. if (cfg_obj_asboolean(only))
  3088. CHECK(dns_view_adddelegationonly(view, origin));
  3089. }
  3090. /*
  3091. * Mark whether the zone was originally added at runtime or not
  3092. */
  3093. dns_zone_setadded(zone, added);
  3094. /*
  3095. * Configure the zone.
  3096. */
  3097. CHECK(ns_zone_configure(config, vconfig, zconfig, aclconf, zone));
  3098. /*
  3099. * Add the zone to its view in the new view list.
  3100. */
  3101. CHECK(dns_view_addzone(view, zone));
  3102. /*
  3103. * Ensure that zone keys are reloaded on reconfig
  3104. */
  3105. if ((dns_zone_getkeyopts(zone) & DNS_ZONEKEY_MAINTAIN) != 0)
  3106. dns_zone_rekey(zone, ISC_FALSE);
  3107. cleanup:
  3108. if (zone != NULL)
  3109. dns_zone_detach(&zone);
  3110. if (pview != NULL)
  3111. dns_view_detach(&pview);
  3112. return (result);
  3113. }
  3114. /*
  3115. * Configure built-in zone for storing managed-key data.
  3116. */
  3117. #define KEYZONE "managed-keys.bind"
  3118. #define MKEYS ".mkeys"
  3119. static isc_result_t
  3120. add_keydata_zone(dns_view_t *view, const char *directory, isc_mem_t *mctx) {
  3121. isc_result_t result;
  3122. dns_view_t *pview = NULL;
  3123. dns_zone_t *zone = NULL;
  3124. dns_acl_t *none = NULL;
  3125. char filename[PATH_MAX];
  3126. char buffer[ISC_SHA256_DIGESTSTRINGLENGTH + sizeof(MKEYS)];
  3127. int n;
  3128. REQUIRE(view != NULL);
  3129. /* See if we can re-use an existing keydata zone. */
  3130. result = dns_viewlist_find(&ns_g_server->viewlist,
  3131. view->name, view->rdclass,
  3132. &pview);
  3133. if (result != ISC_R_NOTFOUND &&
  3134. result != ISC_R_SUCCESS)
  3135. return (result);
  3136. if (pview != NULL && pview->managed_keys != NULL) {
  3137. dns_zone_attach(pview->managed_keys, &view->managed_keys);
  3138. dns_zone_setview(pview->managed_keys, view);
  3139. dns_view_detach(&pview);
  3140. dns_zone_synckeyzone(view->managed_keys);
  3141. return (ISC_R_SUCCESS);
  3142. }
  3143. /* No existing keydata zone was found; create one */
  3144. CHECK(dns_zone_create(&zone, mctx));
  3145. CHECK(dns_zone_setorigin(zone, dns_rootname));
  3146. isc_sha256_data((void *)view->name, strlen(view->name), buffer);
  3147. strcat(buffer, MKEYS);
  3148. n = snprintf(filename, sizeof(filename), "%s%s%s",
  3149. directory ? directory : "", directory ? "/" : "",
  3150. strcmp(view->name, "_default") == 0 ? KEYZONE : buffer);
  3151. if (n < 0 || (size_t)n >= sizeof(filename)) {
  3152. result = (n < 0) ? ISC_R_FAILURE : ISC_R_NOSPACE;
  3153. goto cleanup;
  3154. }
  3155. CHECK(dns_zone_setfile(zone, filename));
  3156. dns_zone_setview(zone, view);
  3157. dns_zone_settype(zone, dns_zone_key);
  3158. dns_zone_setclass(zone, view->rdclass);
  3159. CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
  3160. if (view->acache != NULL)
  3161. dns_zone_setacache(zone, view->acache);
  3162. CHECK(dns_acl_none(mctx, &none));
  3163. dns_zone_setqueryacl(zone, none);
  3164. dns_zone_setqueryonacl(zone, none);
  3165. dns_acl_detach(&none);
  3166. dns_zone_setdialup(zone, dns_dialuptype_no);
  3167. dns_zone_setnotifytype(zone, dns_notifytype_no);
  3168. dns_zone_setoption(zone, DNS_ZONEOPT_NOCHECKNS, ISC_TRUE);
  3169. dns_zone_setjournalsize(zone, 0);
  3170. dns_zone_setstats(zone, ns_g_server->zonestats);
  3171. CHECK(setquerystats(zone, mctx, ISC_FALSE));
  3172. if (view->managed_keys != NULL)
  3173. dns_zone_detach(&view->managed_keys);
  3174. dns_zone_attach(zone, &view->managed_keys);
  3175. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3176. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  3177. "set up managed keys zone for view %s, file '%s'",
  3178. view->name, filename);
  3179. cleanup:
  3180. if (zone != NULL)
  3181. dns_zone_detach(&zone);
  3182. if (none != NULL)
  3183. dns_acl_detach(&none);
  3184. return (result);
  3185. }
  3186. /*
  3187. * Configure a single server quota.
  3188. */
  3189. static void
  3190. configure_server_quota(const cfg_obj_t **maps, const char *name,
  3191. isc_quota_t *quota)
  3192. {
  3193. const cfg_obj_t *obj = NULL;
  3194. isc_result_t result;
  3195. result = ns_config_get(maps, name, &obj);
  3196. INSIST(result == ISC_R_SUCCESS);
  3197. isc_quota_max(quota, cfg_obj_asuint32(obj));
  3198. }
  3199. /*
  3200. * This function is called as soon as the 'directory' statement has been
  3201. * parsed. This can be extended to support other options if necessary.
  3202. */
  3203. static isc_result_t
  3204. directory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
  3205. isc_result_t result;
  3206. const char *directory;
  3207. REQUIRE(strcasecmp("directory", clausename) == 0);
  3208. UNUSED(arg);
  3209. UNUSED(clausename);
  3210. /*
  3211. * Change directory.
  3212. */
  3213. directory = cfg_obj_asstring(obj);
  3214. if (! isc_file_ischdiridempotent(directory))
  3215. cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
  3216. "option 'directory' contains relative path '%s'",
  3217. directory);
  3218. result = isc_dir_chdir(directory);
  3219. if (result != ISC_R_SUCCESS) {
  3220. cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
  3221. "change directory to '%s' failed: %s",
  3222. directory, isc_result_totext(result));
  3223. return (result);
  3224. }
  3225. return (ISC_R_SUCCESS);
  3226. }
  3227. static void
  3228. scan_interfaces(ns_server_t *server, isc_boolean_t verbose) {
  3229. isc_boolean_t match_mapped = server->aclenv.match_mapped;
  3230. ns_interfacemgr_scan(server->interfacemgr, verbose);
  3231. /*
  3232. * Update the "localhost" and "localnets" ACLs to match the
  3233. * current set of network interfaces.
  3234. */
  3235. dns_aclenv_copy(&server->aclenv,
  3236. ns_interfacemgr_getaclenv(server->interfacemgr));
  3237. server->aclenv.match_mapped = match_mapped;
  3238. }
  3239. static isc_result_t
  3240. add_listenelt(isc_mem_t *mctx, ns_listenlist_t *list, isc_sockaddr_t *addr,
  3241. isc_boolean_t wcardport_ok)
  3242. {
  3243. ns_listenelt_t *lelt = NULL;
  3244. dns_acl_t *src_acl = NULL;
  3245. isc_result_t result;
  3246. isc_sockaddr_t any_sa6;
  3247. isc_netaddr_t netaddr;
  3248. REQUIRE(isc_sockaddr_pf(addr) == AF_INET6);
  3249. isc_sockaddr_any6(&any_sa6);
  3250. if (!isc_sockaddr_equal(&any_sa6, addr) &&
  3251. (wcardport_ok || isc_sockaddr_getport(addr) != 0)) {
  3252. isc_netaddr_fromin6(&netaddr, &addr->type.sin6.sin6_addr);
  3253. result = dns_acl_create(mctx, 0, &src_acl);
  3254. if (result != ISC_R_SUCCESS)
  3255. return (result);
  3256. result = dns_iptable_addprefix(src_acl->iptable,
  3257. &netaddr, 128, ISC_TRUE);
  3258. if (result != ISC_R_SUCCESS)
  3259. goto clean;
  3260. result = ns_listenelt_create(mctx, isc_sockaddr_getport(addr),
  3261. src_acl, &lelt);
  3262. if (result != ISC_R_SUCCESS)
  3263. goto clean;
  3264. ISC_LIST_APPEND(list->elts, lelt, link);
  3265. }
  3266. return (ISC_R_SUCCESS);
  3267. clean:
  3268. INSIST(lelt == NULL);
  3269. dns_acl_detach(&src_acl);
  3270. return (result);
  3271. }
  3272. /*
  3273. * Make a list of xxx-source addresses and call ns_interfacemgr_adjust()
  3274. * to update the listening interfaces accordingly.
  3275. * We currently only consider IPv6, because this only affects IPv6 wildcard
  3276. * sockets.
  3277. */
  3278. static void
  3279. adjust_interfaces(ns_server_t *server, isc_mem_t *mctx) {
  3280. isc_result_t result;
  3281. ns_listenlist_t *list = NULL;
  3282. dns_view_t *view;
  3283. dns_zone_t *zone, *next;
  3284. isc_sockaddr_t addr, *addrp;
  3285. result = ns_listenlist_create(mctx, &list);
  3286. if (result != ISC_R_SUCCESS)
  3287. return;
  3288. for (view = ISC_LIST_HEAD(server->viewlist);
  3289. view != NULL;
  3290. view = ISC_LIST_NEXT(view, link)) {
  3291. dns_dispatch_t *dispatch6;
  3292. dispatch6 = dns_resolver_dispatchv6(view->resolver);
  3293. if (dispatch6 == NULL)
  3294. continue;
  3295. result = dns_dispatch_getlocaladdress(dispatch6, &addr);
  3296. if (result != ISC_R_SUCCESS)
  3297. goto fail;
  3298. /*
  3299. * We always add non-wildcard address regardless of whether
  3300. * the port is 'any' (the fourth arg is TRUE): if the port is
  3301. * specific, we need to add it since it may conflict with a
  3302. * listening interface; if it's zero, we'll dynamically open
  3303. * query ports, and some of them may override an existing
  3304. * wildcard IPv6 port.
  3305. */
  3306. result = add_listenelt(mctx, list, &addr, ISC_TRUE);
  3307. if (result != ISC_R_SUCCESS)
  3308. goto fail;
  3309. }
  3310. zone = NULL;
  3311. for (result = dns_zone_first(server->zonemgr, &zone);
  3312. result == ISC_R_SUCCESS;
  3313. next = NULL, result = dns_zone_next(zone, &next), zone = next) {
  3314. dns_view_t *zoneview;
  3315. /*
  3316. * At this point the zone list may contain a stale zone
  3317. * just removed from the configuration. To see the validity,
  3318. * check if the corresponding view is in our current view list.
  3319. * There may also be old zones that are still in the process
  3320. * of shutting down and have detached from their old view
  3321. * (zoneview == NULL).
  3322. */
  3323. zoneview = dns_zone_getview(zone);
  3324. if (zoneview == NULL)
  3325. continue;
  3326. for (view = ISC_LIST_HEAD(server->viewlist);
  3327. view != NULL && view != zoneview;
  3328. view = ISC_LIST_NEXT(view, link))
  3329. ;
  3330. if (view == NULL)
  3331. continue;
  3332. addrp = dns_zone_getnotifysrc6(zone);
  3333. result = add_listenelt(mctx, list, addrp, ISC_FALSE);
  3334. if (result != ISC_R_SUCCESS)
  3335. goto fail;
  3336. addrp = dns_zone_getxfrsource6(zone);
  3337. result = add_listenelt(mctx, list, addrp, ISC_FALSE);
  3338. if (result != ISC_R_SUCCESS)
  3339. goto fail;
  3340. }
  3341. ns_interfacemgr_adjust(server->interfacemgr, list, ISC_TRUE);
  3342. clean:
  3343. ns_listenlist_detach(&list);
  3344. return;
  3345. fail:
  3346. /*
  3347. * Even when we failed the procedure, most of other interfaces
  3348. * should work correctly. We therefore just warn it.
  3349. */
  3350. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3351. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  3352. "could not adjust the listen-on list; "
  3353. "some interfaces may not work");
  3354. goto clean;
  3355. }
  3356. /*
  3357. * This event callback is invoked to do periodic network
  3358. * interface scanning.
  3359. */
  3360. static void
  3361. interface_timer_tick(isc_task_t *task, isc_event_t *event) {
  3362. isc_result_t result;
  3363. ns_server_t *server = (ns_server_t *) event->ev_arg;
  3364. INSIST(task == server->task);
  3365. UNUSED(task);
  3366. isc_event_free(&event);
  3367. /*
  3368. * XXX should scan interfaces unlocked and get exclusive access
  3369. * only to replace ACLs.
  3370. */
  3371. result = isc_task_beginexclusive(server->task);
  3372. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  3373. scan_interfaces(server, ISC_FALSE);
  3374. isc_task_endexclusive(server->task);
  3375. }
  3376. static void
  3377. heartbeat_timer_tick(isc_task_t *task, isc_event_t *event) {
  3378. ns_server_t *server = (ns_server_t *) event->ev_arg;
  3379. dns_view_t *view;
  3380. UNUSED(task);
  3381. isc_event_free(&event);
  3382. view = ISC_LIST_HEAD(server->viewlist);
  3383. while (view != NULL) {
  3384. dns_view_dialup(view);
  3385. view = ISC_LIST_NEXT(view, link);
  3386. }
  3387. }
  3388. static void
  3389. pps_timer_tick(isc_task_t *task, isc_event_t *event) {
  3390. static unsigned int oldrequests = 0;
  3391. unsigned int requests = ns_client_requests;
  3392. UNUSED(task);
  3393. isc_event_free(&event);
  3394. /*
  3395. * Don't worry about wrapping as the overflow result will be right.
  3396. */
  3397. dns_pps = (requests - oldrequests) / 1200;
  3398. oldrequests = requests;
  3399. }
  3400. /*
  3401. * Replace the current value of '*field', a dynamically allocated
  3402. * string or NULL, with a dynamically allocated copy of the
  3403. * null-terminated string pointed to by 'value', or NULL.
  3404. */
  3405. static isc_result_t
  3406. setstring(ns_server_t *server, char **field, const char *value) {
  3407. char *copy;
  3408. if (value != NULL) {
  3409. copy = isc_mem_strdup(server->mctx, value);
  3410. if (copy == NULL)
  3411. return (ISC_R_NOMEMORY);
  3412. } else {
  3413. copy = NULL;
  3414. }
  3415. if (*field != NULL)
  3416. isc_mem_free(server->mctx, *field);
  3417. *field = copy;
  3418. return (ISC_R_SUCCESS);
  3419. }
  3420. /*
  3421. * Replace the current value of '*field', a dynamically allocated
  3422. * string or NULL, with another dynamically allocated string
  3423. * or NULL if whether 'obj' is a string or void value, respectively.
  3424. */
  3425. static isc_result_t
  3426. setoptstring(ns_server_t *server, char **field, const cfg_obj_t *obj) {
  3427. if (cfg_obj_isvoid(obj))
  3428. return (setstring(server, field, NULL));
  3429. else
  3430. return (setstring(server, field, cfg_obj_asstring(obj)));
  3431. }
  3432. static void
  3433. set_limit(const cfg_obj_t **maps, const char *configname,
  3434. const char *description, isc_resource_t resourceid,
  3435. isc_resourcevalue_t defaultvalue)
  3436. {
  3437. const cfg_obj_t *obj = NULL;
  3438. const char *resource;
  3439. isc_resourcevalue_t value;
  3440. isc_result_t result;
  3441. if (ns_config_get(maps, configname, &obj) != ISC_R_SUCCESS)
  3442. return;
  3443. if (cfg_obj_isstring(obj)) {
  3444. resource = cfg_obj_asstring(obj);
  3445. if (strcasecmp(resource, "unlimited") == 0)
  3446. value = ISC_RESOURCE_UNLIMITED;
  3447. else {
  3448. INSIST(strcasecmp(resource, "default") == 0);
  3449. value = defaultvalue;
  3450. }
  3451. } else
  3452. value = cfg_obj_asuint64(obj);
  3453. result = isc_resource_setlimit(resourceid, value);
  3454. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  3455. result == ISC_R_SUCCESS ?
  3456. ISC_LOG_DEBUG(3) : ISC_LOG_WARNING,
  3457. "set maximum %s to %" ISC_PRINT_QUADFORMAT "u: %s",
  3458. description, value, isc_result_totext(result));
  3459. }
  3460. #define SETLIMIT(cfgvar, resource, description) \
  3461. set_limit(maps, cfgvar, description, isc_resource_ ## resource, \
  3462. ns_g_init ## resource)
  3463. static void
  3464. set_limits(const cfg_obj_t **maps) {
  3465. SETLIMIT("stacksize", stacksize, "stack size");
  3466. SETLIMIT("datasize", datasize, "data size");
  3467. SETLIMIT("coresize", coresize, "core size");
  3468. SETLIMIT("files", openfiles, "open files");
  3469. }
  3470. static void
  3471. portset_fromconf(isc_portset_t *portset, const cfg_obj_t *ports,
  3472. isc_boolean_t positive)
  3473. {
  3474. const cfg_listelt_t *element;
  3475. for (element = cfg_list_first(ports);
  3476. element != NULL;
  3477. element = cfg_list_next(element)) {
  3478. const cfg_obj_t *obj = cfg_listelt_value(element);
  3479. if (cfg_obj_isuint32(obj)) {
  3480. in_port_t port = (in_port_t)cfg_obj_asuint32(obj);
  3481. if (positive)
  3482. isc_portset_add(portset, port);
  3483. else
  3484. isc_portset_remove(portset, port);
  3485. } else {
  3486. const cfg_obj_t *obj_loport, *obj_hiport;
  3487. in_port_t loport, hiport;
  3488. obj_loport = cfg_tuple_get(obj, "loport");
  3489. loport = (in_port_t)cfg_obj_asuint32(obj_loport);
  3490. obj_hiport = cfg_tuple_get(obj, "hiport");
  3491. hiport = (in_port_t)cfg_obj_asuint32(obj_hiport);
  3492. if (positive)
  3493. isc_portset_addrange(portset, loport, hiport);
  3494. else {
  3495. isc_portset_removerange(portset, loport,
  3496. hiport);
  3497. }
  3498. }
  3499. }
  3500. }
  3501. static isc_result_t
  3502. removed(dns_zone_t *zone, void *uap) {
  3503. const char *type;
  3504. if (dns_zone_getview(zone) != uap)
  3505. return (ISC_R_SUCCESS);
  3506. switch (dns_zone_gettype(zone)) {
  3507. case dns_zone_master:
  3508. type = "master";
  3509. break;
  3510. case dns_zone_slave:
  3511. type = "slave";
  3512. break;
  3513. case dns_zone_stub:
  3514. type = "stub";
  3515. break;
  3516. default:
  3517. type = "other";
  3518. break;
  3519. }
  3520. dns_zone_log(zone, ISC_LOG_INFO, "(%s) removed", type);
  3521. return (ISC_R_SUCCESS);
  3522. }
  3523. static void
  3524. cleanup_session_key(ns_server_t *server, isc_mem_t *mctx) {
  3525. if (server->session_keyfile != NULL) {
  3526. isc_file_remove(server->session_keyfile);
  3527. isc_mem_free(mctx, server->session_keyfile);
  3528. server->session_keyfile = NULL;
  3529. }
  3530. if (server->session_keyname != NULL) {
  3531. if (dns_name_dynamic(server->session_keyname))
  3532. dns_name_free(server->session_keyname, mctx);
  3533. isc_mem_put(mctx, server->session_keyname, sizeof(dns_name_t));
  3534. server->session_keyname = NULL;
  3535. }
  3536. if (server->sessionkey != NULL)
  3537. dns_tsigkey_detach(&server->sessionkey);
  3538. server->session_keyalg = DST_ALG_UNKNOWN;
  3539. server->session_keybits = 0;
  3540. }
  3541. static isc_result_t
  3542. generate_session_key(const char *filename, const char *keynamestr,
  3543. dns_name_t *keyname, const char *algstr,
  3544. dns_name_t *algname, unsigned int algtype,
  3545. isc_uint16_t bits, isc_mem_t *mctx,
  3546. dns_tsigkey_t **tsigkeyp)
  3547. {
  3548. isc_result_t result = ISC_R_SUCCESS;
  3549. dst_key_t *key = NULL;
  3550. isc_buffer_t key_txtbuffer;
  3551. isc_buffer_t key_rawbuffer;
  3552. char key_txtsecret[256];
  3553. char key_rawsecret[64];
  3554. isc_region_t key_rawregion;
  3555. isc_stdtime_t now;
  3556. dns_tsigkey_t *tsigkey = NULL;
  3557. FILE *fp = NULL;
  3558. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3559. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  3560. "generating session key for dynamic DNS");
  3561. /* generate key */
  3562. result = dst_key_generate(keyname, algtype, bits, 1, 0,
  3563. DNS_KEYPROTO_ANY, dns_rdataclass_in,
  3564. mctx, &key);
  3565. if (result != ISC_R_SUCCESS)
  3566. return (result);
  3567. /*
  3568. * Dump the key to the buffer for later use. Should be done before
  3569. * we transfer the ownership of key to tsigkey.
  3570. */
  3571. isc_buffer_init(&key_rawbuffer, &key_rawsecret, sizeof(key_rawsecret));
  3572. CHECK(dst_key_tobuffer(key, &key_rawbuffer));
  3573. isc_buffer_usedregion(&key_rawbuffer, &key_rawregion);
  3574. isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
  3575. CHECK(isc_base64_totext(&key_rawregion, -1, "", &key_txtbuffer));
  3576. /* Store the key in tsigkey. */
  3577. isc_stdtime_get(&now);
  3578. CHECK(dns_tsigkey_createfromkey(dst_key_name(key), algname, key,
  3579. ISC_FALSE, NULL, now, now, mctx, NULL,
  3580. &tsigkey));
  3581. /* Dump the key to the key file. */
  3582. fp = ns_os_openfile(filename, S_IRUSR|S_IWUSR, ISC_TRUE);
  3583. if (fp == NULL) {
  3584. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3585. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  3586. "could not create %s", filename);
  3587. result = ISC_R_NOPERM;
  3588. goto cleanup;
  3589. }
  3590. fprintf(fp, "key \"%s\" {\n"
  3591. "\talgorithm %s;\n"
  3592. "\tsecret \"%.*s\";\n};\n", keynamestr, algstr,
  3593. (int) isc_buffer_usedlength(&key_txtbuffer),
  3594. (char*) isc_buffer_base(&key_txtbuffer));
  3595. RUNTIME_CHECK(isc_stdio_flush(fp) == ISC_R_SUCCESS);
  3596. RUNTIME_CHECK(isc_stdio_close(fp) == ISC_R_SUCCESS);
  3597. dst_key_free(&key);
  3598. *tsigkeyp = tsigkey;
  3599. return (ISC_R_SUCCESS);
  3600. cleanup:
  3601. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3602. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  3603. "failed to generate session key "
  3604. "for dynamic DNS: %s", isc_result_totext(result));
  3605. if (tsigkey != NULL)
  3606. dns_tsigkey_detach(&tsigkey);
  3607. if (key != NULL)
  3608. dst_key_free(&key);
  3609. return (result);
  3610. }
  3611. static isc_result_t
  3612. configure_session_key(const cfg_obj_t **maps, ns_server_t *server,
  3613. isc_mem_t *mctx)
  3614. {
  3615. const char *keyfile, *keynamestr, *algstr;
  3616. unsigned int algtype;
  3617. dns_fixedname_t fname;
  3618. dns_name_t *keyname, *algname;
  3619. isc_buffer_t buffer;
  3620. isc_uint16_t bits;
  3621. const cfg_obj_t *obj;
  3622. isc_boolean_t need_deleteold = ISC_FALSE;
  3623. isc_boolean_t need_createnew = ISC_FALSE;
  3624. isc_result_t result;
  3625. obj = NULL;
  3626. result = ns_config_get(maps, "session-keyfile", &obj);
  3627. if (result == ISC_R_SUCCESS) {
  3628. if (cfg_obj_isvoid(obj))
  3629. keyfile = NULL; /* disable it */
  3630. else
  3631. keyfile = cfg_obj_asstring(obj);
  3632. } else
  3633. keyfile = ns_g_defaultsessionkeyfile;
  3634. obj = NULL;
  3635. result = ns_config_get(maps, "session-keyname", &obj);
  3636. INSIST(result == ISC_R_SUCCESS);
  3637. keynamestr = cfg_obj_asstring(obj);
  3638. dns_fixedname_init(&fname);
  3639. isc_buffer_init(&buffer, keynamestr, strlen(keynamestr));
  3640. isc_buffer_add(&buffer, strlen(keynamestr));
  3641. keyname = dns_fixedname_name(&fname);
  3642. result = dns_name_fromtext(keyname, &buffer, dns_rootname, 0, NULL);
  3643. if (result != ISC_R_SUCCESS)
  3644. return (result);
  3645. obj = NULL;
  3646. result = ns_config_get(maps, "session-keyalg", &obj);
  3647. INSIST(result == ISC_R_SUCCESS);
  3648. algstr = cfg_obj_asstring(obj);
  3649. algname = NULL;
  3650. result = ns_config_getkeyalgorithm2(algstr, &algname, &algtype, &bits);
  3651. if (result != ISC_R_SUCCESS) {
  3652. const char *s = " (keeping current key)";
  3653. cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR, "session-keyalg: "
  3654. "unsupported or unknown algorithm '%s'%s",
  3655. algstr,
  3656. server->session_keyfile != NULL ? s : "");
  3657. return (result);
  3658. }
  3659. /* See if we need to (re)generate a new key. */
  3660. if (keyfile == NULL) {
  3661. if (server->session_keyfile != NULL)
  3662. need_deleteold = ISC_TRUE;
  3663. } else if (server->session_keyfile == NULL)
  3664. need_createnew = ISC_TRUE;
  3665. else if (strcmp(keyfile, server->session_keyfile) != 0 ||
  3666. !dns_name_equal(server->session_keyname, keyname) ||
  3667. server->session_keyalg != algtype ||
  3668. server->session_keybits != bits) {
  3669. need_deleteold = ISC_TRUE;
  3670. need_createnew = ISC_TRUE;
  3671. }
  3672. if (need_deleteold) {
  3673. INSIST(server->session_keyfile != NULL);
  3674. INSIST(server->session_keyname != NULL);
  3675. INSIST(server->sessionkey != NULL);
  3676. cleanup_session_key(server, mctx);
  3677. }
  3678. if (need_createnew) {
  3679. INSIST(server->sessionkey == NULL);
  3680. INSIST(server->session_keyfile == NULL);
  3681. INSIST(server->session_keyname == NULL);
  3682. INSIST(server->session_keyalg == DST_ALG_UNKNOWN);
  3683. INSIST(server->session_keybits == 0);
  3684. server->session_keyname = isc_mem_get(mctx, sizeof(dns_name_t));
  3685. if (server->session_keyname == NULL)
  3686. goto cleanup;
  3687. dns_name_init(server->session_keyname, NULL);
  3688. CHECK(dns_name_dup(keyname, mctx, server->session_keyname));
  3689. server->session_keyfile = isc_mem_strdup(mctx, keyfile);
  3690. if (server->session_keyfile == NULL)
  3691. goto cleanup;
  3692. server->session_keyalg = algtype;
  3693. server->session_keybits = bits;
  3694. CHECK(generate_session_key(keyfile, keynamestr, keyname, algstr,
  3695. algname, algtype, bits, mctx,
  3696. &server->sessionkey));
  3697. }
  3698. return (result);
  3699. cleanup:
  3700. cleanup_session_key(server, mctx);
  3701. return (result);
  3702. }
  3703. static isc_result_t
  3704. setup_newzones(dns_view_t *view, cfg_obj_t *config, cfg_obj_t *vconfig,
  3705. cfg_parser_t *parser, cfg_aclconfctx_t *actx)
  3706. {
  3707. isc_result_t result = ISC_R_SUCCESS;
  3708. isc_boolean_t allow = ISC_FALSE;
  3709. struct cfg_context *nzcfg = NULL;
  3710. cfg_parser_t *nzparser = NULL;
  3711. cfg_obj_t *nzconfig = NULL;
  3712. const cfg_obj_t *maps[4];
  3713. const cfg_obj_t *options = NULL, *voptions = NULL;
  3714. const cfg_obj_t *nz = NULL;
  3715. int i = 0;
  3716. REQUIRE (config != NULL);
  3717. if (vconfig != NULL)
  3718. voptions = cfg_tuple_get(vconfig, "options");
  3719. if (voptions != NULL)
  3720. maps[i++] = voptions;
  3721. result = cfg_map_get(config, "options", &options);
  3722. if (result == ISC_R_SUCCESS)
  3723. maps[i++] = options;
  3724. maps[i++] = ns_g_defaults;
  3725. maps[i] = NULL;
  3726. result = ns_config_get(maps, "allow-new-zones", &nz);
  3727. if (result == ISC_R_SUCCESS)
  3728. allow = cfg_obj_asboolean(nz);
  3729. if (!allow) {
  3730. dns_view_setnewzones(view, ISC_FALSE, NULL, NULL);
  3731. return (ISC_R_SUCCESS);
  3732. }
  3733. nzcfg = isc_mem_get(view->mctx, sizeof(*nzcfg));
  3734. if (nzcfg == NULL) {
  3735. dns_view_setnewzones(view, ISC_FALSE, NULL, NULL);
  3736. return (ISC_R_NOMEMORY);
  3737. }
  3738. dns_view_setnewzones(view, allow, nzcfg, newzone_cfgctx_destroy);
  3739. memset(nzcfg, 0, sizeof(*nzcfg));
  3740. isc_mem_attach(view->mctx, &nzcfg->mctx);
  3741. cfg_obj_attach(config, &nzcfg->config);
  3742. cfg_parser_attach(parser, &nzcfg->parser);
  3743. cfg_aclconfctx_attach(actx, &nzcfg->actx);
  3744. /*
  3745. * Attempt to create a parser and parse the newzones
  3746. * file. If successful, preserve both; otherwise leave
  3747. * them NULL.
  3748. */
  3749. result = cfg_parser_create(view->mctx, ns_g_lctx, &nzparser);
  3750. if (result == ISC_R_SUCCESS)
  3751. result = cfg_parse_file(nzparser, view->new_zone_file,
  3752. &cfg_type_newzones, &nzconfig);
  3753. if (result == ISC_R_SUCCESS) {
  3754. cfg_parser_attach(nzparser, &nzcfg->nzparser);
  3755. cfg_obj_attach(nzconfig, &nzcfg->nzconfig);
  3756. }
  3757. if (nzparser != NULL) {
  3758. if (nzconfig != NULL)
  3759. cfg_obj_destroy(nzparser, &nzconfig);
  3760. cfg_parser_destroy(&nzparser);
  3761. }
  3762. return (ISC_R_SUCCESS);
  3763. }
  3764. static int
  3765. count_zones(const cfg_obj_t *conf) {
  3766. const cfg_obj_t *zonelist = NULL;
  3767. const cfg_listelt_t *element;
  3768. int n = 0;
  3769. REQUIRE(conf != NULL);
  3770. cfg_map_get(conf, "zone", &zonelist);
  3771. for (element = cfg_list_first(zonelist);
  3772. element != NULL;
  3773. element = cfg_list_next(element))
  3774. n++;
  3775. return (n);
  3776. }
  3777. static isc_result_t
  3778. load_configuration(const char *filename, ns_server_t *server,
  3779. isc_boolean_t first_time)
  3780. {
  3781. cfg_obj_t *config = NULL, *bindkeys = NULL;
  3782. cfg_parser_t *conf_parser = NULL, *bindkeys_parser = NULL;
  3783. const cfg_listelt_t *element;
  3784. const cfg_obj_t *builtin_views;
  3785. const cfg_obj_t *maps[3];
  3786. const cfg_obj_t *obj;
  3787. const cfg_obj_t *options;
  3788. const cfg_obj_t *usev4ports, *avoidv4ports, *usev6ports, *avoidv6ports;
  3789. const cfg_obj_t *views;
  3790. dns_view_t *view = NULL;
  3791. dns_view_t *view_next;
  3792. dns_viewlist_t tmpviewlist;
  3793. dns_viewlist_t viewlist, builtin_viewlist;
  3794. in_port_t listen_port, udpport_low, udpport_high;
  3795. int i;
  3796. isc_interval_t interval;
  3797. isc_portset_t *v4portset = NULL;
  3798. isc_portset_t *v6portset = NULL;
  3799. isc_resourcevalue_t nfiles;
  3800. isc_result_t result;
  3801. isc_uint32_t heartbeat_interval;
  3802. isc_uint32_t interface_interval;
  3803. isc_uint32_t reserved;
  3804. isc_uint32_t udpsize;
  3805. ns_cachelist_t cachelist, tmpcachelist;
  3806. unsigned int maxsocks;
  3807. ns_cache_t *nsc;
  3808. struct cfg_context *nzctx;
  3809. int num_zones = 0;
  3810. isc_boolean_t exclusive = ISC_FALSE;
  3811. ISC_LIST_INIT(viewlist);
  3812. ISC_LIST_INIT(builtin_viewlist);
  3813. ISC_LIST_INIT(cachelist);
  3814. /* Create the ACL configuration context */
  3815. if (ns_g_aclconfctx != NULL)
  3816. cfg_aclconfctx_detach(&ns_g_aclconfctx);
  3817. CHECK(cfg_aclconfctx_create(ns_g_mctx, &ns_g_aclconfctx));
  3818. /*
  3819. * Parse the global default pseudo-config file.
  3820. */
  3821. if (first_time) {
  3822. CHECK(ns_config_parsedefaults(ns_g_parser, &ns_g_config));
  3823. RUNTIME_CHECK(cfg_map_get(ns_g_config, "options",
  3824. &ns_g_defaults) == ISC_R_SUCCESS);
  3825. }
  3826. /*
  3827. * Parse the configuration file using the new config code.
  3828. */
  3829. result = ISC_R_FAILURE;
  3830. config = NULL;
  3831. /*
  3832. * Unless this is lwresd with the -C option, parse the config file.
  3833. */
  3834. if (!(ns_g_lwresdonly && lwresd_g_useresolvconf)) {
  3835. isc_log_write(ns_g_lctx,
  3836. NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  3837. ISC_LOG_INFO, "loading configuration from '%s'",
  3838. filename);
  3839. CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx, &conf_parser));
  3840. cfg_parser_setcallback(conf_parser, directory_callback, NULL);
  3841. result = cfg_parse_file(conf_parser, filename,
  3842. &cfg_type_namedconf, &config);
  3843. }
  3844. /*
  3845. * If this is lwresd with the -C option, or lwresd with no -C or -c
  3846. * option where the above parsing failed, parse resolv.conf.
  3847. */
  3848. if (ns_g_lwresdonly &&
  3849. (lwresd_g_useresolvconf ||
  3850. (!ns_g_conffileset && result == ISC_R_FILENOTFOUND)))
  3851. {
  3852. isc_log_write(ns_g_lctx,
  3853. NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  3854. ISC_LOG_INFO, "loading configuration from '%s'",
  3855. lwresd_g_resolvconffile);
  3856. if (conf_parser != NULL)
  3857. cfg_parser_destroy(&conf_parser);
  3858. CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx, &conf_parser));
  3859. result = ns_lwresd_parseeresolvconf(ns_g_mctx, conf_parser,
  3860. &config);
  3861. }
  3862. CHECK(result);
  3863. /*
  3864. * Check the validity of the configuration.
  3865. */
  3866. CHECK(bind9_check_namedconf(config, ns_g_lctx, ns_g_mctx));
  3867. /*
  3868. * Fill in the maps array, used for resolving defaults.
  3869. */
  3870. i = 0;
  3871. options = NULL;
  3872. result = cfg_map_get(config, "options", &options);
  3873. if (result == ISC_R_SUCCESS)
  3874. maps[i++] = options;
  3875. maps[i++] = ns_g_defaults;
  3876. maps[i] = NULL;
  3877. /*
  3878. * If bind.keys exists, load it. If "dnssec-lookaside auto"
  3879. * is turned on, the keys found there will be used as default
  3880. * trust anchors.
  3881. */
  3882. obj = NULL;
  3883. result = ns_config_get(maps, "bindkeys-file", &obj);
  3884. INSIST(result == ISC_R_SUCCESS);
  3885. CHECKM(setstring(server, &server->bindkeysfile,
  3886. cfg_obj_asstring(obj)), "strdup");
  3887. if (access(server->bindkeysfile, R_OK) == 0) {
  3888. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3889. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  3890. "reading built-in trusted "
  3891. "keys from file '%s'", server->bindkeysfile);
  3892. CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx,
  3893. &bindkeys_parser));
  3894. result = cfg_parse_file(bindkeys_parser, server->bindkeysfile,
  3895. &cfg_type_bindkeys, &bindkeys);
  3896. CHECK(result);
  3897. }
  3898. /* Ensure exclusive access to configuration data. */
  3899. if (!exclusive) {
  3900. result = isc_task_beginexclusive(server->task);
  3901. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  3902. exclusive = ISC_TRUE;
  3903. }
  3904. /*
  3905. * Set process limits, which (usually) needs to be done as root.
  3906. */
  3907. set_limits(maps);
  3908. /*
  3909. * Check if max number of open sockets that the system allows is
  3910. * sufficiently large. Failing this condition is not necessarily fatal,
  3911. * but may cause subsequent runtime failures for a busy recursive
  3912. * server.
  3913. */
  3914. result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &maxsocks);
  3915. if (result != ISC_R_SUCCESS)
  3916. maxsocks = 0;
  3917. result = isc_resource_getcurlimit(isc_resource_openfiles, &nfiles);
  3918. if (result == ISC_R_SUCCESS && (isc_resourcevalue_t)maxsocks > nfiles) {
  3919. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3920. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  3921. "max open files (%" ISC_PRINT_QUADFORMAT "u)"
  3922. " is smaller than max sockets (%u)",
  3923. nfiles, maxsocks);
  3924. }
  3925. /*
  3926. * Set the number of socket reserved for TCP, stdio etc.
  3927. */
  3928. obj = NULL;
  3929. result = ns_config_get(maps, "reserved-sockets", &obj);
  3930. INSIST(result == ISC_R_SUCCESS);
  3931. reserved = cfg_obj_asuint32(obj);
  3932. if (maxsocks != 0) {
  3933. if (maxsocks < 128U) /* Prevent underflow. */
  3934. reserved = 0;
  3935. else if (reserved > maxsocks - 128U) /* Minimum UDP space. */
  3936. reserved = maxsocks - 128;
  3937. }
  3938. /* Minimum TCP/stdio space. */
  3939. if (reserved < 128U)
  3940. reserved = 128;
  3941. if (reserved + 128U > maxsocks && maxsocks != 0) {
  3942. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3943. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  3944. "less than 128 UDP sockets available after "
  3945. "applying 'reserved-sockets' and 'maxsockets'");
  3946. }
  3947. isc__socketmgr_setreserved(ns_g_socketmgr, reserved);
  3948. /*
  3949. * Configure various server options.
  3950. */
  3951. configure_server_quota(maps, "transfers-out", &server->xfroutquota);
  3952. configure_server_quota(maps, "tcp-clients", &server->tcpquota);
  3953. configure_server_quota(maps, "recursive-clients",
  3954. &server->recursionquota);
  3955. if (server->recursionquota.max > 1000)
  3956. isc_quota_soft(&server->recursionquota,
  3957. server->recursionquota.max - 100);
  3958. else
  3959. isc_quota_soft(&server->recursionquota, 0);
  3960. CHECK(configure_view_acl(NULL, config, "blackhole", NULL,
  3961. ns_g_aclconfctx, ns_g_mctx,
  3962. &server->blackholeacl));
  3963. if (server->blackholeacl != NULL)
  3964. dns_dispatchmgr_setblackhole(ns_g_dispatchmgr,
  3965. server->blackholeacl);
  3966. obj = NULL;
  3967. result = ns_config_get(maps, "match-mapped-addresses", &obj);
  3968. INSIST(result == ISC_R_SUCCESS);
  3969. server->aclenv.match_mapped = cfg_obj_asboolean(obj);
  3970. CHECKM(ns_statschannels_configure(ns_g_server, config, ns_g_aclconfctx),
  3971. "configuring statistics server(s)");
  3972. /*
  3973. * Configure sets of UDP query source ports.
  3974. */
  3975. CHECKM(isc_portset_create(ns_g_mctx, &v4portset),
  3976. "creating UDP port set");
  3977. CHECKM(isc_portset_create(ns_g_mctx, &v6portset),
  3978. "creating UDP port set");
  3979. usev4ports = NULL;
  3980. usev6ports = NULL;
  3981. avoidv4ports = NULL;
  3982. avoidv6ports = NULL;
  3983. (void)ns_config_get(maps, "use-v4-udp-ports", &usev4ports);
  3984. if (usev4ports != NULL)
  3985. portset_fromconf(v4portset, usev4ports, ISC_TRUE);
  3986. else {
  3987. CHECKM(isc_net_getudpportrange(AF_INET, &udpport_low,
  3988. &udpport_high),
  3989. "get the default UDP/IPv4 port range");
  3990. if (udpport_low == udpport_high)
  3991. isc_portset_add(v4portset, udpport_low);
  3992. else {
  3993. isc_portset_addrange(v4portset, udpport_low,
  3994. udpport_high);
  3995. }
  3996. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  3997. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  3998. "using default UDP/IPv4 port range: [%d, %d]",
  3999. udpport_low, udpport_high);
  4000. }
  4001. (void)ns_config_get(maps, "avoid-v4-udp-ports", &avoidv4ports);
  4002. if (avoidv4ports != NULL)
  4003. portset_fromconf(v4portset, avoidv4ports, ISC_FALSE);
  4004. (void)ns_config_get(maps, "use-v6-udp-ports", &usev6ports);
  4005. if (usev6ports != NULL)
  4006. portset_fromconf(v6portset, usev6ports, ISC_TRUE);
  4007. else {
  4008. CHECKM(isc_net_getudpportrange(AF_INET6, &udpport_low,
  4009. &udpport_high),
  4010. "get the default UDP/IPv6 port range");
  4011. if (udpport_low == udpport_high)
  4012. isc_portset_add(v6portset, udpport_low);
  4013. else {
  4014. isc_portset_addrange(v6portset, udpport_low,
  4015. udpport_high);
  4016. }
  4017. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  4018. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  4019. "using default UDP/IPv6 port range: [%d, %d]",
  4020. udpport_low, udpport_high);
  4021. }
  4022. (void)ns_config_get(maps, "avoid-v6-udp-ports", &avoidv6ports);
  4023. if (avoidv6ports != NULL)
  4024. portset_fromconf(v6portset, avoidv6ports, ISC_FALSE);
  4025. dns_dispatchmgr_setavailports(ns_g_dispatchmgr, v4portset, v6portset);
  4026. /*
  4027. * Set the EDNS UDP size when we don't match a view.
  4028. */
  4029. obj = NULL;
  4030. result = ns_config_get(maps, "edns-udp-size", &obj);
  4031. INSIST(result == ISC_R_SUCCESS);
  4032. udpsize = cfg_obj_asuint32(obj);
  4033. if (udpsize < 512)
  4034. udpsize = 512;
  4035. if (udpsize > 4096)
  4036. udpsize = 4096;
  4037. ns_g_udpsize = (isc_uint16_t)udpsize;
  4038. /*
  4039. * Configure the zone manager.
  4040. */
  4041. obj = NULL;
  4042. result = ns_config_get(maps, "transfers-in", &obj);
  4043. INSIST(result == ISC_R_SUCCESS);
  4044. dns_zonemgr_settransfersin(server->zonemgr, cfg_obj_asuint32(obj));
  4045. obj = NULL;
  4046. result = ns_config_get(maps, "transfers-per-ns", &obj);
  4047. INSIST(result == ISC_R_SUCCESS);
  4048. dns_zonemgr_settransfersperns(server->zonemgr, cfg_obj_asuint32(obj));
  4049. obj = NULL;
  4050. result = ns_config_get(maps, "serial-query-rate", &obj);
  4051. INSIST(result == ISC_R_SUCCESS);
  4052. dns_zonemgr_setserialqueryrate(server->zonemgr, cfg_obj_asuint32(obj));
  4053. /*
  4054. * Determine which port to use for listening for incoming connections.
  4055. */
  4056. if (ns_g_port != 0)
  4057. listen_port = ns_g_port;
  4058. else
  4059. CHECKM(ns_config_getport(config, &listen_port), "port");
  4060. /*
  4061. * Find the listen queue depth.
  4062. */
  4063. obj = NULL;
  4064. result = ns_config_get(maps, "tcp-listen-queue", &obj);
  4065. INSIST(result == ISC_R_SUCCESS);
  4066. ns_g_listen = cfg_obj_asuint32(obj);
  4067. if (ns_g_listen < 3)
  4068. ns_g_listen = 3;
  4069. /*
  4070. * Configure the interface manager according to the "listen-on"
  4071. * statement.
  4072. */
  4073. {
  4074. const cfg_obj_t *clistenon = NULL;
  4075. ns_listenlist_t *listenon = NULL;
  4076. clistenon = NULL;
  4077. /*
  4078. * Even though listen-on is present in the default
  4079. * configuration, we can't use it here, since it isn't
  4080. * used if we're in lwresd mode. This way is easier.
  4081. */
  4082. if (options != NULL)
  4083. (void)cfg_map_get(options, "listen-on", &clistenon);
  4084. if (clistenon != NULL) {
  4085. /* check return code? */
  4086. (void)ns_listenlist_fromconfig(clistenon, config,
  4087. ns_g_aclconfctx,
  4088. ns_g_mctx, &listenon);
  4089. } else if (!ns_g_lwresdonly) {
  4090. /*
  4091. * Not specified, use default.
  4092. */
  4093. CHECK(ns_listenlist_default(ns_g_mctx, listen_port,
  4094. ISC_TRUE, &listenon));
  4095. }
  4096. if (listenon != NULL) {
  4097. ns_interfacemgr_setlistenon4(server->interfacemgr,
  4098. listenon);
  4099. ns_listenlist_detach(&listenon);
  4100. }
  4101. }
  4102. /*
  4103. * Ditto for IPv6.
  4104. */
  4105. {
  4106. const cfg_obj_t *clistenon = NULL;
  4107. ns_listenlist_t *listenon = NULL;
  4108. if (options != NULL)
  4109. (void)cfg_map_get(options, "listen-on-v6", &clistenon);
  4110. if (clistenon != NULL) {
  4111. /* check return code? */
  4112. (void)ns_listenlist_fromconfig(clistenon, config,
  4113. ns_g_aclconfctx,
  4114. ns_g_mctx, &listenon);
  4115. } else if (!ns_g_lwresdonly) {
  4116. isc_boolean_t enable;
  4117. /*
  4118. * Not specified, use default.
  4119. */
  4120. enable = ISC_TF(isc_net_probeipv4() != ISC_R_SUCCESS);
  4121. CHECK(ns_listenlist_default(ns_g_mctx, listen_port,
  4122. enable, &listenon));
  4123. }
  4124. if (listenon != NULL) {
  4125. ns_interfacemgr_setlistenon6(server->interfacemgr,
  4126. listenon);
  4127. ns_listenlist_detach(&listenon);
  4128. }
  4129. }
  4130. /*
  4131. * Rescan the interface list to pick up changes in the
  4132. * listen-on option. It's important that we do this before we try
  4133. * to configure the query source, since the dispatcher we use might
  4134. * be shared with an interface.
  4135. */
  4136. scan_interfaces(server, ISC_TRUE);
  4137. /*
  4138. * Arrange for further interface scanning to occur periodically
  4139. * as specified by the "interface-interval" option.
  4140. */
  4141. obj = NULL;
  4142. result = ns_config_get(maps, "interface-interval", &obj);
  4143. INSIST(result == ISC_R_SUCCESS);
  4144. interface_interval = cfg_obj_asuint32(obj) * 60;
  4145. if (interface_interval == 0) {
  4146. CHECK(isc_timer_reset(server->interface_timer,
  4147. isc_timertype_inactive,
  4148. NULL, NULL, ISC_TRUE));
  4149. } else if (server->interface_interval != interface_interval) {
  4150. isc_interval_set(&interval, interface_interval, 0);
  4151. CHECK(isc_timer_reset(server->interface_timer,
  4152. isc_timertype_ticker,
  4153. NULL, &interval, ISC_FALSE));
  4154. }
  4155. server->interface_interval = interface_interval;
  4156. /*
  4157. * Configure the dialup heartbeat timer.
  4158. */
  4159. obj = NULL;
  4160. result = ns_config_get(maps, "heartbeat-interval", &obj);
  4161. INSIST(result == ISC_R_SUCCESS);
  4162. heartbeat_interval = cfg_obj_asuint32(obj) * 60;
  4163. if (heartbeat_interval == 0) {
  4164. CHECK(isc_timer_reset(server->heartbeat_timer,
  4165. isc_timertype_inactive,
  4166. NULL, NULL, ISC_TRUE));
  4167. } else if (server->heartbeat_interval != heartbeat_interval) {
  4168. isc_interval_set(&interval, heartbeat_interval, 0);
  4169. CHECK(isc_timer_reset(server->heartbeat_timer,
  4170. isc_timertype_ticker,
  4171. NULL, &interval, ISC_FALSE));
  4172. }
  4173. server->heartbeat_interval = heartbeat_interval;
  4174. isc_interval_set(&interval, 1200, 0);
  4175. CHECK(isc_timer_reset(server->pps_timer, isc_timertype_ticker, NULL,
  4176. &interval, ISC_FALSE));
  4177. /*
  4178. * Write the PID file.
  4179. */
  4180. obj = NULL;
  4181. if (ns_config_get(maps, "pid-file", &obj) == ISC_R_SUCCESS)
  4182. if (cfg_obj_isvoid(obj))
  4183. ns_os_writepidfile(NULL, first_time);
  4184. else
  4185. ns_os_writepidfile(cfg_obj_asstring(obj), first_time);
  4186. else if (ns_g_lwresdonly)
  4187. ns_os_writepidfile(lwresd_g_defaultpidfile, first_time);
  4188. else
  4189. ns_os_writepidfile(ns_g_defaultpidfile, first_time);
  4190. /*
  4191. * Configure the server-wide session key. This must be done before
  4192. * configure views because zone configuration may need to know
  4193. * session-keyname.
  4194. *
  4195. * Failure of session key generation isn't fatal at this time; if it
  4196. * turns out that a session key is really needed but doesn't exist,
  4197. * we'll treat it as a fatal error then.
  4198. */
  4199. (void)configure_session_key(maps, server, ns_g_mctx);
  4200. views = NULL;
  4201. (void)cfg_map_get(config, "view", &views);
  4202. /*
  4203. * Create the views and count all the configured zones in
  4204. * order to correctly size the zone manager's task table.
  4205. * (We only count zones for configured views; the built-in
  4206. * "bind" view can be ignored as it only adds a negligible
  4207. * number of zones.)
  4208. *
  4209. * If we're allowing new zones, we need to be able to find the
  4210. * new zone file and count those as well. So we setup the new
  4211. * zone configuration context, but otherwise view configuration
  4212. * waits until after the zone manager's task list has been sized.
  4213. */
  4214. for (element = cfg_list_first(views);
  4215. element != NULL;
  4216. element = cfg_list_next(element))
  4217. {
  4218. cfg_obj_t *vconfig = cfg_listelt_value(element);
  4219. const cfg_obj_t *voptions = cfg_tuple_get(vconfig, "options");
  4220. view = NULL;
  4221. CHECK(create_view(vconfig, &viewlist, &view));
  4222. INSIST(view != NULL);
  4223. num_zones += count_zones(voptions);
  4224. CHECK(setup_newzones(view, config, vconfig, conf_parser,
  4225. ns_g_aclconfctx));
  4226. nzctx = view->new_zone_config;
  4227. if (nzctx != NULL && nzctx->nzconfig != NULL)
  4228. num_zones += count_zones(nzctx->nzconfig);
  4229. dns_view_detach(&view);
  4230. }
  4231. /*
  4232. * If there were no explicit views then we do the default
  4233. * view here.
  4234. */
  4235. if (views == NULL) {
  4236. CHECK(create_view(NULL, &viewlist, &view));
  4237. INSIST(view != NULL);
  4238. num_zones = count_zones(config);
  4239. CHECK(setup_newzones(view, config, NULL, conf_parser,
  4240. ns_g_aclconfctx));
  4241. nzctx = view->new_zone_config;
  4242. if (nzctx != NULL && nzctx->nzconfig != NULL)
  4243. num_zones += count_zones(nzctx->nzconfig);
  4244. dns_view_detach(&view);
  4245. }
  4246. /*
  4247. * Zones have been counted; set the zone manager task pool size.
  4248. */
  4249. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  4250. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  4251. "sizing zone task pool based on %d zones", num_zones);
  4252. CHECK(dns_zonemgr_setsize(ns_g_server->zonemgr, num_zones));
  4253. /*
  4254. * Configure and freeze all explicit views. Explicit
  4255. * views that have zones were already created at parsing
  4256. * time, but views with no zones must be created here.
  4257. */
  4258. for (element = cfg_list_first(views);
  4259. element != NULL;
  4260. element = cfg_list_next(element))
  4261. {
  4262. cfg_obj_t *vconfig = cfg_listelt_value(element);
  4263. view = NULL;
  4264. CHECK(find_view(vconfig, &viewlist, &view));
  4265. CHECK(configure_view(view, config, vconfig,
  4266. &cachelist, bindkeys, ns_g_mctx,
  4267. ns_g_aclconfctx, ISC_TRUE));
  4268. dns_view_freeze(view);
  4269. dns_view_detach(&view);
  4270. }
  4271. /*
  4272. * Make sure we have a default view if and only if there
  4273. * were no explicit views.
  4274. */
  4275. if (views == NULL) {
  4276. view = NULL;
  4277. CHECK(find_view(NULL, &viewlist, &view));
  4278. CHECK(configure_view(view, config, NULL,
  4279. &cachelist, bindkeys,
  4280. ns_g_mctx, ns_g_aclconfctx, ISC_TRUE));
  4281. dns_view_freeze(view);
  4282. dns_view_detach(&view);
  4283. }
  4284. /*
  4285. * Create (or recreate) the built-in views.
  4286. */
  4287. builtin_views = NULL;
  4288. RUNTIME_CHECK(cfg_map_get(ns_g_config, "view",
  4289. &builtin_views) == ISC_R_SUCCESS);
  4290. for (element = cfg_list_first(builtin_views);
  4291. element != NULL;
  4292. element = cfg_list_next(element))
  4293. {
  4294. cfg_obj_t *vconfig = cfg_listelt_value(element);
  4295. CHECK(create_view(vconfig, &builtin_viewlist, &view));
  4296. CHECK(configure_view(view, config, vconfig,
  4297. &cachelist, bindkeys,
  4298. ns_g_mctx, ns_g_aclconfctx, ISC_FALSE));
  4299. dns_view_freeze(view);
  4300. dns_view_detach(&view);
  4301. view = NULL;
  4302. }
  4303. /* Now combine the two viewlists into one */
  4304. ISC_LIST_APPENDLIST(viewlist, builtin_viewlist, link);
  4305. /* Swap our new view list with the production one. */
  4306. tmpviewlist = server->viewlist;
  4307. server->viewlist = viewlist;
  4308. viewlist = tmpviewlist;
  4309. /* Make the view list available to each of the views */
  4310. view = ISC_LIST_HEAD(server->viewlist);
  4311. while (view != NULL) {
  4312. view->viewlist = &server->viewlist;
  4313. view = ISC_LIST_NEXT(view, link);
  4314. }
  4315. /* Swap our new cache list with the production one. */
  4316. tmpcachelist = server->cachelist;
  4317. server->cachelist = cachelist;
  4318. cachelist = tmpcachelist;
  4319. /* Load the TKEY information from the configuration. */
  4320. if (options != NULL) {
  4321. dns_tkeyctx_t *t = NULL;
  4322. CHECKM(ns_tkeyctx_fromconfig(options, ns_g_mctx, ns_g_entropy,
  4323. &t),
  4324. "configuring TKEY");
  4325. if (server->tkeyctx != NULL)
  4326. dns_tkeyctx_destroy(&server->tkeyctx);
  4327. server->tkeyctx = t;
  4328. }
  4329. /*
  4330. * Bind the control port(s).
  4331. */
  4332. CHECKM(ns_controls_configure(ns_g_server->controls, config,
  4333. ns_g_aclconfctx),
  4334. "binding control channel(s)");
  4335. /*
  4336. * Bind the lwresd port(s).
  4337. */
  4338. CHECKM(ns_lwresd_configure(ns_g_mctx, config),
  4339. "binding lightweight resolver ports");
  4340. /*
  4341. * Open the source of entropy.
  4342. */
  4343. if (first_time) {
  4344. obj = NULL;
  4345. result = ns_config_get(maps, "random-device", &obj);
  4346. if (result != ISC_R_SUCCESS) {
  4347. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  4348. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  4349. "no source of entropy found");
  4350. } else {
  4351. const char *randomdev = cfg_obj_asstring(obj);
  4352. result = isc_entropy_createfilesource(ns_g_entropy,
  4353. randomdev);
  4354. if (result != ISC_R_SUCCESS)
  4355. isc_log_write(ns_g_lctx,
  4356. NS_LOGCATEGORY_GENERAL,
  4357. NS_LOGMODULE_SERVER,
  4358. ISC_LOG_INFO,
  4359. "could not open entropy source "
  4360. "%s: %s",
  4361. randomdev,
  4362. isc_result_totext(result));
  4363. #ifdef PATH_RANDOMDEV
  4364. if (ns_g_fallbackentropy != NULL) {
  4365. if (result != ISC_R_SUCCESS) {
  4366. isc_log_write(ns_g_lctx,
  4367. NS_LOGCATEGORY_GENERAL,
  4368. NS_LOGMODULE_SERVER,
  4369. ISC_LOG_INFO,
  4370. "using pre-chroot entropy source "
  4371. "%s",
  4372. PATH_RANDOMDEV);
  4373. isc_entropy_detach(&ns_g_entropy);
  4374. isc_entropy_attach(ns_g_fallbackentropy,
  4375. &ns_g_entropy);
  4376. }
  4377. isc_entropy_detach(&ns_g_fallbackentropy);
  4378. }
  4379. #endif
  4380. }
  4381. }
  4382. /*
  4383. * Relinquish root privileges.
  4384. */
  4385. if (first_time)
  4386. ns_os_changeuser();
  4387. /*
  4388. * Check that the working directory is writable.
  4389. */
  4390. if (access(".", W_OK) != 0) {
  4391. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  4392. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  4393. "the working directory is not writable");
  4394. }
  4395. /*
  4396. * Configure the logging system.
  4397. *
  4398. * Do this after changing UID to make sure that any log
  4399. * files specified in named.conf get created by the
  4400. * unprivileged user, not root.
  4401. */
  4402. if (ns_g_logstderr) {
  4403. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  4404. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  4405. "ignoring config file logging "
  4406. "statement due to -g option");
  4407. } else {
  4408. const cfg_obj_t *logobj = NULL;
  4409. isc_logconfig_t *logc = NULL;
  4410. CHECKM(isc_logconfig_create(ns_g_lctx, &logc),
  4411. "creating new logging configuration");
  4412. logobj = NULL;
  4413. (void)cfg_map_get(config, "logging", &logobj);
  4414. if (logobj != NULL) {
  4415. CHECKM(ns_log_configure(logc, logobj),
  4416. "configuring logging");
  4417. } else {
  4418. CHECKM(ns_log_setdefaultchannels(logc),
  4419. "setting up default logging channels");
  4420. CHECKM(ns_log_setunmatchedcategory(logc),
  4421. "setting up default 'category unmatched'");
  4422. CHECKM(ns_log_setdefaultcategory(logc),
  4423. "setting up default 'category default'");
  4424. }
  4425. result = isc_logconfig_use(ns_g_lctx, logc);
  4426. if (result != ISC_R_SUCCESS) {
  4427. isc_logconfig_destroy(&logc);
  4428. CHECKM(result, "installing logging configuration");
  4429. }
  4430. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  4431. NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(1),
  4432. "now using logging configuration from "
  4433. "config file");
  4434. }
  4435. /*
  4436. * Set the default value of the query logging flag depending
  4437. * whether a "queries" category has been defined. This is
  4438. * a disgusting hack, but we need to do this for BIND 8
  4439. * compatibility.
  4440. */
  4441. if (first_time) {
  4442. const cfg_obj_t *logobj = NULL;
  4443. const cfg_obj_t *categories = NULL;
  4444. obj = NULL;
  4445. if (ns_config_get(maps, "querylog", &obj) == ISC_R_SUCCESS) {
  4446. server->log_queries = cfg_obj_asboolean(obj);
  4447. } else {
  4448. (void)cfg_map_get(config, "logging", &logobj);
  4449. if (logobj != NULL)
  4450. (void)cfg_map_get(logobj, "category",
  4451. &categories);
  4452. if (categories != NULL) {
  4453. const cfg_listelt_t *element;
  4454. for (element = cfg_list_first(categories);
  4455. element != NULL;
  4456. element = cfg_list_next(element))
  4457. {
  4458. const cfg_obj_t *catobj;
  4459. const char *str;
  4460. obj = cfg_listelt_value(element);
  4461. catobj = cfg_tuple_get(obj, "name");
  4462. str = cfg_obj_asstring(catobj);
  4463. if (strcasecmp(str, "queries") == 0)
  4464. server->log_queries = ISC_TRUE;
  4465. }
  4466. }
  4467. }
  4468. }
  4469. obj = NULL;
  4470. if (options != NULL &&
  4471. cfg_map_get(options, "memstatistics", &obj) == ISC_R_SUCCESS)
  4472. ns_g_memstatistics = cfg_obj_asboolean(obj);
  4473. else
  4474. ns_g_memstatistics =
  4475. ISC_TF((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0);
  4476. obj = NULL;
  4477. if (ns_config_get(maps, "memstatistics-file", &obj) == ISC_R_SUCCESS)
  4478. ns_main_setmemstats(cfg_obj_asstring(obj));
  4479. else if (ns_g_memstatistics)
  4480. ns_main_setmemstats("named.memstats");
  4481. else
  4482. ns_main_setmemstats(NULL);
  4483. obj = NULL;
  4484. result = ns_config_get(maps, "statistics-file", &obj);
  4485. INSIST(result == ISC_R_SUCCESS);
  4486. CHECKM(setstring(server, &server->statsfile, cfg_obj_asstring(obj)),
  4487. "strdup");
  4488. obj = NULL;
  4489. result = ns_config_get(maps, "dump-file", &obj);
  4490. INSIST(result == ISC_R_SUCCESS);
  4491. CHECKM(setstring(server, &server->dumpfile, cfg_obj_asstring(obj)),
  4492. "strdup");
  4493. obj = NULL;
  4494. result = ns_config_get(maps, "secroots-file", &obj);
  4495. INSIST(result == ISC_R_SUCCESS);
  4496. CHECKM(setstring(server, &server->secrootsfile, cfg_obj_asstring(obj)),
  4497. "strdup");
  4498. obj = NULL;
  4499. result = ns_config_get(maps, "recursing-file", &obj);
  4500. INSIST(result == ISC_R_SUCCESS);
  4501. CHECKM(setstring(server, &server->recfile, cfg_obj_asstring(obj)),
  4502. "strdup");
  4503. obj = NULL;
  4504. result = ns_config_get(maps, "version", &obj);
  4505. if (result == ISC_R_SUCCESS) {
  4506. CHECKM(setoptstring(server, &server->version, obj), "strdup");
  4507. server->version_set = ISC_TRUE;
  4508. } else {
  4509. server->version_set = ISC_FALSE;
  4510. }
  4511. obj = NULL;
  4512. result = ns_config_get(maps, "hostname", &obj);
  4513. if (result == ISC_R_SUCCESS) {
  4514. CHECKM(setoptstring(server, &server->hostname, obj), "strdup");
  4515. server->hostname_set = ISC_TRUE;
  4516. } else {
  4517. server->hostname_set = ISC_FALSE;
  4518. }
  4519. obj = NULL;
  4520. result = ns_config_get(maps, "server-id", &obj);
  4521. server->server_usehostname = ISC_FALSE;
  4522. if (result == ISC_R_SUCCESS && cfg_obj_isboolean(obj)) {
  4523. /* The parser translates "hostname" to ISC_TRUE */
  4524. server->server_usehostname = cfg_obj_asboolean(obj);
  4525. result = setstring(server, &server->server_id, NULL);
  4526. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4527. } else if (result == ISC_R_SUCCESS) {
  4528. /* Found a quoted string */
  4529. CHECKM(setoptstring(server, &server->server_id, obj), "strdup");
  4530. } else {
  4531. result = setstring(server, &server->server_id, NULL);
  4532. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4533. }
  4534. obj = NULL;
  4535. result = ns_config_get(maps, "flush-zones-on-shutdown", &obj);
  4536. if (result == ISC_R_SUCCESS) {
  4537. server->flushonshutdown = cfg_obj_asboolean(obj);
  4538. } else {
  4539. server->flushonshutdown = ISC_FALSE;
  4540. }
  4541. result = ISC_R_SUCCESS;
  4542. cleanup:
  4543. if (v4portset != NULL)
  4544. isc_portset_destroy(ns_g_mctx, &v4portset);
  4545. if (v6portset != NULL)
  4546. isc_portset_destroy(ns_g_mctx, &v6portset);
  4547. if (conf_parser != NULL) {
  4548. if (config != NULL)
  4549. cfg_obj_destroy(conf_parser, &config);
  4550. cfg_parser_destroy(&conf_parser);
  4551. }
  4552. if (bindkeys_parser != NULL) {
  4553. if (bindkeys != NULL)
  4554. cfg_obj_destroy(bindkeys_parser, &bindkeys);
  4555. cfg_parser_destroy(&bindkeys_parser);
  4556. }
  4557. if (view != NULL)
  4558. dns_view_detach(&view);
  4559. /*
  4560. * This cleans up either the old production view list
  4561. * or our temporary list depending on whether they
  4562. * were swapped above or not.
  4563. */
  4564. for (view = ISC_LIST_HEAD(viewlist);
  4565. view != NULL;
  4566. view = view_next) {
  4567. view_next = ISC_LIST_NEXT(view, link);
  4568. ISC_LIST_UNLINK(viewlist, view, link);
  4569. if (result == ISC_R_SUCCESS &&
  4570. strcmp(view->name, "_bind") != 0)
  4571. (void)dns_zt_apply(view->zonetable, ISC_FALSE,
  4572. removed, view);
  4573. dns_view_detach(&view);
  4574. }
  4575. /* Same cleanup for cache list. */
  4576. while ((nsc = ISC_LIST_HEAD(cachelist)) != NULL) {
  4577. ISC_LIST_UNLINK(cachelist, nsc, link);
  4578. dns_cache_detach(&nsc->cache);
  4579. isc_mem_put(server->mctx, nsc, sizeof(*nsc));
  4580. }
  4581. /*
  4582. * Adjust the listening interfaces in accordance with the source
  4583. * addresses specified in views and zones.
  4584. */
  4585. if (isc_net_probeipv6() == ISC_R_SUCCESS)
  4586. adjust_interfaces(server, ns_g_mctx);
  4587. /* Relinquish exclusive access to configuration data. */
  4588. if (exclusive)
  4589. isc_task_endexclusive(server->task);
  4590. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  4591. ISC_LOG_DEBUG(1), "load_configuration: %s",
  4592. isc_result_totext(result));
  4593. return (result);
  4594. }
  4595. static isc_result_t
  4596. load_zones(ns_server_t *server, isc_boolean_t stop) {
  4597. isc_result_t result;
  4598. dns_view_t *view;
  4599. result = isc_task_beginexclusive(server->task);
  4600. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4601. /*
  4602. * Load zone data from disk.
  4603. */
  4604. for (view = ISC_LIST_HEAD(server->viewlist);
  4605. view != NULL;
  4606. view = ISC_LIST_NEXT(view, link))
  4607. {
  4608. CHECK(dns_view_load(view, stop));
  4609. if (view->managed_keys != NULL)
  4610. CHECK(dns_zone_load(view->managed_keys));
  4611. }
  4612. /*
  4613. * Force zone maintenance. Do this after loading
  4614. * so that we know when we need to force AXFR of
  4615. * slave zones whose master files are missing.
  4616. */
  4617. CHECK(dns_zonemgr_forcemaint(server->zonemgr));
  4618. cleanup:
  4619. isc_task_endexclusive(server->task);
  4620. return (result);
  4621. }
  4622. static isc_result_t
  4623. load_new_zones(ns_server_t *server, isc_boolean_t stop) {
  4624. isc_result_t result;
  4625. dns_view_t *view;
  4626. result = isc_task_beginexclusive(server->task);
  4627. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4628. /*
  4629. * Load zone data from disk.
  4630. */
  4631. for (view = ISC_LIST_HEAD(server->viewlist);
  4632. view != NULL;
  4633. view = ISC_LIST_NEXT(view, link))
  4634. {
  4635. CHECK(dns_view_loadnew(view, stop));
  4636. /* Load managed-keys data */
  4637. if (view->managed_keys != NULL)
  4638. CHECK(dns_zone_loadnew(view->managed_keys));
  4639. }
  4640. /*
  4641. * Resume zone XFRs.
  4642. */
  4643. dns_zonemgr_resumexfrs(server->zonemgr);
  4644. cleanup:
  4645. isc_task_endexclusive(server->task);
  4646. return (result);
  4647. }
  4648. static void
  4649. run_server(isc_task_t *task, isc_event_t *event) {
  4650. isc_result_t result;
  4651. ns_server_t *server = (ns_server_t *)event->ev_arg;
  4652. INSIST(task == server->task);
  4653. isc_event_free(&event);
  4654. CHECKFATAL(dns_dispatchmgr_create(ns_g_mctx, ns_g_entropy,
  4655. &ns_g_dispatchmgr),
  4656. "creating dispatch manager");
  4657. dns_dispatchmgr_setstats(ns_g_dispatchmgr, server->resolverstats);
  4658. CHECKFATAL(ns_interfacemgr_create(ns_g_mctx, ns_g_taskmgr,
  4659. ns_g_socketmgr, ns_g_dispatchmgr,
  4660. &server->interfacemgr),
  4661. "creating interface manager");
  4662. CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
  4663. NULL, NULL, server->task,
  4664. interface_timer_tick,
  4665. server, &server->interface_timer),
  4666. "creating interface timer");
  4667. CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
  4668. NULL, NULL, server->task,
  4669. heartbeat_timer_tick,
  4670. server, &server->heartbeat_timer),
  4671. "creating heartbeat timer");
  4672. CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
  4673. NULL, NULL, server->task, pps_timer_tick,
  4674. server, &server->pps_timer),
  4675. "creating pps timer");
  4676. CHECKFATAL(cfg_parser_create(ns_g_mctx, NULL, &ns_g_parser),
  4677. "creating default configuration parser");
  4678. if (ns_g_lwresdonly)
  4679. CHECKFATAL(load_configuration(lwresd_g_conffile, server,
  4680. ISC_TRUE),
  4681. "loading configuration");
  4682. else
  4683. CHECKFATAL(load_configuration(ns_g_conffile, server, ISC_TRUE),
  4684. "loading configuration");
  4685. isc_hash_init();
  4686. CHECKFATAL(load_zones(server, ISC_FALSE), "loading zones");
  4687. ns_os_started();
  4688. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  4689. ISC_LOG_NOTICE, "running");
  4690. }
  4691. void
  4692. ns_server_flushonshutdown(ns_server_t *server, isc_boolean_t flush) {
  4693. REQUIRE(NS_SERVER_VALID(server));
  4694. server->flushonshutdown = flush;
  4695. }
  4696. static void
  4697. shutdown_server(isc_task_t *task, isc_event_t *event) {
  4698. isc_result_t result;
  4699. dns_view_t *view, *view_next;
  4700. ns_server_t *server = (ns_server_t *)event->ev_arg;
  4701. isc_boolean_t flush = server->flushonshutdown;
  4702. ns_cache_t *nsc;
  4703. UNUSED(task);
  4704. INSIST(task == server->task);
  4705. result = isc_task_beginexclusive(server->task);
  4706. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4707. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  4708. ISC_LOG_INFO, "shutting down%s",
  4709. flush ? ": flushing changes" : "");
  4710. ns_statschannels_shutdown(server);
  4711. ns_controls_shutdown(server->controls);
  4712. end_reserved_dispatches(server, ISC_TRUE);
  4713. cleanup_session_key(server, server->mctx);
  4714. if (ns_g_aclconfctx != NULL)
  4715. cfg_aclconfctx_detach(&ns_g_aclconfctx);
  4716. cfg_obj_destroy(ns_g_parser, &ns_g_config);
  4717. cfg_parser_destroy(&ns_g_parser);
  4718. for (view = ISC_LIST_HEAD(server->viewlist);
  4719. view != NULL;
  4720. view = view_next) {
  4721. view_next = ISC_LIST_NEXT(view, link);
  4722. ISC_LIST_UNLINK(server->viewlist, view, link);
  4723. if (flush)
  4724. dns_view_flushanddetach(&view);
  4725. else
  4726. dns_view_detach(&view);
  4727. }
  4728. while ((nsc = ISC_LIST_HEAD(server->cachelist)) != NULL) {
  4729. ISC_LIST_UNLINK(server->cachelist, nsc, link);
  4730. dns_cache_detach(&nsc->cache);
  4731. isc_mem_put(server->mctx, nsc, sizeof(*nsc));
  4732. }
  4733. isc_timer_detach(&server->interface_timer);
  4734. isc_timer_detach(&server->heartbeat_timer);
  4735. isc_timer_detach(&server->pps_timer);
  4736. ns_interfacemgr_shutdown(server->interfacemgr);
  4737. ns_interfacemgr_detach(&server->interfacemgr);
  4738. dns_dispatchmgr_destroy(&ns_g_dispatchmgr);
  4739. dns_zonemgr_shutdown(server->zonemgr);
  4740. if (ns_g_sessionkey != NULL) {
  4741. dns_tsigkey_detach(&ns_g_sessionkey);
  4742. dns_name_free(&ns_g_sessionkeyname, server->mctx);
  4743. }
  4744. if (server->blackholeacl != NULL)
  4745. dns_acl_detach(&server->blackholeacl);
  4746. dns_db_detach(&server->in_roothints);
  4747. isc_task_endexclusive(server->task);
  4748. isc_task_detach(&server->task);
  4749. isc_event_free(&event);
  4750. }
  4751. void
  4752. ns_server_create(isc_mem_t *mctx, ns_server_t **serverp) {
  4753. isc_result_t result;
  4754. ns_server_t *server = isc_mem_get(mctx, sizeof(*server));
  4755. if (server == NULL)
  4756. fatal("allocating server object", ISC_R_NOMEMORY);
  4757. server->mctx = mctx;
  4758. server->task = NULL;
  4759. /* Initialize configuration data with default values. */
  4760. result = isc_quota_init(&server->xfroutquota, 10);
  4761. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4762. result = isc_quota_init(&server->tcpquota, 10);
  4763. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4764. result = isc_quota_init(&server->recursionquota, 100);
  4765. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4766. result = dns_aclenv_init(mctx, &server->aclenv);
  4767. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  4768. /* Initialize server data structures. */
  4769. server->zonemgr = NULL;
  4770. server->interfacemgr = NULL;
  4771. ISC_LIST_INIT(server->viewlist);
  4772. server->in_roothints = NULL;
  4773. server->blackholeacl = NULL;
  4774. CHECKFATAL(dns_rootns_create(mctx, dns_rdataclass_in, NULL,
  4775. &server->in_roothints),
  4776. "setting up root hints");
  4777. CHECKFATAL(isc_mutex_init(&server->reload_event_lock),
  4778. "initializing reload event lock");
  4779. server->reload_event =
  4780. isc_event_allocate(ns_g_mctx, server,
  4781. NS_EVENT_RELOAD,
  4782. ns_server_reload,
  4783. server,
  4784. sizeof(isc_event_t));
  4785. CHECKFATAL(server->reload_event == NULL ?
  4786. ISC_R_NOMEMORY : ISC_R_SUCCESS,
  4787. "allocating reload event");
  4788. CHECKFATAL(dst_lib_init2(ns_g_mctx, ns_g_entropy,
  4789. ns_g_engine, ISC_ENTROPY_GOODONLY),
  4790. "initializing DST");
  4791. server->tkeyctx = NULL;
  4792. CHECKFATAL(dns_tkeyctx_create(ns_g_mctx, ns_g_entropy,
  4793. &server->tkeyctx),
  4794. "creating TKEY context");
  4795. /*
  4796. * Setup the server task, which is responsible for coordinating
  4797. * startup and shutdown of the server.
  4798. */
  4799. CHECKFATAL(isc_task_create(ns_g_taskmgr, 0, &server->task),
  4800. "creating server task");
  4801. isc_task_setname(server->task, "server", server);
  4802. CHECKFATAL(isc_task_onshutdown(server->task, shutdown_server, server),
  4803. "isc_task_onshutdown");
  4804. CHECKFATAL(isc_app_onrun(ns_g_mctx, server->task, run_server, server),
  4805. "isc_app_onrun");
  4806. server->interface_timer = NULL;
  4807. server->heartbeat_timer = NULL;
  4808. server->pps_timer = NULL;
  4809. server->interface_interval = 0;
  4810. server->heartbeat_interval = 0;
  4811. CHECKFATAL(dns_zonemgr_create(ns_g_mctx, ns_g_taskmgr, ns_g_timermgr,
  4812. ns_g_socketmgr, &server->zonemgr),
  4813. "dns_zonemgr_create");
  4814. CHECKFATAL(dns_zonemgr_setsize(server->zonemgr, 1000),
  4815. "dns_zonemgr_setsize");
  4816. server->statsfile = isc_mem_strdup(server->mctx, "named.stats");
  4817. CHECKFATAL(server->statsfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
  4818. "isc_mem_strdup");
  4819. server->nsstats = NULL;
  4820. server->rcvquerystats = NULL;
  4821. server->opcodestats = NULL;
  4822. server->zonestats = NULL;
  4823. server->resolverstats = NULL;
  4824. server->sockstats = NULL;
  4825. CHECKFATAL(isc_stats_create(server->mctx, &server->sockstats,
  4826. isc_sockstatscounter_max),
  4827. "isc_stats_create");
  4828. isc_socketmgr_setstats(ns_g_socketmgr, server->sockstats);
  4829. server->bindkeysfile = isc_mem_strdup(server->mctx, "bind.keys");
  4830. CHECKFATAL(server->bindkeysfile == NULL ? ISC_R_NOMEMORY :
  4831. ISC_R_SUCCESS,
  4832. "isc_mem_strdup");
  4833. server->dumpfile = isc_mem_strdup(server->mctx, "named_dump.db");
  4834. CHECKFATAL(server->dumpfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
  4835. "isc_mem_strdup");
  4836. server->secrootsfile = isc_mem_strdup(server->mctx, "named.secroots");
  4837. CHECKFATAL(server->secrootsfile == NULL ? ISC_R_NOMEMORY :
  4838. ISC_R_SUCCESS,
  4839. "isc_mem_strdup");
  4840. server->recfile = isc_mem_strdup(server->mctx, "named.recursing");
  4841. CHECKFATAL(server->recfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
  4842. "isc_mem_strdup");
  4843. server->hostname_set = ISC_FALSE;
  4844. server->hostname = NULL;
  4845. server->version_set = ISC_FALSE;
  4846. server->version = NULL;
  4847. server->server_usehostname = ISC_FALSE;
  4848. server->server_id = NULL;
  4849. CHECKFATAL(isc_stats_create(ns_g_mctx, &server->nsstats,
  4850. dns_nsstatscounter_max),
  4851. "dns_stats_create (server)");
  4852. CHECKFATAL(dns_rdatatypestats_create(ns_g_mctx,
  4853. &server->rcvquerystats),
  4854. "dns_stats_create (rcvquery)");
  4855. CHECKFATAL(dns_opcodestats_create(ns_g_mctx, &server->opcodestats),
  4856. "dns_stats_create (opcode)");
  4857. CHECKFATAL(isc_stats_create(ns_g_mctx, &server->zonestats,
  4858. dns_zonestatscounter_max),
  4859. "dns_stats_create (zone)");
  4860. CHECKFATAL(isc_stats_create(ns_g_mctx, &server->resolverstats,
  4861. dns_resstatscounter_max),
  4862. "dns_stats_create (resolver)");
  4863. server->flushonshutdown = ISC_FALSE;
  4864. server->log_queries = ISC_FALSE;
  4865. server->controls = NULL;
  4866. CHECKFATAL(ns_controls_create(server, &server->controls),
  4867. "ns_controls_create");
  4868. server->dispatchgen = 0;
  4869. ISC_LIST_INIT(server->dispatches);
  4870. ISC_LIST_INIT(server->statschannels);
  4871. ISC_LIST_INIT(server->cachelist);
  4872. server->sessionkey = NULL;
  4873. server->session_keyfile = NULL;
  4874. server->session_keyname = NULL;
  4875. server->session_keyalg = DST_ALG_UNKNOWN;
  4876. server->session_keybits = 0;
  4877. server->magic = NS_SERVER_MAGIC;
  4878. *serverp = server;
  4879. }
  4880. void
  4881. ns_server_destroy(ns_server_t **serverp) {
  4882. ns_server_t *server = *serverp;
  4883. REQUIRE(NS_SERVER_VALID(server));
  4884. ns_controls_destroy(&server->controls);
  4885. isc_stats_detach(&server->nsstats);
  4886. dns_stats_detach(&server->rcvquerystats);
  4887. dns_stats_detach(&server->opcodestats);
  4888. isc_stats_detach(&server->zonestats);
  4889. isc_stats_detach(&server->resolverstats);
  4890. isc_stats_detach(&server->sockstats);
  4891. isc_mem_free(server->mctx, server->statsfile);
  4892. isc_mem_free(server->mctx, server->bindkeysfile);
  4893. isc_mem_free(server->mctx, server->dumpfile);
  4894. isc_mem_free(server->mctx, server->secrootsfile);
  4895. isc_mem_free(server->mctx, server->recfile);
  4896. if (server->version != NULL)
  4897. isc_mem_free(server->mctx, server->version);
  4898. if (server->hostname != NULL)
  4899. isc_mem_free(server->mctx, server->hostname);
  4900. if (server->server_id != NULL)
  4901. isc_mem_free(server->mctx, server->server_id);
  4902. if (server->zonemgr != NULL)
  4903. dns_zonemgr_detach(&server->zonemgr);
  4904. if (server->tkeyctx != NULL)
  4905. dns_tkeyctx_destroy(&server->tkeyctx);
  4906. dst_lib_destroy();
  4907. isc_event_free(&server->reload_event);
  4908. INSIST(ISC_LIST_EMPTY(server->viewlist));
  4909. INSIST(ISC_LIST_EMPTY(server->cachelist));
  4910. dns_aclenv_destroy(&server->aclenv);
  4911. isc_quota_destroy(&server->recursionquota);
  4912. isc_quota_destroy(&server->tcpquota);
  4913. isc_quota_destroy(&server->xfroutquota);
  4914. server->magic = 0;
  4915. isc_mem_put(server->mctx, server, sizeof(*server));
  4916. *serverp = NULL;
  4917. }
  4918. static void
  4919. fatal(const char *msg, isc_result_t result) {
  4920. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  4921. ISC_LOG_CRITICAL, "%s: %s", msg,
  4922. isc_result_totext(result));
  4923. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
  4924. ISC_LOG_CRITICAL, "exiting (due to fatal error)");
  4925. exit(1);
  4926. }
  4927. static void
  4928. start_reserved_dispatches(ns_server_t *server) {
  4929. REQUIRE(NS_SERVER_VALID(server));
  4930. server->dispatchgen++;
  4931. }
  4932. static void
  4933. end_reserved_dispatches(ns_server_t *server, isc_boolean_t all) {
  4934. ns_dispatch_t *dispatch, *nextdispatch;
  4935. REQUIRE(NS_SERVER_VALID(server));
  4936. for (dispatch = ISC_LIST_HEAD(server->dispatches);
  4937. dispatch != NULL;
  4938. dispatch = nextdispatch) {
  4939. nextdispatch = ISC_LIST_NEXT(dispatch, link);
  4940. if (!all && server->dispatchgen == dispatch-> dispatchgen)
  4941. continue;
  4942. ISC_LIST_UNLINK(server->dispatches, dispatch, link);
  4943. dns_dispatch_detach(&dispatch->dispatch);
  4944. isc_mem_put(server->mctx, dispatch, sizeof(*dispatch));
  4945. }
  4946. }
  4947. void
  4948. ns_add_reserved_dispatch(ns_server_t *server, const isc_sockaddr_t *addr) {
  4949. ns_dispatch_t *dispatch;
  4950. in_port_t port;
  4951. char addrbuf[ISC_SOCKADDR_FORMATSIZE];
  4952. isc_result_t result;
  4953. unsigned int attrs, attrmask;
  4954. REQUIRE(NS_SERVER_VALID(server));
  4955. port = isc_sockaddr_getport(addr);
  4956. if (port == 0 || port >= 1024)
  4957. return;
  4958. for (dispatch = ISC_LIST_HEAD(server->dispatches);
  4959. dispatch != NULL;
  4960. dispatch = ISC_LIST_NEXT(dispatch, link)) {
  4961. if (isc_sockaddr_equal(&dispatch->addr, addr))
  4962. break;
  4963. }
  4964. if (dispatch != NULL) {
  4965. dispatch->dispatchgen = server->dispatchgen;
  4966. return;
  4967. }
  4968. dispatch = isc_mem_get(server->mctx, sizeof(*dispatch));
  4969. if (dispatch == NULL) {
  4970. result = ISC_R_NOMEMORY;
  4971. goto cleanup;
  4972. }
  4973. dispatch->addr = *addr;
  4974. dispatch->dispatchgen = server->dispatchgen;
  4975. dispatch->dispatch = NULL;
  4976. attrs = 0;
  4977. attrs |= DNS_DISPATCHATTR_UDP;
  4978. switch (isc_sockaddr_pf(addr)) {
  4979. case AF_INET:
  4980. attrs |= DNS_DISPATCHATTR_IPV4;
  4981. break;
  4982. case AF_INET6:
  4983. attrs |= DNS_DISPATCHATTR_IPV6;
  4984. break;
  4985. default:
  4986. result = ISC_R_NOTIMPLEMENTED;
  4987. goto cleanup;
  4988. }
  4989. attrmask = 0;
  4990. attrmask |= DNS_DISPATCHATTR_UDP;
  4991. attrmask |= DNS_DISPATCHATTR_TCP;
  4992. attrmask |= DNS_DISPATCHATTR_IPV4;
  4993. attrmask |= DNS_DISPATCHATTR_IPV6;
  4994. result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
  4995. ns_g_taskmgr, &dispatch->addr, 4096,
  4996. 1000, 32768, 16411, 16433,
  4997. attrs, attrmask, &dispatch->dispatch);
  4998. if (result != ISC_R_SUCCESS)
  4999. goto cleanup;
  5000. ISC_LIST_INITANDPREPEND(server->dispatches, dispatch, link);
  5001. return;
  5002. cleanup:
  5003. if (dispatch != NULL)
  5004. isc_mem_put(server->mctx, dispatch, sizeof(*dispatch));
  5005. isc_sockaddr_format(addr, addrbuf, sizeof(addrbuf));
  5006. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5007. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  5008. "unable to create dispatch for reserved port %s: %s",
  5009. addrbuf, isc_result_totext(result));
  5010. }
  5011. static isc_result_t
  5012. loadconfig(ns_server_t *server) {
  5013. isc_result_t result;
  5014. start_reserved_dispatches(server);
  5015. result = load_configuration(ns_g_lwresdonly ?
  5016. lwresd_g_conffile : ns_g_conffile,
  5017. server, ISC_FALSE);
  5018. if (result == ISC_R_SUCCESS) {
  5019. end_reserved_dispatches(server, ISC_FALSE);
  5020. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5021. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5022. "reloading configuration succeeded");
  5023. } else {
  5024. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5025. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5026. "reloading configuration failed: %s",
  5027. isc_result_totext(result));
  5028. }
  5029. return (result);
  5030. }
  5031. static isc_result_t
  5032. reload(ns_server_t *server) {
  5033. isc_result_t result;
  5034. CHECK(loadconfig(server));
  5035. result = load_zones(server, ISC_FALSE);
  5036. if (result == ISC_R_SUCCESS)
  5037. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5038. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5039. "reloading zones succeeded");
  5040. else
  5041. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5042. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5043. "reloading zones failed: %s",
  5044. isc_result_totext(result));
  5045. cleanup:
  5046. return (result);
  5047. }
  5048. static void
  5049. reconfig(ns_server_t *server) {
  5050. isc_result_t result;
  5051. CHECK(loadconfig(server));
  5052. result = load_new_zones(server, ISC_FALSE);
  5053. if (result == ISC_R_SUCCESS)
  5054. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5055. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5056. "any newly configured zones are now loaded");
  5057. else
  5058. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5059. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5060. "loading new zones failed: %s",
  5061. isc_result_totext(result));
  5062. cleanup: ;
  5063. }
  5064. /*
  5065. * Handle a reload event (from SIGHUP).
  5066. */
  5067. static void
  5068. ns_server_reload(isc_task_t *task, isc_event_t *event) {
  5069. ns_server_t *server = (ns_server_t *)event->ev_arg;
  5070. INSIST(task = server->task);
  5071. UNUSED(task);
  5072. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5073. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5074. "received SIGHUP signal to reload zones");
  5075. (void)reload(server);
  5076. LOCK(&server->reload_event_lock);
  5077. INSIST(server->reload_event == NULL);
  5078. server->reload_event = event;
  5079. UNLOCK(&server->reload_event_lock);
  5080. }
  5081. void
  5082. ns_server_reloadwanted(ns_server_t *server) {
  5083. LOCK(&server->reload_event_lock);
  5084. if (server->reload_event != NULL)
  5085. isc_task_send(server->task, &server->reload_event);
  5086. UNLOCK(&server->reload_event_lock);
  5087. }
  5088. static char *
  5089. next_token(char **stringp, const char *delim) {
  5090. char *res;
  5091. do {
  5092. res = strsep(stringp, delim);
  5093. if (res == NULL)
  5094. break;
  5095. } while (*res == '\0');
  5096. return (res);
  5097. }
  5098. /*
  5099. * Find the zone specified in the control channel command 'args',
  5100. * if any. If a zone is specified, point '*zonep' at it, otherwise
  5101. * set '*zonep' to NULL.
  5102. */
  5103. static isc_result_t
  5104. zone_from_args(ns_server_t *server, char *args, dns_zone_t **zonep,
  5105. const char **zonename)
  5106. {
  5107. char *input, *ptr;
  5108. const char *zonetxt;
  5109. char *classtxt;
  5110. const char *viewtxt = NULL;
  5111. dns_fixedname_t name;
  5112. isc_result_t result;
  5113. isc_buffer_t buf;
  5114. dns_view_t *view = NULL;
  5115. dns_rdataclass_t rdclass;
  5116. REQUIRE(zonep != NULL && *zonep == NULL);
  5117. input = args;
  5118. /* Skip the command name. */
  5119. ptr = next_token(&input, " \t");
  5120. if (ptr == NULL)
  5121. return (ISC_R_UNEXPECTEDEND);
  5122. /* Look for the zone name. */
  5123. zonetxt = next_token(&input, " \t");
  5124. if (zonetxt == NULL)
  5125. return (ISC_R_SUCCESS);
  5126. if (zonename)
  5127. *zonename = zonetxt;
  5128. /* Look for the optional class name. */
  5129. classtxt = next_token(&input, " \t");
  5130. if (classtxt != NULL) {
  5131. /* Look for the optional view name. */
  5132. viewtxt = next_token(&input, " \t");
  5133. }
  5134. isc_buffer_init(&buf, zonetxt, strlen(zonetxt));
  5135. isc_buffer_add(&buf, strlen(zonetxt));
  5136. dns_fixedname_init(&name);
  5137. result = dns_name_fromtext(dns_fixedname_name(&name),
  5138. &buf, dns_rootname, 0, NULL);
  5139. if (result != ISC_R_SUCCESS)
  5140. goto fail1;
  5141. if (classtxt != NULL) {
  5142. isc_textregion_t r;
  5143. r.base = classtxt;
  5144. r.length = strlen(classtxt);
  5145. result = dns_rdataclass_fromtext(&rdclass, &r);
  5146. if (result != ISC_R_SUCCESS)
  5147. goto fail1;
  5148. } else
  5149. rdclass = dns_rdataclass_in;
  5150. if (viewtxt == NULL) {
  5151. result = dns_viewlist_findzone(&server->viewlist,
  5152. dns_fixedname_name(&name),
  5153. ISC_TF(classtxt == NULL),
  5154. rdclass, zonep);
  5155. } else {
  5156. result = dns_viewlist_find(&server->viewlist, viewtxt,
  5157. rdclass, &view);
  5158. if (result != ISC_R_SUCCESS)
  5159. goto fail1;
  5160. result = dns_zt_find(view->zonetable, dns_fixedname_name(&name),
  5161. 0, NULL, zonep);
  5162. dns_view_detach(&view);
  5163. }
  5164. /* Partial match? */
  5165. if (result != ISC_R_SUCCESS && *zonep != NULL)
  5166. dns_zone_detach(zonep);
  5167. if (result == DNS_R_PARTIALMATCH)
  5168. result = ISC_R_NOTFOUND;
  5169. fail1:
  5170. return (result);
  5171. }
  5172. /*
  5173. * Act on a "retransfer" command from the command channel.
  5174. */
  5175. isc_result_t
  5176. ns_server_retransfercommand(ns_server_t *server, char *args) {
  5177. isc_result_t result;
  5178. dns_zone_t *zone = NULL;
  5179. dns_zonetype_t type;
  5180. result = zone_from_args(server, args, &zone, NULL);
  5181. if (result != ISC_R_SUCCESS)
  5182. return (result);
  5183. if (zone == NULL)
  5184. return (ISC_R_UNEXPECTEDEND);
  5185. type = dns_zone_gettype(zone);
  5186. if (type == dns_zone_slave || type == dns_zone_stub)
  5187. dns_zone_forcereload(zone);
  5188. else
  5189. result = ISC_R_NOTFOUND;
  5190. dns_zone_detach(&zone);
  5191. return (result);
  5192. }
  5193. /*
  5194. * Act on a "reload" command from the command channel.
  5195. */
  5196. isc_result_t
  5197. ns_server_reloadcommand(ns_server_t *server, char *args, isc_buffer_t *text) {
  5198. isc_result_t result;
  5199. dns_zone_t *zone = NULL;
  5200. dns_zonetype_t type;
  5201. const char *msg = NULL;
  5202. result = zone_from_args(server, args, &zone, NULL);
  5203. if (result != ISC_R_SUCCESS)
  5204. return (result);
  5205. if (zone == NULL) {
  5206. result = reload(server);
  5207. if (result == ISC_R_SUCCESS)
  5208. msg = "server reload successful";
  5209. } else {
  5210. type = dns_zone_gettype(zone);
  5211. if (type == dns_zone_slave || type == dns_zone_stub) {
  5212. dns_zone_refresh(zone);
  5213. dns_zone_detach(&zone);
  5214. msg = "zone refresh queued";
  5215. } else {
  5216. result = dns_zone_load(zone);
  5217. dns_zone_detach(&zone);
  5218. switch (result) {
  5219. case ISC_R_SUCCESS:
  5220. msg = "zone reload successful";
  5221. break;
  5222. case DNS_R_CONTINUE:
  5223. msg = "zone reload queued";
  5224. result = ISC_R_SUCCESS;
  5225. break;
  5226. case DNS_R_UPTODATE:
  5227. msg = "zone reload up-to-date";
  5228. result = ISC_R_SUCCESS;
  5229. break;
  5230. default:
  5231. /* failure message will be generated by rndc */
  5232. break;
  5233. }
  5234. }
  5235. }
  5236. if (msg != NULL && strlen(msg) < isc_buffer_availablelength(text))
  5237. isc_buffer_putmem(text, (const unsigned char *)msg,
  5238. strlen(msg) + 1);
  5239. return (result);
  5240. }
  5241. /*
  5242. * Act on a "reconfig" command from the command channel.
  5243. */
  5244. isc_result_t
  5245. ns_server_reconfigcommand(ns_server_t *server, char *args) {
  5246. UNUSED(args);
  5247. reconfig(server);
  5248. return (ISC_R_SUCCESS);
  5249. }
  5250. /*
  5251. * Act on a "notify" command from the command channel.
  5252. */
  5253. isc_result_t
  5254. ns_server_notifycommand(ns_server_t *server, char *args, isc_buffer_t *text) {
  5255. isc_result_t result;
  5256. dns_zone_t *zone = NULL;
  5257. const unsigned char msg[] = "zone notify queued";
  5258. result = zone_from_args(server, args, &zone, NULL);
  5259. if (result != ISC_R_SUCCESS)
  5260. return (result);
  5261. if (zone == NULL)
  5262. return (ISC_R_UNEXPECTEDEND);
  5263. dns_zone_notify(zone);
  5264. dns_zone_detach(&zone);
  5265. if (sizeof(msg) <= isc_buffer_availablelength(text))
  5266. isc_buffer_putmem(text, msg, sizeof(msg));
  5267. return (ISC_R_SUCCESS);
  5268. }
  5269. /*
  5270. * Act on a "refresh" command from the command channel.
  5271. */
  5272. isc_result_t
  5273. ns_server_refreshcommand(ns_server_t *server, char *args, isc_buffer_t *text) {
  5274. isc_result_t result;
  5275. dns_zone_t *zone = NULL;
  5276. const unsigned char msg1[] = "zone refresh queued";
  5277. const unsigned char msg2[] = "not a slave or stub zone";
  5278. dns_zonetype_t type;
  5279. result = zone_from_args(server, args, &zone, NULL);
  5280. if (result != ISC_R_SUCCESS)
  5281. return (result);
  5282. if (zone == NULL)
  5283. return (ISC_R_UNEXPECTEDEND);
  5284. type = dns_zone_gettype(zone);
  5285. if (type == dns_zone_slave || type == dns_zone_stub) {
  5286. dns_zone_refresh(zone);
  5287. dns_zone_detach(&zone);
  5288. if (sizeof(msg1) <= isc_buffer_availablelength(text))
  5289. isc_buffer_putmem(text, msg1, sizeof(msg1));
  5290. return (ISC_R_SUCCESS);
  5291. }
  5292. dns_zone_detach(&zone);
  5293. if (sizeof(msg2) <= isc_buffer_availablelength(text))
  5294. isc_buffer_putmem(text, msg2, sizeof(msg2));
  5295. return (ISC_R_FAILURE);
  5296. }
  5297. isc_result_t
  5298. ns_server_togglequerylog(ns_server_t *server) {
  5299. server->log_queries = server->log_queries ? ISC_FALSE : ISC_TRUE;
  5300. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5301. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5302. "query logging is now %s",
  5303. server->log_queries ? "on" : "off");
  5304. return (ISC_R_SUCCESS);
  5305. }
  5306. static isc_result_t
  5307. ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
  5308. cfg_aclconfctx_t *actx,
  5309. isc_mem_t *mctx, ns_listenlist_t **target)
  5310. {
  5311. isc_result_t result;
  5312. const cfg_listelt_t *element;
  5313. ns_listenlist_t *dlist = NULL;
  5314. REQUIRE(target != NULL && *target == NULL);
  5315. result = ns_listenlist_create(mctx, &dlist);
  5316. if (result != ISC_R_SUCCESS)
  5317. return (result);
  5318. for (element = cfg_list_first(listenlist);
  5319. element != NULL;
  5320. element = cfg_list_next(element))
  5321. {
  5322. ns_listenelt_t *delt = NULL;
  5323. const cfg_obj_t *listener = cfg_listelt_value(element);
  5324. result = ns_listenelt_fromconfig(listener, config, actx,
  5325. mctx, &delt);
  5326. if (result != ISC_R_SUCCESS)
  5327. goto cleanup;
  5328. ISC_LIST_APPEND(dlist->elts, delt, link);
  5329. }
  5330. *target = dlist;
  5331. return (ISC_R_SUCCESS);
  5332. cleanup:
  5333. ns_listenlist_detach(&dlist);
  5334. return (result);
  5335. }
  5336. /*
  5337. * Create a listen list from the corresponding configuration
  5338. * data structure.
  5339. */
  5340. static isc_result_t
  5341. ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
  5342. cfg_aclconfctx_t *actx,
  5343. isc_mem_t *mctx, ns_listenelt_t **target)
  5344. {
  5345. isc_result_t result;
  5346. const cfg_obj_t *portobj;
  5347. in_port_t port;
  5348. ns_listenelt_t *delt = NULL;
  5349. REQUIRE(target != NULL && *target == NULL);
  5350. portobj = cfg_tuple_get(listener, "port");
  5351. if (!cfg_obj_isuint32(portobj)) {
  5352. if (ns_g_port != 0) {
  5353. port = ns_g_port;
  5354. } else {
  5355. result = ns_config_getport(config, &port);
  5356. if (result != ISC_R_SUCCESS)
  5357. return (result);
  5358. }
  5359. } else {
  5360. if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
  5361. cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
  5362. "port value '%u' is out of range",
  5363. cfg_obj_asuint32(portobj));
  5364. return (ISC_R_RANGE);
  5365. }
  5366. port = (in_port_t)cfg_obj_asuint32(portobj);
  5367. }
  5368. result = ns_listenelt_create(mctx, port, NULL, &delt);
  5369. if (result != ISC_R_SUCCESS)
  5370. return (result);
  5371. result = cfg_acl_fromconfig(cfg_tuple_get(listener, "acl"),
  5372. config, ns_g_lctx, actx, mctx, 0,
  5373. &delt->acl);
  5374. if (result != ISC_R_SUCCESS) {
  5375. ns_listenelt_destroy(delt);
  5376. return (result);
  5377. }
  5378. *target = delt;
  5379. return (ISC_R_SUCCESS);
  5380. }
  5381. isc_result_t
  5382. ns_server_dumpstats(ns_server_t *server) {
  5383. isc_result_t result;
  5384. FILE *fp = NULL;
  5385. CHECKMF(isc_stdio_open(server->statsfile, "a", &fp),
  5386. "could not open statistics dump file", server->statsfile);
  5387. result = ns_stats_dump(server, fp);
  5388. cleanup:
  5389. if (fp != NULL)
  5390. (void)isc_stdio_close(fp);
  5391. if (result == ISC_R_SUCCESS)
  5392. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5393. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5394. "dumpstats complete");
  5395. else
  5396. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5397. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5398. "dumpstats failed: %s",
  5399. dns_result_totext(result));
  5400. return (result);
  5401. }
  5402. static isc_result_t
  5403. add_zone_tolist(dns_zone_t *zone, void *uap) {
  5404. struct dumpcontext *dctx = uap;
  5405. struct zonelistentry *zle;
  5406. zle = isc_mem_get(dctx->mctx, sizeof *zle);
  5407. if (zle == NULL)
  5408. return (ISC_R_NOMEMORY);
  5409. zle->zone = NULL;
  5410. dns_zone_attach(zone, &zle->zone);
  5411. ISC_LINK_INIT(zle, link);
  5412. ISC_LIST_APPEND(ISC_LIST_TAIL(dctx->viewlist)->zonelist, zle, link);
  5413. return (ISC_R_SUCCESS);
  5414. }
  5415. static isc_result_t
  5416. add_view_tolist(struct dumpcontext *dctx, dns_view_t *view) {
  5417. struct viewlistentry *vle;
  5418. isc_result_t result = ISC_R_SUCCESS;
  5419. /*
  5420. * Prevent duplicate views.
  5421. */
  5422. for (vle = ISC_LIST_HEAD(dctx->viewlist);
  5423. vle != NULL;
  5424. vle = ISC_LIST_NEXT(vle, link))
  5425. if (vle->view == view)
  5426. return (ISC_R_SUCCESS);
  5427. vle = isc_mem_get(dctx->mctx, sizeof *vle);
  5428. if (vle == NULL)
  5429. return (ISC_R_NOMEMORY);
  5430. vle->view = NULL;
  5431. dns_view_attach(view, &vle->view);
  5432. ISC_LINK_INIT(vle, link);
  5433. ISC_LIST_INIT(vle->zonelist);
  5434. ISC_LIST_APPEND(dctx->viewlist, vle, link);
  5435. if (dctx->dumpzones)
  5436. result = dns_zt_apply(view->zonetable, ISC_TRUE,
  5437. add_zone_tolist, dctx);
  5438. return (result);
  5439. }
  5440. static void
  5441. dumpcontext_destroy(struct dumpcontext *dctx) {
  5442. struct viewlistentry *vle;
  5443. struct zonelistentry *zle;
  5444. vle = ISC_LIST_HEAD(dctx->viewlist);
  5445. while (vle != NULL) {
  5446. ISC_LIST_UNLINK(dctx->viewlist, vle, link);
  5447. zle = ISC_LIST_HEAD(vle->zonelist);
  5448. while (zle != NULL) {
  5449. ISC_LIST_UNLINK(vle->zonelist, zle, link);
  5450. dns_zone_detach(&zle->zone);
  5451. isc_mem_put(dctx->mctx, zle, sizeof *zle);
  5452. zle = ISC_LIST_HEAD(vle->zonelist);
  5453. }
  5454. dns_view_detach(&vle->view);
  5455. isc_mem_put(dctx->mctx, vle, sizeof *vle);
  5456. vle = ISC_LIST_HEAD(dctx->viewlist);
  5457. }
  5458. if (dctx->version != NULL)
  5459. dns_db_closeversion(dctx->db, &dctx->version, ISC_FALSE);
  5460. if (dctx->db != NULL)
  5461. dns_db_detach(&dctx->db);
  5462. if (dctx->cache != NULL)
  5463. dns_db_detach(&dctx->cache);
  5464. if (dctx->task != NULL)
  5465. isc_task_detach(&dctx->task);
  5466. if (dctx->fp != NULL)
  5467. (void)isc_stdio_close(dctx->fp);
  5468. if (dctx->mdctx != NULL)
  5469. dns_dumpctx_detach(&dctx->mdctx);
  5470. isc_mem_put(dctx->mctx, dctx, sizeof *dctx);
  5471. }
  5472. static void
  5473. dumpdone(void *arg, isc_result_t result) {
  5474. struct dumpcontext *dctx = arg;
  5475. char buf[1024+32];
  5476. const dns_master_style_t *style;
  5477. if (result != ISC_R_SUCCESS)
  5478. goto cleanup;
  5479. if (dctx->mdctx != NULL)
  5480. dns_dumpctx_detach(&dctx->mdctx);
  5481. if (dctx->view == NULL) {
  5482. dctx->view = ISC_LIST_HEAD(dctx->viewlist);
  5483. if (dctx->view == NULL)
  5484. goto done;
  5485. INSIST(dctx->zone == NULL);
  5486. } else
  5487. goto resume;
  5488. nextview:
  5489. fprintf(dctx->fp, ";\n; Start view %s\n;\n", dctx->view->view->name);
  5490. resume:
  5491. if (dctx->dumpcache && dns_view_iscacheshared(dctx->view->view)) {
  5492. fprintf(dctx->fp,
  5493. ";\n; Cache of view '%s' is shared as '%s'\n",
  5494. dctx->view->view->name,
  5495. dns_cache_getname(dctx->view->view->cache));
  5496. } else if (dctx->zone == NULL && dctx->cache == NULL &&
  5497. dctx->dumpcache)
  5498. {
  5499. style = &dns_master_style_cache;
  5500. /* start cache dump */
  5501. if (dctx->view->view->cachedb != NULL)
  5502. dns_db_attach(dctx->view->view->cachedb, &dctx->cache);
  5503. if (dctx->cache != NULL) {
  5504. fprintf(dctx->fp,
  5505. ";\n; Cache dump of view '%s' (cache %s)\n;\n",
  5506. dctx->view->view->name,
  5507. dns_cache_getname(dctx->view->view->cache));
  5508. result = dns_master_dumptostreaminc(dctx->mctx,
  5509. dctx->cache, NULL,
  5510. style, dctx->fp,
  5511. dctx->task,
  5512. dumpdone, dctx,
  5513. &dctx->mdctx);
  5514. if (result == DNS_R_CONTINUE)
  5515. return;
  5516. if (result == ISC_R_NOTIMPLEMENTED)
  5517. fprintf(dctx->fp, "; %s\n",
  5518. dns_result_totext(result));
  5519. else if (result != ISC_R_SUCCESS)
  5520. goto cleanup;
  5521. }
  5522. }
  5523. if (dctx->cache != NULL) {
  5524. dns_adb_dump(dctx->view->view->adb, dctx->fp);
  5525. dns_resolver_printbadcache(dctx->view->view->resolver,
  5526. dctx->fp);
  5527. dns_db_detach(&dctx->cache);
  5528. }
  5529. if (dctx->dumpzones) {
  5530. style = &dns_master_style_full;
  5531. nextzone:
  5532. if (dctx->version != NULL)
  5533. dns_db_closeversion(dctx->db, &dctx->version,
  5534. ISC_FALSE);
  5535. if (dctx->db != NULL)
  5536. dns_db_detach(&dctx->db);
  5537. if (dctx->zone == NULL)
  5538. dctx->zone = ISC_LIST_HEAD(dctx->view->zonelist);
  5539. else
  5540. dctx->zone = ISC_LIST_NEXT(dctx->zone, link);
  5541. if (dctx->zone != NULL) {
  5542. /* start zone dump */
  5543. dns_zone_name(dctx->zone->zone, buf, sizeof(buf));
  5544. fprintf(dctx->fp, ";\n; Zone dump of '%s'\n;\n", buf);
  5545. result = dns_zone_getdb(dctx->zone->zone, &dctx->db);
  5546. if (result != ISC_R_SUCCESS) {
  5547. fprintf(dctx->fp, "; %s\n",
  5548. dns_result_totext(result));
  5549. goto nextzone;
  5550. }
  5551. dns_db_currentversion(dctx->db, &dctx->version);
  5552. result = dns_master_dumptostreaminc(dctx->mctx,
  5553. dctx->db,
  5554. dctx->version,
  5555. style, dctx->fp,
  5556. dctx->task,
  5557. dumpdone, dctx,
  5558. &dctx->mdctx);
  5559. if (result == DNS_R_CONTINUE)
  5560. return;
  5561. if (result == ISC_R_NOTIMPLEMENTED) {
  5562. fprintf(dctx->fp, "; %s\n",
  5563. dns_result_totext(result));
  5564. result = ISC_R_SUCCESS;
  5565. POST(result);
  5566. goto nextzone;
  5567. }
  5568. if (result != ISC_R_SUCCESS)
  5569. goto cleanup;
  5570. }
  5571. }
  5572. if (dctx->view != NULL)
  5573. dctx->view = ISC_LIST_NEXT(dctx->view, link);
  5574. if (dctx->view != NULL)
  5575. goto nextview;
  5576. done:
  5577. fprintf(dctx->fp, "; Dump complete\n");
  5578. result = isc_stdio_flush(dctx->fp);
  5579. if (result == ISC_R_SUCCESS)
  5580. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5581. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5582. "dumpdb complete");
  5583. cleanup:
  5584. if (result != ISC_R_SUCCESS)
  5585. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5586. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5587. "dumpdb failed: %s", dns_result_totext(result));
  5588. dumpcontext_destroy(dctx);
  5589. }
  5590. isc_result_t
  5591. ns_server_dumpdb(ns_server_t *server, char *args) {
  5592. struct dumpcontext *dctx = NULL;
  5593. dns_view_t *view;
  5594. isc_result_t result;
  5595. char *ptr;
  5596. const char *sep;
  5597. /* Skip the command name. */
  5598. ptr = next_token(&args, " \t");
  5599. if (ptr == NULL)
  5600. return (ISC_R_UNEXPECTEDEND);
  5601. dctx = isc_mem_get(server->mctx, sizeof(*dctx));
  5602. if (dctx == NULL)
  5603. return (ISC_R_NOMEMORY);
  5604. dctx->mctx = server->mctx;
  5605. dctx->dumpcache = ISC_TRUE;
  5606. dctx->dumpzones = ISC_FALSE;
  5607. dctx->fp = NULL;
  5608. ISC_LIST_INIT(dctx->viewlist);
  5609. dctx->view = NULL;
  5610. dctx->zone = NULL;
  5611. dctx->cache = NULL;
  5612. dctx->mdctx = NULL;
  5613. dctx->db = NULL;
  5614. dctx->cache = NULL;
  5615. dctx->task = NULL;
  5616. dctx->version = NULL;
  5617. isc_task_attach(server->task, &dctx->task);
  5618. CHECKMF(isc_stdio_open(server->dumpfile, "w", &dctx->fp),
  5619. "could not open dump file", server->dumpfile);
  5620. sep = (args == NULL) ? "" : ": ";
  5621. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5622. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5623. "dumpdb started%s%s", sep, (args != NULL) ? args : "");
  5624. ptr = next_token(&args, " \t");
  5625. if (ptr != NULL && strcmp(ptr, "-all") == 0) {
  5626. dctx->dumpzones = ISC_TRUE;
  5627. dctx->dumpcache = ISC_TRUE;
  5628. ptr = next_token(&args, " \t");
  5629. } else if (ptr != NULL && strcmp(ptr, "-cache") == 0) {
  5630. dctx->dumpzones = ISC_FALSE;
  5631. dctx->dumpcache = ISC_TRUE;
  5632. ptr = next_token(&args, " \t");
  5633. } else if (ptr != NULL && strcmp(ptr, "-zones") == 0) {
  5634. dctx->dumpzones = ISC_TRUE;
  5635. dctx->dumpcache = ISC_FALSE;
  5636. ptr = next_token(&args, " \t");
  5637. }
  5638. nextview:
  5639. for (view = ISC_LIST_HEAD(server->viewlist);
  5640. view != NULL;
  5641. view = ISC_LIST_NEXT(view, link))
  5642. {
  5643. if (ptr != NULL && strcmp(view->name, ptr) != 0)
  5644. continue;
  5645. CHECK(add_view_tolist(dctx, view));
  5646. }
  5647. if (ptr != NULL) {
  5648. ptr = next_token(&args, " \t");
  5649. if (ptr != NULL)
  5650. goto nextview;
  5651. }
  5652. dumpdone(dctx, ISC_R_SUCCESS);
  5653. return (ISC_R_SUCCESS);
  5654. cleanup:
  5655. if (dctx != NULL)
  5656. dumpcontext_destroy(dctx);
  5657. return (result);
  5658. }
  5659. isc_result_t
  5660. ns_server_dumpsecroots(ns_server_t *server, char *args) {
  5661. dns_view_t *view;
  5662. dns_keytable_t *secroots = NULL;
  5663. isc_result_t result;
  5664. char *ptr;
  5665. FILE *fp = NULL;
  5666. isc_time_t now;
  5667. char tbuf[64];
  5668. /* Skip the command name. */
  5669. ptr = next_token(&args, " \t");
  5670. if (ptr == NULL)
  5671. return (ISC_R_UNEXPECTEDEND);
  5672. ptr = next_token(&args, " \t");
  5673. CHECKMF(isc_stdio_open(server->secrootsfile, "w", &fp),
  5674. "could not open secroots dump file", server->secrootsfile);
  5675. TIME_NOW(&now);
  5676. isc_time_formattimestamp(&now, tbuf, sizeof(tbuf));
  5677. fprintf(fp, "%s\n", tbuf);
  5678. do {
  5679. for (view = ISC_LIST_HEAD(server->viewlist);
  5680. view != NULL;
  5681. view = ISC_LIST_NEXT(view, link))
  5682. {
  5683. if (ptr != NULL && strcmp(view->name, ptr) != 0)
  5684. continue;
  5685. if (secroots != NULL)
  5686. dns_keytable_detach(&secroots);
  5687. result = dns_view_getsecroots(view, &secroots);
  5688. if (result == ISC_R_NOTFOUND) {
  5689. result = ISC_R_SUCCESS;
  5690. continue;
  5691. }
  5692. fprintf(fp, "\n Start view %s\n\n", view->name);
  5693. result = dns_keytable_dump(secroots, fp);
  5694. if (result != ISC_R_SUCCESS)
  5695. fprintf(fp, " dumpsecroots failed: %s\n",
  5696. isc_result_totext(result));
  5697. }
  5698. if (ptr != NULL)
  5699. ptr = next_token(&args, " \t");
  5700. } while (ptr != NULL);
  5701. cleanup:
  5702. if (secroots != NULL)
  5703. dns_keytable_detach(&secroots);
  5704. if (fp != NULL)
  5705. (void)isc_stdio_close(fp);
  5706. if (result == ISC_R_SUCCESS)
  5707. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5708. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5709. "dumpsecroots complete");
  5710. else
  5711. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5712. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5713. "dumpsecroots failed: %s",
  5714. dns_result_totext(result));
  5715. return (result);
  5716. }
  5717. isc_result_t
  5718. ns_server_dumprecursing(ns_server_t *server) {
  5719. FILE *fp = NULL;
  5720. isc_result_t result;
  5721. CHECKMF(isc_stdio_open(server->recfile, "w", &fp),
  5722. "could not open dump file", server->recfile);
  5723. fprintf(fp,";\n; Recursing Queries\n;\n");
  5724. ns_interfacemgr_dumprecursing(fp, server->interfacemgr);
  5725. fprintf(fp, "; Dump complete\n");
  5726. cleanup:
  5727. if (fp != NULL)
  5728. result = isc_stdio_close(fp);
  5729. if (result == ISC_R_SUCCESS)
  5730. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5731. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5732. "dumprecursing complete");
  5733. else
  5734. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5735. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5736. "dumprecursing failed: %s",
  5737. dns_result_totext(result));
  5738. return (result);
  5739. }
  5740. isc_result_t
  5741. ns_server_setdebuglevel(ns_server_t *server, char *args) {
  5742. char *ptr;
  5743. char *levelstr;
  5744. char *endp;
  5745. long newlevel;
  5746. UNUSED(server);
  5747. /* Skip the command name. */
  5748. ptr = next_token(&args, " \t");
  5749. if (ptr == NULL)
  5750. return (ISC_R_UNEXPECTEDEND);
  5751. /* Look for the new level name. */
  5752. levelstr = next_token(&args, " \t");
  5753. if (levelstr == NULL) {
  5754. if (ns_g_debuglevel < 99)
  5755. ns_g_debuglevel++;
  5756. } else {
  5757. newlevel = strtol(levelstr, &endp, 10);
  5758. if (*endp != '\0' || newlevel < 0 || newlevel > 99)
  5759. return (ISC_R_RANGE);
  5760. ns_g_debuglevel = (unsigned int)newlevel;
  5761. }
  5762. isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
  5763. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5764. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5765. "debug level is now %d", ns_g_debuglevel);
  5766. return (ISC_R_SUCCESS);
  5767. }
  5768. isc_result_t
  5769. ns_server_validation(ns_server_t *server, char *args) {
  5770. char *ptr, *viewname;
  5771. dns_view_t *view;
  5772. isc_boolean_t changed = ISC_FALSE;
  5773. isc_result_t result;
  5774. isc_boolean_t enable;
  5775. /* Skip the command name. */
  5776. ptr = next_token(&args, " \t");
  5777. if (ptr == NULL)
  5778. return (ISC_R_UNEXPECTEDEND);
  5779. /* Find out what we are to do. */
  5780. ptr = next_token(&args, " \t");
  5781. if (ptr == NULL)
  5782. return (ISC_R_UNEXPECTEDEND);
  5783. if (!strcasecmp(ptr, "on") || !strcasecmp(ptr, "yes") ||
  5784. !strcasecmp(ptr, "enable") || !strcasecmp(ptr, "true"))
  5785. enable = ISC_TRUE;
  5786. else if (!strcasecmp(ptr, "off") || !strcasecmp(ptr, "no") ||
  5787. !strcasecmp(ptr, "disable") || !strcasecmp(ptr, "false"))
  5788. enable = ISC_FALSE;
  5789. else
  5790. return (DNS_R_SYNTAX);
  5791. /* Look for the view name. */
  5792. viewname = next_token(&args, " \t");
  5793. result = isc_task_beginexclusive(server->task);
  5794. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  5795. for (view = ISC_LIST_HEAD(server->viewlist);
  5796. view != NULL;
  5797. view = ISC_LIST_NEXT(view, link))
  5798. {
  5799. if (viewname != NULL && strcasecmp(viewname, view->name) != 0)
  5800. continue;
  5801. result = dns_view_flushcache(view);
  5802. if (result != ISC_R_SUCCESS)
  5803. goto out;
  5804. view->enablevalidation = enable;
  5805. changed = ISC_TRUE;
  5806. }
  5807. if (changed)
  5808. result = ISC_R_SUCCESS;
  5809. else
  5810. result = ISC_R_FAILURE;
  5811. out:
  5812. isc_task_endexclusive(server->task);
  5813. return (result);
  5814. }
  5815. isc_result_t
  5816. ns_server_flushcache(ns_server_t *server, char *args) {
  5817. char *ptr, *viewname;
  5818. dns_view_t *view;
  5819. isc_boolean_t flushed;
  5820. isc_boolean_t found;
  5821. isc_result_t result;
  5822. ns_cache_t *nsc;
  5823. /* Skip the command name. */
  5824. ptr = next_token(&args, " \t");
  5825. if (ptr == NULL)
  5826. return (ISC_R_UNEXPECTEDEND);
  5827. /* Look for the view name. */
  5828. viewname = next_token(&args, " \t");
  5829. result = isc_task_beginexclusive(server->task);
  5830. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  5831. flushed = ISC_TRUE;
  5832. found = ISC_FALSE;
  5833. /*
  5834. * Flushing a cache is tricky when caches are shared by multiple views.
  5835. * We first identify which caches should be flushed in the local cache
  5836. * list, flush these caches, and then update other views that refer to
  5837. * the flushed cache DB.
  5838. */
  5839. if (viewname != NULL) {
  5840. /*
  5841. * Mark caches that need to be flushed. This is an O(#view^2)
  5842. * operation in the very worst case, but should be normally
  5843. * much more lightweight because only a few (most typically just
  5844. * one) views will match.
  5845. */
  5846. for (view = ISC_LIST_HEAD(server->viewlist);
  5847. view != NULL;
  5848. view = ISC_LIST_NEXT(view, link))
  5849. {
  5850. if (strcasecmp(viewname, view->name) != 0)
  5851. continue;
  5852. found = ISC_TRUE;
  5853. for (nsc = ISC_LIST_HEAD(server->cachelist);
  5854. nsc != NULL;
  5855. nsc = ISC_LIST_NEXT(nsc, link)) {
  5856. if (nsc->cache == view->cache)
  5857. break;
  5858. }
  5859. INSIST(nsc != NULL);
  5860. nsc->needflush = ISC_TRUE;
  5861. }
  5862. } else
  5863. found = ISC_TRUE;
  5864. /* Perform flush */
  5865. for (nsc = ISC_LIST_HEAD(server->cachelist);
  5866. nsc != NULL;
  5867. nsc = ISC_LIST_NEXT(nsc, link)) {
  5868. if (viewname != NULL && !nsc->needflush)
  5869. continue;
  5870. nsc->needflush = ISC_TRUE;
  5871. result = dns_view_flushcache2(nsc->primaryview, ISC_FALSE);
  5872. if (result != ISC_R_SUCCESS) {
  5873. flushed = ISC_FALSE;
  5874. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5875. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5876. "flushing cache in view '%s' failed: %s",
  5877. nsc->primaryview->name,
  5878. isc_result_totext(result));
  5879. }
  5880. }
  5881. /*
  5882. * Fix up views that share a flushed cache: let the views update the
  5883. * cache DB they're referring to. This could also be an expensive
  5884. * operation, but should typically be marginal: the inner loop is only
  5885. * necessary for views that share a cache, and if there are many such
  5886. * views the number of shared cache should normally be small.
  5887. * A worst case is that we have n views and n/2 caches, each shared by
  5888. * two views. Then this will be a O(n^2/4) operation.
  5889. */
  5890. for (view = ISC_LIST_HEAD(server->viewlist);
  5891. view != NULL;
  5892. view = ISC_LIST_NEXT(view, link))
  5893. {
  5894. if (!dns_view_iscacheshared(view))
  5895. continue;
  5896. for (nsc = ISC_LIST_HEAD(server->cachelist);
  5897. nsc != NULL;
  5898. nsc = ISC_LIST_NEXT(nsc, link)) {
  5899. if (!nsc->needflush || nsc->cache != view->cache)
  5900. continue;
  5901. result = dns_view_flushcache2(view, ISC_TRUE);
  5902. if (result != ISC_R_SUCCESS) {
  5903. flushed = ISC_FALSE;
  5904. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5905. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5906. "fixing cache in view '%s' "
  5907. "failed: %s", view->name,
  5908. isc_result_totext(result));
  5909. }
  5910. }
  5911. }
  5912. /* Cleanup the cache list. */
  5913. for (nsc = ISC_LIST_HEAD(server->cachelist);
  5914. nsc != NULL;
  5915. nsc = ISC_LIST_NEXT(nsc, link)) {
  5916. nsc->needflush = ISC_FALSE;
  5917. }
  5918. if (flushed && found) {
  5919. if (viewname != NULL)
  5920. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5921. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5922. "flushing cache in view '%s' succeeded",
  5923. viewname);
  5924. else
  5925. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5926. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5927. "flushing caches in all views succeeded");
  5928. result = ISC_R_SUCCESS;
  5929. } else {
  5930. if (!found) {
  5931. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5932. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5933. "flushing cache in view '%s' failed: "
  5934. "view not found", viewname);
  5935. result = ISC_R_NOTFOUND;
  5936. } else
  5937. result = ISC_R_FAILURE;
  5938. }
  5939. isc_task_endexclusive(server->task);
  5940. return (result);
  5941. }
  5942. isc_result_t
  5943. ns_server_flushname(ns_server_t *server, char *args) {
  5944. char *ptr, *target, *viewname;
  5945. dns_view_t *view;
  5946. isc_boolean_t flushed;
  5947. isc_boolean_t found;
  5948. isc_result_t result;
  5949. isc_buffer_t b;
  5950. dns_fixedname_t fixed;
  5951. dns_name_t *name;
  5952. /* Skip the command name. */
  5953. ptr = next_token(&args, " \t");
  5954. if (ptr == NULL)
  5955. return (ISC_R_UNEXPECTEDEND);
  5956. /* Find the domain name to flush. */
  5957. target = next_token(&args, " \t");
  5958. if (target == NULL)
  5959. return (ISC_R_UNEXPECTEDEND);
  5960. isc_buffer_init(&b, target, strlen(target));
  5961. isc_buffer_add(&b, strlen(target));
  5962. dns_fixedname_init(&fixed);
  5963. name = dns_fixedname_name(&fixed);
  5964. result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
  5965. if (result != ISC_R_SUCCESS)
  5966. return (result);
  5967. /* Look for the view name. */
  5968. viewname = next_token(&args, " \t");
  5969. result = isc_task_beginexclusive(server->task);
  5970. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  5971. flushed = ISC_TRUE;
  5972. found = ISC_FALSE;
  5973. for (view = ISC_LIST_HEAD(server->viewlist);
  5974. view != NULL;
  5975. view = ISC_LIST_NEXT(view, link))
  5976. {
  5977. if (viewname != NULL && strcasecmp(viewname, view->name) != 0)
  5978. continue;
  5979. found = ISC_TRUE;
  5980. /*
  5981. * It's a little inefficient to try flushing name for all views
  5982. * if some of the views share a single cache. But since the
  5983. * operation is lightweight we prefer simplicity here.
  5984. */
  5985. result = dns_view_flushname(view, name);
  5986. if (result != ISC_R_SUCCESS) {
  5987. flushed = ISC_FALSE;
  5988. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5989. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  5990. "flushing name '%s' in cache view '%s' "
  5991. "failed: %s", target, view->name,
  5992. isc_result_totext(result));
  5993. }
  5994. }
  5995. if (flushed && found) {
  5996. if (viewname != NULL)
  5997. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  5998. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  5999. "flushing name '%s' in cache view '%s' "
  6000. "succeeded", target, viewname);
  6001. else
  6002. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  6003. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  6004. "flushing name '%s' in all cache views "
  6005. "succeeded", target);
  6006. result = ISC_R_SUCCESS;
  6007. } else {
  6008. if (!found)
  6009. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  6010. NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
  6011. "flushing name '%s' in cache view '%s' "
  6012. "failed: view not found", target,
  6013. viewname);
  6014. result = ISC_R_FAILURE;
  6015. }
  6016. isc_task_endexclusive(server->task);
  6017. return (result);
  6018. }
  6019. isc_result_t
  6020. ns_server_status(ns_server_t *server, isc_buffer_t *text) {
  6021. int zonecount, xferrunning, xferdeferred, soaqueries;
  6022. unsigned int n;
  6023. const char *ob = "", *cb = "", *alt = "";
  6024. if (ns_g_server->version_set) {
  6025. ob = " (";
  6026. cb = ")";
  6027. if (ns_g_server->version == NULL)
  6028. alt = "version.bind/txt/ch disabled";
  6029. else
  6030. alt = ns_g_server->version;
  6031. }
  6032. zonecount = dns_zonemgr_getcount(server->zonemgr, DNS_ZONESTATE_ANY);
  6033. xferrunning = dns_zonemgr_getcount(server->zonemgr,
  6034. DNS_ZONESTATE_XFERRUNNING);
  6035. xferdeferred = dns_zonemgr_getcount(server->zonemgr,
  6036. DNS_ZONESTATE_XFERDEFERRED);
  6037. soaqueries = dns_zonemgr_getcount(server->zonemgr,
  6038. DNS_ZONESTATE_SOAQUERY);
  6039. n = snprintf((char *)isc_buffer_used(text),
  6040. isc_buffer_availablelength(text),
  6041. "version: %s%s%s%s\n"
  6042. #ifdef ISC_PLATFORM_USETHREADS
  6043. "CPUs found: %u\n"
  6044. "worker threads: %u\n"
  6045. #endif
  6046. "number of zones: %u\n"
  6047. "debug level: %d\n"
  6048. "xfers running: %u\n"
  6049. "xfers deferred: %u\n"
  6050. "soa queries in progress: %u\n"
  6051. "query logging is %s\n"
  6052. "recursive clients: %d/%d/%d\n"
  6053. "tcp clients: %d/%d\n"
  6054. "server is up and running",
  6055. ns_g_version, ob, alt, cb,
  6056. #ifdef ISC_PLATFORM_USETHREADS
  6057. ns_g_cpus_detected, ns_g_cpus,
  6058. #endif
  6059. zonecount, ns_g_debuglevel, xferrunning, xferdeferred,
  6060. soaqueries, server->log_queries ? "ON" : "OFF",
  6061. server->recursionquota.used, server->recursionquota.soft,
  6062. server->recursionquota.max,
  6063. server->tcpquota.used, server->tcpquota.max);
  6064. if (n >= isc_buffer_availablelength(text))
  6065. return (ISC_R_NOSPACE);
  6066. isc_buffer_add(text, n);
  6067. return (ISC_R_SUCCESS);
  6068. }
  6069. static isc_result_t
  6070. delete_keynames(dns_tsig_keyring_t *ring, char *target,
  6071. unsigned int *foundkeys)
  6072. {
  6073. char namestr[DNS_NAME_FORMATSIZE];
  6074. isc_result_t result;
  6075. dns_rbtnodechain_t chain;
  6076. dns_name_t foundname;
  6077. dns_fixedname_t fixedorigin;
  6078. dns_name_t *origin;
  6079. dns_rbtnode_t *node;
  6080. dns_tsigkey_t *tkey;
  6081. dns_name_init(&foundname, NULL);
  6082. dns_fixedname_init(&fixedorigin);
  6083. origin = dns_fixedname_name(&fixedorigin);
  6084. again:
  6085. dns_rbtnodechain_init(&chain, ring->mctx);
  6086. result = dns_rbtnodechain_first(&chain, ring->keys, &foundname,
  6087. origin);
  6088. if (result == ISC_R_NOTFOUND) {
  6089. dns_rbtnodechain_invalidate(&chain);
  6090. return (ISC_R_SUCCESS);
  6091. }
  6092. if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
  6093. dns_rbtnodechain_invalidate(&chain);
  6094. return (result);
  6095. }
  6096. for (;;) {
  6097. node = NULL;
  6098. dns_rbtnodechain_current(&chain, &foundname, origin, &node);
  6099. tkey = node->data;
  6100. if (tkey != NULL) {
  6101. if (!tkey->generated)
  6102. goto nextkey;
  6103. dns_name_format(&tkey->name, namestr, sizeof(namestr));
  6104. if (strcmp(namestr, target) == 0) {
  6105. (*foundkeys)++;
  6106. dns_rbtnodechain_invalidate(&chain);
  6107. (void)dns_rbt_deletename(ring->keys,
  6108. &tkey->name,
  6109. ISC_FALSE);
  6110. goto again;
  6111. }
  6112. }
  6113. nextkey:
  6114. result = dns_rbtnodechain_next(&chain, &foundname, origin);
  6115. if (result == ISC_R_NOMORE)
  6116. break;
  6117. if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
  6118. dns_rbtnodechain_invalidate(&chain);
  6119. return (result);
  6120. }
  6121. }
  6122. return (ISC_R_SUCCESS);
  6123. }
  6124. isc_result_t
  6125. ns_server_tsigdelete(ns_server_t *server, char *command, isc_buffer_t *text) {
  6126. isc_result_t result;
  6127. unsigned int n;
  6128. dns_view_t *view;
  6129. unsigned int foundkeys = 0;
  6130. char *target;
  6131. char *viewname;
  6132. (void)next_token(&command, " \t"); /* skip command name */
  6133. target = next_token(&command, " \t");
  6134. if (target == NULL)
  6135. return (ISC_R_UNEXPECTEDEND);
  6136. viewname = next_token(&command, " \t");
  6137. result = isc_task_beginexclusive(server->task);
  6138. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  6139. for (view = ISC_LIST_HEAD(server->viewlist);
  6140. view != NULL;
  6141. view = ISC_LIST_NEXT(view, link)) {
  6142. if (viewname == NULL || strcmp(view->name, viewname) == 0) {
  6143. RWLOCK(&view->dynamickeys->lock, isc_rwlocktype_write);
  6144. result = delete_keynames(view->dynamickeys, target,
  6145. &foundkeys);
  6146. RWUNLOCK(&view->dynamickeys->lock,
  6147. isc_rwlocktype_write);
  6148. if (result != ISC_R_SUCCESS) {
  6149. isc_task_endexclusive(server->task);
  6150. return (result);
  6151. }
  6152. }
  6153. }
  6154. isc_task_endexclusive(server->task);
  6155. n = snprintf((char *)isc_buffer_used(text),
  6156. isc_buffer_availablelength(text),
  6157. "%d tsig keys deleted.\n", foundkeys);
  6158. if (n >= isc_buffer_availablelength(text))
  6159. return (ISC_R_NOSPACE);
  6160. isc_buffer_add(text, n);
  6161. return (ISC_R_SUCCESS);
  6162. }
  6163. static isc_result_t
  6164. list_keynames(dns_view_t *view, dns_tsig_keyring_t *ring, isc_buffer_t *text,
  6165. unsigned int *foundkeys)
  6166. {
  6167. char namestr[DNS_NAME_FORMATSIZE];
  6168. char creatorstr[DNS_NAME_FORMATSIZE];
  6169. isc_result_t result;
  6170. dns_rbtnodechain_t chain;
  6171. dns_name_t foundname;
  6172. dns_fixedname_t fixedorigin;
  6173. dns_name_t *origin;
  6174. dns_rbtnode_t *node;
  6175. dns_tsigkey_t *tkey;
  6176. unsigned int n;
  6177. const char *viewname;
  6178. if (view != NULL)
  6179. viewname = view->name;
  6180. else
  6181. viewname = "(global)";
  6182. dns_name_init(&foundname, NULL);
  6183. dns_fixedname_init(&fixedorigin);
  6184. origin = dns_fixedname_name(&fixedorigin);
  6185. dns_rbtnodechain_init(&chain, ring->mctx);
  6186. result = dns_rbtnodechain_first(&chain, ring->keys, &foundname,
  6187. origin);
  6188. if (result == ISC_R_NOTFOUND) {
  6189. dns_rbtnodechain_invalidate(&chain);
  6190. return (ISC_R_SUCCESS);
  6191. }
  6192. if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
  6193. dns_rbtnodechain_invalidate(&chain);
  6194. return (result);
  6195. }
  6196. for (;;) {
  6197. node = NULL;
  6198. dns_rbtnodechain_current(&chain, &foundname, origin, &node);
  6199. tkey = node->data;
  6200. if (tkey != NULL) {
  6201. (*foundkeys)++;
  6202. dns_name_format(&tkey->name, namestr, sizeof(namestr));
  6203. if (tkey->generated) {
  6204. dns_name_format(tkey->creator, creatorstr,
  6205. sizeof(creatorstr));
  6206. n = snprintf((char *)isc_buffer_used(text),
  6207. isc_buffer_availablelength(text),
  6208. "view \"%s\"; type \"dynamic\"; key \"%s\"; creator \"%s\";\n",
  6209. viewname, namestr, creatorstr);
  6210. } else {
  6211. n = snprintf((char *)isc_buffer_used(text),
  6212. isc_buffer_availablelength(text),
  6213. "view \"%s\"; type \"static\"; key \"%s\";\n",
  6214. viewname, namestr);
  6215. }
  6216. if (n >= isc_buffer_availablelength(text)) {
  6217. dns_rbtnodechain_invalidate(&chain);
  6218. return (ISC_R_NOSPACE);
  6219. }
  6220. isc_buffer_add(text, n);
  6221. }
  6222. result = dns_rbtnodechain_next(&chain, &foundname, origin);
  6223. if (result == ISC_R_NOMORE)
  6224. break;
  6225. if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
  6226. dns_rbtnodechain_invalidate(&chain);
  6227. return (result);
  6228. }
  6229. }
  6230. return (ISC_R_SUCCESS);
  6231. }
  6232. isc_result_t
  6233. ns_server_tsiglist(ns_server_t *server, isc_buffer_t *text) {
  6234. isc_result_t result;
  6235. unsigned int n;
  6236. dns_view_t *view;
  6237. unsigned int foundkeys = 0;
  6238. result = isc_task_beginexclusive(server->task);
  6239. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  6240. for (view = ISC_LIST_HEAD(server->viewlist);
  6241. view != NULL;
  6242. view = ISC_LIST_NEXT(view, link)) {
  6243. RWLOCK(&view->statickeys->lock, isc_rwlocktype_read);
  6244. result = list_keynames(view, view->statickeys, text,
  6245. &foundkeys);
  6246. RWUNLOCK(&view->statickeys->lock, isc_rwlocktype_read);
  6247. if (result != ISC_R_SUCCESS) {
  6248. isc_task_endexclusive(server->task);
  6249. return (result);
  6250. }
  6251. RWLOCK(&view->dynamickeys->lock, isc_rwlocktype_read);
  6252. result = list_keynames(view, view->dynamickeys, text,
  6253. &foundkeys);
  6254. RWUNLOCK(&view->dynamickeys->lock, isc_rwlocktype_read);
  6255. if (result != ISC_R_SUCCESS) {
  6256. isc_task_endexclusive(server->task);
  6257. return (result);
  6258. }
  6259. }
  6260. isc_task_endexclusive(server->task);
  6261. if (foundkeys == 0) {
  6262. n = snprintf((char *)isc_buffer_used(text),
  6263. isc_buffer_availablelength(text),
  6264. "no tsig keys found.\n");
  6265. if (n >= isc_buffer_availablelength(text))
  6266. return (ISC_R_NOSPACE);
  6267. isc_buffer_add(text, n);
  6268. }
  6269. return (ISC_R_SUCCESS);
  6270. }
  6271. /*
  6272. * Act on a "sign" or "loadkeys" command from the command channel.
  6273. */
  6274. isc_result_t
  6275. ns_server_rekey(ns_server_t *server, char *args) {
  6276. isc_result_t result;
  6277. dns_zone_t *zone = NULL;
  6278. dns_zonetype_t type;
  6279. isc_uint16_t keyopts;
  6280. isc_boolean_t fullsign = ISC_FALSE;
  6281. if (strncasecmp(args, NS_COMMAND_SIGN, strlen(NS_COMMAND_SIGN)) == 0)
  6282. fullsign = ISC_TRUE;
  6283. result = zone_from_args(server, args, &zone, NULL);
  6284. if (result != ISC_R_SUCCESS)
  6285. return (result);
  6286. if (zone == NULL)
  6287. return (ISC_R_UNEXPECTEDEND); /* XXX: or do all zones? */
  6288. type = dns_zone_gettype(zone);
  6289. if (type != dns_zone_master) {
  6290. dns_zone_detach(&zone);
  6291. return (DNS_R_NOTMASTER);
  6292. }
  6293. keyopts = dns_zone_getkeyopts(zone);
  6294. /* "rndc loadkeys" requires "auto-dnssec maintain". */
  6295. if ((keyopts & DNS_ZONEKEY_ALLOW) == 0)
  6296. result = ISC_R_NOPERM;
  6297. else if ((keyopts & DNS_ZONEKEY_MAINTAIN) == 0 && !fullsign)
  6298. result = ISC_R_NOPERM;
  6299. else
  6300. dns_zone_rekey(zone, fullsign);
  6301. dns_zone_detach(&zone);
  6302. return (result);
  6303. }
  6304. /*
  6305. * Act on a "freeze" or "thaw" command from the command channel.
  6306. */
  6307. isc_result_t
  6308. ns_server_freeze(ns_server_t *server, isc_boolean_t freeze, char *args,
  6309. isc_buffer_t *text)
  6310. {
  6311. isc_result_t result, tresult;
  6312. dns_zone_t *zone = NULL;
  6313. dns_zonetype_t type;
  6314. char classstr[DNS_RDATACLASS_FORMATSIZE];
  6315. char zonename[DNS_NAME_FORMATSIZE];
  6316. dns_view_t *view;
  6317. char *journal;
  6318. const char *vname, *sep;
  6319. isc_boolean_t frozen;
  6320. const char *msg = NULL;
  6321. result = zone_from_args(server, args, &zone, NULL);
  6322. if (result != ISC_R_SUCCESS)
  6323. return (result);
  6324. if (zone == NULL) {
  6325. result = isc_task_beginexclusive(server->task);
  6326. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  6327. tresult = ISC_R_SUCCESS;
  6328. for (view = ISC_LIST_HEAD(server->viewlist);
  6329. view != NULL;
  6330. view = ISC_LIST_NEXT(view, link)) {
  6331. result = dns_view_freezezones(view, freeze);
  6332. if (result != ISC_R_SUCCESS &&
  6333. tresult == ISC_R_SUCCESS)
  6334. tresult = result;
  6335. }
  6336. isc_task_endexclusive(server->task);
  6337. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  6338. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  6339. "%s all zones: %s",
  6340. freeze ? "freezing" : "thawing",
  6341. isc_result_totext(tresult));
  6342. return (tresult);
  6343. }
  6344. type = dns_zone_gettype(zone);
  6345. if (type != dns_zone_master) {
  6346. dns_zone_detach(&zone);
  6347. return (DNS_R_NOTMASTER);
  6348. }
  6349. result = isc_task_beginexclusive(server->task);
  6350. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  6351. frozen = dns_zone_getupdatedisabled(zone);
  6352. if (freeze) {
  6353. if (frozen) {
  6354. msg = "WARNING: The zone was already frozen.\n"
  6355. "Someone else may be editing it or "
  6356. "it may still be re-loading.";
  6357. result = DNS_R_FROZEN;
  6358. }
  6359. if (result == ISC_R_SUCCESS) {
  6360. result = dns_zone_flush(zone);
  6361. if (result != ISC_R_SUCCESS)
  6362. msg = "Flushing the zone updates to "
  6363. "disk failed.";
  6364. }
  6365. if (result == ISC_R_SUCCESS) {
  6366. journal = dns_zone_getjournal(zone);
  6367. if (journal != NULL)
  6368. (void)isc_file_remove(journal);
  6369. }
  6370. if (result == ISC_R_SUCCESS)
  6371. dns_zone_setupdatedisabled(zone, freeze);
  6372. } else {
  6373. if (frozen) {
  6374. result = dns_zone_loadandthaw(zone);
  6375. switch (result) {
  6376. case ISC_R_SUCCESS:
  6377. case DNS_R_UPTODATE:
  6378. msg = "The zone reload and thaw was "
  6379. "successful.";
  6380. result = ISC_R_SUCCESS;
  6381. break;
  6382. case DNS_R_CONTINUE:
  6383. msg = "A zone reload and thaw was started.\n"
  6384. "Check the logs to see the result.";
  6385. result = ISC_R_SUCCESS;
  6386. break;
  6387. }
  6388. }
  6389. }
  6390. isc_task_endexclusive(server->task);
  6391. if (msg != NULL && strlen(msg) < isc_buffer_availablelength(text))
  6392. isc_buffer_putmem(text, (const unsigned char *)msg,
  6393. strlen(msg) + 1);
  6394. view = dns_zone_getview(zone);
  6395. if (strcmp(view->name, "_default") == 0 ||
  6396. strcmp(view->name, "_bind") == 0)
  6397. {
  6398. vname = "";
  6399. sep = "";
  6400. } else {
  6401. vname = view->name;
  6402. sep = " ";
  6403. }
  6404. dns_rdataclass_format(dns_zone_getclass(zone), classstr,
  6405. sizeof(classstr));
  6406. dns_name_format(dns_zone_getorigin(zone),
  6407. zonename, sizeof(zonename));
  6408. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  6409. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  6410. "%s zone '%s/%s'%s%s: %s",
  6411. freeze ? "freezing" : "thawing",
  6412. zonename, classstr, sep, vname,
  6413. isc_result_totext(result));
  6414. dns_zone_detach(&zone);
  6415. return (result);
  6416. }
  6417. #ifdef HAVE_LIBSCF
  6418. /*
  6419. * This function adds a message for rndc to echo if named
  6420. * is managed by smf and is also running chroot.
  6421. */
  6422. isc_result_t
  6423. ns_smf_add_message(isc_buffer_t *text) {
  6424. unsigned int n;
  6425. n = snprintf((char *)isc_buffer_used(text),
  6426. isc_buffer_availablelength(text),
  6427. "use svcadm(1M) to manage named");
  6428. if (n >= isc_buffer_availablelength(text))
  6429. return (ISC_R_NOSPACE);
  6430. isc_buffer_add(text, n);
  6431. return (ISC_R_SUCCESS);
  6432. }
  6433. #endif /* HAVE_LIBSCF */
  6434. /*
  6435. * Act on an "addzone" command from the command channel.
  6436. */
  6437. isc_result_t
  6438. ns_server_add_zone(ns_server_t *server, char *args) {
  6439. isc_result_t result;
  6440. isc_buffer_t argbuf;
  6441. size_t arglen;
  6442. cfg_parser_t *parser = NULL;
  6443. cfg_obj_t *config = NULL;
  6444. const cfg_obj_t *vconfig = NULL;
  6445. const cfg_obj_t *views = NULL;
  6446. const cfg_obj_t *parms = NULL;
  6447. const cfg_obj_t *obj = NULL;
  6448. const cfg_listelt_t *element;
  6449. const char *zonename;
  6450. const char *classname = NULL;
  6451. const char *argp;
  6452. const char *viewname = NULL;
  6453. dns_rdataclass_t rdclass;
  6454. dns_view_t *view = 0;
  6455. isc_buffer_t buf, *nbuf = NULL;
  6456. dns_name_t dnsname;
  6457. dns_zone_t *zone = NULL;
  6458. FILE *fp = NULL;
  6459. struct cfg_context *cfg = NULL;
  6460. /* Try to parse the argument string */
  6461. arglen = strlen(args);
  6462. isc_buffer_init(&argbuf, args, arglen);
  6463. isc_buffer_add(&argbuf, strlen(args));
  6464. CHECK(cfg_parser_create(server->mctx, ns_g_lctx, &parser));
  6465. CHECK(cfg_parse_buffer(parser, &argbuf, &cfg_type_addzoneconf,
  6466. &config));
  6467. CHECK(cfg_map_get(config, "addzone", &parms));
  6468. zonename = cfg_obj_asstring(cfg_tuple_get(parms, "name"));
  6469. isc_buffer_init(&buf, zonename, strlen(zonename));
  6470. isc_buffer_add(&buf, strlen(zonename));
  6471. dns_name_init(&dnsname, NULL);
  6472. isc_buffer_allocate(server->mctx, &nbuf, 256);
  6473. dns_name_setbuffer(&dnsname, nbuf);
  6474. CHECK(dns_name_fromtext(&dnsname, &buf, dns_rootname, ISC_FALSE, NULL));
  6475. /* Make sense of optional class argument */
  6476. obj = cfg_tuple_get(parms, "class");
  6477. CHECK(ns_config_getclass(obj, dns_rdataclass_in, &rdclass));
  6478. if (rdclass != dns_rdataclass_in && obj)
  6479. classname = cfg_obj_asstring(obj);
  6480. /* Make sense of optional view argument */
  6481. obj = cfg_tuple_get(parms, "view");
  6482. if (obj && cfg_obj_isstring(obj))
  6483. viewname = cfg_obj_asstring(obj);
  6484. if (viewname == NULL || *viewname == '\0')
  6485. viewname = "_default";
  6486. CHECK(dns_viewlist_find(&server->viewlist, viewname, rdclass, &view));
  6487. /* Are we accepting new zones? */
  6488. if (view->new_zone_file == NULL) {
  6489. result = ISC_R_NOPERM;
  6490. goto cleanup;
  6491. }
  6492. cfg = (struct cfg_context *) view->new_zone_config;
  6493. if (cfg == NULL) {
  6494. result = ISC_R_FAILURE;
  6495. goto cleanup;
  6496. }
  6497. /* Zone shouldn't already exist */
  6498. result = dns_zt_find(view->zonetable, &dnsname, 0, NULL, &zone);
  6499. if (result == ISC_R_SUCCESS) {
  6500. result = ISC_R_EXISTS;
  6501. goto cleanup;
  6502. } else if (result == DNS_R_PARTIALMATCH) {
  6503. /* Create our sub-zone anyway */
  6504. dns_zone_detach(&zone);
  6505. zone = NULL;
  6506. }
  6507. else if (result != ISC_R_NOTFOUND)
  6508. goto cleanup;
  6509. /* Find the view statement */
  6510. cfg_map_get(cfg->config, "view", &views);
  6511. for (element = cfg_list_first(views);
  6512. element != NULL;
  6513. element = cfg_list_next(element))
  6514. {
  6515. const char *vname;
  6516. vconfig = cfg_listelt_value(element);
  6517. vname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
  6518. if (vname && !strcasecmp(vname, viewname))
  6519. break;
  6520. vconfig = NULL;
  6521. }
  6522. /* Open save file for write configuration */
  6523. CHECK(isc_stdio_open(view->new_zone_file, "a", &fp));
  6524. /* Mark view unfrozen so that zone can be added */
  6525. isc_task_beginexclusive(server->task);
  6526. dns_view_thaw(view);
  6527. result = configure_zone(cfg->config, parms, vconfig,
  6528. server->mctx, view, cfg->actx, ISC_FALSE);
  6529. dns_view_freeze(view);
  6530. isc_task_endexclusive(server->task);
  6531. if (result != ISC_R_SUCCESS)
  6532. goto cleanup;
  6533. /* Is it there yet? */
  6534. CHECK(dns_zt_find(view->zonetable, &dnsname, 0, NULL, &zone));
  6535. /*
  6536. * Load the zone from the master file. If this fails, we'll
  6537. * need to undo the configuration we've done already.
  6538. */
  6539. result = dns_zone_loadnew(zone);
  6540. if (result != ISC_R_SUCCESS) {
  6541. dns_db_t *dbp = NULL;
  6542. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  6543. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  6544. "addzone failed; reverting.");
  6545. /* If the zone loaded partially, unload it */
  6546. if (dns_zone_getdb(zone, &dbp) == ISC_R_SUCCESS) {
  6547. dns_db_detach(&dbp);
  6548. dns_zone_unload(zone);
  6549. }
  6550. /* Remove the zone from the zone table */
  6551. dns_zt_unmount(view->zonetable, zone);
  6552. goto cleanup;
  6553. }
  6554. /* Flag the zone as having been added at runtime */
  6555. dns_zone_setadded(zone, ISC_TRUE);
  6556. /* Emit just the zone name from args */
  6557. CHECK(isc_stdio_write("zone ", 5, 1, fp, NULL));
  6558. CHECK(isc_stdio_write(zonename, strlen(zonename), 1, fp, NULL));
  6559. CHECK(isc_stdio_write(" ", 1, 1, fp, NULL));
  6560. /* Classname, if not default */
  6561. if (classname != NULL && *classname != '\0') {
  6562. CHECK(isc_stdio_write(classname, strlen(classname), 1, fp,
  6563. NULL));
  6564. CHECK(isc_stdio_write(" ", 1, 1, fp, NULL));
  6565. }
  6566. /* Find beginning of option block from args */
  6567. for (argp = args; *argp; argp++, arglen--) {
  6568. if (*argp == '{') { /* Assume matching '}' */
  6569. /* Add that to our file */
  6570. CHECK(isc_stdio_write(argp, arglen, 1, fp, NULL));
  6571. /* Make sure we end with a LF */
  6572. if (argp[arglen-1] != '\n') {
  6573. CHECK(isc_stdio_write("\n", 1, 1, fp, NULL));
  6574. }
  6575. break;
  6576. }
  6577. }
  6578. CHECK(isc_stdio_close(fp));
  6579. fp = NULL;
  6580. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  6581. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  6582. "zone %s added to view %s via addzone",
  6583. zonename, viewname);
  6584. result = ISC_R_SUCCESS;
  6585. cleanup:
  6586. if (fp != NULL)
  6587. isc_stdio_close(fp);
  6588. if (parser != NULL) {
  6589. if (config != NULL)
  6590. cfg_obj_destroy(parser, &config);
  6591. cfg_parser_destroy(&parser);
  6592. }
  6593. if (zone != NULL)
  6594. dns_zone_detach(&zone);
  6595. if (view != NULL)
  6596. dns_view_detach(&view);
  6597. if (nbuf != NULL)
  6598. isc_buffer_free(&nbuf);
  6599. return (result);
  6600. }
  6601. /*
  6602. * Act on a "delzone" command from the command channel.
  6603. */
  6604. isc_result_t
  6605. ns_server_del_zone(ns_server_t *server, char *args) {
  6606. isc_result_t result;
  6607. dns_zone_t *zone = NULL;
  6608. dns_view_t *view = NULL;
  6609. dns_db_t *dbp = NULL;
  6610. const char *filename = NULL;
  6611. char *tmpname = NULL;
  6612. char buf[1024];
  6613. const char *zonename = NULL;
  6614. size_t znamelen = 0;
  6615. FILE *ifp = NULL, *ofp = NULL;
  6616. /* Parse parameters */
  6617. CHECK(zone_from_args(server, args, &zone, &zonename));
  6618. if (result != ISC_R_SUCCESS)
  6619. return (result);
  6620. if (zone == NULL) {
  6621. result = ISC_R_UNEXPECTEDEND;
  6622. goto cleanup;
  6623. }
  6624. /*
  6625. * Was this zone originally added at runtime?
  6626. * If not, we can't delete it now.
  6627. */
  6628. if (!dns_zone_getadded(zone)) {
  6629. result = ISC_R_NOPERM;
  6630. goto cleanup;
  6631. }
  6632. if (zonename != NULL)
  6633. znamelen = strlen(zonename);
  6634. /* Dig out configuration for this zone */
  6635. view = dns_zone_getview(zone);
  6636. filename = view->new_zone_file;
  6637. if (filename == NULL) {
  6638. /* No adding zones in this view */
  6639. result = ISC_R_FAILURE;
  6640. goto cleanup;
  6641. }
  6642. /* Rewrite zone list */
  6643. result = isc_stdio_open(filename, "r", &ifp);
  6644. if (ifp != NULL && result == ISC_R_SUCCESS) {
  6645. char *found = NULL, *p = NULL;
  6646. size_t n;
  6647. /* Create a temporary file */
  6648. CHECK(isc_string_printf(buf, 1023, "%s.%ld", filename,
  6649. (long)getpid()));
  6650. if (!(tmpname = isc_mem_strdup(server->mctx, buf))) {
  6651. result = ISC_R_NOMEMORY;
  6652. goto cleanup;
  6653. }
  6654. CHECK(isc_stdio_open(tmpname, "w", &ofp));
  6655. /* Look for the entry for that zone */
  6656. while (fgets(buf, 1024, ifp)) {
  6657. /* A 'zone' line */
  6658. if (strncasecmp(buf, "zone", 4)) {
  6659. fputs(buf, ofp);
  6660. continue;
  6661. }
  6662. p = buf+4;
  6663. /* Locate a name */
  6664. while (*p &&
  6665. ((*p == '"') || isspace((unsigned char)*p)))
  6666. p++;
  6667. /* Is that the zone we're looking for */
  6668. if (strncasecmp(p, zonename, znamelen)) {
  6669. fputs(buf, ofp);
  6670. continue;
  6671. }
  6672. /* And nothing else? */
  6673. p += znamelen;
  6674. if (isspace((unsigned char)*p) ||
  6675. *p == '"' || *p == '{') {
  6676. /* This must be the entry */
  6677. found = p;
  6678. break;
  6679. }
  6680. /* Spit it out, keep looking */
  6681. fputs(buf, ofp);
  6682. }
  6683. /* Skip over an option block (matching # of braces) */
  6684. if (found) {
  6685. int obrace = 0, cbrace = 0;
  6686. for (;;) {
  6687. while (*p) {
  6688. if (*p == '{') obrace++;
  6689. if (*p == '}') cbrace++;
  6690. p++;
  6691. }
  6692. if (obrace && (obrace == cbrace))
  6693. break;
  6694. if (!fgets(buf, 1024, ifp))
  6695. break;
  6696. p = buf;
  6697. }
  6698. /* Just spool the remainder of the file out */
  6699. result = isc_stdio_read(buf, 1, 1024, ifp, &n);
  6700. while (n > 0U) {
  6701. if (result == ISC_R_EOF)
  6702. result = ISC_R_SUCCESS;
  6703. CHECK(result);
  6704. isc_stdio_write(buf, 1, n, ofp, NULL);
  6705. result = isc_stdio_read(buf, 1, 1024, ifp, &n);
  6706. }
  6707. /* Move temporary into place */
  6708. CHECK(isc_file_rename(tmpname, view->new_zone_file));
  6709. } else {
  6710. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  6711. NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
  6712. "deleted zone %s was missing from "
  6713. "new zone file", zonename);
  6714. goto cleanup;
  6715. }
  6716. }
  6717. /* Stop answering for this zone */
  6718. if (dns_zone_getdb(zone, &dbp) == ISC_R_SUCCESS) {
  6719. dns_db_detach(&dbp);
  6720. dns_zone_unload(zone);
  6721. }
  6722. CHECK(dns_zt_unmount(view->zonetable, zone));
  6723. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
  6724. NS_LOGMODULE_SERVER, ISC_LOG_INFO,
  6725. "zone %s removed via delzone", zonename);
  6726. result = ISC_R_SUCCESS;
  6727. cleanup:
  6728. if (ifp != NULL)
  6729. isc_stdio_close(ifp);
  6730. if (ofp != NULL) {
  6731. isc_stdio_close(ofp);
  6732. isc_file_remove(tmpname);
  6733. }
  6734. if (tmpname != NULL)
  6735. isc_mem_free(server->mctx, tmpname);
  6736. if (zone != NULL)
  6737. dns_zone_detach(&zone);
  6738. return (result);
  6739. }
  6740. static void
  6741. newzone_cfgctx_destroy(void **cfgp) {
  6742. struct cfg_context *cfg;
  6743. REQUIRE(cfgp != NULL && *cfgp != NULL);
  6744. cfg = *cfgp;
  6745. if (cfg->actx != NULL)
  6746. cfg_aclconfctx_detach(&cfg->actx);
  6747. if (cfg->parser != NULL) {
  6748. if (cfg->config != NULL)
  6749. cfg_obj_destroy(cfg->parser, &cfg->config);
  6750. cfg_parser_destroy(&cfg->parser);
  6751. }
  6752. if (cfg->nzparser != NULL) {
  6753. if (cfg->nzconfig != NULL)
  6754. cfg_obj_destroy(cfg->nzparser, &cfg->nzconfig);
  6755. cfg_parser_destroy(&cfg->nzparser);
  6756. }
  6757. isc_mem_putanddetach(&cfg->mctx, cfg, sizeof(*cfg));
  6758. *cfgp = NULL;
  6759. }