func_shell.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006-2012, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * SHELL function to return the output generated by a command issued to the system shell.
  19. *
  20. * \note Inspiration and Guidance from Russell! Thank You!
  21. *
  22. * \author Brandon Kruse <bkruse@digium.com>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/app.h"
  36. static int shell_helper(struct ast_channel *chan, const char *cmd, char *data,
  37. char *buf, size_t len)
  38. {
  39. int res = 0;
  40. if (ast_strlen_zero(data)) {
  41. ast_log(LOG_WARNING, "Missing Argument! Example: Set(foo=${SHELL(echo \"bar\")})\n");
  42. return -1;
  43. }
  44. if (chan) {
  45. ast_autoservice_start(chan);
  46. }
  47. if (len >= 1) {
  48. FILE *ptr;
  49. char plbuff[4096];
  50. ptr = popen(data, "r");
  51. if (ptr) {
  52. while (fgets(plbuff, sizeof(plbuff), ptr)) {
  53. strncat(buf, plbuff, len - strlen(buf) - 1);
  54. }
  55. pclose(ptr);
  56. } else {
  57. ast_log(LOG_WARNING, "Failed to execute shell command '%s'\n", data);
  58. res = -1;
  59. }
  60. }
  61. if (chan) {
  62. ast_autoservice_stop(chan);
  63. }
  64. return res;
  65. }
  66. /*** DOCUMENTATION
  67. <function name="SHELL" language="en_US">
  68. <synopsis>
  69. Executes a command using the system shell and captures its output.
  70. </synopsis>
  71. <syntax>
  72. <parameter name="command" required="true">
  73. <para>The command that the shell should execute.</para>
  74. <warning><para>Do not use untrusted strings such as <variable>CALLERID(num)</variable>
  75. or <variable>CALLERID(name)</variable> as part of the command parameters. You
  76. risk a command injection attack executing arbitrary commands if the untrusted
  77. strings aren't filtered to remove dangerous characters. See function
  78. <variable>FILTER()</variable>.</para></warning>
  79. </parameter>
  80. </syntax>
  81. <description>
  82. <para>Collects the output generated by a command executed by the system shell</para>
  83. <para>Example: <literal>Set(foo=${SHELL(echo bar)})</literal></para>
  84. <note>
  85. <para>The command supplied to this function will be executed by the
  86. system's shell, typically specified in the SHELL environment variable. There
  87. are many different system shells available with somewhat different behaviors,
  88. so the output generated by this function may vary between platforms.</para>
  89. <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
  90. is set to <literal>no</literal>, this function can only be executed from the
  91. dialplan, and not directly from external protocols.</para>
  92. </note>
  93. </description>
  94. </function>
  95. ***/
  96. static struct ast_custom_function shell_function = {
  97. .name = "SHELL",
  98. .read = shell_helper,
  99. };
  100. static int unload_module(void)
  101. {
  102. return ast_custom_function_unregister(&shell_function);
  103. }
  104. static int load_module(void)
  105. {
  106. return ast_custom_function_register_escalating(&shell_function, AST_CFE_READ);
  107. }
  108. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Collects the output generated by a command executed by the system shell");