res_pjsip_phoneprov_provider.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. ast_var_list_destroy(pp->vars);
  127. }
  128. /*! \brief Allocator for phoneprov */
  129. static void *phoneprov_alloc(const char *name)
  130. {
  131. struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy);
  132. if (!pp || !(pp->vars = ast_var_list_create())) {
  133. ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n",
  134. name);
  135. ao2_cleanup(pp);
  136. return NULL;
  137. }
  138. return pp;
  139. }
  140. /*! \brief Helper that creates an ast_var_t and inserts it into the list */
  141. static int assign_and_insert(const char *name, const char *value, struct varshead *vars)
  142. {
  143. struct ast_var_t *var;
  144. if (ast_strlen_zero(name) || !vars) {
  145. return -1;
  146. }
  147. /* Just ignore if the value is NULL or empty */
  148. if (ast_strlen_zero(value)) {
  149. return 0;
  150. }
  151. var = ast_var_assign(name, value);
  152. if (!var) {
  153. ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n");
  154. return -1;
  155. }
  156. AST_VAR_LIST_INSERT_TAIL(vars, var);
  157. return 0;
  158. }
  159. /*! \brief Adds a config name/value pair to the phoneprov object */
  160. static int aco_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  161. {
  162. struct phoneprov *pp = obj;
  163. return assign_and_insert(var->name, var->value, pp->vars);
  164. }
  165. /*! \brief Converts the phoneprov varlist to an ast_variable list */
  166. static int fields_handler(const void *obj, struct ast_variable **fields)
  167. {
  168. const struct phoneprov *pp = obj;
  169. struct ast_var_t *pvar;
  170. struct ast_variable *head = NULL;
  171. struct ast_variable *tail = NULL;
  172. struct ast_variable *var;
  173. AST_VAR_LIST_TRAVERSE(pp->vars, pvar) {
  174. var = ast_variable_new(pvar->name, pvar->value, NULL);
  175. if (!var) {
  176. ast_variables_destroy(head);
  177. return -1;
  178. }
  179. if (!head) {
  180. head = var;
  181. tail = var;
  182. continue;
  183. }
  184. tail->next = var;
  185. tail = var;
  186. }
  187. *fields = head;
  188. return 0;
  189. }
  190. static int load_endpoint(const char *id, const char *endpoint_name, struct varshead *vars,
  191. char *port_string)
  192. {
  193. struct ast_sip_auth *auth;
  194. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  195. RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
  196. const char *auth_name;
  197. *port_string = '\0';
  198. /* We need to use res_pjsip's sorcery instance instead of our own to
  199. * get endpoint and auth.
  200. */
  201. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
  202. endpoint_name);
  203. if (!endpoint) {
  204. ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id,
  205. endpoint_name);
  206. return -1;
  207. }
  208. assign_and_insert("ENDPOINT_ID", endpoint_name, vars);
  209. assign_and_insert("TRANSPORT_ID", endpoint->transport, vars);
  210. if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) {
  211. assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_CALLERID),
  212. endpoint->id.self.number.str, vars);
  213. }
  214. if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) {
  215. assign_and_insert(
  216. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_DISPLAY_NAME),
  217. endpoint->id.self.name.str, vars);
  218. }
  219. transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
  220. endpoint->transport);
  221. if (!transport) {
  222. ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name,
  223. endpoint->transport);
  224. return -1;
  225. }
  226. snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host));
  227. if (!AST_VECTOR_SIZE(&endpoint->inbound_auths)) {
  228. return 0;
  229. }
  230. auth_name = AST_VECTOR_GET(&endpoint->inbound_auths, 0);
  231. auth = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "auth", auth_name);
  232. if (!auth) {
  233. ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name);
  234. return -1;
  235. }
  236. assign_and_insert("AUTH_ID", auth_name, vars);
  237. assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME),
  238. auth->auth_user, vars);
  239. assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SECRET),
  240. auth->auth_pass, vars);
  241. ao2_ref(auth, -1);
  242. return 0;
  243. }
  244. /*! \brief Callback that loads the users from phoneprov sections */
  245. static int load_users(void)
  246. {
  247. struct phoneprov *pp;
  248. struct ao2_container *c;
  249. struct ao2_iterator i;
  250. int user_count = 0;
  251. char port_string[6];
  252. c = ast_sorcery_retrieve_by_regex(sorcery, "phoneprov", "");
  253. if (!c) {
  254. ast_log(LOG_ERROR, "Retrieve by regex failed to allocate a container.\n");
  255. return -1;
  256. }
  257. if (ao2_container_count(c) == 0) {
  258. ast_log(LOG_ERROR, "Unable to find any phoneprov users.\n");
  259. ao2_cleanup(c);
  260. return -1;
  261. }
  262. i = ao2_iterator_init(c, 0);
  263. while ((pp = ao2_iterator_next(&i))) {
  264. const char *endpoint_name;
  265. const char *id = ast_sorcery_object_get_id(pp);
  266. endpoint_name = ast_var_find(pp->vars, "endpoint");
  267. if (endpoint_name) {
  268. if (load_endpoint(id, endpoint_name, pp->vars, port_string)) {
  269. goto cleanup;
  270. }
  271. }
  272. if (!ast_var_find(pp->vars,
  273. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME))) {
  274. assign_and_insert(
  275. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME), id,
  276. pp->vars);
  277. }
  278. if (!ast_var_find(pp->vars,
  279. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL))) {
  280. assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL),
  281. id, pp->vars);
  282. }
  283. if (!ast_var_find(pp->vars,
  284. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SERVER_PORT))) {
  285. assign_and_insert("SERVER_PORT", S_OR(port_string, "5060"), pp->vars);
  286. }
  287. if (!ast_var_find(pp->vars,
  288. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
  289. ast_log(LOG_ERROR, "phoneprov %s didn't contain a PROFILE entry.\n", id);
  290. } else if (!ast_phoneprov_add_extension(AST_MODULE, pp->vars)) {
  291. user_count++;
  292. }
  293. ao2_ref(pp, -1);
  294. }
  295. cleanup:
  296. ao2_iterator_destroy(&i);
  297. ao2_cleanup(pp);
  298. ao2_cleanup(c);
  299. return user_count > 0 ? 0 : -1;
  300. }
  301. /*! \brief Callback that validates the phoneprov object */
  302. static int users_apply_handler(const struct ast_sorcery *sorcery, void *obj)
  303. {
  304. struct phoneprov *pp = obj;
  305. const char *id = ast_sorcery_object_get_id(pp);
  306. if (!ast_var_find(pp->vars,
  307. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_MAC))) {
  308. ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id);
  309. return -1;
  310. }
  311. if (!ast_var_find(pp->vars,
  312. ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
  313. ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id);
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. static int load_module(void)
  319. {
  320. CHECK_PJSIP_MODULE_LOADED();
  321. if (!(sorcery = ast_sorcery_open())) {
  322. ast_log(LOG_ERROR, "Unable to open a sorcery instance.\n");
  323. return AST_MODULE_LOAD_DECLINE;
  324. }
  325. if (ast_sorcery_apply_default(sorcery, "phoneprov", "config",
  326. "pjsip.conf,criteria=type=phoneprov")) {
  327. ast_log(LOG_ERROR, "Unable to register object phoneprov.\n");
  328. ast_sorcery_unref(sorcery);
  329. return AST_MODULE_LOAD_DECLINE;
  330. }
  331. ast_sorcery_object_register(sorcery, "phoneprov", phoneprov_alloc, NULL,
  332. users_apply_handler);
  333. ast_sorcery_object_field_register(sorcery, "phoneprov", "type", "", OPT_NOOP_T, 0, 0);
  334. ast_sorcery_object_fields_register(sorcery, "phoneprov", "^", aco_handler,
  335. fields_handler);
  336. ast_sorcery_reload_object(sorcery, "phoneprov");
  337. if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
  338. ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
  339. ast_sorcery_unref(sorcery);
  340. return AST_MODULE_LOAD_DECLINE;
  341. }
  342. return AST_MODULE_LOAD_SUCCESS;
  343. }
  344. static int unload_module(void)
  345. {
  346. ast_phoneprov_provider_unregister(AST_MODULE);
  347. ast_sorcery_unref(sorcery);
  348. return 0;
  349. }
  350. static int reload_module(void)
  351. {
  352. unload_module();
  353. load_module();
  354. return 0;
  355. }
  356. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Phoneprov Provider",
  357. .load = load_module,
  358. .reload = reload_module,
  359. .unload = unload_module,
  360. .load_pri = AST_MODPRI_APP_DEPEND,
  361. );