res_pjsip_acl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include <pjsip.h>
  25. #include "asterisk/res_pjsip.h"
  26. #include "asterisk/module.h"
  27. #include "asterisk/logger.h"
  28. #include "asterisk/sorcery.h"
  29. #include "asterisk/acl.h"
  30. /*** DOCUMENTATION
  31. <configInfo name="res_pjsip_acl" language="en_US">
  32. <synopsis>SIP ACL module</synopsis>
  33. <description><para>
  34. <emphasis>ACL</emphasis>
  35. </para><para>
  36. The ACL module used by <literal>res_pjsip</literal>. This module is
  37. independent of <literal>endpoints</literal> and operates on all inbound
  38. SIP communication using res_pjsip.
  39. </para><para>
  40. There are two main ways of defining your ACL with the options
  41. provided. You can use the <literal>permit</literal> and <literal>deny</literal> options
  42. which act on <emphasis>IP</emphasis> addresses, or the <literal>contactpermit</literal>
  43. and <literal>contactdeny</literal> options which act on <emphasis>Contact header</emphasis>
  44. addresses in incoming REGISTER requests. You can combine the various options to
  45. create a mixed ACL.
  46. </para><para>
  47. Additionally, instead of defining an ACL with options, you can reference IP or
  48. Contact header ACLs from the file <filename>acl.conf</filename> by using the <literal>acl</literal>
  49. or <literal>contactacl</literal> options.
  50. </para></description>
  51. <configFile name="pjsip.conf">
  52. <configObject name="acl">
  53. <synopsis>Access Control List</synopsis>
  54. <configOption name="acl">
  55. <synopsis>List of IP ACL section names in acl.conf</synopsis>
  56. <description><para>
  57. This matches sections configured in <literal>acl.conf</literal>. The value is
  58. defined as a list of comma-delimited section names.
  59. </para></description>
  60. </configOption>
  61. <configOption name="contact_acl">
  62. <synopsis>List of Contact ACL section names in acl.conf</synopsis>
  63. <description><para>
  64. This matches sections configured in <literal>acl.conf</literal>. The value is
  65. defined as a list of comma-delimited section names.
  66. </para></description>
  67. </configOption>
  68. <configOption name="contact_deny">
  69. <synopsis>List of Contact header addresses to deny</synopsis>
  70. <description><para>
  71. The value is a comma-delimited list of IP addresses. IP addresses may
  72. have a subnet mask appended. The subnet mask may be written in either
  73. CIDR or dotted-decimal notation. Separate the IP address and subnet
  74. mask with a slash ('/')
  75. </para></description>
  76. </configOption>
  77. <configOption name="contact_permit">
  78. <synopsis>List of Contact header addresses to permit</synopsis>
  79. <description><para>
  80. The value is a comma-delimited list of IP addresses. IP addresses may
  81. have a subnet mask appended. The subnet mask may be written in either
  82. CIDR or dotted-decimal notation. Separate the IP address and subnet
  83. mask with a slash ('/')
  84. </para></description>
  85. </configOption>
  86. <configOption name="deny">
  87. <synopsis>List of IP addresses to deny access from</synopsis>
  88. <description><para>
  89. The value is a comma-delimited list of IP addresses. IP addresses may
  90. have a subnet mask appended. The subnet mask may be written in either
  91. CIDR or dotted-decimal notation. Separate the IP address and subnet
  92. mask with a slash ('/')
  93. </para></description>
  94. </configOption>
  95. <configOption name="permit">
  96. <synopsis>List of IP addresses to permit access from</synopsis>
  97. <description><para>
  98. The value is a comma-delimited list of IP addresses. IP addresses may
  99. have a subnet mask appended. The subnet mask may be written in either
  100. CIDR or dotted-decimal notation. Separate the IP address and subnet
  101. mask with a slash ('/')
  102. </para></description>
  103. </configOption>
  104. <configOption name="type">
  105. <synopsis>Must be of type 'acl'.</synopsis>
  106. </configOption>
  107. </configObject>
  108. </configFile>
  109. </configInfo>
  110. ***/
  111. static int apply_acl(pjsip_rx_data *rdata, struct ast_acl_list *acl)
  112. {
  113. struct ast_sockaddr addr;
  114. if (ast_acl_list_is_empty(acl)) {
  115. return 0;
  116. }
  117. memset(&addr, 0, sizeof(addr));
  118. ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID);
  119. ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port);
  120. if (ast_apply_acl(acl, &addr, "SIP ACL: ") != AST_SENSE_ALLOW) {
  121. ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&addr));
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. static int extract_contact_addr(pjsip_contact_hdr *contact, struct ast_sockaddr **addrs)
  127. {
  128. pjsip_sip_uri *sip_uri;
  129. char host[256];
  130. if (!contact || contact->star) {
  131. return 0;
  132. }
  133. if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) {
  134. return 0;
  135. }
  136. sip_uri = pjsip_uri_get_uri(contact->uri);
  137. ast_copy_pj_str(host, &sip_uri->host, sizeof(host));
  138. return ast_sockaddr_resolve(addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC);
  139. }
  140. static int apply_contact_acl(pjsip_rx_data *rdata, struct ast_acl_list *contact_acl)
  141. {
  142. int num_contact_addrs;
  143. int forbidden = 0;
  144. struct ast_sockaddr *contact_addrs;
  145. int i;
  146. pjsip_contact_hdr *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr;
  147. if (ast_acl_list_is_empty(contact_acl)) {
  148. return 0;
  149. }
  150. while ((contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next))) {
  151. num_contact_addrs = extract_contact_addr(contact, &contact_addrs);
  152. if (num_contact_addrs <= 0) {
  153. continue;
  154. }
  155. for (i = 0; i < num_contact_addrs; ++i) {
  156. if (ast_apply_acl(contact_acl, &contact_addrs[i], "SIP Contact ACL: ") != AST_SENSE_ALLOW) {
  157. ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&contact_addrs[i]));
  158. forbidden = 1;
  159. break;
  160. }
  161. }
  162. ast_free(contact_addrs);
  163. if (forbidden) {
  164. /* No use checking other contacts if we already have failed ACL check */
  165. break;
  166. }
  167. }
  168. return forbidden;
  169. }
  170. #define SIP_SORCERY_ACL_TYPE "acl"
  171. /*!
  172. * \brief SIP ACL details and configuration.
  173. */
  174. struct ast_sip_acl {
  175. SORCERY_OBJECT(details);
  176. struct ast_acl_list *acl;
  177. struct ast_acl_list *contact_acl;
  178. };
  179. static int check_acls(void *obj, void *arg, int flags)
  180. {
  181. struct ast_sip_acl *sip_acl = obj;
  182. pjsip_rx_data *rdata = arg;
  183. if (apply_acl(rdata, sip_acl->acl) ||
  184. apply_contact_acl(rdata, sip_acl->contact_acl)) {
  185. return CMP_MATCH | CMP_STOP;
  186. }
  187. return 0;
  188. }
  189. static pj_bool_t acl_on_rx_msg(pjsip_rx_data *rdata)
  190. {
  191. RAII_VAR(struct ao2_container *, acls, ast_sorcery_retrieve_by_fields(
  192. ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
  193. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL), ao2_cleanup);
  194. RAII_VAR(struct ast_sip_acl *, matched_acl, NULL, ao2_cleanup);
  195. if (!acls) {
  196. ast_log(LOG_ERROR, "Unable to retrieve ACL sorcery data\n");
  197. return PJ_FALSE;
  198. }
  199. if ((matched_acl = ao2_callback(acls, 0, check_acls, rdata))) {
  200. if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
  201. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
  202. }
  203. return PJ_TRUE;
  204. }
  205. return PJ_FALSE;
  206. }
  207. static int acl_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  208. {
  209. struct ast_sip_acl *sip_acl = obj;
  210. int error = 0;
  211. int ignore;
  212. if (!strncmp(var->name, "contact_", 8)) {
  213. ast_append_acl(var->name + 8, var->value, &sip_acl->contact_acl, &error, &ignore);
  214. } else {
  215. ast_append_acl(var->name, var->value, &sip_acl->acl, &error, &ignore);
  216. }
  217. return error;
  218. }
  219. static pjsip_module acl_module = {
  220. .name = { "ACL Module", 14 },
  221. /* This should run after a logger but before anything else */
  222. .priority = 1,
  223. .on_rx_request = acl_on_rx_msg,
  224. };
  225. static void acl_destroy(void *obj)
  226. {
  227. struct ast_sip_acl *sip_acl = obj;
  228. sip_acl->acl = ast_free_acl_list(sip_acl->acl);
  229. sip_acl->contact_acl = ast_free_acl_list(sip_acl->contact_acl);
  230. }
  231. static void *acl_alloc(const char *name)
  232. {
  233. struct ast_sip_acl *sip_acl =
  234. ast_sorcery_generic_alloc(sizeof(*sip_acl), acl_destroy);
  235. return sip_acl;
  236. }
  237. static int load_module(void)
  238. {
  239. CHECK_PJSIP_MODULE_LOADED();
  240. ast_sorcery_apply_default(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
  241. "config", "pjsip.conf,criteria=type=acl");
  242. if (ast_sorcery_object_register(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
  243. acl_alloc, NULL, NULL)) {
  244. ast_log(LOG_ERROR, "Failed to register SIP %s object with sorcery\n",
  245. SIP_SORCERY_ACL_TYPE);
  246. return AST_MODULE_LOAD_DECLINE;
  247. }
  248. ast_sorcery_object_field_register(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "type", "", OPT_NOOP_T, 0, 0);
  249. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "permit", "", acl_handler, NULL, NULL, 0, 0);
  250. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "deny", "", acl_handler, NULL, NULL, 0, 0);
  251. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "acl", "", acl_handler, NULL, NULL, 0, 0);
  252. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_permit", "", acl_handler, NULL, NULL, 0, 0);
  253. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_deny", "", acl_handler, NULL, NULL, 0, 0);
  254. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_acl", "", acl_handler, NULL, NULL, 0, 0);
  255. ast_sorcery_load_object(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE);
  256. ast_sip_register_service(&acl_module);
  257. return AST_MODULE_LOAD_SUCCESS;
  258. }
  259. static int unload_module(void)
  260. {
  261. ast_sip_unregister_service(&acl_module);
  262. return 0;
  263. }
  264. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP ACL Resource",
  265. .support_level = AST_MODULE_SUPPORT_CORE,
  266. .load = load_module,
  267. .unload = unload_module,
  268. .load_pri = AST_MODPRI_APP_DEPEND,
  269. );