Merge "res_rtp_asterisk.c: Fix rtp source address learning for broken clients"
[asterisk/asterisk.git] / res / res_pjsip_endpoint_identifier_user.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_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
32 static int get_from_header(pjsip_rx_data *rdata, char *username, size_t username_size, char *domain, size_t domain_size)
33 {
34         pjsip_uri *from = rdata->msg_info.from->uri;
35         pjsip_sip_uri *sip_from;
36
37         if (!PJSIP_URI_SCHEME_IS_SIP(from) && !PJSIP_URI_SCHEME_IS_SIPS(from)) {
38                 return -1;
39         }
40         sip_from = (pjsip_sip_uri *) pjsip_uri_get_uri(from);
41         ast_copy_pj_str(username, &sip_from->user, username_size);
42         ast_copy_pj_str(domain, &sip_from->host, domain_size);
43         return 0;
44 }
45
46 static pjsip_authorization_hdr *get_auth_header(pjsip_rx_data *rdata, char *username,
47         size_t username_size, char *realm, size_t realm_size, pjsip_authorization_hdr *start)
48 {
49         pjsip_authorization_hdr *header;
50
51         header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_AUTHORIZATION, start);
52
53         if (!header || pj_stricmp2(&header->scheme, "digest")) {
54                 return NULL;
55         }
56
57         ast_copy_pj_str(username, &header->credential.digest.username, username_size);
58         ast_copy_pj_str(realm, &header->credential.digest.realm, realm_size);
59
60         return header;
61 }
62
63 static int find_transport_state_in_use(void *obj, void *arg, int flags)
64 {
65         struct ast_sip_transport_state *transport_state = obj;
66         pjsip_rx_data *rdata = arg;
67
68         if (transport_state && ((transport_state->transport == rdata->tp_info.transport) ||
69                 (transport_state->factory && !pj_strcmp(&transport_state->factory->addr_name.host, &rdata->tp_info.transport->local_name.host) &&
70                         transport_state->factory->addr_name.port == rdata->tp_info.transport->local_name.port))) {
71                 return CMP_MATCH | CMP_STOP;
72         }
73
74         return 0;
75 }
76
77 #define DOMAIN_NAME_LEN 255
78 #define USERNAME_LEN    255
79
80 static struct ast_sip_endpoint *find_endpoint(pjsip_rx_data *rdata, char *endpoint_name,
81         char *domain_name)
82 {
83         struct ast_sip_endpoint *endpoint;
84         RAII_VAR(struct ast_sip_domain_alias *, alias, NULL, ao2_cleanup);
85         RAII_VAR(struct ao2_container *, transport_states, NULL, ao2_cleanup);
86         RAII_VAR(struct ast_sip_transport_state *, transport_state, NULL, ao2_cleanup);
87         RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
88
89         if (!ast_sip_get_disable_multi_domain()) {
90                 char id[DOMAIN_NAME_LEN + USERNAME_LEN + sizeof("@")];
91
92                 /* Attempt to find the endpoint given the name and domain provided */
93                 snprintf(id, sizeof(id), "%s@%s", endpoint_name, domain_name);
94                 if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
95                         return endpoint;
96                 }
97
98                 /* See if an alias exists for the domain provided */
99                 if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
100                         snprintf(id, sizeof(id), "%s@%s", endpoint_name, alias->domain);
101                         if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
102                                 return endpoint;
103                         }
104                 }
105                 /* See if the transport this came in on has a provided domain */
106                 if ((transport_states = ast_sip_get_transport_states())
107                         && (transport_state = ao2_callback(transport_states, 0, find_transport_state_in_use, rdata))
108                         && (transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_state->id))
109                         && !ast_strlen_zero(transport->domain)) {
110                         snprintf(id, sizeof(id), "anonymous@%s", transport->domain);
111                         if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
112                                 return endpoint;
113                         }
114                 }
115         }
116
117         /* Fall back to no domain */
118         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name);
119 }
120
121 static struct ast_sip_endpoint *username_identify(pjsip_rx_data *rdata)
122 {
123         char username[USERNAME_LEN + 1];
124         char domain[DOMAIN_NAME_LEN + 1];
125         struct ast_sip_endpoint *endpoint;
126
127         if (get_from_header(rdata, username, sizeof(username), domain, sizeof(domain))) {
128                 return NULL;
129         }
130
131         /*
132          * We may want to be matched without any user options getting
133          * in the way.
134          */
135         AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(username);
136
137         ast_debug(3, "Attempting identify by From username '%s' domain '%s'\n", username, domain);
138
139         endpoint = find_endpoint(rdata, username, domain);
140         if (!endpoint) {
141                 ast_debug(3, "Endpoint not found for From username '%s' domain '%s'\n", username, domain);
142                 return NULL;
143         }
144         if (!(endpoint->ident_method & AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME)) {
145                 ast_debug(3, "Endpoint found for '%s' but 'username' method not supported'\n", username);
146                 ao2_cleanup(endpoint);
147                 return NULL;
148         }
149         ast_debug(3, "Identified by From username '%s' domain '%s'\n", username, domain);
150
151         return endpoint;
152 }
153
154 static struct ast_sip_endpoint *auth_username_identify(pjsip_rx_data *rdata)
155 {
156         char username[USERNAME_LEN + 1], realm[DOMAIN_NAME_LEN + 1];
157         struct ast_sip_endpoint *endpoint;
158         pjsip_authorization_hdr *auth_header = NULL;
159
160         while ((auth_header = get_auth_header(rdata, username, sizeof(username), realm, sizeof(realm),
161                 auth_header ? auth_header->next : NULL))) {
162                 ast_debug(3, "Attempting identify by Authorization username '%s' realm '%s'\n", username,
163                         realm);
164
165                 endpoint = find_endpoint(rdata, username, realm);
166                 if (!endpoint) {
167                         ast_debug(3, "Endpoint not found for Authentication username '%s' realm '%s'\n",
168                                 username, realm);
169                         ao2_cleanup(endpoint);
170                         continue;
171                 }
172                 if (!(endpoint->ident_method & AST_SIP_ENDPOINT_IDENTIFY_BY_AUTH_USERNAME)) {
173                         ast_debug(3, "Endpoint found for '%s' but 'auth_username' method not supported'\n",
174                                 username);
175                         ao2_cleanup(endpoint);
176                         continue;
177                 }
178                 ast_debug(3, "Identified by Authorization username '%s' realm '%s'\n", username, realm);
179
180                 return endpoint;
181         }
182
183         return NULL;
184 }
185
186
187 static struct ast_sip_endpoint_identifier username_identifier = {
188         .identify_endpoint = username_identify,
189 };
190
191 static struct ast_sip_endpoint_identifier auth_username_identifier = {
192         .identify_endpoint = auth_username_identify,
193 };
194
195
196 static int load_module(void)
197 {
198         CHECK_PJSIP_MODULE_LOADED();
199
200         ast_sip_register_endpoint_identifier_with_name(&username_identifier, "username");
201         ast_sip_register_endpoint_identifier_with_name(&auth_username_identifier, "auth_username");
202         return AST_MODULE_LOAD_SUCCESS;
203 }
204
205 static int unload_module(void)
206 {
207         ast_sip_unregister_endpoint_identifier(&auth_username_identifier);
208         ast_sip_unregister_endpoint_identifier(&username_identifier);
209         return 0;
210 }
211
212 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP username endpoint identifier",
213         .support_level = AST_MODULE_SUPPORT_CORE,
214         .load = load_module,
215         .unload = unload_module,
216         .load_pri = AST_MODPRI_CHANNEL_DEPEND - 4,
217 );