app_system.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, 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. *
  20. * \brief Execute arbitrary system commands
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/app.h"
  34. #include "asterisk/channel.h" /* autoservice */
  35. #include "asterisk/strings.h"
  36. #include "asterisk/threadstorage.h"
  37. /*** DOCUMENTATION
  38. <application name="System" language="en_US">
  39. <synopsis>
  40. Execute a system command.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="command" required="true">
  44. <para>Command to execute</para>
  45. <warning><para>Do not use untrusted strings such as <variable>CALLERID(num)</variable>
  46. or <variable>CALLERID(name)</variable> as part of the command parameters. You
  47. risk a command injection attack executing arbitrary commands if the untrusted
  48. strings aren't filtered to remove dangerous characters. See function
  49. <variable>FILTER()</variable>.</para></warning>
  50. </parameter>
  51. </syntax>
  52. <description>
  53. <para>Executes a command by using system(). If the command
  54. fails, the console should report a fallthrough.</para>
  55. <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
  56. <variablelist>
  57. <variable name="SYSTEMSTATUS">
  58. <value name="FAILURE">
  59. Could not execute the specified command.
  60. </value>
  61. <value name="SUCCESS">
  62. Specified command successfully executed.
  63. </value>
  64. </variable>
  65. </variablelist>
  66. </description>
  67. </application>
  68. <application name="TrySystem" language="en_US">
  69. <synopsis>
  70. Try executing a system command.
  71. </synopsis>
  72. <syntax>
  73. <parameter name="command" required="true">
  74. <para>Command to execute</para>
  75. <warning><para>Do not use untrusted strings such as <variable>CALLERID(num)</variable>
  76. or <variable>CALLERID(name)</variable> as part of the command parameters. You
  77. risk a command injection attack executing arbitrary commands if the untrusted
  78. strings aren't filtered to remove dangerous characters. See function
  79. <variable>FILTER()</variable>.</para></warning>
  80. </parameter>
  81. </syntax>
  82. <description>
  83. <para>Executes a command by using system().</para>
  84. <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
  85. <variablelist>
  86. <variable name="SYSTEMSTATUS">
  87. <value name="FAILURE">
  88. Could not execute the specified command.
  89. </value>
  90. <value name="SUCCESS">
  91. Specified command successfully executed.
  92. </value>
  93. <value name="APPERROR">
  94. Specified command successfully executed, but returned error code.
  95. </value>
  96. </variable>
  97. </variablelist>
  98. </description>
  99. </application>
  100. ***/
  101. AST_THREADSTORAGE(buf_buf);
  102. static char *app = "System";
  103. static char *app2 = "TrySystem";
  104. static char *chanvar = "SYSTEMSTATUS";
  105. static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
  106. {
  107. int res = 0;
  108. struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
  109. char *cbuf;
  110. if (ast_strlen_zero(data)) {
  111. ast_log(LOG_WARNING, "System requires an argument(command)\n");
  112. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  113. return failmode;
  114. }
  115. ast_autoservice_start(chan);
  116. /* Do our thing here */
  117. ast_str_get_encoded_str(&buf, 0, (char *) data);
  118. cbuf = ast_str_buffer(buf);
  119. if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
  120. cbuf[ast_str_strlen(buf) - 1] = '\0';
  121. cbuf++;
  122. ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
  123. }
  124. res = ast_safe_system(cbuf);
  125. if ((res < 0) && (errno != ECHILD)) {
  126. ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
  127. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  128. res = failmode;
  129. } else if (res == 127) {
  130. ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
  131. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  132. res = failmode;
  133. } else {
  134. if (res < 0)
  135. res = 0;
  136. if (res != 0)
  137. pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
  138. else
  139. pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
  140. res = 0;
  141. }
  142. ast_autoservice_stop(chan);
  143. return res;
  144. }
  145. static int system_exec(struct ast_channel *chan, const char *data)
  146. {
  147. return system_exec_helper(chan, data, -1);
  148. }
  149. static int trysystem_exec(struct ast_channel *chan, const char *data)
  150. {
  151. return system_exec_helper(chan, data, 0);
  152. }
  153. static int unload_module(void)
  154. {
  155. int res;
  156. res = ast_unregister_application(app);
  157. res |= ast_unregister_application(app2);
  158. return res;
  159. }
  160. static int load_module(void)
  161. {
  162. int res;
  163. res = ast_register_application_xml(app2, trysystem_exec);
  164. res |= ast_register_application_xml(app, system_exec);
  165. return res;
  166. }
  167. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");