res_clialiases.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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;
  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. generator = NULL;
  103. } else if (!ast_strlen_zero(a->word)) {
  104. struct ast_str *real_cmd = ast_str_alloca(strlen(alias->real_cmd) + strlen(line) + 1);
  105. ast_str_append(&real_cmd, 0, "%s%s", alias->real_cmd, line);
  106. generator = ast_cli_generator(ast_str_buffer(real_cmd), a->word, a->n);
  107. } else {
  108. generator = ast_cli_generator(alias->real_cmd, a->word, a->n);
  109. }
  110. ao2_ref(alias, -1);
  111. return generator;
  112. }
  113. /* If they gave us extra arguments we need to construct a string to pass in */
  114. if (a->argc != e->args) {
  115. struct ast_str *real_cmd = ast_str_alloca(2048);
  116. int i;
  117. ast_str_append(&real_cmd, 0, "%s", alias->real_cmd);
  118. /* Add the additional arguments that have been passed in */
  119. for (i = e->args + 1; i <= a->argc; i++) {
  120. ast_str_append(&real_cmd, 0, " %s", a->argv[i - 1]);
  121. }
  122. ast_cli_command(a->fd, ast_str_buffer(real_cmd));
  123. } else {
  124. ast_cli_command(a->fd, alias->real_cmd);
  125. }
  126. ao2_ref(alias, -1);
  127. return CLI_SUCCESS;
  128. }
  129. /*! \brief CLI Command to display CLI Aliases */
  130. static char *alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  131. {
  132. #define FORMAT "%-50.50s %-50.50s\n"
  133. struct cli_alias *alias;
  134. struct ao2_iterator i;
  135. switch (cmd) {
  136. case CLI_INIT:
  137. e->command = "cli show aliases";
  138. e->usage =
  139. "Usage: cli show aliases\n"
  140. " Displays a list of aliased CLI commands.\n";
  141. return NULL;
  142. case CLI_GENERATE:
  143. return NULL;
  144. }
  145. ast_cli(a->fd, FORMAT, "Alias Command", "Real Command");
  146. i = ao2_iterator_init(cli_aliases, 0);
  147. for (; (alias = ao2_iterator_next(&i)); ao2_ref(alias, -1)) {
  148. ast_cli(a->fd, FORMAT, alias->alias, alias->real_cmd);
  149. }
  150. ao2_iterator_destroy(&i);
  151. return CLI_SUCCESS;
  152. #undef FORMAT
  153. }
  154. /*! \brief CLI commands to interact with things */
  155. static struct ast_cli_entry cli_alias[] = {
  156. AST_CLI_DEFINE(alias_show, "Show CLI command aliases"),
  157. };
  158. /*! \brief Function called to load or reload the configuration file */
  159. static void load_config(int reload)
  160. {
  161. struct ast_config *cfg = NULL;
  162. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  163. struct cli_alias *alias;
  164. struct ast_variable *v, *v1;
  165. if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  166. ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file);
  167. return;
  168. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  169. return;
  170. }
  171. /* Destroy any existing CLI aliases */
  172. if (reload) {
  173. ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, alias_unregister_cb, NULL);
  174. }
  175. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  176. if (strcmp(v->name, "template")) {
  177. ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general");
  178. continue;
  179. }
  180. /* Read in those there CLI aliases */
  181. for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) {
  182. struct cli_alias *existing = ao2_callback(cli_aliases, 0, alias_name_cb, (char*)v1->name);
  183. if (existing) {
  184. ast_log(LOG_WARNING, "Alias '%s' could not be unregistered and has been retained\n",
  185. existing->alias);
  186. ao2_ref(existing, -1);
  187. continue;
  188. }
  189. if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), NULL))) {
  190. continue;
  191. }
  192. alias->alias = ((char *) alias) + sizeof(*alias);
  193. alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1;
  194. strcpy(alias->alias, v1->name);
  195. strcpy(alias->real_cmd, v1->value);
  196. alias->cli_entry.handler = cli_alias_passthrough;
  197. alias->cli_entry.command = alias->alias;
  198. alias->cli_entry.usage = "Aliased CLI Command\n";
  199. if (ast_cli_register(&alias->cli_entry)) {
  200. ao2_ref(alias, -1);
  201. continue;
  202. }
  203. ao2_link(cli_aliases, alias);
  204. ast_verb(2, "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
  205. ao2_ref(alias, -1);
  206. }
  207. }
  208. ast_config_destroy(cfg);
  209. return;
  210. }
  211. /*! \brief Function called to reload the module */
  212. static int reload_module(void)
  213. {
  214. load_config(1);
  215. return 0;
  216. }
  217. /*! \brief Function called to unload the module */
  218. static int unload_module(void)
  219. {
  220. ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, alias_unregister_cb, NULL);
  221. if (ao2_container_count(cli_aliases)) {
  222. ast_log(LOG_ERROR, "Could not unregister all CLI aliases\n");
  223. return -1;
  224. }
  225. ao2_ref(cli_aliases, -1);
  226. ast_cli_unregister_multiple(cli_alias, ARRAY_LEN(cli_alias));
  227. return 0;
  228. }
  229. /*!
  230. * \brief Load the module
  231. *
  232. * Module loading including tests for configuration or dependencies.
  233. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  234. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  235. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  236. * configuration file or other non-critical problem return
  237. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  238. */
  239. static int load_module(void)
  240. {
  241. if (!(cli_aliases = ao2_container_alloc(MAX_ALIAS_BUCKETS, alias_hash_cb, alias_cmp_cb))) {
  242. return AST_MODULE_LOAD_DECLINE;
  243. }
  244. load_config(0);
  245. ast_cli_register_multiple(cli_alias, ARRAY_LEN(cli_alias));
  246. return AST_MODULE_LOAD_SUCCESS;
  247. }
  248. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "CLI Aliases",
  249. .support_level = AST_MODULE_SUPPORT_CORE,
  250. .load = load_module,
  251. .unload = unload_module,
  252. .reload = reload_module,
  253. );