res_pjsip_phoneprov_provider.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Fairview 5 Engineering, LLC
  5. *
  6. * George Joseph <george.joseph@fairview5.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. /*! \file
  19. *
  20. * \brief PJSIP Phoneprov Configuration Provider
  21. *
  22. * \author George Joseph <george.joseph@fairview5.com>
  23. */
  24. /*! \li \ref res_pjsip_phoneprov_provider.c uses the configuration file \ref pjsip.conf
  25. * \addtogroup configuration_file Configuration Files
  26. */
  27. /*!
  28. * \page pjsip.conf pjsip.conf
  29. * \verbinclude pjsip.conf.sample
  30. */
  31. /*** MODULEINFO
  32. <depend>pjproject</depend>
  33. <depend>res_pjsip</depend>
  34. <depend>res_phoneprov</depend>
  35. <support_level>extended</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. #include <pjsip.h>
  39. #include "asterisk/res_pjsip.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/sorcery.h"
  42. #include "asterisk/phoneprov.h"
  43. #include "res_pjsip/include/res_pjsip_private.h"
  44. /*** DOCUMENTATION
  45. <configInfo name="res_pjsip_phoneprov_provider" language="en_US">
  46. <synopsis>Module that integrates res_pjsip with res_phoneprov.</synopsis>
  47. <description><para>
  48. <emphasis>PJSIP Phoneprov Provider</emphasis>
  49. </para>
  50. <para>This module creates the integration between <literal>res_pjsip</literal> and
  51. <literal>res_phoneprov</literal>.
  52. </para>
  53. <para>Each user to be integrated requires a <literal>phoneprov</literal>
  54. section defined in <filename>pjsip.conf</filename>. Each section identifies
  55. the endpoint associated with the user and any other name/value pairs to be passed
  56. on to res_phoneprov's template substitution. Only <literal>MAC</literal> and
  57. <literal>PROFILE</literal> variables are required. Any other variables
  58. supplied will be passed through.</para>
  59. <para> </para>
  60. <para>Example:</para>
  61. <para>[1000]</para>
  62. <para>type = phoneprovr</para>
  63. <para>endpoint = ep1000</para>
  64. <para>MAC = deadbeef4dad</para>
  65. <para>PROFILE = grandstream2</para>
  66. <para>LINEKEYS = 2</para>
  67. <para>LINE = 1</para>
  68. <para>OTHERVAR = othervalue</para>
  69. <para> </para>
  70. <para>The following variables are automatically defined if an endpoint
  71. is defined for the user:</para>
  72. <enumlist>
  73. <enum name="USERNAME"><para>Source: The user_name defined in the first auth reference
  74. in the endpoint.</para></enum>
  75. <enum name="SECRET"><para>Source: The user_pass defined in the first auth reference
  76. in the endpoint.</para></enum>
  77. <enum name="CALLERID"><para>Source: The number part of the callerid defined in
  78. the endpoint.</para></enum>
  79. <enum name="DISPLAY_NAME"><para>Source: The name part of the callerid defined in
  80. the endpoint.</para></enum>
  81. <enum name="LABEL"><para>Source: The id of the phoneprov section.</para></enum>
  82. </enumlist>
  83. <para> </para>
  84. <para>In addition to the standard variables, the following are also automatically defined:</para>
  85. <enumlist>
  86. <enum name="ENDPOINT_ID"><para>Source: The id of the endpoint.</para></enum>
  87. <enum name="TRANSPORT_ID"><para>Source: The id of the transport used by the endpoint.</para></enum>
  88. <enum name="AUTH_ID"><para>Source: The id of the auth used by the endpoint.</para></enum>
  89. </enumlist>
  90. <para> </para>
  91. <para>All other template substitution variables must be explicitly defined in the
  92. phoneprov_default or phoneprov sections.</para>
  93. </description>
  94. <configFile name="pjsip.conf">
  95. <configObject name="phoneprov">
  96. <synopsis>Provides variables for each user.</synopsis>
  97. <configOption name="type">
  98. <synopsis>Must be of type 'phoneprov'.</synopsis>
  99. </configOption>
  100. <configOption name="endpoint">
  101. <synopsis>The endpoint from which variables will be retrieved.</synopsis>
  102. </configOption>
  103. <configOption name="MAC">
  104. <synopsis>The mac address for this user. (required)</synopsis>
  105. </configOption>
  106. <configOption name="PROFILE">
  107. <synopsis>The phoneprov profile to use for this user. (required)</synopsis>
  108. </configOption>
  109. <configOption name="*">
  110. <synopsis>Other name/value pairs to be passed through for use in templates.</synopsis>
  111. </configOption>
  112. </configObject>
  113. </configFile>
  114. </configInfo>
  115. ***/
  116. static struct ast_sorcery *sorcery;
  117. /*! \brief Structure for a phoneprov object */
  118. struct phoneprov {
  119. SORCERY_OBJECT(details);
  120. struct varshead *vars;
  121. };
  122. /*! \brief Destructor function for phoneprov */
  123. static void phoneprov_destroy(void *obj)
  124. {
  125. struct phoneprov *pp = obj;
  126. char *mac = ast_var_find(pp->vars, "MAC");
  127. if (mac) {
  128. ast_phoneprov_delete_extension(AST_MODULE, mac);
  129. }
  130. ast_var_list_destroy(pp->vars);
  131. }
  132. /*! \brief Allocator for phoneprov */
  133. static void *phoneprov_alloc(const char *name)
  134. {
  135. struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy);
  136. if (!pp || !(pp->vars = ast_var_list_create())) {
  137. ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n",
  138. name);
  139. ao2_cleanup(pp);
  140. return NULL;
  141. }
  142. return pp;
  143. }
  144. /*! \brief Helper that creates an ast_var_t and inserts it into the list */
  145. static int assign_and_insert(const char *name, const char *value, struct varshead *vars)
  146. {
  147. struct ast_var_t *var;
  148. if (ast_strlen_zero(name) || !vars) {
  149. return -1;
  150. }
  151. /* Just ignore if the value is NULL or empty */
  152. if (ast_strlen_zero(value)) {
  153. return 0;
  154. }
  155. var = ast_var_assign(name, value);
  156. if (!var) {
  157. ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n");
  158. return -1;
  159. }
  160. AST_VAR_LIST_INSERT_TAIL(vars, var);
  161. return 0;
  162. }
  163. /*! \brief Adds a config name/value pair to the phoneprov object */
  164. static int aco_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  165. {
  166. struct phoneprov *pp = obj;
  167. return assign_and_insert(var->name, var->value, pp->vars);
  168. }
  169. /*! \brief Converts the phoneprov varlist to an ast_variable list */
  170. static int fields_handler(const void *obj, struct ast_variable **fields)
  171. {
  172. const struct phoneprov *pp = obj;
  173. struct ast_var_t *pvar;
  174. struct ast_variable *head = NULL;
  175. struct ast_variable *tail = NULL;
  176. struct ast_variable *var;
  177. AST_VAR_LIST_TRAVERSE(pp->vars, pvar) {
  178. var = ast_variable_new(pvar->name, pvar->value, "");
  179. if (!var) {
  180. ast_variables_destroy(head);
  181. return -1;
  182. }
  183. if (!head) {
  184. head = var;
  185. tail = var;
  186. continue;
  187. }
  188. tail->next = var;
  189. tail = var;
  190. }
  191. *fields = head;
  192. return 0;
  193. }
  194. static int load_endpoint(const char *id, const char *endpoint_name, struct varshead *vars,
  195. char *port_string)
  196. {
  197. struct ast_sip_auth *auth;
  198. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  199. RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
  200. const char *auth_name;
  201. *port_string = '\0';
  202. /* We need to use res_pjsip's sorcery instance instead of our own to
  203. * get endpoint and auth.
  204. */
  205. endpoint = ast_sorcery_retrieve_by_id(sorcery, "endpoint",
  206. endpoint_name);
  207. if (!endpoint) {
  208. ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id,
  209. endpoint_name);
  210. return -1;
  211. }
  212. assign_and_insert("ENDPOINT_ID", endpoint_name, vars);
  213. assign_and_insert("TRANSPORT_ID", endpoint->transport, vars);
  214. if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) {
  215. assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_CALLERID),
  216. endpoint->id.self.number.str, vars);
  217. }
  218. if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) {
  219. assign_and_insert(
  220. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_DISPLAY_NAME),
  221. endpoint->id.self.name.str, vars);
  222. }
  223. transport = ast_sorcery_retrieve_by_id(sorcery, "transport",
  224. endpoint->transport);
  225. if (!transport) {
  226. ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name,
  227. endpoint->transport);
  228. return -1;
  229. }
  230. snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host));
  231. if (!AST_VECTOR_SIZE(&endpoint->inbound_auths)) {
  232. return 0;
  233. }
  234. auth_name = AST_VECTOR_GET(&endpoint->inbound_auths, 0);
  235. auth = ast_sorcery_retrieve_by_id(sorcery, "auth", auth_name);
  236. if (!auth) {
  237. ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name);
  238. return -1;
  239. }
  240. assign_and_insert("AUTH_ID", auth_name, vars);
  241. assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME),
  242. auth->auth_user, vars);
  243. assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SECRET),
  244. auth->auth_pass, vars);
  245. ao2_ref(auth, -1);
  246. return 0;
  247. }
  248. /*! \brief Callback that validates the phoneprov object */
  249. static void users_apply_handler(struct phoneprov *pp)
  250. {
  251. const char *id = ast_sorcery_object_get_id(pp);
  252. const char *endpoint_name;
  253. char port_string[6];
  254. if (!ast_var_find(pp->vars,
  255. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_MAC))) {
  256. ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id);
  257. return;
  258. }
  259. if (!ast_var_find(pp->vars,
  260. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
  261. ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id);
  262. return;
  263. }
  264. endpoint_name = ast_var_find(pp->vars, "endpoint");
  265. if (endpoint_name) {
  266. if (load_endpoint(id, endpoint_name, pp->vars, port_string)) {
  267. return;
  268. }
  269. }
  270. if (!ast_var_find(pp->vars,
  271. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME))) {
  272. assign_and_insert(
  273. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME), id,
  274. pp->vars);
  275. }
  276. if (!ast_var_find(pp->vars,
  277. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL))) {
  278. assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL),
  279. id, pp->vars);
  280. }
  281. if (!ast_var_find(pp->vars,
  282. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SERVER_PORT))) {
  283. assign_and_insert("SERVER_PORT", S_OR(port_string, "5060"), pp->vars);
  284. }
  285. if (!ast_var_find(pp->vars,
  286. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
  287. ast_log(LOG_ERROR, "phoneprov %s didn't contain a PROFILE entry.\n", id);
  288. }
  289. ast_phoneprov_add_extension(AST_MODULE, pp->vars);
  290. return;
  291. }
  292. /*! \brief Callback that loads the users from phoneprov sections */
  293. static int load_users(void)
  294. {
  295. struct ao2_container *users;
  296. struct ao2_iterator i;
  297. struct phoneprov *pp;
  298. ast_sorcery_reload_object(sorcery, "phoneprov");
  299. users = ast_sorcery_retrieve_by_fields(sorcery, "phoneprov",
  300. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  301. if (!users) {
  302. return 0;
  303. }
  304. i = ao2_iterator_init(users, 0);
  305. while ((pp = ao2_iterator_next(&i))) {
  306. users_apply_handler(pp);
  307. ao2_ref(pp, -1);
  308. }
  309. ao2_iterator_destroy(&i);
  310. ao2_ref(users, -1);
  311. return 0;
  312. }
  313. static int load_module(void)
  314. {
  315. CHECK_PJSIP_MODULE_LOADED();
  316. sorcery = ast_sip_get_sorcery();
  317. ast_sorcery_apply_config(sorcery, "res_pjsip_phoneprov_provider");
  318. ast_sorcery_apply_default(sorcery, "phoneprov", "config",
  319. "pjsip.conf,criteria=type=phoneprov");
  320. ast_sorcery_object_register(sorcery, "phoneprov", phoneprov_alloc, NULL,
  321. NULL);
  322. ast_sorcery_object_field_register(sorcery, "phoneprov", "type", "", OPT_NOOP_T, 0,
  323. 0);
  324. ast_sorcery_object_fields_register(sorcery, "phoneprov", "^", aco_handler,
  325. fields_handler);
  326. ast_sorcery_load_object(sorcery, "phoneprov");
  327. if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
  328. ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
  329. return AST_MODULE_LOAD_DECLINE;
  330. }
  331. return AST_MODULE_LOAD_SUCCESS;
  332. }
  333. static int unload_module(void)
  334. {
  335. ast_phoneprov_provider_unregister(AST_MODULE);
  336. return 0;
  337. }
  338. static int reload_module(void)
  339. {
  340. ast_phoneprov_provider_unregister(AST_MODULE);
  341. if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
  342. ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
  343. return AST_MODULE_LOAD_DECLINE;
  344. }
  345. return 0;
  346. }
  347. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Phoneprov Provider",
  348. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  349. .load = load_module,
  350. .reload = reload_module,
  351. .unload = unload_module,
  352. .load_pri = AST_MODPRI_APP_DEPEND,
  353. );