res_pjsip_config_wizard.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 Configuration Wizard
  21. *
  22. * \author George Joseph <george.joseph@fairview5.com>
  23. */
  24. /*! \li \ref res_pjsip_config_wizard.c uses the configuration file \ref pjsip_wizard.conf
  25. */
  26. /*!
  27. * \page pjsip_wizard.conf pjsip_wizard.conf
  28. * \verbinclude pjsip_wizard.conf.sample
  29. */
  30. /*** MODULEINFO
  31. <depend>pjproject</depend>
  32. <depend>res_pjsip</depend>
  33. <support_level>core</support_level>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include <regex.h>
  38. #include <pjsip.h>
  39. #include "asterisk/astobj2.h"
  40. #include "asterisk/cli.h"
  41. #include "asterisk/res_pjsip.h"
  42. #include "asterisk/module.h"
  43. #include "asterisk/pbx.h"
  44. #include "asterisk/sorcery.h"
  45. #include "asterisk/vector.h"
  46. /*** DOCUMENTATION
  47. <configInfo name="res_pjsip_config_wizard" language="en_US">
  48. <synopsis>Module that privides simple configuration wizard capabilities.</synopsis>
  49. <description><para>
  50. <emphasis>PJSIP Configuration Wizard</emphasis>
  51. </para>
  52. <para>This module allows creation of common PJSIP configuration scenarios
  53. without having to specify individual endpoint, aor, auth, identify and registration objects.
  54. </para>
  55. <para> </para>
  56. <para>For example, the following configuration snippet would create the
  57. endpoint, aor, contact, auth and phoneprov objects necessary for a phone to
  58. get phone provisioning information, register, and make and receive calls.
  59. A hint is also created in the default context for extension 1000.</para>
  60. <para> </para>
  61. <para>[myphone]</para>
  62. <para>type = wizard</para>
  63. <para>sends_auth = no</para>
  64. <para>accepts_auth = yes</para>
  65. <para>sends_registrations = no</para>
  66. <para>accepts_registrations = yes</para>
  67. <para>has_phoneprov = yes</para>
  68. <para>transport = ipv4</para>
  69. <para>has_hint = yes</para>
  70. <para>hint_exten = 1000</para>
  71. <para>inbound_auth/username = testname</para>
  72. <para>inbound_auth/password = test password</para>
  73. <para>endpoint/allow = ulaw</para>
  74. <para>endpoint/context = default</para>
  75. <para>phoneprov/MAC = 001122aa4455</para>
  76. <para>phoneprov/PROFILE = profile1</para>
  77. <para> </para>
  78. <para>The first 8 items are specific to the wizard. The rest of the items
  79. are passed verbatim to the underlying objects.</para>
  80. <para> </para>
  81. <para>The following configuration snippet would create the
  82. endpoint, aor, contact, auth, identify and registration objects necessary for a trunk
  83. to another pbx or ITSP that requires registration.</para>
  84. <para> </para>
  85. <para>[mytrunk]</para>
  86. <para>type = wizard</para>
  87. <para>sends_auth = yes</para>
  88. <para>accepts_auth = no</para>
  89. <para>sends_registrations = yes</para>
  90. <para>accepts_registrations = no</para>
  91. <para>transport = ipv4</para>
  92. <para>remote_hosts = sip1.myitsp.com:5060,sip2.myitsp.com:5060</para>
  93. <para>outbound_auth/username = testname</para>
  94. <para>outbound_auth/password = test password</para>
  95. <para>endpoint/allow = ulaw</para>
  96. <para>endpoint/context = default</para>
  97. <para> </para>
  98. <para>Of course, any of the items in either example could be placed into
  99. templates and shared among wizard objects.</para>
  100. <para> </para>
  101. <para>For more information, visit:</para>
  102. <para><literal>https://wiki.asterisk.org/wiki/display/AST/PJSIP+Configuration+Wizard</literal></para>
  103. </description>
  104. <configFile name="pjsip_wizard.conf">
  105. <configObject name="wizard">
  106. <synopsis>Provides config wizard.</synopsis>
  107. <description>
  108. <para>For more information, visit:</para>
  109. <para><literal>https://wiki.asterisk.org/wiki/display/AST/PJSIP+Configuration+Wizard</literal></para>
  110. </description>
  111. <configOption name="type">
  112. <synopsis>Must be 'wizard'.</synopsis>
  113. </configOption>
  114. <configOption name="transport">
  115. <synopsis>The name of a transport to use for this object.</synopsis>
  116. <description><para>If not specified,
  117. the default will be used.</para></description>
  118. </configOption>
  119. <configOption name="remote_hosts">
  120. <synopsis>List of remote hosts.</synopsis>
  121. <description><para>A comma-separated list of remote hosts in the form of
  122. <replaceable>host</replaceable>[:<replaceable>port</replaceable>].
  123. If set, an aor static contact and an identify match will be created for each
  124. entry in the list. If send_registrations is also set, a registration will
  125. also be created for each.</para></description>
  126. </configOption>
  127. <configOption name="outbound_proxy">
  128. <synopsis>Shortcut for specifying proxy on individual objects.</synopsis>
  129. <description><para>Shortcut for specifying endpoint/outbound_proxy,
  130. aor/outbound_proxy, and registration/outbound_proxy individually.
  131. </para></description>
  132. </configOption>
  133. <configOption name="sends_auth" default="no">
  134. <synopsis>Send outbound authentication to remote hosts.</synopsis>
  135. <description><para>At least outbound_auth/username is required.</para></description>
  136. </configOption>
  137. <configOption name="accepts_auth" default="no">
  138. <synopsis>Accept incoming authentication from remote hosts.</synopsis>
  139. <description><para>At least inbound_auth/username is required.</para></description>
  140. </configOption>
  141. <configOption name="sends_registrations" default="no">
  142. <synopsis>Send outbound registrations to remote hosts.</synopsis>
  143. <description><para>remote_hosts is required and a registration object will
  144. be created for each host in the remote _hosts string. If authentication is required,
  145. sends_auth and an outbound_auth/username must also be supplied.</para></description>
  146. </configOption>
  147. <configOption name="sends_line_with_registrations" default="no">
  148. <synopsis>Sets "line" and "endpoint parameters on registrations.</synopsis>
  149. <description><para>Setting this to true will cause the wizard to skip the
  150. creation of an identify object to match incoming requests to the endpoint and
  151. instead add the line and endpoint parameters to the outbound registration object.
  152. </para></description>
  153. </configOption>
  154. <configOption name="accepts_registrations" default="no">
  155. <synopsis>Accept inbound registration from remote hosts.</synopsis>
  156. <description><para>An AOR with dynamic contacts will be created. If
  157. the number of contacts nneds to be limited, set aor/max_contacts.</para></description>
  158. </configOption>
  159. <configOption name="has_phoneprov" default="no">
  160. <synopsis>Create a phoneprov object for this endpoint.</synopsis>
  161. <description><para>A phoneprov object will be created. phoneprov/MAC
  162. must be specified.</para></description>
  163. </configOption>
  164. <configOption name="server_uri_pattern" default="sip:${REMOTE_HOST}">
  165. <synopsis>A pattern to use for constructing outbound registration server_uris.</synopsis>
  166. <description><para>
  167. The literal <literal>${REMOTE_HOST}</literal> will be substituted with the
  168. appropriate remote_host for each registration.</para></description>
  169. </configOption>
  170. <configOption name="client_uri_pattern" default="sip:${USERNAME}@${REMOTE_HOST}">
  171. <synopsis>A pattern to use for constructing outbound registration client_uris.</synopsis>
  172. <description><para>
  173. The literals <literal>${REMOTE_HOST}</literal> and <literal>${USERNAME}</literal>
  174. will be substituted with the appropriate remote_host and outbound_auth/username.</para></description>
  175. </configOption>
  176. <configOption name="contact_pattern" default="sip:${REMOTE_HOST}">
  177. <synopsis>A pattern to use for constructing outbound contact uris.</synopsis>
  178. <description><para>
  179. The literal <literal>${REMOTE_HOST}</literal> will be substituted with the
  180. appropriate remote_host for each contact.</para></description>
  181. </configOption>
  182. <configOption name="has_hint" default="no">
  183. <synopsis>Create hint and optionally a default application.</synopsis>
  184. <description><para>Create hint and optionally a default application.</para></description>
  185. </configOption>
  186. <configOption name="hint_context" default="endpoint/context or 'default'">
  187. <synopsis>The context in which to place hints.</synopsis>
  188. <description>
  189. <para>Ignored if <literal>hint_exten</literal> is not specified otherwise specifies the
  190. context into which the dialplan hints will be placed. If not specified,
  191. defaults to the endpoint's context or <literal>default</literal> if that isn't
  192. found.
  193. </para></description>
  194. </configOption>
  195. <configOption name="hint_exten">
  196. <synopsis>Extension to map a PJSIP hint to.</synopsis>
  197. <description>
  198. <para>Will create the following entry in <literal>hint_context</literal>:</para>
  199. <para> <literal>exten =&gt; &lt;hint_exten&gt;,hint,PJSIP/&lt;wizard_id&gt;</literal></para>
  200. <para> </para>
  201. <para>Normal dialplan precedence rules apply so if there's already a hint for
  202. this extension in <literal>hint_context</literal>, this one will be ignored.
  203. For more information, visit: </para>
  204. <para><literal>https://wiki.asterisk.org/wiki/display/AST/PJSIP+Configuration+Wizard</literal></para>
  205. </description>
  206. </configOption>
  207. <configOption name="hint_application">
  208. <synopsis>Application to call when 'hint_exten' is dialed.</synopsis>
  209. <description>
  210. <para>Ignored if <literal>hint_exten</literal> isn't specified otherwise
  211. will create the following priority 1 extension in <literal>hint_context</literal>:</para>
  212. <para> <literal>exten =&gt; &lt;hint_exten&gt;,1,&lt;hint_application&gt;</literal></para>
  213. <para> </para>
  214. <para>You can specify any valid extensions.conf application expression.</para>
  215. <para>Examples: </para>
  216. <para> <literal>Dial(${HINT})</literal></para>
  217. <para> <literal>Gosub(stdexten,${EXTEN},1(${HINT}))</literal></para>
  218. <para> </para>
  219. <para>Any extensions.conf style variables specified are passed directly to the
  220. dialplan.</para>
  221. <para> </para>
  222. <para>Normal dialplan precedence rules apply so if there's already a priority 1
  223. application for this specific extension in <literal>hint_context</literal>,
  224. this one will be ignored. For more information, visit: </para>
  225. <para><literal>https://wiki.asterisk.org/wiki/display/AST/PJSIP+Configuration+Wizard</literal></para>
  226. </description>
  227. </configOption>
  228. <configOption name="endpoint&#47;*">
  229. <synopsis>Variables to be passed directly to the endpoint.</synopsis>
  230. </configOption>
  231. <configOption name="aor&#47;*">
  232. <synopsis>Variables to be passed directly to the aor.</synopsis>
  233. <description><para>If an aor/contact is explicitly defined then remote_hosts
  234. will not be used to create contacts automatically.</para></description>
  235. </configOption>
  236. <configOption name="inbound_auth&#47;*">
  237. <synopsis>Variables to be passed directly to the inbound auth.</synopsis>
  238. </configOption>
  239. <configOption name="outbound_auth&#47;*">
  240. <synopsis>Variables to be passed directly to the outbound auth.</synopsis>
  241. </configOption>
  242. <configOption name="identify&#47;*">
  243. <synopsis>Variables to be passed directly to the identify.</synopsis>
  244. <description><para>If an identify/match is explicitly defined then remote_hosts
  245. will not be used to create matches automatically.</para></description>
  246. </configOption>
  247. <configOption name="registration&#47;*">
  248. <synopsis>Variables to be passed directly to the outbound registrations.</synopsis>
  249. </configOption>
  250. <configOption name="phoneprov&#47;*">
  251. <synopsis>Variables to be passed directly to the phoneprov object.</synopsis>
  252. <description><para>
  253. To activate phoneprov, at least phoneprov/MAC must be set.</para></description>
  254. </configOption>
  255. </configObject>
  256. </configFile>
  257. </configInfo>
  258. ***/
  259. /*! \brief Defines the maximum number of characters that can be added to a wizard id. */
  260. #define MAX_ID_SUFFIX 20
  261. #define BASE_REGISTRAR "res_pjsip_config_wizard"
  262. /*! \brief A generic char * vector definition. */
  263. AST_VECTOR(string_vector, char *);
  264. /*! \brief Keeps track of the sorcery wizard and last config for each object type */
  265. struct object_type_wizard {
  266. struct ast_sorcery *sorcery;
  267. struct ast_sorcery_wizard *wizard;
  268. void *wizard_data;
  269. struct ast_config *last_config;
  270. char object_type[];
  271. };
  272. static AST_VECTOR_RW(object_type_wizards, struct object_type_wizard *) object_type_wizards;
  273. /*! \brief Callbacks for vector deletes */
  274. #define NOT_EQUALS(a, b) (a != b)
  275. #define OTW_DELETE_CB(otw) ({ \
  276. ast_config_destroy(otw->last_config); \
  277. ast_free(otw); \
  278. })
  279. const static char *object_types[] = {"phoneprov", "registration", "identify", "endpoint", "aor", "auth", NULL};
  280. static int is_one_of(const char *needle, const char *haystack[])
  281. {
  282. int i;
  283. for (i = 0; haystack[i]; i++) {
  284. if (!strcmp(needle, haystack[i])) {
  285. return 1;
  286. }
  287. }
  288. return 0;
  289. }
  290. /*! \brief Finds the otw for the object type */
  291. static struct object_type_wizard *find_wizard(const char *object_type)
  292. {
  293. int idx;
  294. AST_VECTOR_RW_RDLOCK(&object_type_wizards);
  295. for(idx = 0; idx < AST_VECTOR_SIZE(&object_type_wizards); idx++) {
  296. struct object_type_wizard *otw = AST_VECTOR_GET(&object_type_wizards, idx);
  297. if (!strcmp(otw->object_type, object_type)) {
  298. AST_VECTOR_RW_UNLOCK(&object_type_wizards);
  299. return otw;
  300. }
  301. }
  302. AST_VECTOR_RW_UNLOCK(&object_type_wizards);
  303. return NULL;
  304. }
  305. /*! \brief Creates a sorcery object and applies a variable list */
  306. static void *create_object(const struct ast_sorcery *sorcery,
  307. const char *id, const char *type, struct ast_variable *vars)
  308. {
  309. struct ast_sorcery_object *obj = ast_sorcery_alloc(sorcery, type, id);
  310. if (!obj) {
  311. ast_log(LOG_ERROR, "Unable to allocate an object of type '%s' with id '%s'.\n", type, id);
  312. return NULL;
  313. }
  314. if (ast_sorcery_objectset_apply(sorcery, obj, vars)) {
  315. ast_log(LOG_ERROR, "Unable to apply object type '%s' with id '%s'. Check preceeding errors.\n", type, id);
  316. ao2_ref(obj, -1);
  317. return NULL;
  318. }
  319. return obj;
  320. }
  321. /*! \brief Finds the last variable in a list and tests it */
  322. static int is_variable_true(struct ast_variable *vars, const char *name)
  323. {
  324. return ast_true(ast_variable_find_last_in_list(vars, name));
  325. }
  326. /*! \brief Appends a variable to the end of an existing list */
  327. static int variable_list_append(struct ast_variable **existing, const char *name, const char *value)
  328. {
  329. struct ast_variable *new = ast_variable_new(name, value, "");
  330. if (!new) {
  331. ast_log(LOG_ERROR, "Unable to allocate memory for new variable '%s'.\n", name);
  332. return -1;
  333. }
  334. ast_variable_list_append(existing, new);
  335. return 0;
  336. }
  337. /*! \brief Appends a variable to the end of an existing list. On failure, cause the calling
  338. * function to return -1 */
  339. #define variable_list_append_return(existing, name, value) ({ \
  340. struct ast_variable *new = ast_variable_new(name, value, ""); \
  341. if (!new) { \
  342. ast_log(LOG_ERROR, "Unable to allocate memory for new variable '%s'.\n", name); \
  343. return -1; \
  344. } \
  345. ast_variable_list_append(existing, new); \
  346. })
  347. /*! \brief We need to strip off the prefix from the name of each variable
  348. * so they're suitable for objectset_apply.
  349. * I.E. will transform outbound_auth/username to username.
  350. */
  351. static struct ast_variable *get_object_variables(struct ast_variable *vars, char *prefix)
  352. {
  353. struct ast_variable *return_vars = NULL;
  354. struct ast_variable *v = vars;
  355. int plen = strlen(prefix);
  356. for(; v; v = v->next) {
  357. if (ast_begins_with(v->name, prefix) && strlen(v->name) > plen) {
  358. if (variable_list_append(&return_vars, v->name + plen, v->value)) {
  359. ast_variables_destroy(return_vars);
  360. return NULL;
  361. }
  362. }
  363. }
  364. return return_vars;
  365. }
  366. /* Don't call while holding context locks. */
  367. static int delete_extens(const char *context, const char *exten)
  368. {
  369. struct pbx_find_info find_info = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
  370. if (pbx_find_extension(NULL, NULL, &find_info, context, exten, PRIORITY_HINT, NULL, NULL, E_MATCH)) {
  371. ast_context_remove_extension(context, exten, PRIORITY_HINT, BASE_REGISTRAR);
  372. }
  373. if (pbx_find_extension(NULL, NULL, &find_info, context, exten, 1, NULL, NULL, E_MATCH)) {
  374. ast_context_remove_extension(context, exten, 1, BASE_REGISTRAR);
  375. }
  376. return 0;
  377. }
  378. static int add_extension(struct ast_context *context, const char *exten,
  379. int priority, const char *application)
  380. {
  381. struct pbx_find_info find_info = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
  382. struct ast_exten *existing_exten;
  383. char *data = NULL;
  384. char *app = NULL;
  385. void *free_ptr = NULL;
  386. char *paren;
  387. const char *context_name;
  388. if (!context || ast_strlen_zero(exten) || ast_strlen_zero(application)) {
  389. return -1;
  390. }
  391. /* The incoming application has to be split into the app name and the
  392. * arguments (data). The app name can be any storage type as add_extension
  393. * copies it into its own buffer. Data however, needs to be dynamically
  394. * allocated and a free function provided.
  395. */
  396. paren = strchr(application, '(');
  397. if (!paren) {
  398. app = (char *)application;
  399. } else {
  400. app = ast_strdupa(application);
  401. app[paren - application] = '\0';
  402. data = ast_strdup(paren + 1);
  403. if (!data) {
  404. return -1;
  405. }
  406. data[strlen(data) - 1] = '\0';
  407. free_ptr = ast_free_ptr;
  408. if (ast_strlen_zero(app) || ast_strlen_zero(data)) {
  409. ast_free(data);
  410. return -1;
  411. }
  412. }
  413. /* Don't disturb existing, exact-match, entries. */
  414. context_name = ast_get_context_name(context);
  415. if ((existing_exten = pbx_find_extension(NULL, NULL, &find_info, context_name, exten,
  416. priority, NULL, NULL, E_MATCH))) {
  417. const char *existing_app = ast_get_extension_app(existing_exten);
  418. const char *existing_data = ast_get_extension_app_data(existing_exten);
  419. if (!strcmp(existing_app, app)
  420. && !strcmp(existing_data ? existing_data : "", data ? data : "")) {
  421. ast_free(data);
  422. return 0;
  423. }
  424. ast_context_remove_extension2(context, exten, priority, BASE_REGISTRAR, 1);
  425. }
  426. if (ast_add_extension2_nolock(context, 0, exten, priority, NULL, NULL,
  427. app, data, free_ptr, BASE_REGISTRAR)) {
  428. ast_free(data);
  429. return -1;
  430. }
  431. return 0;
  432. }
  433. static int add_hints(const char *context, const char *exten, const char *application, const char *id)
  434. {
  435. struct ast_context *hint_context;
  436. char *hint_device;
  437. hint_device = ast_alloca(strlen("PJSIP/") + strlen(id) + 1);
  438. sprintf(hint_device, "PJSIP/%s", id);
  439. /* We need the contexts list locked to safely be able to both read and lock the specific context within */
  440. if (ast_wrlock_contexts()) {
  441. ast_log(LOG_ERROR, "Failed to lock the contexts list.\n");
  442. return -1;
  443. }
  444. if (!(hint_context = ast_context_find_or_create(NULL, NULL, context, BASE_REGISTRAR))) {
  445. ast_log(LOG_ERROR, "Unable to find or create hint context '%s'\n", context);
  446. if (ast_unlock_contexts()) {
  447. ast_assert(0);
  448. }
  449. return -1;
  450. }
  451. /* Transfer the all-contexts lock to the specific context */
  452. if (ast_wrlock_context(hint_context)) {
  453. ast_unlock_contexts();
  454. ast_log(LOG_ERROR, "failed to obtain write lock on context\n");
  455. return -1;
  456. }
  457. ast_unlock_contexts();
  458. if (add_extension(hint_context, exten, PRIORITY_HINT, hint_device)) {
  459. ast_log(LOG_ERROR, "Failed to add hint '%s@%s' to the PBX.\n",
  460. exten, context);
  461. }
  462. if (!ast_strlen_zero(application)) {
  463. if (add_extension(hint_context, exten, 1, application)) {
  464. ast_log(LOG_ERROR, "Failed to add hint '%s@%s' to the PBX.\n",
  465. exten, context);
  466. }
  467. } else {
  468. ast_context_remove_extension2(hint_context, exten, 1, BASE_REGISTRAR, 1);
  469. }
  470. ast_unlock_context(hint_context);
  471. return 0;
  472. }
  473. static int handle_auth(const struct ast_sorcery *sorcery, struct object_type_wizard *otw,
  474. struct ast_category *wiz, char *direction)
  475. {
  476. struct ast_variable *wizvars = ast_category_first(wiz);
  477. struct ast_sorcery_object *obj = NULL;
  478. const char *id = ast_category_get_name(wiz);
  479. char new_id[strlen(id) + MAX_ID_SUFFIX];
  480. char prefix[strlen(direction) + strlen("_auth/") + 1];
  481. char *test_variable = NULL;
  482. RAII_VAR(struct ast_variable *, vars, NULL, ast_variables_destroy);
  483. snprintf(prefix, sizeof(prefix), "%s_auth/", direction);
  484. vars = get_object_variables(wizvars, prefix);
  485. if (!strcmp(direction, "outbound")) {
  486. snprintf(new_id, sizeof(new_id), "%s-oauth", id);
  487. test_variable = "sends_auth";
  488. } else {
  489. snprintf(new_id, sizeof(new_id), "%s-iauth", id);
  490. test_variable = "accepts_auth";
  491. }
  492. if (is_variable_true(wizvars, test_variable)) {
  493. if (!ast_variable_find_last_in_list(vars, "username")) {
  494. ast_log(LOG_ERROR,
  495. "Wizard '%s' must have '%s_auth/username' if it %s.\n", id, direction, test_variable);
  496. return -1;
  497. }
  498. } else {
  499. /* Delete auth if sends or accepts is now false. */
  500. obj = otw->wizard->retrieve_id(sorcery, otw->wizard_data, "auth", new_id);
  501. if (obj) {
  502. otw->wizard->delete(sorcery, otw->wizard_data, obj);
  503. ao2_ref(obj, -1);
  504. }
  505. return 0;
  506. }
  507. variable_list_append_return(&vars, "@pjsip_wizard", id);
  508. /* If the user set auth_type, don't override it. */
  509. if (!ast_variable_find_last_in_list(vars, "auth_type")) {
  510. variable_list_append_return(&vars, "auth_type", "userpass");
  511. }
  512. obj = create_object(sorcery, new_id, "auth", vars);
  513. if (!obj) {
  514. return -1;
  515. }
  516. if (otw->wizard->update(sorcery, otw->wizard_data, obj)) {
  517. otw->wizard->create(sorcery, otw->wizard_data, obj);
  518. }
  519. ao2_ref(obj, -1);
  520. return 0;
  521. }
  522. static int handle_auths(const struct ast_sorcery *sorcery, struct object_type_wizard *otw,
  523. struct ast_category *wiz)
  524. {
  525. int rc;
  526. if ((rc = handle_auth(sorcery, otw, wiz, "outbound"))) {
  527. return rc;
  528. }
  529. return handle_auth(sorcery, otw, wiz, "inbound");
  530. }
  531. static int handle_aor(const struct ast_sorcery *sorcery, struct object_type_wizard *otw,
  532. struct ast_category *wiz, struct string_vector *remote_hosts_vector)
  533. {
  534. struct ast_variable *wizvars = ast_category_first(wiz);
  535. struct ast_sorcery_object *obj = NULL;
  536. const char *id = ast_category_get_name(wiz);
  537. const char *contact_pattern;
  538. const char *outbound_proxy = ast_variable_find_last_in_list(wizvars, "outbound_proxy");
  539. int host_count = AST_VECTOR_SIZE(remote_hosts_vector);
  540. RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "aor/"), ast_variables_destroy);
  541. variable_list_append(&vars, "@pjsip_wizard", id);
  542. if (!ast_strlen_zero(outbound_proxy)) {
  543. variable_list_append_return(&vars, "outbound_proxy", outbound_proxy);
  544. }
  545. /* If the user explicitly specified an aor/contact, don't use remote hosts. */
  546. if (!ast_variable_find_last_in_list(vars, "contact")) {
  547. if (!(contact_pattern = ast_variable_find_last_in_list(wizvars, "contact_pattern"))) {
  548. contact_pattern = "sip:${REMOTE_HOST}";
  549. }
  550. if (host_count > 0 && !ast_strlen_zero(contact_pattern)) {
  551. int host_counter;
  552. /* ast_str_substitute_variables operate on a varshead list so we have
  553. * to create one to hold the REPORT_HOST substitution, do the substitution,
  554. * then append the result to the ast_variable list.
  555. */
  556. for (host_counter = 0; host_counter < host_count; host_counter++) {
  557. RAII_VAR(struct ast_str *, new_str, ast_str_create(64), ast_free);
  558. RAII_VAR(struct varshead *, subst_vars, ast_var_list_create(), ast_var_list_destroy);
  559. struct ast_var_t *var = ast_var_assign("REMOTE_HOST",
  560. AST_VECTOR_GET(remote_hosts_vector, host_counter));
  561. AST_VAR_LIST_INSERT_TAIL(subst_vars, var);
  562. ast_str_substitute_variables_varshead(&new_str, 0, subst_vars,
  563. contact_pattern);
  564. variable_list_append_return(&vars, "contact", ast_str_buffer(new_str));
  565. }
  566. }
  567. }
  568. obj = create_object(sorcery, id, "aor", vars);
  569. if (!obj) {
  570. return -1;
  571. }
  572. if (otw->wizard->update(sorcery, otw->wizard_data, obj)) {
  573. otw->wizard->create(sorcery, otw->wizard_data, obj);
  574. }
  575. ao2_ref(obj, -1);
  576. return 0;
  577. }
  578. static int handle_endpoint(const struct ast_sorcery *sorcery, struct object_type_wizard *otw,
  579. struct ast_category *wiz)
  580. {
  581. struct ast_variable *wizvars = ast_category_first(wiz);
  582. struct ast_sorcery_object *obj = NULL;
  583. const char *id = ast_category_get_name(wiz);
  584. const char *outbound_proxy = ast_variable_find_last_in_list(wizvars, "outbound_proxy");
  585. const char *transport = ast_variable_find_last_in_list(wizvars, "transport");
  586. const char *hint_context = hint_context = ast_variable_find_last_in_list(wizvars, "hint_context");
  587. const char *hint_exten = ast_variable_find_last_in_list(wizvars, "hint_exten");
  588. const char *hint_application= ast_variable_find_last_in_list(wizvars, "hint_application");
  589. char new_id[strlen(id) + MAX_ID_SUFFIX];
  590. RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "endpoint/"), ast_variables_destroy);
  591. variable_list_append_return(&vars, "@pjsip_wizard", id);
  592. variable_list_append_return(&vars, "aors", id);
  593. if (!ast_strlen_zero(outbound_proxy)) {
  594. variable_list_append_return(&vars, "outbound_proxy", outbound_proxy);
  595. }
  596. if (ast_strlen_zero(hint_context)) {
  597. hint_context = ast_variable_find_last_in_list(vars, "context");
  598. }
  599. if (ast_strlen_zero(hint_context)) {
  600. hint_context = "default";
  601. }
  602. if (!ast_strlen_zero(hint_exten)) {
  603. /* These are added so we can find and delete the hints when the endpoint gets deleted */
  604. variable_list_append_return(&vars, "@hint_context", hint_context);
  605. variable_list_append_return(&vars, "@hint_exten", hint_exten);
  606. }
  607. if (!ast_strlen_zero(transport)) {
  608. variable_list_append_return(&vars, "transport", transport);
  609. }
  610. if (is_variable_true(wizvars, "sends_auth")) {
  611. snprintf(new_id, sizeof(new_id), "%s-oauth", id);
  612. variable_list_append_return(&vars, "outbound_auth", new_id);
  613. }
  614. if (is_variable_true(wizvars, "accepts_auth")) {
  615. snprintf(new_id, sizeof(new_id), "%s-iauth", id);
  616. variable_list_append_return(&vars, "auth", new_id);
  617. }
  618. obj = create_object(sorcery, id, "endpoint", vars);
  619. if (!obj) {
  620. return -1;
  621. }
  622. if (otw->wizard->update(sorcery, otw->wizard_data, obj)) {
  623. otw->wizard->create(sorcery, otw->wizard_data, obj);
  624. }
  625. ao2_ref(obj, -1);
  626. if (!ast_strlen_zero(hint_exten)) {
  627. if (is_variable_true(wizvars, "has_hint")) {
  628. add_hints(hint_context, hint_exten, hint_application, id);
  629. } else {
  630. delete_extens(hint_context, hint_exten);
  631. }
  632. }
  633. return 0;
  634. }
  635. static int handle_identify(const struct ast_sorcery *sorcery, struct object_type_wizard *otw,
  636. struct ast_category *wiz, struct string_vector *remote_hosts_vector)
  637. {
  638. struct ast_variable *wizvars = ast_category_first(wiz);
  639. struct ast_sorcery_object *obj = NULL;
  640. const char *id = ast_category_get_name(wiz);
  641. char new_id[strlen(id) + MAX_ID_SUFFIX];
  642. int host_count = AST_VECTOR_SIZE(remote_hosts_vector);
  643. int host_counter;
  644. RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "identify/"), ast_variables_destroy);
  645. snprintf(new_id, sizeof(new_id), "%s-identify", id);
  646. /* If accepting registrations or we're sending line, we don't need an identify. */
  647. if (is_variable_true(wizvars, "accepts_registrations")
  648. || is_variable_true(wizvars, "sends_line_with_registrations")) {
  649. /* If one exists, delete it. */
  650. obj = otw->wizard->retrieve_id(sorcery, otw->wizard_data, "identify", new_id);
  651. if (obj) {
  652. otw->wizard->delete(sorcery, otw->wizard_data, obj);
  653. ao2_ref(obj, -1);
  654. }
  655. return 0;
  656. }
  657. if (!host_count) {
  658. ast_log(LOG_ERROR,
  659. "Wizard '%s' must have 'remote_hosts' if it doesn't accept registrations.\n", id);
  660. return -1;
  661. }
  662. variable_list_append_return(&vars, "endpoint", id);
  663. variable_list_append_return(&vars, "@pjsip_wizard", id);
  664. if (!ast_variable_find_last_in_list(vars, "match")) {
  665. for (host_counter = 0; host_counter < host_count; host_counter++) {
  666. char *rhost = AST_VECTOR_GET(remote_hosts_vector, host_counter);
  667. char host[strlen(rhost) + 1];
  668. char *colon;
  669. /* If there's a :port specified, we have to remove it. */
  670. strcpy(host, rhost); /* Safe */
  671. colon = strchr(host, ':');
  672. if (colon) {
  673. *colon = '\0';
  674. }
  675. variable_list_append_return(&vars, "match", host);
  676. }
  677. }
  678. obj = create_object(sorcery, new_id, "identify", vars);
  679. if (!obj) {
  680. return -1;
  681. }
  682. if (otw->wizard->update(sorcery, otw->wizard_data, obj)) {
  683. otw->wizard->create(sorcery, otw->wizard_data, obj);
  684. }
  685. ao2_ref(obj, -1);
  686. return 0;
  687. }
  688. static int handle_phoneprov(const struct ast_sorcery *sorcery, struct object_type_wizard *otw,
  689. struct ast_category *wiz)
  690. {
  691. struct ast_variable *wizvars = ast_category_first(wiz);
  692. struct ast_sorcery_object *obj = NULL;
  693. const char *id = ast_category_get_name(wiz);
  694. char new_id[strlen(id) + MAX_ID_SUFFIX];
  695. RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "phoneprov/"), ast_variables_destroy);
  696. snprintf(new_id, sizeof(new_id), "%s-phoneprov", id);
  697. if (!is_variable_true(wizvars, "has_phoneprov")) {
  698. obj = otw->wizard->retrieve_id(sorcery, otw->wizard_data, "phoneprov", new_id);
  699. if (obj) {
  700. otw->wizard->delete(sorcery, otw->wizard_data, obj);
  701. ao2_ref(obj, -1);
  702. }
  703. return 0;
  704. }
  705. if (!ast_variable_find_last_in_list(wizvars, "phoneprov/MAC")) {
  706. ast_log(LOG_ERROR,
  707. "Wizard '%s' must have 'phoneprov/MAC' if it has_phoneprov.\n", id);
  708. return -1;
  709. }
  710. variable_list_append_return(&vars, "endpoint", id);
  711. variable_list_append_return(&vars, "@pjsip_wizard", id);
  712. obj = create_object(sorcery, new_id, "phoneprov", vars);
  713. if (!obj) {
  714. return -1;
  715. }
  716. if (otw->wizard->update(sorcery, otw->wizard_data, obj)) {
  717. otw->wizard->create(sorcery, otw->wizard_data, obj);
  718. }
  719. ao2_ref(obj, -1);
  720. return 0;
  721. }
  722. static int delete_existing_cb(void *obj, void *arg, int flags)
  723. {
  724. struct object_type_wizard *otw = arg;
  725. if (!strcmp(otw->object_type, "endpoint")) {
  726. const char *context = ast_sorcery_object_get_extended(obj, "hint_context");
  727. const char *exten = ast_sorcery_object_get_extended(obj, "hint_exten");
  728. if (!ast_strlen_zero(context) && !ast_strlen_zero(exten)) {
  729. delete_extens(context, exten);
  730. }
  731. }
  732. otw->wizard->delete(otw->sorcery, otw->wizard_data, obj);
  733. return CMP_MATCH;
  734. }
  735. static int handle_registrations(const struct ast_sorcery *sorcery, struct object_type_wizard *otw,
  736. struct ast_category *wiz, struct string_vector *remote_hosts_vector)
  737. {
  738. struct ast_variable *search;
  739. struct ast_variable *wizvars = ast_category_first(wiz);
  740. const char *id = ast_category_get_name(wiz);
  741. const char *server_uri_pattern;
  742. const char *client_uri_pattern;
  743. const char *outbound_proxy = ast_variable_find_last_in_list(wizvars, "outbound_proxy");
  744. const char *transport = ast_variable_find_last_in_list(wizvars, "transport");
  745. const char *username;
  746. char new_id[strlen(id) + MAX_ID_SUFFIX];
  747. int host_count = AST_VECTOR_SIZE(remote_hosts_vector);
  748. int host_counter;
  749. RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "registration/"), ast_variables_destroy);
  750. RAII_VAR(struct ao2_container *, existing,
  751. ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL), ao2_cleanup);
  752. if (!existing) {
  753. return -1;
  754. }
  755. /* Find any existing registrations. */
  756. search = ast_variable_new("@pjsip_wizard", id, "");
  757. if (!search) {
  758. return -1;
  759. }
  760. if (!ast_strlen_zero(outbound_proxy)) {
  761. variable_list_append_return(&vars, "outbound_proxy", outbound_proxy);
  762. }
  763. otw->wizard->retrieve_multiple(sorcery, otw->wizard_data, "registration", existing, search);
  764. ast_variables_destroy(search);
  765. /* If not sending registrations, delete ALL existing registrations for this wizard. */
  766. if (!is_variable_true(wizvars, "sends_registrations")) {
  767. if (ao2_container_count(existing) > 0) {
  768. ao2_callback(existing, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, delete_existing_cb, otw);
  769. }
  770. return 0;
  771. }
  772. if (!host_count) {
  773. ast_log(LOG_ERROR, "Wizard '%s' must have 'remote_hosts' if it sends registrations.\n", id);
  774. return -1;
  775. }
  776. variable_list_append_return(&vars, "@pjsip_wizard", id);
  777. if (!(server_uri_pattern = ast_variable_find_last_in_list(wizvars, "server_uri_pattern"))) {
  778. server_uri_pattern = "sip:${REMOTE_HOST}";
  779. }
  780. if (!(client_uri_pattern = ast_variable_find_last_in_list(wizvars, "client_uri_pattern"))) {
  781. client_uri_pattern = "sip:${USERNAME}@${REMOTE_HOST}";
  782. }
  783. if(is_variable_true(wizvars, "sends_auth")) {
  784. username = ast_variable_find_last_in_list(wizvars, "outbound_auth/username");
  785. } else {
  786. username = id;
  787. }
  788. /* Unlike aor and identify, we need to create a separate registration object
  789. * for each remote host.
  790. */
  791. for (host_counter = 0; host_counter < host_count; host_counter++) {
  792. struct ast_var_t *rh = ast_var_assign("REMOTE_HOST",
  793. AST_VECTOR_GET(remote_hosts_vector, host_counter));
  794. struct ast_var_t *un = ast_var_assign("USERNAME", username);
  795. struct ast_sorcery_object *obj;
  796. RAII_VAR(struct ast_str *, uri, ast_str_create(64), ast_free);
  797. RAII_VAR(struct varshead *, subst_vars, ast_var_list_create(), ast_var_list_destroy);
  798. RAII_VAR(struct ast_variable *, registration_vars, vars ? ast_variables_dup(vars) : NULL, ast_variables_destroy);
  799. AST_VAR_LIST_INSERT_TAIL(subst_vars, rh);
  800. AST_VAR_LIST_INSERT_TAIL(subst_vars, un);
  801. if (!ast_strlen_zero(server_uri_pattern)) {
  802. ast_str_substitute_variables_varshead(&uri, 0, subst_vars,
  803. server_uri_pattern);
  804. variable_list_append_return(&registration_vars, "server_uri", ast_str_buffer(uri));
  805. }
  806. if (!ast_strlen_zero(client_uri_pattern)) {
  807. ast_str_reset(uri);
  808. ast_str_substitute_variables_varshead(&uri, 0, subst_vars,
  809. client_uri_pattern);
  810. variable_list_append_return(&registration_vars, "client_uri", ast_str_buffer(uri));
  811. }
  812. if (is_variable_true(wizvars, "sends_auth")) {
  813. snprintf(new_id, sizeof(new_id), "%s-oauth", id);
  814. variable_list_append_return(&registration_vars, "outbound_auth", new_id);
  815. }
  816. if (!ast_strlen_zero(transport)) {
  817. variable_list_append_return(&registration_vars, "transport", transport);
  818. }
  819. if (is_variable_true(wizvars, "sends_line_with_registrations")) {
  820. variable_list_append_return(&registration_vars, "line", "yes");
  821. variable_list_append_return(&registration_vars, "endpoint", id);
  822. }
  823. snprintf(new_id, sizeof(new_id), "%s-reg-%d", id, host_counter);
  824. obj = create_object(sorcery, new_id, "registration", registration_vars);
  825. if (!obj) {
  826. return -1;
  827. }
  828. if (otw->wizard->update(sorcery, otw->wizard_data, obj)) {
  829. otw->wizard->create(sorcery, otw->wizard_data, obj);
  830. }
  831. ao2_ref(obj, -1);
  832. /* Unlink it from the 'existing' container. Any left will be deleted from
  833. * sorcery. If it wasn't in the existing container, no harm.
  834. */
  835. ao2_callback(existing, OBJ_NODATA | OBJ_UNLINK | OBJ_SEARCH_KEY, ast_sorcery_object_id_compare, new_id);
  836. }
  837. /* If there are any excess registrations, delete them. */
  838. if (ao2_container_count(existing) > 0) {
  839. ao2_callback(existing, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, delete_existing_cb, otw);
  840. }
  841. return 0;
  842. }
  843. static int wizard_apply_handler(const struct ast_sorcery *sorcery, struct object_type_wizard *otw,
  844. struct ast_category *wiz)
  845. {
  846. struct ast_variable *wizvars = ast_category_first(wiz);
  847. struct string_vector remote_hosts_vector;
  848. const char *remote_hosts;
  849. int rc = -1;
  850. AST_VECTOR_INIT(&remote_hosts_vector, 16);
  851. remote_hosts = ast_variable_find_last_in_list(wizvars, "remote_hosts");
  852. if (!ast_strlen_zero(remote_hosts)) {
  853. char *host;
  854. char *hosts = ast_strdupa(remote_hosts);
  855. while ((host = ast_strsep(&hosts, ',', AST_STRSEP_TRIM))) {
  856. host = ast_strdup(host);
  857. if (host && AST_VECTOR_APPEND(&remote_hosts_vector, host)) {
  858. ast_free(host);
  859. }
  860. }
  861. }
  862. ast_debug(4, "%s handler starting.\n", otw->object_type);
  863. if (!strcmp(otw->object_type, "auth")) {
  864. rc = handle_auths(sorcery, otw, wiz);
  865. } else if (!strcmp(otw->object_type, "aor")) {
  866. rc = handle_aor(sorcery, otw, wiz, &remote_hosts_vector);
  867. } else if (!strcmp(otw->object_type, "endpoint")) {
  868. rc = handle_endpoint(sorcery, otw, wiz);
  869. } else if (!strcmp(otw->object_type, "identify")) {
  870. rc = handle_identify(sorcery, otw, wiz, &remote_hosts_vector);
  871. } else if (!strcmp(otw->object_type, "phoneprov")) {
  872. rc = handle_phoneprov(sorcery, otw, wiz);
  873. } else if (!strcmp(otw->object_type, "registration")) {
  874. rc = handle_registrations(sorcery, otw, wiz, &remote_hosts_vector);
  875. }
  876. AST_VECTOR_REMOVE_ALL_CMP_UNORDERED(&remote_hosts_vector, NULL, NOT_EQUALS, ast_free);
  877. AST_VECTOR_FREE(&remote_hosts_vector);
  878. ast_debug(4, "%s handler complete. rc: %d\n", otw->object_type, rc);
  879. return rc;
  880. }
  881. /*
  882. * Everything below are the sorcery observers.
  883. */
  884. static void instance_created_observer(const char *name, struct ast_sorcery *sorcery);
  885. static void instance_destroying_observer(const char *name, struct ast_sorcery *sorcery);
  886. static void object_type_loaded_observer(const char *name,
  887. const struct ast_sorcery *sorcery, const char *object_type, int reloaded);
  888. static void wizard_mapped_observer(const char *name, struct ast_sorcery *sorcery,
  889. const char *object_type, struct ast_sorcery_wizard *wizard,
  890. const char *wizard_args, void *wizard_data);
  891. static void object_type_registered_observer(const char *name,
  892. struct ast_sorcery *sorcery, const char *object_type);
  893. const static struct ast_sorcery_global_observer global_observer = {
  894. .instance_created = instance_created_observer,
  895. .instance_destroying = instance_destroying_observer,
  896. };
  897. struct ast_sorcery_instance_observer observer = {
  898. .wizard_mapped = wizard_mapped_observer,
  899. .object_type_registered = object_type_registered_observer,
  900. .object_type_loaded = object_type_loaded_observer,
  901. };
  902. /*! \brief Called after an object type is loaded/reloaded */
  903. static void object_type_loaded_observer(const char *name,
  904. const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
  905. {
  906. struct ast_category *category = NULL;
  907. struct object_type_wizard *otw = NULL;
  908. char *filename = "pjsip_wizard.conf";
  909. struct ast_flags flags = { 0 };
  910. struct ast_config *cfg;
  911. if (!strstr("auth aor endpoint identify registration phoneprov", object_type)) {
  912. /* Not interested. */
  913. return;
  914. }
  915. otw = find_wizard(object_type);
  916. if (!otw) {
  917. ast_log(LOG_ERROR, "There was no wizard for object type '%s'\n", object_type);
  918. return;
  919. }
  920. if (reloaded && otw->last_config) {
  921. flags.flags = CONFIG_FLAG_FILEUNCHANGED;
  922. }
  923. cfg = ast_config_load2(filename, object_type, flags);
  924. if (!cfg) {
  925. ast_log(LOG_ERROR, "Unable to load config file '%s'\n", filename);
  926. return;
  927. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  928. ast_debug(2, "Config file '%s' was unchanged for '%s'.\n", filename, object_type);
  929. return;
  930. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  931. ast_log(LOG_ERROR, "Contents of config file '%s' are invalid and cannot be parsed\n", filename);
  932. return;
  933. }
  934. while ((category = ast_category_browse_filtered(cfg, NULL, category, "type=^wizard$"))) {
  935. const char *id = ast_category_get_name(category);
  936. struct ast_category *last_cat = NULL;
  937. struct ast_variable *change_set = NULL;
  938. if (otw->last_config) {
  939. last_cat = ast_category_get(otw->last_config, id, "type=^wizard$");
  940. ast_sorcery_changeset_create(ast_category_first(category), ast_category_first(last_cat), &change_set);
  941. if (last_cat) {
  942. ast_category_delete(otw->last_config, last_cat);
  943. }
  944. }
  945. if (!last_cat || change_set) {
  946. ast_variables_destroy(change_set);
  947. ast_debug(3, "%s: %s(s) for wizard '%s'\n", reloaded ? "Reload" : "Load", object_type, id);
  948. if (wizard_apply_handler(sorcery, otw, category)) {
  949. ast_log(LOG_ERROR, "Unable to create objects for wizard '%s'\n", id);
  950. }
  951. }
  952. }
  953. if (!otw->last_config) {
  954. otw->last_config = cfg;
  955. return;
  956. }
  957. /* Only wizards that weren't in the new config are left in last_config now so we need to delete
  958. * all objects belonging to them.
  959. */
  960. category = NULL;
  961. while ((category = ast_category_browse_filtered(otw->last_config, NULL, category, "type=^wizard$"))) {
  962. const char *id = ast_category_get_name(category);
  963. struct ast_variable *search;
  964. RAII_VAR(struct ao2_container *, existing,
  965. ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL), ao2_cleanup);
  966. if (!existing) {
  967. ast_log(LOG_ERROR, "Unable to allocate temporary container.\n");
  968. break;
  969. }
  970. search = ast_variable_new("@pjsip_wizard", id, "");
  971. if (!search) {
  972. ast_log(LOG_ERROR, "Unable to allocate memory for vaiable '@pjsip_wizard'.\n");
  973. break;
  974. }
  975. otw->wizard->retrieve_multiple(sorcery, otw->wizard_data, object_type, existing, search);
  976. ast_variables_destroy(search);
  977. if (ao2_container_count(existing) > 0) {
  978. ast_debug(3, "Delete on %s: %d %s(s) for wizard: %s\n",
  979. reloaded ? "Reload" : "Load", ao2_container_count(existing), object_type, id);
  980. ao2_callback(existing, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE,
  981. delete_existing_cb, otw);
  982. }
  983. }
  984. ast_config_destroy(otw->last_config);
  985. otw->last_config = cfg;
  986. }
  987. /*! \brief When each wizard is mapped, save it off to the vector. */
  988. static void wizard_mapped_observer(const char *name, struct ast_sorcery *sorcery,
  989. const char *object_type, struct ast_sorcery_wizard *wizard,
  990. const char *wizard_args, void *wizard_data)
  991. {
  992. struct object_type_wizard *otw;
  993. if (!is_one_of(object_type, object_types)) {
  994. /* Not interested. */
  995. return;
  996. }
  997. /* We're only interested in memory wizards with the pjsip_wizard tag. */
  998. if (wizard_args && !strcmp(wizard_args, "pjsip_wizard")) {
  999. otw = ast_malloc(sizeof(*otw) + strlen(object_type) + 1);
  1000. if (!otw) {
  1001. return;
  1002. }
  1003. otw->sorcery = sorcery;
  1004. otw->wizard = wizard;
  1005. otw->wizard_data = wizard_data;
  1006. otw->last_config = NULL;
  1007. strcpy(otw->object_type, object_type); /* Safe */
  1008. AST_VECTOR_RW_WRLOCK(&object_type_wizards);
  1009. if (AST_VECTOR_APPEND(&object_type_wizards, otw)) {
  1010. ast_free(otw);
  1011. } else {
  1012. ast_debug(1, "Wizard mapped for object_type '%s'\n", object_type);
  1013. }
  1014. AST_VECTOR_RW_UNLOCK(&object_type_wizards);
  1015. }
  1016. }
  1017. /*! \brief When each object type is registered, map a memory wizard to it. */
  1018. static void object_type_registered_observer(const char *name,
  1019. struct ast_sorcery *sorcery, const char *object_type)
  1020. {
  1021. if (is_one_of(object_type, object_types)) {
  1022. ast_sorcery_apply_wizard_mapping(sorcery, object_type, "memory", "pjsip_wizard", 0);
  1023. }
  1024. }
  1025. /*! \brief When the res_pjsip instance is created, add an observer to it and initialize the wizard vector.
  1026. * Also, bump the module's ref count so it can't be unloaded before the sorcery instance is
  1027. * destroyed.
  1028. */
  1029. static void instance_created_observer(const char *name, struct ast_sorcery *sorcery)
  1030. {
  1031. if (strcmp(name, "res_pjsip")) {
  1032. return;
  1033. }
  1034. ast_module_ref(ast_module_info->self);
  1035. ast_sorcery_instance_observer_add(sorcery, &observer);
  1036. }
  1037. /*! \brief When the res_pjsip instance is destroyed, remove the observer
  1038. * and unref the module. This should then allow this module to unload cleanly.
  1039. */
  1040. static void instance_destroying_observer(const char *name, struct ast_sorcery *sorcery)
  1041. {
  1042. if (strcmp(name, "res_pjsip")) {
  1043. return;
  1044. }
  1045. ast_sorcery_instance_observer_remove(sorcery, &observer);
  1046. ast_module_unref(ast_module_info->self);
  1047. }
  1048. static char *handle_export_primitives(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1049. {
  1050. struct ast_sorcery *sorcery;
  1051. int idx;
  1052. FILE *f = NULL;
  1053. const char *fn = NULL;
  1054. switch (cmd) {
  1055. case CLI_INIT:
  1056. e->command = "pjsip export config_wizard primitives [to]";
  1057. e->usage =
  1058. "Usage: pjsip export config_wizard primitives [ to <filename ]\n"
  1059. " Export the config_wizard objects as pjsip primitives to\n"
  1060. " the console or to <filename>\n";
  1061. return NULL;
  1062. case CLI_GENERATE:
  1063. return NULL;
  1064. }
  1065. if (a->argc > 5) {
  1066. char date[256]="";
  1067. time_t t;
  1068. fn = a->argv[5];
  1069. time(&t);
  1070. ast_copy_string(date, ctime(&t), sizeof(date));
  1071. f = fopen(fn, "w");
  1072. if (!f) {
  1073. ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
  1074. return CLI_FAILURE;
  1075. }
  1076. fprintf(f, ";!\n");
  1077. fprintf(f, ";! Automatically generated configuration file\n");
  1078. fprintf(f, ";! Filename: %s\n", fn);
  1079. fprintf(f, ";! Generator: %s\n", "'pjsip export config_wizard primitives'");
  1080. fprintf(f, ";! Creation Date: %s", date);
  1081. fprintf(f, ";!\n");
  1082. }
  1083. sorcery = ast_sip_get_sorcery();
  1084. AST_VECTOR_RW_RDLOCK(&object_type_wizards);
  1085. for(idx = 0; idx < AST_VECTOR_SIZE(&object_type_wizards); idx++) {
  1086. struct object_type_wizard *otw = AST_VECTOR_GET(&object_type_wizards, idx);
  1087. struct ao2_container *container;
  1088. struct ao2_iterator i;
  1089. void *o;
  1090. container = ast_sorcery_retrieve_by_fields(sorcery, otw->object_type, AST_RETRIEVE_FLAG_MULTIPLE, NULL);
  1091. if (!container) {
  1092. continue;
  1093. }
  1094. i = ao2_iterator_init(container, 0);
  1095. while ((o = ao2_iterator_next(&i))) {
  1096. struct ast_variable *vars;
  1097. struct ast_variable *v;
  1098. vars = ast_sorcery_objectset_create(sorcery, o);
  1099. if (vars && ast_variable_find_in_list(vars, "@pjsip_wizard")) {
  1100. if (f) {
  1101. fprintf(f, "\n[%s]\ntype = %s\n", ast_sorcery_object_get_id(o), otw->object_type);
  1102. } else {
  1103. ast_cli(a->fd, "\n[%s]\ntype = %s\n", ast_sorcery_object_get_id(o), otw->object_type);
  1104. }
  1105. for (v = vars; v; v = v->next) {
  1106. if (!ast_strlen_zero(v->value)) {
  1107. if (f) {
  1108. fprintf(f, "%s = %s\n", v->name, v->value);
  1109. } else {
  1110. ast_cli(a->fd, "%s = %s\n", v->name, v->value);
  1111. }
  1112. }
  1113. }
  1114. }
  1115. ast_variables_destroy(vars);
  1116. ao2_ref(o, -1);
  1117. }
  1118. ao2_iterator_destroy(&i);
  1119. ao2_cleanup(container);
  1120. }
  1121. AST_VECTOR_RW_UNLOCK(&object_type_wizards);
  1122. if (f) {
  1123. fclose(f);
  1124. ast_cli(a->fd, "Wrote configuration to %s\n", fn);
  1125. }
  1126. return CLI_SUCCESS;
  1127. }
  1128. static struct ast_cli_entry config_wizard_cli[] = {
  1129. AST_CLI_DEFINE(handle_export_primitives, "Export config wizard primitives"),
  1130. };
  1131. static int load_module(void)
  1132. {
  1133. AST_VECTOR_RW_INIT(&object_type_wizards, 12);
  1134. ast_sorcery_global_observer_add(&global_observer);
  1135. ast_cli_register_multiple(config_wizard_cli, ARRAY_LEN(config_wizard_cli));
  1136. return AST_MODULE_LOAD_SUCCESS;
  1137. }
  1138. static int unload_module(void)
  1139. {
  1140. ast_cli_unregister_multiple(config_wizard_cli, ARRAY_LEN(config_wizard_cli));
  1141. ast_sorcery_global_observer_remove(&global_observer);
  1142. AST_VECTOR_REMOVE_ALL_CMP_UNORDERED(&object_type_wizards, NULL, NOT_EQUALS, OTW_DELETE_CB);
  1143. AST_VECTOR_RW_FREE(&object_type_wizards);
  1144. return 0;
  1145. }
  1146. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP Config Wizard",
  1147. .support_level = AST_MODULE_SUPPORT_CORE,
  1148. .load = load_module,
  1149. .unload = unload_module,
  1150. .load_pri = AST_MODPRI_REALTIME_DRIVER,
  1151. );