Merge the pimp_my_sip branch into trunk.
[asterisk/asterisk.git] / res / res_sip / config_auth.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 #include "asterisk.h"
20
21 #include <pjsip.h>
22 #include <pjlib.h>
23 #include "asterisk/res_sip.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/sorcery.h"
26
27 static void auth_destroy(void *obj)
28 {
29         struct ast_sip_auth *auth = obj;
30         ast_string_field_free_memory(auth);
31 }
32
33 static void *auth_alloc(const char *name)
34 {
35         struct ast_sip_auth *auth = ao2_alloc(sizeof(*auth), auth_destroy);
36
37         if (!auth) {
38                 return NULL;
39         }
40
41         if (ast_string_field_init(auth, 64)) {
42                 ao2_cleanup(auth);
43                 return NULL;
44         }
45
46         return auth;
47 }
48
49 static int auth_type_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
50 {
51         struct ast_sip_auth *auth = obj;
52         if (!strcasecmp(var->value, "userpass")) {
53                 auth->type = AST_SIP_AUTH_TYPE_USER_PASS;
54         } else if (!strcasecmp(var->value, "md5")) {
55                 auth->type = AST_SIP_AUTH_TYPE_MD5;
56         } else {
57                 ast_log(LOG_WARNING, "Unknown authentication storage type '%s' specified for %s\n",
58                                 var->value, var->name);
59                 return -1;
60         }
61         return 0;
62 }
63
64 static int auth_apply(const struct ast_sorcery *sorcery, void *obj)
65 {
66         struct ast_sip_auth *auth = obj;
67         int res = 0;
68
69         if (ast_strlen_zero(auth->auth_user)) {
70                 ast_log(LOG_ERROR, "No authentication username for auth '%s'\n",
71                                 ast_sorcery_object_get_id(auth));
72                 return -1;
73         }
74
75         switch (auth->type) {
76         case AST_SIP_AUTH_TYPE_USER_PASS:
77                 if (ast_strlen_zero(auth->auth_pass)) {
78                         ast_log(LOG_ERROR, "'userpass' authentication specified but no"
79                                         "password specified for auth '%s'\n", ast_sorcery_object_get_id(auth));
80                         res = -1;
81                 }
82                 break;
83         case AST_SIP_AUTH_TYPE_MD5:
84                 if (ast_strlen_zero(auth->md5_creds)) {
85                         ast_log(LOG_ERROR, "'md5' authentication specified but no md5_cred"
86                                         "specified for auth '%s'\n", ast_sorcery_object_get_id(auth));
87                         res = -1;
88                 }
89                 break;
90         }
91
92         return res;
93 }
94
95 /*! \brief Initialize sorcery with auth support */
96 int ast_sip_initialize_sorcery_auth(struct ast_sorcery *sorcery)
97 {
98         ast_sorcery_apply_default(sorcery, SIP_SORCERY_AUTH_TYPE, "config", "res_sip.conf,criteria=type=auth");
99
100         if (ast_sorcery_object_register(sorcery, SIP_SORCERY_AUTH_TYPE, auth_alloc, NULL, auth_apply)) {
101                 return -1;
102         }
103
104         ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "type", "",
105                         OPT_NOOP_T, 0, 0);
106         ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "username",
107                         "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, auth_user));
108         ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "password",
109                         "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, auth_pass));
110         ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "md5_cred",
111                         "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, md5_creds));
112         ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "realm",
113                         "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, realm));
114         ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "nonce_lifetime",
115                         "32", OPT_UINT_T, 0, FLDSET(struct ast_sip_auth, nonce_lifetime));
116         ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_AUTH_TYPE, "auth_type",
117                         "userpass", auth_type_handler, NULL, 0, 0);
118
119         return 0;
120 }