Debian Codegraph @

apps/app_system.c

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