named_acl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Jonathan Rose <jrose@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Named Access Control Lists
  22. *
  23. * \author Jonathan Rose <jrose@digium.com>
  24. *
  25. * \note Based on a feature proposed by
  26. * Olle E. Johansson <oej@edvina.net>
  27. */
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/config.h"
  31. #include "asterisk/config_options.h"
  32. #include "asterisk/utils.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/cli.h"
  35. #include "asterisk/acl.h"
  36. #include "asterisk/astobj2.h"
  37. #include "asterisk/paths.h"
  38. #include "asterisk/stasis.h"
  39. #include "asterisk/json.h"
  40. #include "asterisk/security_events.h"
  41. #define NACL_CONFIG "acl.conf"
  42. #define ACL_FAMILY "acls"
  43. /*** DOCUMENTATION
  44. <configInfo name="named_acl" language="en_US">
  45. <configFile name="named_acl.conf">
  46. <configObject name="named_acl">
  47. <synopsis>Options for configuring a named ACL</synopsis>
  48. <configOption name="permit">
  49. <synopsis>An address/subnet from which to allow access</synopsis>
  50. </configOption>
  51. <configOption name="deny">
  52. <synopsis>An address/subnet from which to disallow access</synopsis>
  53. </configOption>
  54. </configObject>
  55. </configFile>
  56. </configInfo>
  57. ***/
  58. /*
  59. * Configuration structure - holds pointers to ao2 containers used for configuration
  60. * Since there isn't a general level or any other special levels for acl.conf at this
  61. * time, it's really a config options friendly wrapper for the named ACL container
  62. */
  63. struct named_acl_config {
  64. struct ao2_container *named_acl_list;
  65. };
  66. static AO2_GLOBAL_OBJ_STATIC(globals);
  67. /*! \note These functions are used for placing/retrieving named ACLs in their ao2_container. */
  68. static void *named_acl_config_alloc(void);
  69. static void *named_acl_alloc(const char *cat);
  70. static void *named_acl_find(struct ao2_container *container, const char *cat);
  71. /* Config type for named ACL profiles (must not be named general) */
  72. static struct aco_type named_acl_type = {
  73. .type = ACO_ITEM, /*!< named_acls are items stored in containers, not individual global objects */
  74. .name = "named_acl",
  75. .category_match = ACO_BLACKLIST,
  76. .category = "^general$", /*!< Match everything but "general" */
  77. .item_alloc = named_acl_alloc, /*!< A callback to allocate a new named_acl based on category */
  78. .item_find = named_acl_find, /*!< A callback to find a named_acl in some container of named_acls */
  79. .item_offset = offsetof(struct named_acl_config, named_acl_list), /*!< Could leave this out since 0 */
  80. };
  81. /* This array of aco_type structs is necessary to use aco_option_register */
  82. struct aco_type *named_acl_types[] = ACO_TYPES(&named_acl_type);
  83. struct aco_file named_acl_conf = {
  84. .filename = "acl.conf",
  85. .types = ACO_TYPES(&named_acl_type),
  86. };
  87. /* Create a config info struct that describes the config processing for named ACLs. */
  88. CONFIG_INFO_CORE("named_acl", cfg_info, globals, named_acl_config_alloc,
  89. .files = ACO_FILES(&named_acl_conf),
  90. );
  91. struct named_acl {
  92. struct ast_ha *ha;
  93. char name[ACL_NAME_LENGTH]; /* Same max length as a configuration category */
  94. };
  95. static int named_acl_hash_fn(const void *obj, const int flags)
  96. {
  97. const struct named_acl *entry = obj;
  98. return ast_str_hash(entry->name);
  99. }
  100. static int named_acl_cmp_fn(void *obj, void *arg, const int flags)
  101. {
  102. struct named_acl *entry1 = obj;
  103. struct named_acl *entry2 = arg;
  104. return (!strcmp(entry1->name, entry2->name)) ? (CMP_MATCH | CMP_STOP) : 0;
  105. }
  106. /*! \brief destructor for named_acl_config */
  107. static void named_acl_config_destructor(void *obj)
  108. {
  109. struct named_acl_config *cfg = obj;
  110. ao2_cleanup(cfg->named_acl_list);
  111. }
  112. /*! \brief allocator callback for named_acl_config. Notice it returns void * since it is used by
  113. * the backend config code
  114. */
  115. static void *named_acl_config_alloc(void)
  116. {
  117. struct named_acl_config *cfg;
  118. if (!(cfg = ao2_alloc(sizeof(*cfg), named_acl_config_destructor))) {
  119. return NULL;
  120. }
  121. if (!(cfg->named_acl_list = ao2_container_alloc(37, named_acl_hash_fn, named_acl_cmp_fn))) {
  122. goto error;
  123. }
  124. return cfg;
  125. error:
  126. ao2_ref(cfg, -1);
  127. return NULL;
  128. }
  129. /*! \brief Destroy a named ACL object */
  130. static void destroy_named_acl(void *obj)
  131. {
  132. struct named_acl *named_acl = obj;
  133. ast_free_ha(named_acl->ha);
  134. }
  135. /*!
  136. * \brief Create a named ACL structure
  137. *
  138. * \param cat name given to the ACL
  139. * \retval NULL failure
  140. *\retval non-NULL successfully allocated named ACL
  141. */
  142. static void *named_acl_alloc(const char *cat)
  143. {
  144. struct named_acl *named_acl;
  145. named_acl = ao2_alloc(sizeof(*named_acl), destroy_named_acl);
  146. if (!named_acl) {
  147. return NULL;
  148. }
  149. ast_copy_string(named_acl->name, cat, sizeof(named_acl->name));
  150. return named_acl;
  151. }
  152. /*!
  153. * \brief Find a named ACL in a container by its name
  154. *
  155. * \param container ao2container holding the named ACLs
  156. * \param cat name of the ACL wanted to be found
  157. * \retval pointer to the named ACL if available. Null if not found.
  158. */
  159. static void *named_acl_find(struct ao2_container *container, const char *cat)
  160. {
  161. struct named_acl tmp;
  162. ast_copy_string(tmp.name, cat, sizeof(tmp.name));
  163. return ao2_find(container, &tmp, OBJ_POINTER);
  164. }
  165. /*!
  166. * \internal
  167. * \brief Callback function to compare the ACL order of two given categories.
  168. * This function is used to sort lists of ACLs received from realtime.
  169. *
  170. * \param p first category being compared
  171. * \param q second category being compared
  172. *
  173. * \retval -1 (p < q)
  174. * \retval 0 (p == q)
  175. * \retval 1 (p > q)
  176. */
  177. static int acl_order_comparator(struct ast_category *p, struct ast_category *q)
  178. {
  179. int p_value = 0, q_value = 0;
  180. struct ast_variable *p_var = ast_category_first(p);
  181. struct ast_variable *q_var = ast_category_first(q);
  182. while (p_var) {
  183. if (!strcasecmp(p_var->name, "rule_order")) {
  184. p_value = atoi(p_var->value);
  185. break;
  186. }
  187. p_var = p_var->next;
  188. }
  189. while (q_var) {
  190. if (!strcasecmp(q_var->name, "rule_order")) {
  191. q_value = atoi(q_var->value);
  192. break;
  193. }
  194. q_var = q_var->next;
  195. }
  196. if (p_value < q_value) {
  197. return -1;
  198. } else if (q_value < p_value) {
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. /*!
  204. * \internal
  205. * \brief Search for a named ACL via realtime Database and build the named_acl
  206. * if it is valid.
  207. *
  208. * \param name of the ACL wanted to be found
  209. * \retval pointer to the named ACL if available. Null if the ACL subsystem is unconfigured.
  210. */
  211. static struct named_acl *named_acl_find_realtime(const char *name)
  212. {
  213. struct ast_config *cfg;
  214. char *item = NULL;
  215. const char *systemname = NULL;
  216. struct ast_ha *built_ha = NULL;
  217. struct named_acl *acl;
  218. /* If we have a systemname set in the global options, we only want to retrieve entries with a matching systemname field. */
  219. systemname = ast_config_AST_SYSTEM_NAME;
  220. if (ast_strlen_zero(systemname)) {
  221. cfg = ast_load_realtime_multientry(ACL_FAMILY, "name", name, SENTINEL);
  222. } else {
  223. cfg = ast_load_realtime_multientry(ACL_FAMILY, "name", name, "systemname", systemname, SENTINEL);
  224. }
  225. if (!cfg) {
  226. return NULL;
  227. }
  228. /* At this point, the configuration must be sorted by the order field. */
  229. ast_config_sort_categories(cfg, 0, acl_order_comparator);
  230. while ((item = ast_category_browse(cfg, item))) {
  231. int append_ha_error = 0;
  232. const char *order = ast_variable_retrieve(cfg, item, "rule_order");
  233. const char *sense = ast_variable_retrieve(cfg, item, "sense");
  234. const char *rule = ast_variable_retrieve(cfg, item, "rule");
  235. built_ha = ast_append_ha(sense, rule, built_ha, &append_ha_error);
  236. if (append_ha_error) {
  237. /* We need to completely reject an ACL that contains any bad rules. */
  238. ast_log(LOG_ERROR, "Rejecting realtime ACL due to bad ACL definition '%s': %s - %s - %s\n", name, order, sense, rule);
  239. ast_free_ha(built_ha);
  240. return NULL;
  241. }
  242. }
  243. ast_config_destroy(cfg);
  244. acl = named_acl_alloc(name);
  245. if (!acl) {
  246. ast_log(LOG_ERROR, "allocation error\n");
  247. ast_free_ha(built_ha);
  248. return NULL;
  249. }
  250. acl->ha = built_ha;
  251. return acl;
  252. }
  253. struct ast_ha *ast_named_acl_find(const char *name, int *is_realtime, int *is_undefined)
  254. {
  255. struct ast_ha *ha = NULL;
  256. RAII_VAR(struct named_acl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  257. RAII_VAR(struct named_acl *, named_acl, NULL, ao2_cleanup);
  258. if (is_realtime) {
  259. *is_realtime = 0;
  260. }
  261. if (is_undefined) {
  262. *is_undefined = 0;
  263. }
  264. /* If the config or its named_acl_list hasn't been initialized, abort immediately. */
  265. if ((!cfg) || (!(cfg->named_acl_list))) {
  266. ast_log(LOG_ERROR, "Attempted to find named ACL '%s', but the ACL configuration isn't available.\n", name);
  267. return NULL;
  268. }
  269. named_acl = named_acl_find(cfg->named_acl_list, name);
  270. /* If a named ACL couldn't be retrieved locally, we need to try realtime storage. */
  271. if (!named_acl) {
  272. RAII_VAR(struct named_acl *, realtime_acl, NULL, ao2_cleanup);
  273. /* Attempt to create from realtime */
  274. if ((realtime_acl = named_acl_find_realtime(name))) {
  275. if (is_realtime) {
  276. *is_realtime = 1;
  277. }
  278. ha = ast_duplicate_ha_list(realtime_acl->ha);
  279. return ha;
  280. }
  281. /* Couldn't create from realtime. Raise relevant flags and print relevant warnings. */
  282. if (ast_realtime_is_mapping_defined(ACL_FAMILY) && !ast_check_realtime(ACL_FAMILY)) {
  283. ast_log(LOG_WARNING, "ACL '%s' does not exist. The ACL will be marked as undefined and will automatically fail if applied.\n"
  284. "This ACL may exist in the configured realtime backend, but that backend hasn't been registered yet. "
  285. "Fix this establishing preload for the backend in 'modules.conf'.\n", name);
  286. } else {
  287. ast_log(LOG_WARNING, "ACL '%s' does not exist. The ACL will be marked as undefined and will automatically fail if applied.\n", name);
  288. }
  289. if (is_undefined) {
  290. *is_undefined = 1;
  291. }
  292. return NULL;
  293. }
  294. ha = ast_duplicate_ha_list(named_acl->ha);
  295. if (!ha) {
  296. ast_log(LOG_NOTICE, "ACL '%s' contains no rules. It is valid, but it will accept addresses unconditionally.\n", name);
  297. }
  298. return ha;
  299. }
  300. /*! \brief Message type for named ACL changes */
  301. STASIS_MESSAGE_TYPE_DEFN(ast_named_acl_change_type);
  302. /*!
  303. * \internal
  304. * \brief Sends a stasis message corresponding to a given named ACL that has changed or
  305. * that all ACLs have been updated and old copies must be refreshed. Consumers of
  306. * named ACLs should subscribe to the ast_security_topic and respond to messages
  307. * of the ast_named_acl_change_type stasis message type in order to be able to
  308. * accommodate changes to named ACLs.
  309. *
  310. * \param name Name of the ACL that has changed. May be an empty string (but not NULL)
  311. * If name is an empty string, then all ACLs must be refreshed.
  312. *
  313. * \retval 0 success
  314. * \retval 1 failure
  315. */
  316. static int publish_acl_change(const char *name)
  317. {
  318. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  319. RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
  320. RAII_VAR(struct ast_json *, json_object, ast_json_object_create(), ast_json_unref);
  321. if (!json_object || !ast_named_acl_change_type()) {
  322. goto publish_failure;
  323. }
  324. if (ast_json_object_set(json_object, "name", ast_json_string_create(name))) {
  325. goto publish_failure;
  326. }
  327. if (!(json_payload = ast_json_payload_create(json_object))) {
  328. goto publish_failure;
  329. }
  330. msg = stasis_message_create(ast_named_acl_change_type(), json_payload);
  331. if (!msg) {
  332. goto publish_failure;
  333. }
  334. stasis_publish(ast_security_topic(), msg);
  335. return 0;
  336. publish_failure:
  337. ast_log(LOG_ERROR, "Failed to to issue ACL change message for %s.\n",
  338. ast_strlen_zero(name) ? "all named ACLs" : name);
  339. return -1;
  340. }
  341. /*!
  342. * \internal
  343. * \brief reload configuration for named ACLs
  344. *
  345. * \param fd file descriptor for CLI client
  346. */
  347. int ast_named_acl_reload(void)
  348. {
  349. enum aco_process_status status;
  350. status = aco_process_config(&cfg_info, 1);
  351. if (status == ACO_PROCESS_ERROR) {
  352. ast_log(LOG_WARNING, "Could not reload ACL config\n");
  353. return 0;
  354. }
  355. if (status == ACO_PROCESS_UNCHANGED) {
  356. /* We don't actually log anything if the config was unchanged,
  357. * but we don't need to send a config change event either.
  358. */
  359. return 0;
  360. }
  361. /* We need to push an ACL change event with no ACL name so that all subscribers update with all ACLs */
  362. publish_acl_change("");
  363. return 0;
  364. }
  365. /*!
  366. * \internal
  367. * \brief secondary handler for the 'acl show <name>' command (with arg)
  368. *
  369. * \param fd file descriptor of the cli
  370. * \name name of the ACL requested for display
  371. */
  372. static void cli_display_named_acl(int fd, const char *name)
  373. {
  374. struct ast_ha *ha;
  375. int ha_index = 0;
  376. int is_realtime = 0;
  377. RAII_VAR(struct named_acl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  378. RAII_VAR(struct named_acl *, named_acl, NULL, ao2_cleanup);
  379. /* If the configuration or the configuration's named_acl_list is unavailable, abort. */
  380. if ((!cfg) || (!cfg->named_acl_list)) {
  381. ast_log(LOG_ERROR, "Attempted to show named ACL '%s', but the acl configuration isn't available.\n", name);
  382. return;
  383. }
  384. named_acl = named_acl_find(cfg->named_acl_list, name);
  385. /* If the named_acl couldn't be found with the search, also abort. */
  386. if (!named_acl) {
  387. if (!(named_acl = named_acl_find_realtime(name))) {
  388. ast_cli(fd, "\nCould not find ACL named '%s'\n", name);
  389. return;
  390. }
  391. is_realtime = 1;
  392. }
  393. ast_cli(fd, "\nACL: %s%s\n---------------------------------------------\n", name, is_realtime ? " (realtime)" : "");
  394. for (ha = named_acl->ha; ha; ha = ha->next) {
  395. char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
  396. char *mask = ast_sockaddr_stringify_addr(&ha->netmask);
  397. ast_cli(fd, "%3d: %s - %s/%s\n", ha_index, ha->sense == AST_SENSE_ALLOW ? "allow" : " deny", addr, mask);
  398. ha_index++;
  399. }
  400. }
  401. /*!
  402. * \internal
  403. * \brief secondary handler for the 'acl show' command (no args)
  404. *
  405. * \param fd file descriptor of the cli
  406. */
  407. static void cli_display_named_acl_list(int fd)
  408. {
  409. struct ao2_iterator i;
  410. void *o;
  411. RAII_VAR(struct named_acl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  412. ast_cli(fd, "\nacl\n---\n");
  413. if (!cfg || !cfg->named_acl_list) {
  414. ast_cli(fd, "ACL configuration isn't available.\n");
  415. return;
  416. }
  417. i = ao2_iterator_init(cfg->named_acl_list, 0);
  418. while ((o = ao2_iterator_next(&i))) {
  419. struct named_acl *named_acl = o;
  420. ast_cli(fd, "%s\n", named_acl->name);
  421. ao2_ref(o, -1);
  422. }
  423. ao2_iterator_destroy(&i);
  424. }
  425. /* \brief ACL command show <name> */
  426. static char *handle_show_named_acl_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  427. {
  428. RAII_VAR(struct named_acl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  429. int length;
  430. int which;
  431. struct ao2_iterator i;
  432. struct named_acl *named_acl;
  433. char *match = NULL;
  434. switch (cmd) {
  435. case CLI_INIT:
  436. e->command = "acl show";
  437. e->usage =
  438. "Usage: acl show [name]\n"
  439. " Shows a list of named ACLs or lists all entries in a given named ACL.\n";
  440. return NULL;
  441. case CLI_GENERATE:
  442. if (!cfg) {
  443. return NULL;
  444. }
  445. length = strlen(a->word);
  446. which = 0;
  447. i = ao2_iterator_init(cfg->named_acl_list, 0);
  448. while ((named_acl = ao2_iterator_next(&i))) {
  449. if (!strncasecmp(a->word, named_acl->name, length) && ++which > a->n) {
  450. match = ast_strdup(named_acl->name);
  451. ao2_ref(named_acl, -1);
  452. break;
  453. }
  454. ao2_ref(named_acl, -1);
  455. }
  456. ao2_iterator_destroy(&i);
  457. return match;
  458. }
  459. if (a->argc == 2) {
  460. cli_display_named_acl_list(a->fd);
  461. return CLI_SUCCESS;
  462. }
  463. if (a->argc == 3) {
  464. cli_display_named_acl(a->fd, a->argv[2]);
  465. return CLI_SUCCESS;
  466. }
  467. return CLI_SHOWUSAGE;
  468. }
  469. static struct ast_cli_entry cli_named_acl[] = {
  470. AST_CLI_DEFINE(handle_show_named_acl_cmd, "Show a named ACL or list all named ACLs"),
  471. };
  472. static void named_acl_cleanup(void)
  473. {
  474. ast_cli_unregister_multiple(cli_named_acl, ARRAY_LEN(cli_named_acl));
  475. STASIS_MESSAGE_TYPE_CLEANUP(ast_named_acl_change_type);
  476. aco_info_destroy(&cfg_info);
  477. ao2_global_obj_release(globals);
  478. }
  479. int ast_named_acl_init()
  480. {
  481. ast_cli_register_multiple(cli_named_acl, ARRAY_LEN(cli_named_acl));
  482. STASIS_MESSAGE_TYPE_INIT(ast_named_acl_change_type);
  483. ast_register_cleanup(named_acl_cleanup);
  484. if (aco_info_init(&cfg_info)) {
  485. return 0;
  486. }
  487. /* Register the per level options. */
  488. aco_option_register(&cfg_info, "permit", ACO_EXACT, named_acl_types, NULL, OPT_ACL_T, 1, FLDSET(struct named_acl, ha));
  489. aco_option_register(&cfg_info, "deny", ACO_EXACT, named_acl_types, NULL, OPT_ACL_T, 0, FLDSET(struct named_acl, ha));
  490. aco_process_config(&cfg_info, 0);
  491. return 0;
  492. }