Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_system.c
Go to the documentation of this file.
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
19/*! \file
20 *
21 * \brief Execute arbitrary system commands
22 *
23 * \author Mark Spencer <markster@digium.com>
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"
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
114
115static char *app = "System";
116
117static char *app2 = "TrySystem";
118
119static char *chanvar = "SYSTEMSTATUS";
120
121static 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
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
166
167 return res;
168}
169
170static int system_exec(struct ast_channel *chan, const char *data)
171{
172 return system_exec_helper(chan, data, -1);
173}
174
175static int trysystem_exec(struct ast_channel *chan, const char *data)
176{
177 return system_exec_helper(chan, data, 0);
178}
179
180static int unload_module(void)
181{
182 int res;
183
186
187 return res;
188}
189
190static int load_module(void)
191{
192 int res;
193
196
197 return res;
198}
199
200AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");
static int system_exec(struct ast_channel *chan, const char *data)
Definition: app_system.c:170
static char * app2
Definition: app_system.c:117
static struct ast_threadstorage buf_buf
Definition: app_system.c:113
static char * app
Definition: app_system.c:115
static int load_module(void)
Definition: app_system.c:190
static int trysystem_exec(struct ast_channel *chan, const char *data)
Definition: app_system.c:175
static int unload_module(void)
Definition: app_system.c:180
static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
Definition: app_system.c:121
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
int ast_str_get_encoded_str(struct ast_str **str, int maxlen, const char *stream)
Decode a stream of encoded control or extended ASCII characters.
Definition: main/app.c:3175
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
Definition: extconf.c:829
#define LOG_NOTICE
#define LOG_WARNING
int errno
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Core PBX routines and definitions.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
String manipulation functions.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:909
Main Channel structure associated with a channel.
Support for dynamic strings.
Definition: strings.h:623
structure for queuing ARI channel variable setting
Definition: control.c:711
Definitions to aid in the use of thread local storage.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:86