res_pjsip_acl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. *addrs = NULL;
  132. return 0;
  133. }
  134. if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) {
  135. *addrs = NULL;
  136. return 0;
  137. }
  138. sip_uri = pjsip_uri_get_uri(contact->uri);
  139. ast_copy_pj_str(host, &sip_uri->host, sizeof(host));
  140. return ast_sockaddr_resolve(addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC);
  141. }
  142. static int apply_contact_acl(pjsip_rx_data *rdata, struct ast_acl_list *contact_acl)
  143. {
  144. int num_contact_addrs;
  145. int forbidden = 0;
  146. struct ast_sockaddr *contact_addrs;
  147. int i;
  148. pjsip_contact_hdr *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr;
  149. if (ast_acl_list_is_empty(contact_acl)) {
  150. return 0;
  151. }
  152. while ((contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next))) {
  153. num_contact_addrs = extract_contact_addr(contact, &contact_addrs);
  154. if (num_contact_addrs <= 0) {
  155. continue;
  156. }
  157. for (i = 0; i < num_contact_addrs; ++i) {
  158. if (ast_apply_acl(contact_acl, &contact_addrs[i], "SIP Contact ACL: ") != AST_SENSE_ALLOW) {
  159. ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&contact_addrs[i]));
  160. forbidden = 1;
  161. break;
  162. }
  163. }
  164. ast_free(contact_addrs);
  165. if (forbidden) {
  166. /* No use checking other contacts if we already have failed ACL check */
  167. break;
  168. }
  169. }
  170. return forbidden;
  171. }
  172. #define SIP_SORCERY_ACL_TYPE "acl"
  173. /*!
  174. * \brief SIP ACL details and configuration.
  175. */
  176. struct ast_sip_acl {
  177. SORCERY_OBJECT(details);
  178. struct ast_acl_list *acl;
  179. struct ast_acl_list *contact_acl;
  180. };
  181. static int check_acls(void *obj, void *arg, int flags)
  182. {
  183. struct ast_sip_acl *sip_acl = obj;
  184. pjsip_rx_data *rdata = arg;
  185. if (apply_acl(rdata, sip_acl->acl) ||
  186. apply_contact_acl(rdata, sip_acl->contact_acl)) {
  187. return CMP_MATCH | CMP_STOP;
  188. }
  189. return 0;
  190. }
  191. static pj_bool_t acl_on_rx_msg(pjsip_rx_data *rdata)
  192. {
  193. RAII_VAR(struct ao2_container *, acls, ast_sorcery_retrieve_by_fields(
  194. ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
  195. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL), ao2_cleanup);
  196. RAII_VAR(struct ast_sip_acl *, matched_acl, NULL, ao2_cleanup);
  197. if (!acls) {
  198. ast_log(LOG_ERROR, "Unable to retrieve ACL sorcery data\n");
  199. return PJ_FALSE;
  200. }
  201. if ((matched_acl = ao2_callback(acls, 0, check_acls, rdata))) {
  202. if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
  203. pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
  204. }
  205. return PJ_TRUE;
  206. }
  207. return PJ_FALSE;
  208. }
  209. static int acl_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  210. {
  211. struct ast_sip_acl *sip_acl = obj;
  212. int error = 0;
  213. int ignore;
  214. if (!strncmp(var->name, "contact_", 8)) {
  215. ast_append_acl(var->name + 8, var->value, &sip_acl->contact_acl, &error, &ignore);
  216. } else {
  217. ast_append_acl(var->name, var->value, &sip_acl->acl, &error, &ignore);
  218. }
  219. return error;
  220. }
  221. static pjsip_module acl_module = {
  222. .name = { "ACL Module", 14 },
  223. /* This should run after a logger but before anything else */
  224. .priority = 1,
  225. .on_rx_request = acl_on_rx_msg,
  226. };
  227. static void acl_destroy(void *obj)
  228. {
  229. struct ast_sip_acl *sip_acl = obj;
  230. sip_acl->acl = ast_free_acl_list(sip_acl->acl);
  231. sip_acl->contact_acl = ast_free_acl_list(sip_acl->contact_acl);
  232. }
  233. static void *acl_alloc(const char *name)
  234. {
  235. struct ast_sip_acl *sip_acl =
  236. ast_sorcery_generic_alloc(sizeof(*sip_acl), acl_destroy);
  237. return sip_acl;
  238. }
  239. static int load_module(void)
  240. {
  241. CHECK_PJSIP_MODULE_LOADED();
  242. ast_sorcery_apply_config(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE);
  243. ast_sorcery_apply_default(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
  244. "config", "pjsip.conf,criteria=type=acl");
  245. if (ast_sorcery_object_register(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
  246. acl_alloc, NULL, NULL)) {
  247. ast_log(LOG_ERROR, "Failed to register SIP %s object with sorcery\n",
  248. SIP_SORCERY_ACL_TYPE);
  249. return AST_MODULE_LOAD_DECLINE;
  250. }
  251. ast_sorcery_object_field_register(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "type", "", OPT_NOOP_T, 0, 0);
  252. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "permit", "", acl_handler, NULL, NULL, 0, 0);
  253. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "deny", "", acl_handler, NULL, NULL, 0, 0);
  254. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "acl", "", acl_handler, NULL, NULL, 0, 0);
  255. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_permit", "", acl_handler, NULL, NULL, 0, 0);
  256. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_deny", "", acl_handler, NULL, NULL, 0, 0);
  257. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_acl", "", acl_handler, NULL, NULL, 0, 0);
  258. ast_sorcery_load_object(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE);
  259. ast_sip_register_service(&acl_module);
  260. return AST_MODULE_LOAD_SUCCESS;
  261. }
  262. static int unload_module(void)
  263. {
  264. ast_sip_unregister_service(&acl_module);
  265. return 0;
  266. }
  267. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP ACL Resource",
  268. .support_level = AST_MODULE_SUPPORT_CORE,
  269. .load = load_module,
  270. .unload = unload_module,
  271. .load_pri = AST_MODPRI_APP_DEPEND,
  272. );