config_options.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@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. /*! \file
  19. * \brief Configuration Option-handling
  20. * \author Terry Wilson <twilson@digium.com>
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include <regex.h>
  28. #include "asterisk/_private.h"
  29. #include "asterisk/config.h"
  30. #include "asterisk/config_options.h"
  31. #include "asterisk/stringfields.h"
  32. #include "asterisk/acl.h"
  33. #include "asterisk/frame.h"
  34. #include "asterisk/xmldoc.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/term.h"
  37. #ifdef LOW_MEMORY
  38. #define CONFIG_OPT_BUCKETS 5
  39. #else
  40. #define CONFIG_OPT_BUCKETS 53
  41. #endif /* LOW_MEMORY */
  42. /*! \brief Bits of aco_info that shouldn't be assigned outside this file
  43. * \internal
  44. */
  45. struct aco_info_internal {
  46. void *pending; /*!< The user-defined config object awaiting application */
  47. };
  48. struct aco_type_internal {
  49. regex_t *regex;
  50. struct ao2_container *opts; /*!< The container of options registered to the aco_info */
  51. };
  52. struct aco_option {
  53. const char *name;
  54. const char *aliased_to;
  55. const char *default_val;
  56. enum aco_matchtype match_type;
  57. regex_t *name_regex;
  58. struct aco_type **obj;
  59. enum aco_option_type type;
  60. aco_option_handler handler;
  61. unsigned int flags;
  62. unsigned int no_doc:1;
  63. unsigned char deprecated:1;
  64. size_t argc;
  65. intptr_t args[0];
  66. };
  67. #ifdef AST_XML_DOCS
  68. static struct ao2_container *xmldocs;
  69. #endif /* AST_XML_DOCS */
  70. /*! \brief Value of the aco_option_type enum as strings */
  71. static char *aco_option_type_string[] = {
  72. "ACL", /* OPT_ACL_T, */
  73. "Boolean", /* OPT_BOOL_T, */
  74. "Boolean", /* OPT_BOOLFLAG_T, */
  75. "String", /* OPT_CHAR_ARRAY_T, */
  76. "Codec", /* OPT_CODEC_T, */
  77. "Custom", /* OPT_CUSTOM_T, */
  78. "Double", /* OPT_DOUBLE_T, */
  79. "Integer", /* OPT_INT_T, */
  80. "None", /* OPT_NOOP_T, */
  81. "IP Address", /* OPT_SOCKADDR_T, */
  82. "String", /* OPT_STRINGFIELD_T, */
  83. "Unsigned Integer", /* OPT_UINT_T, */
  84. };
  85. void *aco_pending_config(struct aco_info *info)
  86. {
  87. if (!(info && info->internal)) {
  88. ast_log(LOG_ERROR, "This may not be called without an initialized aco_info!\n");
  89. return NULL;
  90. }
  91. return info->internal->pending;
  92. }
  93. static void config_option_destroy(void *obj)
  94. {
  95. struct aco_option *opt = obj;
  96. if (opt->match_type == ACO_REGEX && opt->name_regex) {
  97. regfree(opt->name_regex);
  98. ast_free(opt->name_regex);
  99. }
  100. }
  101. static int int_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  102. static int uint_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  103. static int double_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  104. static int sockaddr_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  105. static int stringfield_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  106. static int bool_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  107. static int boolflag_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  108. static int acl_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  109. static int codec_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  110. static int noop_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  111. static int chararray_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj);
  112. #ifdef AST_XML_DOCS
  113. static int xmldoc_update_config_type(const char *module, const char *name, const char *category, const char *matchfield, const char *matchvalue, unsigned int matches);
  114. static int xmldoc_update_config_option(struct aco_type **types, const char *module, const char *name, const char *object_name, const char *default_value, unsigned int regex, enum aco_option_type type);
  115. #endif
  116. static aco_option_handler ast_config_option_default_handler(enum aco_option_type type)
  117. {
  118. switch(type) {
  119. case OPT_ACL_T: return acl_handler_fn;
  120. case OPT_BOOL_T: return bool_handler_fn;
  121. case OPT_BOOLFLAG_T: return boolflag_handler_fn;
  122. case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
  123. case OPT_CODEC_T: return codec_handler_fn;
  124. case OPT_DOUBLE_T: return double_handler_fn;
  125. case OPT_INT_T: return int_handler_fn;
  126. case OPT_NOOP_T: return noop_handler_fn;
  127. case OPT_SOCKADDR_T: return sockaddr_handler_fn;
  128. case OPT_STRINGFIELD_T: return stringfield_handler_fn;
  129. case OPT_UINT_T: return uint_handler_fn;
  130. case OPT_CUSTOM_T: return NULL;
  131. }
  132. return NULL;
  133. }
  134. static regex_t *build_regex(const char *text)
  135. {
  136. int res;
  137. regex_t *regex;
  138. if (!(regex = ast_malloc(sizeof(*regex)))) {
  139. return NULL;
  140. }
  141. if ((res = regcomp(regex, text, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
  142. size_t len = regerror(res, regex, NULL, 0);
  143. char buf[len];
  144. regerror(res, regex, buf, len);
  145. ast_log(LOG_ERROR, "Could not compile regex '%s': %s\n", text, buf);
  146. ast_free(regex);
  147. return NULL;
  148. }
  149. return regex;
  150. }
  151. static int link_option_to_types(struct aco_info *info, struct aco_type **types, struct aco_option *opt)
  152. {
  153. size_t idx = 0;
  154. struct aco_type *type;
  155. while ((type = types[idx++])) {
  156. if (!type->internal) {
  157. ast_log(LOG_ERROR, "Attempting to register option using uninitialized type\n");
  158. return -1;
  159. }
  160. if (!ao2_link(type->internal->opts, opt)
  161. #ifdef AST_XML_DOCS
  162. || (!info->hidden &&
  163. !opt->no_doc &&
  164. xmldoc_update_config_option(types, info->module, opt->name, type->name, opt->default_val, opt->match_type == ACO_REGEX, opt->type))
  165. #endif /* AST_XML_DOCS */
  166. ) {
  167. do {
  168. ao2_unlink(types[idx - 1]->internal->opts, opt);
  169. } while (--idx);
  170. return -1;
  171. }
  172. }
  173. /* The container(s) should hold the only ref to opt */
  174. ao2_ref(opt, -1);
  175. return 0;
  176. }
  177. int aco_option_register_deprecated(struct aco_info *info, const char *name, struct aco_type **types, const char *aliased_to)
  178. {
  179. struct aco_option *opt;
  180. if (!info || ast_strlen_zero(name) || ast_strlen_zero(aliased_to)) {
  181. return -1;
  182. }
  183. if (!(opt = ao2_alloc(sizeof(*opt), config_option_destroy))) {
  184. return -1;
  185. }
  186. opt->name = name;
  187. opt->aliased_to = aliased_to;
  188. opt->deprecated = 1;
  189. opt->match_type = ACO_EXACT;
  190. if (link_option_to_types(info, types, opt)) {
  191. ao2_ref(opt, -1);
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. unsigned int aco_option_get_flags(const struct aco_option *option)
  197. {
  198. return option->flags;
  199. }
  200. intptr_t aco_option_get_argument(const struct aco_option *option, unsigned int position)
  201. {
  202. return option->args[position];
  203. }
  204. #ifdef AST_XML_DOCS
  205. /*! \internal
  206. * \brief Find a particular ast_xml_doc_item from it's parent config_info, types, and name
  207. */
  208. static struct ast_xml_doc_item *find_xmldoc_option(struct ast_xml_doc_item *config_info, struct aco_type **types, const char *name)
  209. {
  210. struct ast_xml_doc_item *iter = config_info;
  211. if (!iter) {
  212. return NULL;
  213. }
  214. /* First is just the configInfo, we can skip it */
  215. while ((iter = iter->next)) {
  216. size_t x;
  217. if (strcasecmp(iter->name, name)) {
  218. continue;
  219. }
  220. for (x = 0; types[x]; x++) {
  221. /* All we care about is that at least one type has the option */
  222. if (!strcasecmp(types[x]->name, iter->ref)) {
  223. return iter;
  224. }
  225. }
  226. }
  227. return NULL;
  228. }
  229. /*! \internal
  230. * \brief Find a particular ast_xml_doc_item from it's parent config_info and name
  231. */
  232. static struct ast_xml_doc_item *find_xmldoc_type(struct ast_xml_doc_item *config_info, const char *name)
  233. {
  234. struct ast_xml_doc_item *iter = config_info;
  235. if (!iter) {
  236. return NULL;
  237. }
  238. /* First is just the config Info, skip it */
  239. while ((iter = iter->next)) {
  240. if (!strcasecmp(iter->type, "configObject") && !strcasecmp(iter->name, name)) {
  241. break;
  242. }
  243. }
  244. return iter;
  245. }
  246. #endif /* AST_XML_DOCS */
  247. int __aco_option_register(struct aco_info *info, const char *name, enum aco_matchtype matchtype, struct aco_type **types,
  248. const char *default_val, enum aco_option_type kind, aco_option_handler handler, unsigned int flags,
  249. unsigned int no_doc, size_t argc, ...)
  250. {
  251. struct aco_option *opt;
  252. va_list ap;
  253. int tmp;
  254. /* Custom option types require a handler */
  255. if (!handler && kind == OPT_CUSTOM_T) {
  256. return -1;
  257. }
  258. if (!(types && types[0])) {
  259. return -1;
  260. }
  261. if (!(opt = ao2_alloc(sizeof(*opt) + argc * sizeof(opt->args[0]), config_option_destroy))) {
  262. return -1;
  263. }
  264. if (matchtype == ACO_REGEX && !(opt->name_regex = build_regex(name))) {
  265. ao2_ref(opt, -1);
  266. return -1;
  267. }
  268. va_start(ap, argc);
  269. for (tmp = 0; tmp < argc; tmp++) {
  270. opt->args[tmp] = va_arg(ap, size_t);
  271. }
  272. va_end(ap);
  273. opt->name = name;
  274. opt->match_type = matchtype;
  275. opt->default_val = default_val;
  276. opt->type = kind;
  277. opt->handler = handler;
  278. opt->flags = flags;
  279. opt->argc = argc;
  280. opt->no_doc = no_doc;
  281. if (!opt->handler && !(opt->handler = ast_config_option_default_handler(opt->type))) {
  282. /* This should never happen */
  283. ast_log(LOG_ERROR, "No handler provided, and no default handler exists for type %d\n", opt->type);
  284. ao2_ref(opt, -1);
  285. return -1;
  286. };
  287. if (link_option_to_types(info, types, opt)) {
  288. ao2_ref(opt, -1);
  289. return -1;
  290. }
  291. return 0;
  292. }
  293. static int config_opt_hash(const void *obj, const int flags)
  294. {
  295. const struct aco_option *opt = obj;
  296. const char *name = (flags & OBJ_KEY) ? obj : opt->name;
  297. return ast_str_case_hash(name);
  298. }
  299. static int config_opt_cmp(void *obj, void *arg, int flags)
  300. {
  301. struct aco_option *opt1 = obj, *opt2 = arg;
  302. const char *name = (flags & OBJ_KEY) ? arg : opt2->name;
  303. return strcasecmp(opt1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
  304. }
  305. static int find_option_cb(void *obj, void *arg, int flags)
  306. {
  307. struct aco_option *match = obj;
  308. const char *name = arg;
  309. switch (match->match_type) {
  310. case ACO_EXACT:
  311. return strcasecmp(name, match->name) ? 0 : CMP_MATCH | CMP_STOP;
  312. case ACO_REGEX:
  313. return regexec(match->name_regex, name, 0, NULL, 0) ? 0 : CMP_MATCH | CMP_STOP;
  314. }
  315. ast_log(LOG_ERROR, "Unknown match type. This should not be possible.\n");
  316. return CMP_STOP;
  317. }
  318. static struct aco_option *aco_option_find(struct aco_type *type, const char *name)
  319. {
  320. struct aco_option *opt;
  321. if (!type || !type->internal || !type->internal->opts) {
  322. ast_log(LOG_NOTICE, "Attempting to use NULL or unitialized config type\n");
  323. return NULL;
  324. }
  325. /* Try an exact match with OBJ_KEY for the common/fast case, then iterate through
  326. * all options for the regex cases */
  327. if (!(opt = ao2_callback(type->internal->opts, OBJ_KEY, find_option_cb, (void *) name))) {
  328. opt = ao2_callback(type->internal->opts, 0, find_option_cb, (void *) name);
  329. }
  330. return opt;
  331. }
  332. struct ao2_container *aco_option_container_alloc(void)
  333. {
  334. return ao2_container_alloc(CONFIG_OPT_BUCKETS, config_opt_hash, config_opt_cmp);
  335. }
  336. static struct aco_type *internal_aco_type_find(struct aco_file *file, struct ast_config *cfg, const char *category)
  337. {
  338. size_t x;
  339. struct aco_type *match;
  340. const char *val;
  341. for (x = 0, match = file->types[x]; match; match = file->types[++x]) {
  342. /* First make sure we are an object that can service this category */
  343. if (!regexec(match->internal->regex, category, 0, NULL, 0) == !match->category_match) {
  344. continue;
  345. }
  346. /* Then, see if we need to match a particular field */
  347. if (!ast_strlen_zero(match->matchfield) && (!ast_strlen_zero(match->matchvalue) || match->matchfunc)) {
  348. if (!(val = ast_variable_retrieve(cfg, category, match->matchfield))) {
  349. ast_log(LOG_ERROR, "Required match field '%s' not found\n", match->matchfield);
  350. return NULL;
  351. }
  352. if (match->matchfunc) {
  353. if (!match->matchfunc(val)) {
  354. continue;
  355. }
  356. } else if (strcasecmp(val, match->matchvalue)) {
  357. continue;
  358. }
  359. }
  360. /* If we get this far, we're a match */
  361. break;
  362. }
  363. return match;
  364. }
  365. static int is_preload(struct aco_file *file, const char *cat)
  366. {
  367. int i;
  368. if (!file->preload) {
  369. return 0;
  370. }
  371. for (i = 0; !ast_strlen_zero(file->preload[i]); i++) {
  372. if (!strcasecmp(cat, file->preload[i])) {
  373. return 1;
  374. }
  375. }
  376. return 0;
  377. }
  378. static int process_category(struct ast_config *cfg, struct aco_info *info, struct aco_file *file, const char *cat, int preload) {
  379. RAII_VAR(void *, new_item, NULL, ao2_cleanup);
  380. struct aco_type *type;
  381. /* For global types, field is the global option struct. For non-global, it is the container for items.
  382. * We do not grab a reference to these objects, as the info already holds references to them. This
  383. * pointer is just a convenience. Do not actually store it somewhere. */
  384. void **field;
  385. regex_t *regex_skip;
  386. /* Skip preloaded categories if we aren't preloading */
  387. if (!preload && is_preload(file, cat)) {
  388. return 0;
  389. }
  390. /* Skip the category if we've been told to ignore it */
  391. if (!ast_strlen_zero(file->skip_category)) {
  392. regex_skip = build_regex(file->skip_category);
  393. if (!regexec(regex_skip, cat, 0, NULL, 0)) {
  394. regfree(regex_skip);
  395. ast_free(regex_skip);
  396. return 0;
  397. }
  398. regfree(regex_skip);
  399. ast_free(regex_skip);
  400. }
  401. /* Find aco_type by category, if not found it is an error */
  402. if (!(type = internal_aco_type_find(file, cfg, cat))) {
  403. ast_log(LOG_ERROR, "Could not find config type for category '%s' in '%s'\n", cat, file->filename);
  404. return -1;
  405. }
  406. field = info->internal->pending + type->item_offset;
  407. if (!*field) {
  408. ast_log(LOG_ERROR, "In %s: %s - No object to update!\n", file->filename, cat);
  409. return -1;
  410. }
  411. if (type->type == ACO_GLOBAL && *field) {
  412. if (aco_process_category_options(type, cfg, cat, *field)) {
  413. ast_log(LOG_ERROR, "In %s: Processing options for %s failed\n", file->filename, cat);
  414. return -1;
  415. }
  416. } else if (type->type == ACO_ITEM) {
  417. int new = 0;
  418. /* If we have multiple definitions of a category in a file, or can set the values from multiple
  419. * files, look up the entry if we've already added it so we can merge the values together.
  420. * Otherwise, alloc a new item. */
  421. if (*field) {
  422. if (!(new_item = type->item_find(*field, cat))) {
  423. if (!(new_item = type->item_alloc(cat))) {
  424. ast_log(LOG_ERROR, "In %s: Could not create item for %s\n", file->filename, cat);
  425. return -1;
  426. }
  427. if (aco_set_defaults(type, cat, new_item)) {
  428. ast_log(LOG_ERROR, "In %s: Setting defaults for %s failed\n", file->filename, cat);
  429. return -1;
  430. }
  431. new = 1;
  432. }
  433. }
  434. if (type->item_pre_process && type->item_pre_process(new_item)) {
  435. ast_log(LOG_ERROR, "In %s: Preprocess callback for %s failed\n", file->filename, cat);
  436. return -1;
  437. }
  438. if (aco_process_category_options(type, cfg, cat, new_item)) {
  439. ast_log(LOG_ERROR, "In %s: Processing options for %s failed\n", file->filename, cat);
  440. return -1;
  441. }
  442. if (type->item_prelink && type->item_prelink(new_item)) {
  443. ast_log(LOG_ERROR, "In %s: Pre-link callback for %s failed\n", file->filename, cat);
  444. return -1;
  445. }
  446. if (new && !ao2_link(*field, new_item)) {
  447. ast_log(LOG_ERROR, "In %s: Linking config for %s failed\n", file->filename, cat);
  448. return -1;
  449. }
  450. }
  451. return 0;
  452. }
  453. static int apply_config(struct aco_info *info)
  454. {
  455. ao2_global_obj_replace_unref(*info->global_obj, info->internal->pending);
  456. return 0;
  457. }
  458. static enum aco_process_status internal_process_ast_config(struct aco_info *info, struct aco_file *file, struct ast_config *cfg)
  459. {
  460. const char *cat = NULL;
  461. if (file->preload) {
  462. int i;
  463. for (i = 0; !ast_strlen_zero(file->preload[i]); i++) {
  464. if (process_category(cfg, info, file, file->preload[i], 1)) {
  465. return ACO_PROCESS_ERROR;
  466. }
  467. }
  468. }
  469. while ((cat = ast_category_browse(cfg, cat))) {
  470. if (process_category(cfg, info, file, cat, 0)) {
  471. return ACO_PROCESS_ERROR;
  472. }
  473. }
  474. return ACO_PROCESS_OK;
  475. }
  476. enum aco_process_status aco_process_ast_config(struct aco_info *info, struct aco_file *file, struct ast_config *cfg)
  477. {
  478. if (!info->internal) {
  479. ast_log(LOG_ERROR, "Attempt to process %s with uninitialized aco_info\n", file->filename);
  480. goto error;
  481. }
  482. if (!(info->internal->pending = info->snapshot_alloc())) {
  483. ast_log(LOG_ERROR, "In %s: Could not allocate temporary objects\n", file->filename);
  484. goto error;
  485. }
  486. if (internal_process_ast_config(info, file, cfg)) {
  487. goto error;
  488. }
  489. if (info->pre_apply_config && info->pre_apply_config()) {
  490. goto error;
  491. }
  492. if (apply_config(info)) {
  493. goto error;
  494. };
  495. ao2_cleanup(info->internal->pending);
  496. return ACO_PROCESS_OK;
  497. error:
  498. ao2_cleanup(info->internal->pending);
  499. return ACO_PROCESS_ERROR;
  500. }
  501. enum aco_process_status aco_process_config(struct aco_info *info, int reload)
  502. {
  503. struct ast_config *cfg;
  504. struct ast_flags cfg_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0, };
  505. int res = ACO_PROCESS_OK, x = 0;
  506. struct aco_file *file;
  507. if (!(info->files[0])) {
  508. ast_log(LOG_ERROR, "No filename given, cannot proceed!\n");
  509. return ACO_PROCESS_ERROR;
  510. }
  511. if (!info->internal) {
  512. ast_log(LOG_ERROR, "Attempting to process uninitialized aco_info\n");
  513. return ACO_PROCESS_ERROR;
  514. }
  515. if (!(info->internal->pending = info->snapshot_alloc())) {
  516. ast_log(LOG_ERROR, "In %s: Could not allocate temporary objects\n", info->module);
  517. return ACO_PROCESS_ERROR;
  518. }
  519. /*
  520. * XXX ASTERISK-22009 must fix config framework loading of multiple files.
  521. *
  522. * A reload with multiple files must reload all files if any
  523. * file has been touched.
  524. */
  525. while (res != ACO_PROCESS_ERROR && (file = info->files[x++])) {
  526. const char *filename = file->filename;
  527. struct aco_type *match;
  528. int i;
  529. /* set defaults for global objects */
  530. for (i = 0, match = file->types[i]; match; match = file->types[++i]) {
  531. void **field = info->internal->pending + match->item_offset;
  532. if (match->type != ACO_GLOBAL || !*field) {
  533. continue;
  534. }
  535. if (aco_set_defaults(match, match->category, *field)) {
  536. ast_log(LOG_ERROR, "In %s: Setting defaults for %s failed\n", file->filename, match->category);
  537. res = ACO_PROCESS_ERROR;
  538. break;
  539. }
  540. }
  541. if (res == ACO_PROCESS_ERROR) {
  542. break;
  543. }
  544. try_alias:
  545. if (!(cfg = ast_config_load(filename, cfg_flags))) {
  546. if (file->alias && strcmp(file->alias, filename)) {
  547. filename = file->alias;
  548. goto try_alias;
  549. }
  550. ast_log(LOG_ERROR, "Unable to load config file '%s'\n", file->filename);
  551. res = ACO_PROCESS_ERROR;
  552. break;
  553. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  554. ast_debug(1, "%s was unchanged\n", file->filename);
  555. res = ACO_PROCESS_UNCHANGED;
  556. continue;
  557. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  558. ast_log(LOG_ERROR, "Contents of %s are invalid and cannot be parsed\n", file->filename);
  559. res = ACO_PROCESS_ERROR;
  560. break;
  561. } else if (cfg == CONFIG_STATUS_FILEMISSING) {
  562. if (file->alias && strcmp(file->alias, filename)) {
  563. filename = file->alias;
  564. goto try_alias;
  565. }
  566. ast_log(LOG_ERROR, "%s is missing! Cannot load %s\n", file->filename, info->module);
  567. res = ACO_PROCESS_ERROR;
  568. break;
  569. }
  570. res = internal_process_ast_config(info, file, cfg);
  571. ast_config_destroy(cfg);
  572. }
  573. if (res != ACO_PROCESS_OK) {
  574. goto end;
  575. }
  576. if (info->pre_apply_config && (info->pre_apply_config())) {
  577. res = ACO_PROCESS_ERROR;
  578. goto end;
  579. }
  580. if (apply_config(info)) {
  581. res = ACO_PROCESS_ERROR;
  582. goto end;
  583. }
  584. if (info->post_apply_config) {
  585. info->post_apply_config();
  586. }
  587. end:
  588. ao2_cleanup(info->internal->pending);
  589. return res;
  590. }
  591. int aco_process_var(struct aco_type *type, const char *cat, struct ast_variable *var, void *obj)
  592. {
  593. RAII_VAR(struct aco_option *, opt, aco_option_find(type, var->name), ao2_cleanup);
  594. if (opt && opt->deprecated && !ast_strlen_zero(opt->aliased_to)) {
  595. const char *alias = ast_strdupa(opt->aliased_to);
  596. ast_log(LOG_WARNING, "At line %d of %s option '%s' is deprecated. Use '%s' instead\n", var->lineno, var->file, var->name, alias);
  597. ao2_ref(opt, -1);
  598. opt = aco_option_find(type, alias);
  599. }
  600. if (!opt) {
  601. ast_log(LOG_ERROR, "Could not find option suitable for category '%s' named '%s' at line %d of %s\n", cat, var->name, var->lineno, var->file);
  602. return -1;
  603. }
  604. if (!opt->handler) {
  605. /* It should be impossible for an option to not have a handler */
  606. ast_log(LOG_ERROR, "BUG! Somehow a config option for %s/%s was created with no handler!\n", cat, var->name);
  607. return -1;
  608. }
  609. if (opt->handler(opt, var, obj)) {
  610. ast_log(LOG_ERROR, "Error parsing %s=%s at line %d of %s\n", var->name, var->value, var->lineno, var->file);
  611. return -1;
  612. }
  613. return 0;
  614. }
  615. int aco_process_category_options(struct aco_type *type, struct ast_config *cfg, const char *cat, void *obj)
  616. {
  617. struct ast_variable *var;
  618. for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
  619. if (aco_process_var(type, cat, var, obj)) {
  620. return -1;
  621. }
  622. }
  623. return 0;
  624. }
  625. static void internal_type_destroy(struct aco_type *type)
  626. {
  627. /* If we've already had all our internal data cleared out,
  628. * then there's no need to proceed further
  629. */
  630. if (!type->internal) {
  631. return;
  632. }
  633. if (type->internal->regex) {
  634. regfree(type->internal->regex);
  635. ast_free(type->internal->regex);
  636. }
  637. ao2_cleanup(type->internal->opts);
  638. type->internal->opts = NULL;
  639. ast_free(type->internal);
  640. type->internal = NULL;
  641. }
  642. static void internal_file_types_destroy(struct aco_file *file)
  643. {
  644. size_t x;
  645. struct aco_type *t;
  646. for (x = 0, t = file->types[x]; t; t = file->types[++x]) {
  647. internal_type_destroy(t);
  648. t = NULL;
  649. }
  650. }
  651. static int internal_type_init(struct aco_type *type)
  652. {
  653. if (!(type->internal = ast_calloc(1, sizeof(*type->internal)))) {
  654. return -1;
  655. }
  656. if (!(type->internal->regex = build_regex(type->category))) {
  657. internal_type_destroy(type);
  658. return -1;
  659. }
  660. if (!(type->internal->opts = aco_option_container_alloc())) {
  661. internal_type_destroy(type);
  662. return -1;
  663. }
  664. return 0;
  665. }
  666. int aco_info_init(struct aco_info *info)
  667. {
  668. size_t x = 0, y = 0;
  669. struct aco_file *file;
  670. struct aco_type *type;
  671. if (!(info->internal = ast_calloc(1, sizeof(*info->internal)))) {
  672. return -1;
  673. }
  674. while ((file = info->files[x++])) {
  675. while ((type = file->types[y++])) {
  676. if (internal_type_init(type)) {
  677. goto error;
  678. }
  679. #ifdef AST_XML_DOCS
  680. if (!info->hidden &&
  681. !type->hidden &&
  682. xmldoc_update_config_type(info->module, type->name, type->category, type->matchfield, type->matchvalue, type->category_match == ACO_WHITELIST)) {
  683. goto error;
  684. }
  685. #endif /* AST_XML_DOCS */
  686. }
  687. y = 0;
  688. }
  689. return 0;
  690. error:
  691. aco_info_destroy(info);
  692. return -1;
  693. }
  694. void aco_info_destroy(struct aco_info *info)
  695. {
  696. int x;
  697. /* It shouldn't be possible for internal->pending to be in use when this is called because
  698. * of the locks in loader.c around reloads and unloads and the fact that internal->pending
  699. * only exists while those locks are held */
  700. ast_free(info->internal);
  701. info->internal = NULL;
  702. for (x = 0; info->files[x]; x++) {
  703. internal_file_types_destroy(info->files[x]);
  704. }
  705. }
  706. int aco_set_defaults(struct aco_type *type, const char *category, void *obj)
  707. {
  708. struct aco_option *opt;
  709. struct ao2_iterator iter;
  710. iter = ao2_iterator_init(type->internal->opts, 0);
  711. while ((opt = ao2_iterator_next(&iter))) {
  712. RAII_VAR(struct ast_variable *, var, NULL, ast_variables_destroy);
  713. if (ast_strlen_zero(opt->default_val)) {
  714. ao2_ref(opt, -1);
  715. continue;
  716. }
  717. if (!(var = ast_variable_new(opt->name, opt->default_val, ""))) {
  718. ao2_ref(opt, -1);
  719. ao2_iterator_destroy(&iter);
  720. return -1;
  721. }
  722. if (opt->handler(opt, var, obj)) {
  723. ast_log(LOG_ERROR, "Unable to set default for %s, %s=%s\n", category, var->name, var->value);
  724. ao2_ref(opt, -1);
  725. ao2_iterator_destroy(&iter);
  726. return -1;
  727. }
  728. ao2_ref(opt, -1);
  729. }
  730. ao2_iterator_destroy(&iter);
  731. return 0;
  732. }
  733. #ifdef AST_XML_DOCS
  734. /*! \internal
  735. * \brief Complete the name of the module the user is looking for
  736. */
  737. static char *complete_config_module(const char *word, int pos, int state)
  738. {
  739. char *c = NULL;
  740. size_t wordlen = strlen(word);
  741. int which = 0;
  742. struct ao2_iterator i;
  743. struct ast_xml_doc_item *cur;
  744. if (pos != 3) {
  745. return NULL;
  746. }
  747. i = ao2_iterator_init(xmldocs, 0);
  748. while ((cur = ao2_iterator_next(&i))) {
  749. if (!strncasecmp(word, cur->name, wordlen) && ++which > state) {
  750. c = ast_strdup(cur->name);
  751. ao2_ref(cur, -1);
  752. break;
  753. }
  754. ao2_ref(cur, -1);
  755. }
  756. ao2_iterator_destroy(&i);
  757. return c;
  758. }
  759. /*! \internal
  760. * \brief Complete the name of the configuration type the user is looking for
  761. */
  762. static char *complete_config_type(const char *module, const char *word, int pos, int state)
  763. {
  764. char *c = NULL;
  765. size_t wordlen = strlen(word);
  766. int which = 0;
  767. RAII_VAR(struct ast_xml_doc_item *, info, NULL, ao2_cleanup);
  768. struct ast_xml_doc_item *cur;
  769. if (pos != 4) {
  770. return NULL;
  771. }
  772. if (!(info = ao2_find(xmldocs, module, OBJ_KEY))) {
  773. return NULL;
  774. }
  775. cur = info;
  776. while ((cur = cur->next)) {
  777. if (!strcasecmp(cur->type, "configObject") && !strncasecmp(word, cur->name, wordlen) && ++which > state) {
  778. c = ast_strdup(cur->name);
  779. break;
  780. }
  781. }
  782. return c;
  783. }
  784. /*! \internal
  785. * \brief Complete the name of the configuration option the user is looking for
  786. */
  787. static char *complete_config_option(const char *module, const char *option, const char *word, int pos, int state)
  788. {
  789. char *c = NULL;
  790. size_t wordlen = strlen(word);
  791. int which = 0;
  792. RAII_VAR(struct ast_xml_doc_item *, info, NULL, ao2_cleanup);
  793. struct ast_xml_doc_item *cur;
  794. if (pos != 5) {
  795. return NULL;
  796. }
  797. if (!(info = ao2_find(xmldocs, module, OBJ_KEY))) {
  798. return NULL;
  799. }
  800. cur = info;
  801. while ((cur = cur->next)) {
  802. if (!strcasecmp(cur->type, "configOption") && !strcasecmp(cur->ref, option) && !strncasecmp(word, cur->name, wordlen) && ++which > state) {
  803. c = ast_strdup(cur->name);
  804. break;
  805. }
  806. }
  807. return c;
  808. }
  809. /* Define as 0 if we want to allow configurations to be registered without
  810. * documentation
  811. */
  812. #define XMLDOC_STRICT 1
  813. /*! \internal
  814. * \brief Update the XML documentation for a config type based on its registration
  815. */
  816. static int xmldoc_update_config_type(const char *module, const char *name, const char *category, const char *matchfield, const char *matchvalue, unsigned int matches)
  817. {
  818. RAII_VAR(struct ast_xml_xpath_results *, results, NULL, ast_xml_xpath_results_free);
  819. RAII_VAR(struct ast_xml_doc_item *, config_info, ao2_find(xmldocs, module, OBJ_KEY), ao2_cleanup);
  820. struct ast_xml_doc_item *config_type;
  821. struct ast_xml_node *type, *syntax, *matchinfo, *tmp;
  822. /* If we already have a syntax element, bail. This isn't an error, since we may unload a module which
  823. * has updated the docs and then load it again. */
  824. if ((results = ast_xmldoc_query("//configInfo[@name='%s']/*/configObject[@name='%s']/syntax", module, name))) {
  825. return 0;
  826. }
  827. if (!(results = ast_xmldoc_query("//configInfo[@name='%s']/*/configObject[@name='%s']", module, name))) {
  828. ast_log(LOG_WARNING, "Cannot update type '%s' in module '%s' because it has no existing documentation!\n", name, module);
  829. return XMLDOC_STRICT ? -1 : 0;
  830. }
  831. if (!(type = ast_xml_xpath_get_first_result(results))) {
  832. ast_log(LOG_WARNING, "Could not retrieve documentation for type '%s' in module '%s'\n", name, module);
  833. return XMLDOC_STRICT ? -1 : 0;
  834. }
  835. if (!(syntax = ast_xml_new_child(type, "syntax"))) {
  836. ast_log(LOG_WARNING, "Could not create syntax node for type '%s' in module '%s'\n", name, module);
  837. return XMLDOC_STRICT ? -1 : 0;
  838. }
  839. if (!(matchinfo = ast_xml_new_child(syntax, "matchInfo"))) {
  840. ast_log(LOG_WARNING, "Could not create matchInfo node for type '%s' in module '%s'\n", name, module);
  841. return XMLDOC_STRICT ? -1 : 0;
  842. }
  843. if (!(tmp = ast_xml_new_child(matchinfo, "category"))) {
  844. ast_log(LOG_WARNING, "Could not create category node for type '%s' in module '%s'\n", name, module);
  845. return XMLDOC_STRICT ? -1 : 0;
  846. }
  847. ast_xml_set_text(tmp, category);
  848. ast_xml_set_attribute(tmp, "match", matches ? "true" : "false");
  849. if (!ast_strlen_zero(matchfield) && !(tmp = ast_xml_new_child(matchinfo, "field"))) {
  850. ast_log(LOG_WARNING, "Could not add %s attribute for type '%s' in module '%s'\n", matchfield, name, module);
  851. return XMLDOC_STRICT ? -1 : 0;
  852. }
  853. ast_xml_set_attribute(tmp, "name", matchfield);
  854. ast_xml_set_text(tmp, matchvalue);
  855. if (!config_info || !(config_type = find_xmldoc_type(config_info, name))) {
  856. ast_log(LOG_WARNING, "Could not obtain XML documentation item for config type %s\n", name);
  857. return XMLDOC_STRICT ? -1 : 0;
  858. }
  859. if (ast_xmldoc_regenerate_doc_item(config_type)) {
  860. ast_log(LOG_WARNING, "Could not update type '%s' with values from config type registration\n", name);
  861. return XMLDOC_STRICT ? -1 : 0;
  862. }
  863. return 0;
  864. }
  865. /*! \internal
  866. * \brief Update the XML documentation for a config option based on its registration
  867. */
  868. static int xmldoc_update_config_option(struct aco_type **types, const char *module, const char *name, const char *object_name, const char *default_value, unsigned int regex, enum aco_option_type type)
  869. {
  870. RAII_VAR(struct ast_xml_xpath_results *, results, NULL, ast_xml_xpath_results_free);
  871. RAII_VAR(struct ast_xml_doc_item *, config_info, ao2_find(xmldocs, module, OBJ_KEY), ao2_cleanup);
  872. struct ast_xml_doc_item * config_option;
  873. struct ast_xml_node *option;
  874. ast_assert(ARRAY_LEN(aco_option_type_string) > type);
  875. if (!config_info || !(config_option = find_xmldoc_option(config_info, types, name))) {
  876. ast_log(LOG_ERROR, "XML Documentation for option '%s' in modules '%s' not found!\n", name, module);
  877. return XMLDOC_STRICT ? -1 : 0;
  878. }
  879. if (!(results = ast_xmldoc_query("//configInfo[@name='%s']/*/configObject[@name='%s']/configOption[@name='%s']", module, object_name, name))) {
  880. ast_log(LOG_WARNING, "Could not find option '%s' with type '%s' in module '%s'\n", name, object_name, module);
  881. return XMLDOC_STRICT ? -1 : 0;
  882. }
  883. if (!(option = ast_xml_xpath_get_first_result(results))) {
  884. ast_log(LOG_WARNING, "Could obtain results for option '%s' with type '%s' in module '%s'\n", name, object_name, module);
  885. return XMLDOC_STRICT ? -1 : 0;
  886. }
  887. ast_xml_set_attribute(option, "regex", regex ? "true" : "false");
  888. ast_xml_set_attribute(option, "default", default_value);
  889. ast_xml_set_attribute(option, "type", aco_option_type_string[type]);
  890. if (ast_xmldoc_regenerate_doc_item(config_option)) {
  891. ast_log(LOG_WARNING, "Could not update option '%s' with values from config option registration\n", name);
  892. return XMLDOC_STRICT ? -1 : 0;
  893. }
  894. return 0;
  895. }
  896. /*! \internal
  897. * \brief Show the modules with configuration information
  898. */
  899. static void cli_show_modules(struct ast_cli_args *a)
  900. {
  901. struct ast_xml_doc_item *item;
  902. struct ao2_iterator it_items;
  903. ast_assert(a->argc == 3);
  904. if (ao2_container_count(xmldocs) == 0) {
  905. ast_cli(a->fd, "No modules found.\n");
  906. return;
  907. }
  908. it_items = ao2_iterator_init(xmldocs, 0);
  909. ast_cli(a->fd, "The following modules have configuration information:\n");
  910. while ((item = ao2_iterator_next(&it_items))) {
  911. ast_cli(a->fd, "\t%s\n", item->name);
  912. ao2_ref(item, -1);
  913. }
  914. ao2_iterator_destroy(&it_items);
  915. }
  916. /*! \internal
  917. * \brief Show the configuration types for a module
  918. */
  919. static void cli_show_module_types(struct ast_cli_args *a)
  920. {
  921. RAII_VAR(struct ast_xml_doc_item *, item, NULL, ao2_cleanup);
  922. struct ast_xml_doc_item *tmp;
  923. ast_assert(a->argc == 4);
  924. if (!(item = ao2_find(xmldocs, a->argv[3], OBJ_KEY))) {
  925. ast_cli(a->fd, "Module %s not found.\n", a->argv[3]);
  926. return;
  927. }
  928. if (ast_str_strlen(item->synopsis)) {
  929. ast_cli(a->fd, "%s\n\n", ast_xmldoc_printable(ast_str_buffer(item->synopsis), 1));
  930. }
  931. if (ast_str_strlen(item->description)) {
  932. ast_cli(a->fd, "%s\n\n", ast_xmldoc_printable(ast_str_buffer(item->description), 1));
  933. }
  934. tmp = item;
  935. ast_cli(a->fd, "Configuration option types for %s:\n", tmp->name);
  936. while ((tmp = tmp->next)) {
  937. if (!strcasecmp(tmp->type, "configObject")) {
  938. ast_cli(a->fd, "%-25s -- %-65.65s\n", tmp->name,
  939. ast_str_buffer(tmp->synopsis));
  940. }
  941. }
  942. }
  943. /*! \internal
  944. * \brief Show the information for a configuration type
  945. */
  946. static void cli_show_module_type(struct ast_cli_args *a)
  947. {
  948. RAII_VAR(struct ast_xml_doc_item *, item, NULL, ao2_cleanup);
  949. struct ast_xml_doc_item *tmp;
  950. char option_type[64];
  951. int match = 0;
  952. ast_assert(a->argc == 5);
  953. if (!(item = ao2_find(xmldocs, a->argv[3], OBJ_KEY))) {
  954. ast_cli(a->fd, "Unknown module %s\n", a->argv[3]);
  955. return;
  956. }
  957. tmp = item;
  958. while ((tmp = tmp->next)) {
  959. if (!strcasecmp(tmp->type, "configObject") && !strcasecmp(tmp->name, a->argv[4])) {
  960. match = 1;
  961. term_color(option_type, tmp->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(option_type));
  962. ast_cli(a->fd, "%s", option_type);
  963. if (ast_str_strlen(tmp->syntax)) {
  964. ast_cli(a->fd, ": [%s]\n\n", ast_xmldoc_printable(ast_str_buffer(tmp->syntax), 1));
  965. } else {
  966. ast_cli(a->fd, "\n\n");
  967. }
  968. if (ast_str_strlen(tmp->synopsis)) {
  969. ast_cli(a->fd, "%s\n\n", ast_xmldoc_printable(ast_str_buffer(tmp->synopsis), 1));
  970. }
  971. if (ast_str_strlen(tmp->description)) {
  972. ast_cli(a->fd, "%s\n\n", ast_xmldoc_printable(ast_str_buffer(tmp->description), 1));
  973. }
  974. }
  975. }
  976. if (!match) {
  977. ast_cli(a->fd, "Unknown configuration type %s\n", a->argv[4]);
  978. return;
  979. }
  980. /* Now iterate over the options for the type */
  981. tmp = item;
  982. while ((tmp = tmp->next)) {
  983. if (!strcasecmp(tmp->type, "configOption") && !strcasecmp(tmp->ref, a->argv[4])) {
  984. ast_cli(a->fd, "%-25s -- %-65.65s\n", tmp->name,
  985. ast_str_buffer(tmp->synopsis));
  986. }
  987. }
  988. }
  989. /*! \internal
  990. * \brief Show detailed information for an option
  991. */
  992. static void cli_show_module_options(struct ast_cli_args *a)
  993. {
  994. RAII_VAR(struct ast_xml_doc_item *, item, NULL, ao2_cleanup);
  995. struct ast_xml_doc_item *tmp;
  996. char option_name[64];
  997. int match = 0;
  998. ast_assert(a->argc == 6);
  999. if (!(item = ao2_find(xmldocs, a->argv[3], OBJ_KEY))) {
  1000. ast_cli(a->fd, "Unknown module %s\n", a->argv[3]);
  1001. return;
  1002. }
  1003. tmp = item;
  1004. while ((tmp = tmp->next)) {
  1005. if (!strcasecmp(tmp->type, "configOption") && !strcasecmp(tmp->ref, a->argv[4]) && !strcasecmp(tmp->name, a->argv[5])) {
  1006. if (match) {
  1007. ast_cli(a->fd, "\n");
  1008. }
  1009. term_color(option_name, tmp->ref, COLOR_MAGENTA, COLOR_BLACK, sizeof(option_name));
  1010. ast_cli(a->fd, "[%s%s]\n", option_name, term_end());
  1011. if (ast_str_strlen(tmp->syntax)) {
  1012. ast_cli(a->fd, "%s\n", ast_xmldoc_printable(ast_str_buffer(tmp->syntax), 1));
  1013. }
  1014. ast_cli(a->fd, "%s\n\n", ast_xmldoc_printable(AS_OR(tmp->synopsis, "No information available"), 1));
  1015. if (ast_str_strlen(tmp->description)) {
  1016. ast_cli(a->fd, "%s\n\n", ast_xmldoc_printable(ast_str_buffer(tmp->description), 1));
  1017. }
  1018. if (ast_str_strlen(tmp->seealso)) {
  1019. ast_cli(a->fd, "See Also:\n");
  1020. ast_cli(a->fd, "%s\n\n", ast_xmldoc_printable(ast_str_buffer(tmp->seealso), 1));
  1021. }
  1022. match = 1;
  1023. }
  1024. }
  1025. if (!match) {
  1026. ast_cli(a->fd, "No option %s found for %s:%s\n", a->argv[5], a->argv[3], a->argv[4]);
  1027. }
  1028. }
  1029. static char *cli_show_help(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1030. {
  1031. switch (cmd) {
  1032. case CLI_INIT:
  1033. e->command = "config show help";
  1034. e->usage =
  1035. "Usage: config show help [<module> [<type> [<option>]]]\n"
  1036. " Display detailed information about module configuration.\n"
  1037. " * If nothing is specified, the modules that have\n"
  1038. " configuration information are listed.\n"
  1039. " * If <module> is specified, the configuration types\n"
  1040. " for that module will be listed, along with brief\n"
  1041. " information about that type.\n"
  1042. " * If <module> and <type> are specified, detailed\n"
  1043. " information about the type is displayed, as well\n"
  1044. " as the available options.\n"
  1045. " * If <module>, <type>, and <option> are specified,\n"
  1046. " detailed information will be displayed about that\n"
  1047. " option.\n"
  1048. " NOTE: the help documentation is partially generated at run\n"
  1049. " time when a module is loaded. If a module is not loaded,\n"
  1050. " configuration help for that module may be incomplete.\n";
  1051. return NULL;
  1052. case CLI_GENERATE:
  1053. switch(a->pos) {
  1054. case 3: return complete_config_module(a->word, a->pos, a->n);
  1055. case 4: return complete_config_type(a->argv[3], a->word, a->pos, a->n);
  1056. case 5: return complete_config_option(a->argv[3], a->argv[4], a->word, a->pos, a->n);
  1057. default: return NULL;
  1058. }
  1059. }
  1060. switch (a->argc) {
  1061. case 3:
  1062. cli_show_modules(a);
  1063. break;
  1064. case 4:
  1065. cli_show_module_types(a);
  1066. break;
  1067. case 5:
  1068. cli_show_module_type(a);
  1069. break;
  1070. case 6:
  1071. cli_show_module_options(a);
  1072. break;
  1073. default:
  1074. return CLI_SHOWUSAGE;
  1075. }
  1076. return CLI_SUCCESS;
  1077. }
  1078. static struct ast_cli_entry cli_aco[] = {
  1079. AST_CLI_DEFINE(cli_show_help, "Show configuration help for a module"),
  1080. };
  1081. static void aco_deinit(void)
  1082. {
  1083. ast_cli_unregister(cli_aco);
  1084. ao2_cleanup(xmldocs);
  1085. }
  1086. #endif /* AST_XML_DOCS */
  1087. int aco_init(void)
  1088. {
  1089. #ifdef AST_XML_DOCS
  1090. ast_register_atexit(aco_deinit);
  1091. if (!(xmldocs = ast_xmldoc_build_documentation("configInfo"))) {
  1092. ast_log(LOG_ERROR, "Couldn't build config documentation\n");
  1093. return -1;
  1094. }
  1095. ast_cli_register_multiple(cli_aco, ARRAY_LEN(cli_aco));
  1096. #endif /* AST_XML_DOCS */
  1097. return 0;
  1098. }
  1099. /* Default config option handlers */
  1100. /*! \brief Default option handler for signed integers
  1101. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1102. * enum aco_option_type in config_options.h
  1103. */
  1104. static int int_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj) {
  1105. int *field = (int *)(obj + opt->args[0]);
  1106. unsigned int flags = PARSE_INT32 | opt->flags;
  1107. int res = 0;
  1108. if (opt->flags & PARSE_IN_RANGE) {
  1109. res = opt->flags & PARSE_DEFAULT ?
  1110. ast_parse_arg(var->value, flags, field, (int) opt->args[1], (int) opt->args[2], opt->args[3]) :
  1111. ast_parse_arg(var->value, flags, field, (int) opt->args[1], (int) opt->args[2]);
  1112. if (res) {
  1113. if (opt->flags & PARSE_RANGE_DEFAULTS) {
  1114. ast_log(LOG_WARNING, "Failed to set %s=%s. Set to %d instead due to range limit (%d, %d)\n", var->name, var->value, *field, (int) opt->args[1], (int) opt->args[2]);
  1115. res = 0;
  1116. } else if (opt->flags & PARSE_DEFAULT) {
  1117. ast_log(LOG_WARNING, "Failed to set %s=%s, Set to default value %d instead.\n", var->name, var->value, *field);
  1118. res = 0;
  1119. }
  1120. }
  1121. } else if ((opt->flags & PARSE_DEFAULT) && ast_parse_arg(var->value, flags, field, (int) opt->args[1])) {
  1122. ast_log(LOG_WARNING, "Attempted to set %s=%s, but set it to %d instead due to default)\n", var->name, var->value, *field);
  1123. } else {
  1124. res = ast_parse_arg(var->value, flags, field);
  1125. }
  1126. return res;
  1127. }
  1128. /*! \brief Default option handler for unsigned integers
  1129. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1130. * enum aco_option_type in config_options.h
  1131. */
  1132. static int uint_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj) {
  1133. unsigned int *field = (unsigned int *)(obj + opt->args[0]);
  1134. unsigned int flags = PARSE_INT32 | opt->flags;
  1135. int res = 0;
  1136. if (opt->flags & PARSE_IN_RANGE) {
  1137. res = opt->flags & PARSE_DEFAULT ?
  1138. ast_parse_arg(var->value, flags, field, (unsigned int) opt->args[1], (unsigned int) opt->args[2], opt->args[3]) :
  1139. ast_parse_arg(var->value, flags, field, (unsigned int) opt->args[1], (unsigned int) opt->args[2]);
  1140. if (res) {
  1141. if (opt->flags & PARSE_RANGE_DEFAULTS) {
  1142. ast_log(LOG_WARNING, "Failed to set %s=%s. Set to %d instead due to range limit (%d, %d)\n", var->name, var->value, *field, (int) opt->args[1], (int) opt->args[2]);
  1143. res = 0;
  1144. } else if (opt->flags & PARSE_DEFAULT) {
  1145. ast_log(LOG_WARNING, "Failed to set %s=%s, Set to default value %d instead.\n", var->name, var->value, *field);
  1146. res = 0;
  1147. }
  1148. }
  1149. } else if ((opt->flags & PARSE_DEFAULT) && ast_parse_arg(var->value, flags, field, (unsigned int) opt->args[1])) {
  1150. ast_log(LOG_WARNING, "Attempted to set %s=%s, but set it to %u instead due to default)\n", var->name, var->value, *field);
  1151. } else {
  1152. res = ast_parse_arg(var->value, flags, field);
  1153. }
  1154. return res;
  1155. }
  1156. /*! \brief Default option handler for doubles
  1157. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1158. * enum aco_option_type in config_options.h
  1159. */
  1160. static int double_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj) {
  1161. double *field = (double *)(obj + opt->args[0]);
  1162. return ast_parse_arg(var->value, PARSE_DOUBLE | opt->flags, field);
  1163. }
  1164. /*! \brief Default handler for ACLs
  1165. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1166. * enum aco_option_type in config_options.h
  1167. */
  1168. static int acl_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj) {
  1169. struct ast_ha **ha = (struct ast_ha **)(obj + opt->args[0]);
  1170. int error = 0;
  1171. *ha = ast_append_ha(opt->flags ? "permit" : "deny", var->value, *ha, &error);
  1172. return error;
  1173. }
  1174. /*! \brief Default option handler for codec preferences/capabilities
  1175. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1176. * enum aco_option_type in config_options.h
  1177. */
  1178. static int codec_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj) {
  1179. struct ast_codec_pref *pref = (struct ast_codec_pref *)(obj + opt->args[0]);
  1180. struct ast_format_cap **cap = (struct ast_format_cap **)(obj + opt->args[1]);
  1181. return ast_parse_allow_disallow(pref, *cap, var->value, opt->flags);
  1182. }
  1183. /*! \brief Default option handler for stringfields
  1184. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1185. * enum aco_option_type in config_options.h
  1186. */
  1187. static int stringfield_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj)
  1188. {
  1189. ast_string_field *field = (const char **)(obj + opt->args[0]);
  1190. struct ast_string_field_pool **pool = (struct ast_string_field_pool **)(obj + opt->args[1]);
  1191. struct ast_string_field_mgr *mgr = (struct ast_string_field_mgr *)(obj + opt->args[2]);
  1192. if (opt->flags && ast_strlen_zero(var->value)) {
  1193. return -1;
  1194. }
  1195. ast_string_field_ptr_set_by_fields(*pool, *mgr, field, var->value);
  1196. return 0;
  1197. }
  1198. /*! \brief Default option handler for bools (ast_true/ast_false)
  1199. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1200. * enum aco_option_type in config_options.h
  1201. */
  1202. static int bool_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj)
  1203. {
  1204. unsigned int *field = (unsigned int *)(obj + opt->args[0]);
  1205. *field = opt->flags ? ast_true(var->value) : ast_false(var->value);
  1206. return 0;
  1207. }
  1208. /*! \brief Default option handler for bools (ast_true/ast_false) that are stored as flags
  1209. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1210. * enum aco_option_type in config_options.h
  1211. */
  1212. static int boolflag_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj)
  1213. {
  1214. unsigned int *flags_field = (unsigned int *)(obj + opt->args[0]);
  1215. unsigned int val = opt->flags ? ast_true(var->value) : ast_false(var->value);
  1216. unsigned int flag = opt->args[1];
  1217. if (val) {
  1218. *flags_field |= flag;
  1219. } else {
  1220. *flags_field &= ~flag;
  1221. }
  1222. return 0;
  1223. }
  1224. /*! \brief Default handler for ast_sockaddrs
  1225. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1226. * enum aco_option_type in config_options.h
  1227. */
  1228. static int sockaddr_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj)
  1229. {
  1230. struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + opt->args[0]);
  1231. return ast_parse_arg(var->value, PARSE_ADDR | opt->flags, field);
  1232. }
  1233. /*! \brief Default handler for doing nothing
  1234. */
  1235. static int noop_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj)
  1236. {
  1237. return 0;
  1238. }
  1239. /*! \brief Default handler for character arrays
  1240. * \note For a description of the opt->flags and opt->args values, see the documentation for
  1241. * enum aco_option_type in config_options.h
  1242. */
  1243. static int chararray_handler_fn(const struct aco_option *opt, struct ast_variable *var, void *obj)
  1244. {
  1245. char *field = (char *)(obj + opt->args[0]);
  1246. size_t len = opt->args[1];
  1247. if (opt->flags && ast_strlen_zero(var->value)) {
  1248. return -1;
  1249. }
  1250. ast_copy_string(field, var->value, len);
  1251. return 0;
  1252. }