| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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 |
|
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return forbidden; |
| 214 |
} |
| 215 |
|
| 216 |
#define SIP_SORCERY_ACL_TYPE "acl" |
| 217 |
|
| 218 |
|
| 219 |
|
| 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 |
|
| 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 |
); |