Add base XML documentation for res_sip
[asterisk/asterisk.git] / res / res_sip_acl.c
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
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_sip</depend>
22         <support_level>core</support_level>
23  ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28
29 #include "asterisk/res_sip.h"
30 #include "asterisk/module.h"
31 #include "asterisk/logger.h"
32 #include "asterisk/sorcery.h"
33 #include "asterisk/acl.h"
34
35 /*** DOCUMENTATION
36         <configInfo name="res_sip_acl" language="en_US">
37                 <synopsis>SIP ACL module</synopsis>
38                 <description><para>
39                         <emphasis>ACL</emphasis>
40                         </para>
41                         <para>The ACL module used by <literal>res_sip</literal>. This module is
42                         independent of <literal>endpoints</literal> and operates on all inbound
43                         SIP communication using res_sip.
44                         </para><para>
45                         It should be noted that this module can also reference ACLs from
46                         <filename>acl.conf</filename>.
47                         </para><para>
48                         There are two main ways of creating an access list: <literal>IP-Domain</literal>
49                         and <literal>Contact Header</literal>. It is possible to create a combined ACL using
50                         both IP and Contact.
51                 </para></description>
52                 <configFile name="res_sip.conf">
53                         <configObject name="acl">
54                                 <synopsis>Access Control List</synopsis>
55                                 <configOption name="acl">
56                                         <synopsis>Name of IP ACL</synopsis>
57                                         <description><para>
58                                                 This matches sections configured in <literal>acl.conf</literal>
59                                         </para></description>
60                                 </configOption>
61                                 <configOption name="contactacl">
62                                         <synopsis>Name of Contact ACL</synopsis>
63                                         <description><para>
64                                                 This matches sections configured in <literal>acl.conf</literal>
65                                         </para></description>
66                                 </configOption>
67                                 <configOption name="contactdeny">
68                                         <synopsis>List of Contact Header addresses to Deny</synopsis>
69                                 </configOption>
70                                 <configOption name="contactpermit">
71                                         <synopsis>List of Contact Header addresses to Permit</synopsis>
72                                 </configOption>
73                                 <configOption name="deny">
74                                         <synopsis>List of IP-domains to deny access from</synopsis>
75                                 </configOption>
76                                 <configOption name="permit">
77                                         <synopsis>List of IP-domains to allow access from</synopsis>
78                                 </configOption>
79                                 <configOption name="type">
80                                         <synopsis>Must be of type 'acl'.</synopsis>
81                                 </configOption>
82                         </configObject>
83                 </configFile>
84         </configInfo>
85  ***/
86
87 struct sip_acl {
88         SORCERY_OBJECT(details);
89         struct ast_acl_list *acl;
90         struct ast_acl_list *contact_acl;
91 };
92
93 static int apply_acl(pjsip_rx_data *rdata, struct ast_acl_list *acl)
94 {
95         struct ast_sockaddr addr;
96
97         if (ast_acl_list_is_empty(acl)) {
98                 return 0;
99         }
100
101         memset(&addr, 0, sizeof(addr));
102         ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID);
103         ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port);
104
105         if (ast_apply_acl(acl, &addr, "SIP ACL: ") != AST_SENSE_ALLOW) {
106                 ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&addr));
107                 return 1;
108         }
109         return 0;
110 }
111
112 static int extract_contact_addr(pjsip_contact_hdr *contact, struct ast_sockaddr **addrs)
113 {
114         pjsip_sip_uri *sip_uri;
115         char host[256];
116
117         if (!contact) {
118                 return 0;
119         }
120         if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) {
121                 return 0;
122         }
123         sip_uri = pjsip_uri_get_uri(contact->uri);
124         ast_copy_pj_str(host, &sip_uri->host, sizeof(host));
125         return ast_sockaddr_resolve(addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC);
126 }
127
128 static int apply_contact_acl(pjsip_rx_data *rdata, struct ast_acl_list *contact_acl)
129 {
130         int num_contact_addrs;
131         int forbidden = 0;
132         struct ast_sockaddr *contact_addrs;
133         int i;
134         pjsip_contact_hdr *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr;
135
136         if (ast_acl_list_is_empty(contact_acl)) {
137                 return 0;
138         }
139
140         while ((contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next))) {
141                 num_contact_addrs = extract_contact_addr(contact, &contact_addrs);
142                 if (num_contact_addrs <= 0) {
143                         continue;
144                 }
145                 for (i = 0; i < num_contact_addrs; ++i) {
146                         if (ast_apply_acl(contact_acl, &contact_addrs[i], "SIP Contact ACL: ") != AST_SENSE_ALLOW) {
147                                 ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&contact_addrs[i]));
148                                 forbidden = 1;
149                                 break;
150                         }
151                 }
152                 ast_free(contact_addrs);
153                 if (forbidden) {
154                         /* No use checking other contacts if we already have failed ACL check */
155                         break;
156                 }
157         }
158
159         return forbidden;
160 }
161
162 static int check_acls(void *obj, void *arg, int flags)
163 {
164         struct sip_acl *acl = obj;
165         pjsip_rx_data *rdata = arg;
166
167         if (apply_acl(rdata, acl->acl) || apply_contact_acl(rdata, acl->contact_acl)) {
168                 return CMP_MATCH | CMP_STOP;
169         }
170         return 0;
171 }
172
173 static pj_bool_t acl_on_rx_msg(pjsip_rx_data *rdata)
174 {
175         int forbidden = 0;
176         struct ao2_container *acls = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "acl", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
177         struct sip_acl *matched_acl;
178         if (!acls) {
179                 ast_log(LOG_ERROR, "Unable to retrieve ACL sorcery data\n");
180                 return PJ_FALSE;
181         }
182
183         matched_acl = ao2_callback(acls, 0, check_acls, rdata);
184         if (matched_acl) {
185                 forbidden = 1;
186                 ao2_ref(matched_acl, -1);
187         }
188         ao2_ref(acls, -1);
189
190         if (forbidden) {
191                 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
192                         pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
193                 }
194                 return PJ_TRUE;
195         }
196
197         return PJ_FALSE;
198 }
199
200 static pjsip_module acl_module = {
201         .name = { "ACL Module", 14 },
202         /* This should run after a logger but before anything else */
203         .priority = 1,
204         .on_rx_request = acl_on_rx_msg,
205 };
206
207 static int acl_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
208 {
209         struct sip_acl *acl = obj;
210         int error;
211         int ignore;
212         if (!strncmp(var->name, "contact", 7)) {
213                 ast_append_acl(var->name + 7, var->value, &acl->contact_acl, &error, &ignore);
214         } else {
215                 ast_append_acl(var->name, var->value, &acl->acl, &error, &ignore);
216         }
217         return error;
218 }
219
220 static void sip_acl_destructor(void *obj)
221 {
222         struct sip_acl *acl = obj;
223         acl->acl = ast_free_acl_list(acl->acl);
224         acl->contact_acl = ast_free_acl_list(acl->contact_acl);
225 }
226
227 static void *sip_acl_alloc(const char *name)
228 {
229         struct sip_acl *acl = ao2_alloc(sizeof(*acl), sip_acl_destructor);
230         if (!acl) {
231                 return NULL;
232         }
233         return acl;
234 }
235
236 static int load_acls(void)
237 {
238         ast_sorcery_apply_default(ast_sip_get_sorcery(), "acl", "config", "res_sip.conf,criteria=type=acl");
239         if (ast_sorcery_object_register(ast_sip_get_sorcery(), "acl", sip_acl_alloc, NULL, NULL)) {
240                 ast_log(LOG_ERROR, "Failed to register SIP ACL object with sorcery\n");
241                 return -1;
242         }
243         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "acl", "type", "", OPT_NOOP_T, 0, 0);
244         ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "permit", "", acl_handler, NULL, 0, 0);
245         ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "deny", "", acl_handler, NULL, 0, 0);
246         ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "acl", "", acl_handler, NULL, 0, 0);
247         ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "contactpermit", "", acl_handler, NULL, 0, 0);
248         ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "contactdeny", "", acl_handler, NULL, 0, 0);
249         ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "acl", "contactacl", "", acl_handler, NULL, 0, 0);
250
251         /* XXX Is there a more selective way to do this? (i.e. Just reload a specific object type?) */
252         ast_sorcery_reload(ast_sip_get_sorcery());
253         return 0;
254 }
255
256 static int load_module(void)
257 {
258         if (load_acls()) {
259                 return AST_MODULE_LOAD_DECLINE;
260         }
261         ast_sip_register_service(&acl_module);
262         return AST_MODULE_LOAD_SUCCESS;
263 }
264
265 static int unload_module(void)
266 {
267         ast_sip_unregister_service(&acl_module);
268         return 0;
269 }
270
271 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP ACL Resource",
272                 .load = load_module,
273                 .unload = unload_module,
274                 .load_pri = AST_MODPRI_APP_DEPEND,
275                );