]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
acl: implement a centralized ACL output mechanism for HAs and ACLs.
authorJaco Kroon <jaco@uls.co.za>
Wed, 18 Mar 2020 13:49:56 +0000 (15:49 +0200)
committerJoshua Colp <jcolp@sangoma.com>
Tue, 31 Mar 2020 15:45:04 +0000 (10:45 -0500)
named_acl.c (which is really a named_ha) now uses ast_ha_output.

I've also updated main/manager.c to output the actual ACL on "manager
show user <username>" if one is set.  If this works then we can add
similar to other modules as required.

Change-Id: I0ec9876a90dddd379c80ec078d48e3ee6991eb0f

include/asterisk/acl.h
main/acl.c
main/manager.c
main/named_acl.c

index fe49a5bfbb8a4e897b6e9da5539cbd4e9dee50ac..2e42fe1c7ac8a9bb5659ddf9f677ec578e081c43 100644 (file)
@@ -448,6 +448,38 @@ int ast_named_acl_reload(void);
  */
 struct stasis_message_type *ast_named_acl_change_type(void);
 
+/*!
+ * \brief output an HA to the provided fd
+ *
+ * \details
+ * This function can be used centrally to output HAs as used in ACLs from other
+ * modules.  It follows the format as originally used for named ACLs in
+ * named_acl.c.
+ *
+ * \param fd The file-descriptor to which to output the HA.
+ * \param ha The HA to output.
+ * \param prefix If you need a specific prefix output on each line, give it here, may be NULL.
+ *
+ * \since 13.33.0, 16.10.0, 17.4.0
+ */
+void ast_ha_output(int fd, const struct ast_ha *ha, const char *prefix);
+
+/*!
+ * \brief output an ACL to the provided fd
+ *
+ * \details
+ * This function can be used centrally to output HAs as used in ACLs from other
+ * modules.  It follows the format as originally used for named ACLs in
+ * named_acl.c.
+ *
+ * \param fd The file-descriptor to which to output the ACL.
+ * \param acl The ACL to output.
+ * \param prefix If you need a specific prefix output on each line, give it here, may be NULL.
+ *
+ * \since 13.33.0, 16.10.0, 17.4.0
+ */
+void ast_acl_output(int fd, struct ast_acl_list *acl, const char *prefix);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
index 9179753aeec8beadeac36c1de321d5f43b7201e1..3d329768573d7c18e3dfcdd08c8d5ac43456d786 100644 (file)
@@ -50,6 +50,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/utils.h"
 #include "asterisk/lock.h"
 #include "asterisk/srv.h"
+#include "asterisk/cli.h"
 
 #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
 static int get_local_address(struct ast_sockaddr *ourip)
@@ -1084,3 +1085,31 @@ int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindad
        ast_sockaddr_set_port(ourip, port);
        return res;
 }
+
+void ast_ha_output(int fd, const struct ast_ha *ha, const char *prefix)
+{
+       char addr[AST_SOCKADDR_BUFLEN];
+       char *mask;
+       int index = 0;
+       for (; ha; ha = ha->next, ++index) {
+               strcpy(addr, ast_sockaddr_stringify_addr(&ha->addr));
+               mask = ast_sockaddr_stringify_addr(&ha->netmask);
+               ast_cli(fd, "%s%3d: %s - %s/%s\n", prefix ?: "", index, ha->sense == AST_SENSE_ALLOW ? "allow" : " deny", addr, mask);
+       }
+}
+
+void ast_acl_output(int fd, struct ast_acl_list *acl_list, const char *prefix)
+{
+       struct ast_acl *acl;
+
+       AST_LIST_LOCK(acl_list);
+       AST_LIST_TRAVERSE(acl_list, acl, list) {
+               ast_cli(fd, "%sACL: %s%s\n---------------------------------------------\n",
+                               prefix ?: "", ast_strlen_zero(acl->name) ? "(unnamed)" : acl->name,
+                               acl->is_realtime ? " (realtime)" : "");
+
+               ast_ha_output(fd, acl->acl, prefix);
+       }
+       AST_LIST_UNLOCK(acl_list);
+
+}
index 44e25b8e661a4bcbad159b5a11ef1ffbcd1fdfed..c79d4f1eedbc8392ffdb978bce2b9f9a040d5f3a 100644 (file)
@@ -2551,6 +2551,9 @@ static char *handle_showmanager(struct ast_cli_entry *e, int cmd, struct ast_cli
                for (v = user->chanvars ; v ; v = v->next) {
                        ast_cli(a->fd, "                 %s = %s\n", v->name, v->value);
                }
+       if (!ast_acl_list_is_empty(user->acl)) {
+               ast_acl_output(a->fd, user->acl, NULL);
+       }
 
        AST_RWLIST_UNLOCK(&users);
 
index e61bcba87b91d28ba4859ec6e7ba35d48695b9cd..54ad1a912dccfb30cf9b539b453b48a0e576b1e6 100644 (file)
@@ -438,8 +438,6 @@ int ast_named_acl_reload(void)
  */
 static void cli_display_named_acl(int fd, const char *name)
 {
-       struct ast_ha *ha;
-       int ha_index = 0;
        int is_realtime = 0;
 
        RAII_VAR(struct named_acl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
@@ -464,12 +462,7 @@ static void cli_display_named_acl(int fd, const char *name)
        }
 
        ast_cli(fd, "\nACL: %s%s\n---------------------------------------------\n", name, is_realtime ? " (realtime)" : "");
-       for (ha = named_acl->ha; ha; ha = ha->next) {
-               char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
-               char *mask = ast_sockaddr_stringify_addr(&ha->netmask);
-               ast_cli(fd, "%3d: %s - %s/%s\n", ha_index, ha->sense == AST_SENSE_ALLOW ? "allow" : " deny", addr, mask);
-               ha_index++;
-       }
+       ast_ha_output(fd, named_acl->ha, NULL);
 }
 
 /*!