Debian Codegraph @

res/res_pjsip_acl.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Mark Michelson <[email protected]>
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_pjsip</depend>
22 <support_level>core</support_level>
23 ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28
29 #include "asterisk/res_pjsip.h"
30 #include "asterisk/module.h"
31 #include "asterisk/logger.h"
32 #include "asterisk/sorcery.h"
33 #include "asterisk/acl.h"
34 #include "asterisk/stasis.h"
35 #include "asterisk/security_events.h"
36
37 /*** DOCUMENTATION
38 <configInfo name="res_pjsip_acl" language="en_US">
39 <synopsis>SIP ACL module</synopsis>
40 <description><para>
41 <emphasis>ACL</emphasis>
42 </para><para>
43 The ACL module used by <literal>res_pjsip</literal>. This module is
44 independent of <literal>endpoints</literal> and operates on all inbound
45 SIP communication using res_pjsip.
46 </para><para>
47 There are two main ways of defining your ACL with the options
48 provided. You can use the <literal>permit</literal> and <literal>deny</literal> options
49 which act on <emphasis>IP</emphasis> addresses, or the <literal>contactpermit</literal>
50 and <literal>contactdeny</literal> options which act on <emphasis>Contact header</emphasis>
51 addresses in incoming REGISTER requests. You can combine the various options to
52 create a mixed ACL.
53 </para><para>
54 Additionally, instead of defining an ACL with options, you can reference IP or
55 Contact header ACLs from the file <filename>acl.conf</filename> by using the <literal>acl</literal>
56 or <literal>contactacl</literal> options.
57 </para></description>
58 <configFile name="pjsip.conf">
59 <configObject name="acl">
60 <since>
61 <version>12.0.0</version>
62 </since>
63 <synopsis>Access Control List</synopsis>
64 <configOption name="acl">
65 <since>
66 <version>12.2.0</version>
67 </since>
68 <synopsis>List of IP ACL section names in acl.conf</synopsis>
69 <description><para>
70 This matches sections configured in <literal>acl.conf</literal>. The value is
71 defined as a list of comma-delimited section names.
72 </para></description>
73 </configOption>
74 <configOption name="contact_acl">
75 <since>
76 <version>12.2.0</version>
77 </since>
78 <synopsis>List of Contact ACL section names in acl.conf</synopsis>
79 <description><para>
80 This matches sections configured in <literal>acl.conf</literal>. The value is
81 defined as a list of comma-delimited section names.
82 </para></description>
83 </configOption>
84 <configOption name="contact_deny">
85 <since>
86 <version>12.2.0</version>
87 </since>
88 <synopsis>List of Contact header addresses to deny</synopsis>
89 <description><para>
90 The value is a comma-delimited list of IP addresses. IP addresses may
91 have a subnet mask appended. The subnet mask may be written in either
92 CIDR or dotted-decimal notation. Separate the IP address and subnet
93 mask with a slash ('/')
94 </para></description>
95 </configOption>
96 <configOption name="contact_permit">
97 <since>
98 <version>12.2.0</version>
99 </since>
100 <synopsis>List of Contact header addresses to permit</synopsis>
101 <description><para>
102 The value is a comma-delimited list of IP addresses. IP addresses may
103 have a subnet mask appended. The subnet mask may be written in either
104 CIDR or dotted-decimal notation. Separate the IP address and subnet
105 mask with a slash ('/')
106 </para></description>
107 </configOption>
108 <configOption name="deny">
109 <since>
110 <version>12.2.0</version>
111 </since>
112 <synopsis>List of IP addresses to deny access from</synopsis>
113 <description><para>
114 The value is a comma-delimited list of IP addresses. IP addresses may
115 have a subnet mask appended. The subnet mask may be written in either
116 CIDR or dotted-decimal notation. Separate the IP address and subnet
117 mask with a slash ('/')
118 </para></description>
119 </configOption>
120 <configOption name="permit">
121 <since>
122 <version>12.2.0</version>
123 </since>
124 <synopsis>List of IP addresses to permit access from</synopsis>
125 <description><para>
126 The value is a comma-delimited list of IP addresses. IP addresses may
127 have a subnet mask appended. The subnet mask may be written in either
128 CIDR or dotted-decimal notation. Separate the IP address and subnet
129 mask with a slash ('/')
130 </para></description>
131 </configOption>
132 <configOption name="type">
133 <since>
134 <version>12.0.0</version>
135 </since>
136 <synopsis>Must be of type 'acl'.</synopsis>
137 </configOption>
138 </configObject>
139 </configFile>
140 </configInfo>
141 ***/
142
143 static struct stasis_subscription *acl_change_sub;
144
145 static int apply_acl(pjsip_rx_data *rdata, struct ast_acl_list *acl)
146 {
147 struct ast_sockaddr addr;
148
149 if (ast_acl_list_is_empty(acl)) {
150 return 0;
151 }
152
153 memset(&addr, 0, sizeof(addr));
154 ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID);
155 ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port);
156
157 if (ast_apply_acl(acl, &addr, "SIP ACL: ") != AST_SENSE_ALLOW) {
158 ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&addr));
159 return 1;
160 }
161 return 0;
162 }
163
164 static int extract_contact_addr(pjsip_contact_hdr *contact, struct ast_sockaddr **addrs)
165 {
166 pjsip_sip_uri *sip_uri;
167 char host[256];
168
169 if (!contact || contact->star) {
170 *addrs = NULL;
171 return 0;
172 }
173 if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) {
174 *addrs = NULL;
175 return 0;
176 }
177 sip_uri = pjsip_uri_get_uri(contact->uri);
178 ast_copy_pj_str(host, &sip_uri->host, sizeof(host));
179 return ast_sockaddr_resolve(addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC);
180 }
181
182 static int apply_contact_acl(pjsip_rx_data *rdata, struct ast_acl_list *contact_acl)
183 {
184 int num_contact_addrs;
185 int forbidden = 0;
186 struct ast_sockaddr *contact_addrs;
187 int i;
188 pjsip_contact_hdr *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr;
189
190 if (ast_acl_list_is_empty(contact_acl)) {
191 return 0;
192 }
193
194 while ((contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next))) {
195 num_contact_addrs = extract_contact_addr(contact, &contact_addrs);
196 if (num_contact_addrs <= 0) {
197 continue;
198 }
199 for (i = 0; i < num_contact_addrs; ++i) {
200 if (ast_apply_acl(contact_acl, &contact_addrs[i], "SIP Contact ACL: ") != AST_SENSE_ALLOW) {
201 ast_log(LOG_WARNING, "Incoming SIP message from %s did not pass ACL test\n", ast_sockaddr_stringify(&contact_addrs[i]));
202 forbidden = 1;
203 break;
204 }
205 }
206 ast_free(contact_addrs);
207 if (forbidden) {
208 /* No use checking other contacts if we already have failed ACL check */
209 break;
210 }
211 }
212
213 return forbidden;
214 }
215
216 #define SIP_SORCERY_ACL_TYPE "acl"
217
218 /*!
219 * \brief SIP ACL details and configuration.
220 */
221 struct ast_sip_acl {
222 SORCERY_OBJECT(details);
223 struct ast_acl_list *acl;
224 struct ast_acl_list *contact_acl;
225 };
226
227 static int check_acls(void *obj, void *arg, int flags)
228 {
229 struct ast_sip_acl *sip_acl = obj;
230 pjsip_rx_data *rdata = arg;
231
232 if (apply_acl(rdata, sip_acl->acl) ||
233 apply_contact_acl(rdata, sip_acl->contact_acl)) {
234 return CMP_MATCH | CMP_STOP;
235 }
236 return 0;
237 }
238
239 static pj_bool_t acl_on_rx_msg(pjsip_rx_data *rdata)
240 {
241 RAII_VAR(struct ao2_container *, acls, ast_sorcery_retrieve_by_fields(
242 ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
243 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL), ao2_cleanup);
244 RAII_VAR(struct ast_sip_acl *, matched_acl, NULL, ao2_cleanup);
245
246 if (!acls) {
247 ast_log(LOG_ERROR, "Unable to retrieve ACL sorcery data\n");
248 return PJ_FALSE;
249 }
250
251 if ((matched_acl = ao2_callback(acls, 0, check_acls, rdata))) {
252 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
253 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
254 }
255 return PJ_TRUE;
256 }
257
258 return PJ_FALSE;
259 }
260
261 static int acl_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
262 {
263 struct ast_sip_acl *sip_acl = obj;
264 int error = 0;
265 int ignore;
266
267 if (!strncmp(var->name, "contact_", 8)) {
268 ast_append_acl(var->name + 8, var->value, &sip_acl->contact_acl, &error, &ignore);
269 if (error) {
270 ast_log(LOG_ERROR, "Bad contact ACL '%s' at line '%d' of pjsip.conf\n",
271 var->value, var->lineno);
272 }
273 } else {
274 ast_append_acl(var->name, var->value, &sip_acl->acl, &error, &ignore);
275 if (error) {
276 ast_log(LOG_ERROR, "Bad ACL '%s' at line '%d' of pjsip.conf\n",
277 var->value, var->lineno);
278 }
279 }
280
281 if (error) {
282 ast_log(LOG_ERROR, "There is an error in ACL configuration. Blocking ALL SIP traffic.\n");
283 ast_append_acl("deny", "0.0.0.0/0.0.0.0", &sip_acl->acl, NULL, &ignore);
284 }
285
286 return error;
287 }
288
289 static pjsip_module acl_module = {
290 .name = { "ACL Module", 14 },
291 /* This should run after a logger but before anything else */
292 .priority = 1,
293 .on_rx_request = acl_on_rx_msg,
294 };
295
296 static void acl_destroy(void *obj)
297 {
298 struct ast_sip_acl *sip_acl = obj;
299 sip_acl->acl = ast_free_acl_list(sip_acl->acl);
300 sip_acl->contact_acl = ast_free_acl_list(sip_acl->contact_acl);
301 }
302
303 static void *acl_alloc(const char *name)
304 {
305 struct ast_sip_acl *sip_acl =
306 ast_sorcery_generic_alloc(sizeof(*sip_acl), acl_destroy);
307
308 return sip_acl;
309 }
310
311 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub,
312 struct stasis_message *message)
313 {
314 if (stasis_message_type(message) != ast_named_acl_change_type()) {
315 return;
316 }
317
318 ast_sorcery_force_reload_object(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE);
319 }
320
321 static int load_module(void)
322 {
323 ast_sorcery_apply_config(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE);
324 ast_sorcery_apply_default(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
325 "config", "pjsip.conf,criteria=type=acl");
326
327 if (ast_sorcery_object_register(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
328 acl_alloc, NULL, NULL)) {
329
330 ast_log(LOG_ERROR, "Failed to register SIP %s object with sorcery\n",
331 SIP_SORCERY_ACL_TYPE);
332 return AST_MODULE_LOAD_DECLINE;
333 }
334
335 ast_sorcery_object_field_register(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "type", "", OPT_NOOP_T, 0, 0);
336 ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "permit", "", acl_handler, NULL, NULL, 0, 0);
337 ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "deny", "", acl_handler, NULL, NULL, 0, 0);
338 ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "acl", "", acl_handler, NULL, NULL, 0, 0);
339 ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_permit", "", acl_handler, NULL, NULL, 0, 0);
340 ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_deny", "", acl_handler, NULL, NULL, 0, 0);
341 ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE, "contact_acl", "", acl_handler, NULL, NULL, 0, 0);
342
343 ast_sorcery_load_object(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE);
344
345 acl_change_sub = stasis_subscribe(ast_security_topic(), acl_change_stasis_cb, NULL);
346 stasis_subscription_accept_message_type(acl_change_sub, ast_named_acl_change_type());
347 stasis_subscription_set_filter(acl_change_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
348
349 ast_sip_register_service(&acl_module);
350
351 return AST_MODULE_LOAD_SUCCESS;
352 }
353
354 static int unload_module(void)
355 {
356 acl_change_sub = stasis_unsubscribe_and_join(acl_change_sub);
357 ast_sip_unregister_service(&acl_module);
358 return 0;
359 }
360
361 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP ACL Resource",
362 .support_level = AST_MODULE_SUPPORT_CORE,
363 .load = load_module,
364 .unload = unload_module,
365 .load_pri = AST_MODPRI_APP_DEPEND,
366 .requires = "res_pjsip",
367 );