res_clialiases.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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. *
  20. * \brief CLI Aliases
  21. *
  22. * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
  23. *
  24. * This module provides the capability to create aliases to other
  25. * CLI commands.
  26. */
  27. /*! \li \ref res_clialiases.c uses the configuration file \ref cli_aliases.conf
  28. * \addtogroup configuration_file Configuration Files
  29. */
  30. /*!
  31. * \page cli_aliases.conf cli_aliases.conf
  32. * \verbinclude cli_aliases.conf.sample
  33. */
  34. /*** MODULEINFO
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/module.h"
  40. #include "asterisk/config.h"
  41. #include "asterisk/cli.h"
  42. #include "asterisk/astobj2.h"
  43. /*! Maximum number of buckets for CLI aliases */
  44. #define MAX_ALIAS_BUCKETS 53
  45. /*! Configuration file used for this application */
  46. static const char config_file[] = "cli_aliases.conf";
  47. struct cli_alias {
  48. struct ast_cli_entry cli_entry; /*!< Actual CLI structure used for this alias */
  49. char *alias; /*!< CLI Alias */
  50. char *real_cmd; /*!< Actual CLI command it is aliased to */
  51. };
  52. static struct ao2_container *cli_aliases;
  53. /*! \brief Hashing function used for aliases */
  54. static int alias_hash_cb(const void *obj, const int flags)
  55. {
  56. const struct cli_alias *alias = obj;
  57. return ast_str_hash(alias->cli_entry.command);
  58. }
  59. /*! \brief Comparison function used for aliases */
  60. static int alias_cmp_cb(void *obj, void *arg, int flags)
  61. {
  62. const struct cli_alias *alias0 = obj, *alias1 = arg;
  63. return (alias0->cli_entry.command == alias1->cli_entry.command ? CMP_MATCH | CMP_STOP : 0);
  64. }
  65. /*! \brief Callback for unregistering an alias */
  66. static int alias_unregister_cb(void *obj, void *arg, int flags)
  67. {
  68. struct cli_alias *alias = obj;
  69. /* Unregister the CLI entry from the core */
  70. ast_cli_unregister(&alias->cli_entry);
  71. /* We can determine if this worked or not by looking at the cli_entry itself */
  72. return !alias->cli_entry.command ? CMP_MATCH : 0;
  73. }
  74. /*! \brief Callback for finding an alias based on name */
  75. static int alias_name_cb(void *obj, void *arg, int flags)
  76. {
  77. struct cli_alias *alias = obj;
  78. char *name = arg;
  79. return !strcmp(alias->alias, name) ? CMP_MATCH | CMP_STOP : 0;
  80. }
  81. /*! \brief Function which passes through an aliased CLI command to the real one */
  82. static char *cli_alias_passthrough(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  83. {
  84. struct cli_alias *alias;
  85. struct cli_alias tmp = {
  86. .cli_entry.command = e->command,
  87. };
  88. char *generator = NULL;
  89. const char *line;
  90. /* Try to find the alias based on the CLI entry */
  91. if (!(alias = ao2_find(cli_aliases, &tmp, OBJ_POINTER))) {
  92. return 0;
  93. }
  94. switch (cmd) {
  95. case CLI_INIT:
  96. ao2_ref(alias, -1);
  97. return NULL;
  98. case CLI_GENERATE:
  99. line = a->line;
  100. line += (strlen(alias->alias));
  101. if (strncasecmp(alias->alias, alias->real_cmd, strlen(alias->alias))) {
  102. struct ast_str *real_cmd = ast_str_alloca(strlen(alias->real_cmd) + strlen(line) + 1);
  103. ast_str_append(&real_cmd, 0, "%s%s", alias->real_cmd, line);
  104. generator = ast_cli_generator(ast_str_buffer(real_cmd), a->word, a->n);
  105. }
  106. ao2_ref(alias, -1);
  107. return generator;
  108. }
  109. /* If they gave us extra arguments we need to construct a string to pass in */
  110. if (a->argc != e->args) {
  111. struct ast_str *real_cmd = ast_str_alloca(2048);
  112. int i;
  113. ast_str_append(&real_cmd, 0, "%s", alias->real_cmd);
  114. /* Add the additional arguments that have been passed in */
  115. for (i = e->args + 1; i <= a->argc; i++) {
  116. ast_str_append(&real_cmd, 0, " %s", a->argv[i - 1]);
  117. }
  118. ast_cli_command(a->fd, ast_str_buffer(real_cmd));
  119. } else {
  120. ast_cli_command(a->fd, alias->real_cmd);
  121. }
  122. ao2_ref(alias, -1);
  123. return CLI_SUCCESS;
  124. }
  125. /*! \brief CLI Command to display CLI Aliases */
  126. static char *alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  127. {
  128. #define FORMAT "%-50.50s %-50.50s\n"
  129. struct cli_alias *alias;
  130. struct ao2_iterator i;
  131. switch (cmd) {
  132. case CLI_INIT:
  133. e->command = "cli show aliases";
  134. e->usage =
  135. "Usage: cli show aliases\n"
  136. " Displays a list of aliased CLI commands.\n";
  137. return NULL;
  138. case CLI_GENERATE:
  139. return NULL;
  140. }
  141. ast_cli(a->fd, FORMAT, "Alias Command", "Real Command");
  142. i = ao2_iterator_init(cli_aliases, 0);
  143. for (; (alias = ao2_iterator_next(&i)); ao2_ref(alias, -1)) {
  144. ast_cli(a->fd, FORMAT, alias->alias, alias->real_cmd);
  145. }
  146. ao2_iterator_destroy(&i);
  147. return CLI_SUCCESS;
  148. #undef FORMAT
  149. }
  150. /*! \brief CLI commands to interact with things */
  151. static struct ast_cli_entry cli_alias[] = {
  152. AST_CLI_DEFINE(alias_show, "Show CLI command aliases"),
  153. };
  154. /*! \brief Function called to load or reload the configuration file */
  155. static void load_config(int reload)
  156. {
  157. struct ast_config *cfg = NULL;
  158. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  159. struct cli_alias *alias;
  160. struct ast_variable *v, *v1;
  161. if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  162. ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file);
  163. return;
  164. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  165. return;
  166. }
  167. /* Destroy any existing CLI aliases */
  168. if (reload) {
  169. ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, alias_unregister_cb, NULL);
  170. }
  171. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  172. if (strcmp(v->name, "template")) {
  173. ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general");
  174. continue;
  175. }
  176. /* Read in those there CLI aliases */
  177. for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) {
  178. struct cli_alias *existing = ao2_callback(cli_aliases, 0, alias_name_cb, (char*)v1->name);
  179. if (existing) {
  180. ast_log(LOG_WARNING, "Alias '%s' could not be unregistered and has been retained\n",
  181. existing->alias);
  182. ao2_ref(existing, -1);
  183. continue;
  184. }
  185. if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), NULL))) {
  186. continue;
  187. }
  188. alias->alias = ((char *) alias) + sizeof(*alias);
  189. alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1;
  190. strcpy(alias->alias, v1->name);
  191. strcpy(alias->real_cmd, v1->value);
  192. alias->cli_entry.handler = cli_alias_passthrough;
  193. alias->cli_entry.command = alias->alias;
  194. alias->cli_entry.usage = "Aliased CLI Command\n";
  195. if (ast_cli_register(&alias->cli_entry)) {
  196. ao2_ref(alias, -1);
  197. continue;
  198. }
  199. ao2_link(cli_aliases, alias);
  200. ast_verb(2, "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
  201. ao2_ref(alias, -1);
  202. }
  203. }
  204. ast_config_destroy(cfg);
  205. return;
  206. }
  207. /*! \brief Function called to reload the module */
  208. static int reload_module(void)
  209. {
  210. load_config(1);
  211. return 0;
  212. }
  213. /*! \brief Function called to unload the module */
  214. static int unload_module(void)
  215. {
  216. ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, alias_unregister_cb, NULL);
  217. if (ao2_container_count(cli_aliases)) {
  218. ast_log(LOG_ERROR, "Could not unregister all CLI aliases\n");
  219. return -1;
  220. }
  221. ao2_ref(cli_aliases, -1);
  222. ast_cli_unregister_multiple(cli_alias, ARRAY_LEN(cli_alias));
  223. return 0;
  224. }
  225. /*!
  226. * \brief Load the module
  227. *
  228. * Module loading including tests for configuration or dependencies.
  229. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  230. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  231. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  232. * configuration file or other non-critical problem return
  233. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  234. */
  235. static int load_module(void)
  236. {
  237. cli_aliases = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  238. MAX_ALIAS_BUCKETS, alias_hash_cb, NULL, alias_cmp_cb);
  239. if (!cli_aliases) {
  240. return AST_MODULE_LOAD_DECLINE;
  241. }
  242. load_config(0);
  243. ast_cli_register_multiple(cli_alias, ARRAY_LEN(cli_alias));
  244. return AST_MODULE_LOAD_SUCCESS;
  245. }
  246. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "CLI Aliases",
  247. .support_level = AST_MODULE_SUPPORT_CORE,
  248. .load = load_module,
  249. .unload = unload_module,
  250. .reload = reload_module,
  251. );