[asterisk-commits] mmichelson: branch group/pimp_my_sip r380243 - /team/group/pimp_my_sip/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Jan 27 22:06:49 CST 2013


Author: mmichelson
Date: Sun Jan 27 22:06:46 2013
New Revision: 380243

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=380243
Log:
Add a proof of concept for access control lists for SIP.

This uses sorcery in order to create access control lists
for SIP communications. It runs at a very high priority so
that it can reject messages early in the message-handling
process.

This is written as a proof of concept by in my spare time
rather than as something that is likely to be in the final
version of res_sip-based code. I expect that at least some
of this could be used as a basis for the real deal, but
there are some drawbacks to the code as it exists:

1) We have to contend with the fact that multiple global ACL objects may
be configured. It doesn't really make sense to allow for this, but
I'm not sure how to tell sorcery to only allow a single instance
of an object type to exist.
2) There is no support for per-endpoint ACLs, yet.

This is *untested*. I just cobbled this together this evening. I'll
give it some testing in the upcoming days to make sure everything
is sane.


Added:
    team/group/pimp_my_sip/res/res_sip_acl.c   (with props)

Added: team/group/pimp_my_sip/res/res_sip_acl.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip_acl.c?view=auto&rev=380243
==============================================================================
--- team/group/pimp_my_sip/res/res_sip_acl.c (added)
+++ team/group/pimp_my_sip/res/res_sip_acl.c Sun Jan 27 22:06:46 2013
@@ -1,0 +1,214 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Mark Michelson <mmichelson at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#undef bzero
+#define bzero bzero
+#include <pjsip.h>
+
+#include "asterisk/res_sip.h"
+#include "asterisk/module.h"
+#include "asterisk/logger.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/acl.h"
+
+struct sip_acl {
+	SORCERY_OBJECT(details);
+	struct ast_acl_list *acl;
+	struct ast_acl_list *contact_acl;
+};
+
+static int extract_contact_addr(pjsip_rx_data *rdata, struct ast_sockaddr **addrs)
+{
+	pjsip_contact_hdr *contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
+	pjsip_sip_uri *sip_uri;
+	char host[256];
+	if (!contact) {
+		return 0;
+	}
+	if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) {
+		return 0;
+	}
+	sip_uri = pjsip_uri_get_uri(contact->uri);
+	ast_copy_pj_str(host, &sip_uri->host, sizeof(host));
+	return ast_sockaddr_resolve(addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC);
+}
+
+static int apply_acl(pjsip_rx_data *rdata, struct ast_acl_list *acl)
+{
+	struct ast_sockaddr addr;
+
+	if (ast_acl_list_is_empty(acl)) {
+		return 0;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID);
+	ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port);
+
+	if (ast_apply_acl(acl, &addr, "SIP ACL: ") != AST_SENSE_ALLOW) {
+		ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&addr));
+		return 1;
+	}
+	return 0;
+}
+
+static int apply_contact_acl(pjsip_rx_data *rdata, struct ast_acl_list *contact_acl)
+{
+	int num_contact_addrs;
+	int forbidden = 0;
+	struct ast_sockaddr *contact_addrs;
+	int i;
+
+	if (ast_acl_list_is_empty(contact_acl)) {
+		return 0;
+	}
+
+	num_contact_addrs = extract_contact_addr(rdata, &contact_addrs);
+	if (num_contact_addrs <= 0) {
+		return 0;
+	}
+	for (i = 0; i < num_contact_addrs; ++i) {
+		if (ast_apply_acl(contact_acl, &contact_addrs[i], "SIP Contact ACL: ") != AST_SENSE_ALLOW) {
+			ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&contact_addrs[i]));
+			forbidden = 1;
+		}
+		ast_free(&contact_addrs[i]);
+	}
+
+	return forbidden;
+}
+
+static int check_acls(void *obj, void *arg, int flags)
+{
+	struct sip_acl *acl = obj;
+	pjsip_rx_data *rdata = arg;
+
+	if (apply_acl(rdata, acl->acl) || apply_contact_acl(rdata, acl->contact_acl)) {
+		return CMP_MATCH | CMP_STOP;
+	}
+	return 0;
+}
+
+static pj_bool_t acl_on_rx_msg(pjsip_rx_data *rdata)
+{
+	int forbidden;
+	struct ao2_container *acls = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "acl", AST_RETRIEVE_FLAG_ALL, NULL);
+	struct sip_acl *matched_acl;
+	if (!acls) {
+		ast_log(LOG_ERROR, "Unable to retrieve ACL sorcery data\n");
+		return PJ_FALSE;
+	}
+
+	matched_acl = ao2_callback(acls, 0, check_acls, rdata);
+	if (matched_acl) {
+		forbidden = 1;
+		ao2_ref(matched_acl, -1);
+	}
+	ao2_ref(acls, -1);
+
+	if (forbidden) {
+		pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
+		return PJ_TRUE;
+	}
+
+	return PJ_FALSE;
+}
+
+static pjsip_module acl_module = {
+	.name = { "ACL Module", 14 },
+	/* This should run after a logger but before anything else */
+	.priority = 1,
+	.on_rx_request = acl_on_rx_msg,
+	.on_rx_response = acl_on_rx_msg,
+};
+
+static int acl_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+	struct sip_acl *acl = obj;
+	int error;
+	int ignore;
+	if (!strncmp(var->name, "contact", 7)) {
+		ast_append_acl(var->name + 7, var->value, &acl->contact_acl, &error, &ignore);
+	} else {
+		ast_append_acl(var->name, var->value, &acl->acl, &error, &ignore);
+	}
+	return error;
+}
+
+static void sip_acl_destructor(void *obj)
+{
+	struct sip_acl *acl = obj;
+	acl->acl = ast_free_acl_list(acl->acl);
+	acl->contact_acl = ast_free_acl_list(acl->contact_acl);
+}
+
+static void *sip_acl_alloc(const char *name)
+{
+	struct sip_acl *acl = ao2_alloc(sizeof(*acl), sip_acl_destructor);
+	if (!acl) {
+		return NULL;
+	}
+	return acl;
+}
+
+static int load_acls(void)
+{
+	ast_sorcery_apply_default(ast_sip_get_sorcery(), "acl", "config", "res_sip.conf,criteria=type=acl");
+	if (ast_sorcery_object_register(ast_sip_get_sorcery(), "acl", sip_acl_alloc, NULL, NULL)) {
+		ast_log(LOG_ERROR, "Failed to register SIP ACL object with sorcery\n");
+		return -1;
+	}
+	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "acl", "type", "", OPT_NOOP_T, 0, 0);
+	ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "permit", "", acl_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "deny", "", acl_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "acl", "", acl_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "contactpermit", "", acl_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "contactdeny", "", acl_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "contactacl", "", acl_handler, NULL, 0, 0);
+
+	/* XXX Is there a more selective way to do this? (i.e. Just reload a specific object type?) */
+	ast_sorcery_reload(ast_sip_get_sorcery());
+	return 0;
+}
+
+static int load_module(void)
+{
+	if (load_acls()) {
+		return AST_MODULE_LOAD_DECLINE;
+	}
+	ast_sip_register_service(&acl_module);
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	ast_sip_unregister_service(&acl_module);
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP ACL Resource",
+		.load = load_module,
+		.unload = unload_module,
+		.load_pri = AST_MODPRI_APP_DEPEND,
+	       );

Propchange: team/group/pimp_my_sip/res/res_sip_acl.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/group/pimp_my_sip/res/res_sip_acl.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/group/pimp_my_sip/res/res_sip_acl.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list