BuildSystem: Remove unused variables.
[asterisk/asterisk.git] / main / named_acl.c
index 1426931..3a4c454 100644 (file)
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/config.h"
 #include "asterisk/config_options.h"
-#include "asterisk/event.h"
 #include "asterisk/utils.h"
 #include "asterisk/module.h"
 #include "asterisk/cli.h"
 #include "asterisk/acl.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/paths.h"
+#include "asterisk/stasis.h"
+#include "asterisk/json.h"
+#include "asterisk/security_events.h"
 
 #define NACL_CONFIG "acl.conf"
 #define ACL_FAMILY "acls"
@@ -80,8 +80,8 @@ static void *named_acl_find(struct ao2_container *container, const char *cat);
 static struct aco_type named_acl_type = {
        .type = ACO_ITEM,                  /*!< named_acls are items stored in containers, not individual global objects */
        .name = "named_acl",
-       .category_match = ACO_BLACKLIST,
-       .category = "^general$",           /*!< Match everything but "general" */
+       .category_match = ACO_BLACKLIST_EXACT,
+       .category = "general",           /*!< Match everything but "general" */
        .item_alloc = named_acl_alloc,     /*!< A callback to allocate a new named_acl based on category */
        .item_find = named_acl_find,       /*!< A callback to find a named_acl in some container of named_acls */
        .item_offset = offsetof(struct named_acl_config, named_acl_list), /*!< Could leave this out since 0 */
@@ -105,19 +105,8 @@ struct named_acl {
        char name[ACL_NAME_LENGTH]; /* Same max length as a configuration category */
 };
 
-static int named_acl_hash_fn(const void *obj, const int flags)
-{
-       const struct named_acl *entry = obj;
-       return ast_str_hash(entry->name);
-}
-
-static int named_acl_cmp_fn(void *obj, void *arg, const int flags)
-{
-       struct named_acl *entry1 = obj;
-       struct named_acl *entry2 = arg;
-
-       return (!strcmp(entry1->name, entry2->name)) ? (CMP_MATCH | CMP_STOP) : 0;
-}
+AO2_STRING_FIELD_HASH_FN(named_acl, name)
+AO2_STRING_FIELD_CMP_FN(named_acl, name)
 
 /*! \brief destructor for named_acl_config */
 static void named_acl_config_destructor(void *obj)
@@ -355,9 +344,16 @@ struct ast_ha *ast_named_acl_find(const char *name, int *is_realtime, int *is_un
        return ha;
 }
 
+/*! \brief Message type for named ACL changes */
+STASIS_MESSAGE_TYPE_DEFN(ast_named_acl_change_type);
+
 /*!
  * \internal
- * \brief Sends an update event corresponding to a given named ACL that has changed.
+ * \brief Sends a stasis message corresponding to a given named ACL that has changed or
+ *        that all ACLs have been updated and old copies must be refreshed. Consumers of
+ *        named ACLs should subscribe to the ast_security_topic and respond to messages
+ *        of the ast_named_acl_change_type stasis message type in order to be able to
+ *        accommodate changes to named ACLs.
  *
  * \param name Name of the ACL that has changed. May be an empty string (but not NULL)
  *        If name is an empty string, then all ACLs must be refreshed.
@@ -365,23 +361,38 @@ struct ast_ha *ast_named_acl_find(const char *name, int *is_realtime, int *is_un
  * \retval 0 success
  * \retval 1 failure
  */
-static int push_acl_change_event(char *name)
+static int publish_acl_change(const char *name)
 {
-       struct ast_event *event = ast_event_new(AST_EVENT_ACL_CHANGE,
-                                                       AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, name,
-                                                       AST_EVENT_IE_END);
-       if (!event) {
-               ast_log(LOG_ERROR, "Failed to allocate acl.conf reload event. Some modules will have out of date ACLs.\n");
-               return -1;
+       RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json *, json_object, ast_json_object_create(), ast_json_unref);
+
+       if (!json_object || !ast_named_acl_change_type()) {
+               goto publish_failure;
        }
 
-       if (ast_event_queue(event)) {
-               ast_event_destroy(event);
-               ast_log(LOG_ERROR, "Failed to queue acl.conf reload event. Some modules will have out of date ACLs.\n");
-               return -1;
+       if (ast_json_object_set(json_object, "name", ast_json_string_create(name))) {
+               goto publish_failure;
+       }
+
+       if (!(json_payload = ast_json_payload_create(json_object))) {
+               goto publish_failure;
        }
 
+       msg = stasis_message_create(ast_named_acl_change_type(), json_payload);
+
+       if (!msg) {
+               goto publish_failure;
+       }
+
+       stasis_publish(ast_security_topic(), msg);
+
        return 0;
+
+publish_failure:
+       ast_log(LOG_ERROR, "Failed to issue ACL change message for %s.\n",
+               ast_strlen_zero(name) ? "all named ACLs" : name);
+       return -1;
 }
 
 /*!
@@ -409,7 +420,7 @@ int ast_named_acl_reload(void)
        }
 
        /* We need to push an ACL change event with no ACL name so that all subscribers update with all ACLs */
-       push_acl_change_event("");
+       publish_acl_change("");
 
        return 0;
 }
@@ -541,10 +552,23 @@ static struct ast_cli_entry cli_named_acl[] = {
        AST_CLI_DEFINE(handle_show_named_acl_cmd, "Show a named ACL or list all named ACLs"),
 };
 
+static void named_acl_cleanup(void)
+{
+       ast_cli_unregister_multiple(cli_named_acl, ARRAY_LEN(cli_named_acl));
+
+       STASIS_MESSAGE_TYPE_CLEANUP(ast_named_acl_change_type);
+       aco_info_destroy(&cfg_info);
+       ao2_global_obj_release(globals);
+}
+
 int ast_named_acl_init()
 {
        ast_cli_register_multiple(cli_named_acl, ARRAY_LEN(cli_named_acl));
 
+       STASIS_MESSAGE_TYPE_INIT(ast_named_acl_change_type);
+
+       ast_register_cleanup(named_acl_cleanup);
+
        if (aco_info_init(&cfg_info)) {
                return 0;
        }
@@ -553,10 +577,7 @@ int ast_named_acl_init()
        aco_option_register(&cfg_info, "permit", ACO_EXACT, named_acl_types, NULL, OPT_ACL_T, 1, FLDSET(struct named_acl, ha));
        aco_option_register(&cfg_info, "deny", ACO_EXACT, named_acl_types, NULL, OPT_ACL_T, 0, FLDSET(struct named_acl, ha));
 
-       if (aco_process_config(&cfg_info, 0)) {
-               aco_info_destroy(&cfg_info);
-               return 0;
-       }
+       aco_process_config(&cfg_info, 0);
 
        return 0;
 }