res_pjsip_endpoint_identifier_user.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_endpoint_details(pjsip_rx_data *rdata, char *endpoint, size_t endpoint_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(endpoint, &sip_from->user, endpoint_size);
  36. ast_copy_pj_str(domain, &sip_from->host, domain_size);
  37. return 0;
  38. }
  39. static int find_transport_in_use(void *obj, void *arg, int flags)
  40. {
  41. struct ast_sip_transport *transport = obj;
  42. pjsip_rx_data *rdata = arg;
  43. if ((transport->state->transport == rdata->tp_info.transport) ||
  44. (transport->state->factory && !pj_strcmp(&transport->state->factory->addr_name.host, &rdata->tp_info.transport->local_name.host) &&
  45. transport->state->factory->addr_name.port == rdata->tp_info.transport->local_name.port)) {
  46. return CMP_MATCH | CMP_STOP;
  47. }
  48. return 0;
  49. }
  50. static struct ast_sip_endpoint *username_identify(pjsip_rx_data *rdata)
  51. {
  52. char endpoint_name[64], domain_name[64], id[AST_UUID_STR_LEN];
  53. struct ast_sip_endpoint *endpoint;
  54. RAII_VAR(struct ast_sip_domain_alias *, alias, NULL, ao2_cleanup);
  55. RAII_VAR(struct ao2_container *, transports, NULL, ao2_cleanup);
  56. RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
  57. if (get_endpoint_details(rdata, endpoint_name, sizeof(endpoint_name), domain_name, sizeof(domain_name))) {
  58. return NULL;
  59. }
  60. /* Attempt to find the endpoint given the name and domain provided */
  61. snprintf(id, sizeof(id), "%s@%s", endpoint_name, domain_name);
  62. if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
  63. goto done;
  64. }
  65. /* See if an alias exists for the domain provided */
  66. if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
  67. snprintf(id, sizeof(id), "%s@%s", endpoint_name, alias->domain);
  68. if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
  69. goto done;
  70. }
  71. }
  72. /* See if the transport this came in on has a provided domain */
  73. if ((transports = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL)) &&
  74. (transport = ao2_callback(transports, 0, find_transport_in_use, rdata)) &&
  75. !ast_strlen_zero(transport->domain)) {
  76. snprintf(id, sizeof(id), "%s@%s", endpoint_name, transport->domain);
  77. if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
  78. goto done;
  79. }
  80. }
  81. /* Fall back to no domain */
  82. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name);
  83. done:
  84. if (endpoint) {
  85. if (!(endpoint->ident_method & AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME)) {
  86. ao2_ref(endpoint, -1);
  87. return NULL;
  88. }
  89. ast_debug(3, "Retrieved endpoint %s\n", ast_sorcery_object_get_id(endpoint));
  90. } else {
  91. ast_debug(3, "Could not identify endpoint by username '%s'\n", endpoint_name);
  92. }
  93. return endpoint;
  94. }
  95. static struct ast_sip_endpoint_identifier username_identifier = {
  96. .identify_endpoint = username_identify,
  97. };
  98. static int load_module(void)
  99. {
  100. CHECK_PJSIP_MODULE_LOADED();
  101. ast_sip_register_endpoint_identifier(&username_identifier);
  102. return AST_MODULE_LOAD_SUCCESS;
  103. }
  104. static int unload_module(void)
  105. {
  106. ast_sip_unregister_endpoint_identifier(&username_identifier);
  107. return 0;
  108. }
  109. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP username endpoint identifier",
  110. .support_level = AST_MODULE_SUPPORT_CORE,
  111. .load = load_module,
  112. .unload = unload_module,
  113. .load_pri = AST_MODPRI_APP_DEPEND,
  114. );