New SIP Channel driver: Always Auth Reject
[asterisk/asterisk.git] / res / res_sip / config_security.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  * Kevin Harwell <kharwell@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*** MODULEINFO
21         <depend>pjproject</depend>
22         <depend>res_sip</depend>
23         <support_level>core</support_level>
24  ***/
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28
29 #include "asterisk/res_sip.h"
30 #include "asterisk/logger.h"
31 #include "asterisk/sorcery.h"
32 #include "asterisk/acl.h"
33
34 static int acl_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
35 {
36         struct ast_sip_security *security = obj;
37         int error = 0;
38         int ignore;
39         if (!strncmp(var->name, "contact", 7)) {
40                 ast_append_acl(var->name + 7, var->value, &security->contact_acl, &error, &ignore);
41         } else {
42                 ast_append_acl(var->name, var->value, &security->acl, &error, &ignore);
43         }
44
45         return error;
46 }
47
48 static void security_destroy(void *obj)
49 {
50         struct ast_sip_security *security = obj;
51         security->acl = ast_free_acl_list(security->acl);
52         security->contact_acl = ast_free_acl_list(security->contact_acl);
53 }
54
55 static void *security_alloc(const char *name)
56 {
57         struct ast_sip_security *security =
58                 ast_sorcery_generic_alloc(sizeof(*security), security_destroy);
59
60         if (!security) {
61                 return NULL;
62         }
63
64         return security;
65 }
66
67 int ast_sip_initialize_sorcery_security(struct ast_sorcery *sorcery)
68 {
69         ast_sorcery_apply_default(sorcery, SIP_SORCERY_SECURITY_TYPE,
70                                   "config", "res_sip.conf,criteria=type=security");
71
72         if (ast_sorcery_object_register(sorcery, SIP_SORCERY_SECURITY_TYPE,
73                                         security_alloc, NULL, NULL)) {
74
75                 ast_log(LOG_ERROR, "Failed to register SIP %s object with sorcery\n",
76                         SIP_SORCERY_SECURITY_TYPE);
77                 return -1;
78         }
79
80         ast_sorcery_object_field_register(sorcery, SIP_SORCERY_SECURITY_TYPE, "type", "", OPT_NOOP_T, 0, 0);
81         ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_SECURITY_TYPE, "permit", "", acl_handler, NULL, 0, 0);
82         ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_SECURITY_TYPE, "deny", "", acl_handler, NULL, 0, 0);
83         ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_SECURITY_TYPE, "acl", "", acl_handler, NULL, 0, 0);
84         ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_SECURITY_TYPE, "contactpermit", "", acl_handler, NULL, 0, 0);
85         ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_SECURITY_TYPE, "contactdeny", "", acl_handler, NULL, 0, 0);
86         ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_SECURITY_TYPE, "contactacl", "", acl_handler, NULL, 0, 0);
87         return 0;
88 }