res_pjsip_endpoint_identifier_user.c 7.0 KB

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