Allow semicolons to be escaped, when passing arguments to the System command.
[asterisk/asterisk.git] / apps / app_system.c
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 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/pbx.h"
33 #include "asterisk/module.h"
34 #include "asterisk/app.h"
35 #include "asterisk/channel.h"   /* autoservice */
36 #include "asterisk/strings.h"
37 #include "asterisk/threadstorage.h"
38
39 /*** DOCUMENTATION
40         <application name="System" language="en_US">
41                 <synopsis>
42                         Execute a system command.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="command" required="true">
46                                 <para>Command to execute</para>
47                         </parameter>
48                 </syntax>
49                 <description>
50                         <para>Executes a command  by  using  system(). If the command
51                         fails, the console should report a fallthrough.</para>
52                         <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
53                         <variablelist>
54                                 <variable name="SYSTEMSTATUS">
55                                         <value name="FAILURE">
56                                                 Could not execute the specified command.
57                                         </value>
58                                         <value name="SUCCESS">
59                                                 Specified command successfully executed.
60                                         </value>
61                                 </variable>
62                         </variablelist>
63                 </description>
64         </application>
65         <application name="TrySystem" language="en_US">
66                 <synopsis>
67                         Try executing a system command.
68                 </synopsis>
69                 <syntax>
70                         <parameter name="command" required="true">
71                                 <para>Command to execute</para>
72                         </parameter>
73                 </syntax>
74                 <description>
75                         <para>Executes a command  by  using  system().</para>
76                         <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
77                         <variablelist>
78                                 <variable name="SYSTEMSTATUS">
79                                         <value name="FAILURE">
80                                                 Could not execute the specified command.
81                                         </value>
82                                         <value name="SUCCESS">
83                                                 Specified command successfully executed.
84                                         </value>
85                                         <value name="APPERROR">
86                                                 Specified command successfully executed, but returned error code.
87                                         </value>
88                                 </variable>
89                         </variablelist>
90                 </description>
91         </application>
92
93  ***/
94
95 AST_THREADSTORAGE(buf_buf);
96
97 static char *app = "System";
98
99 static char *app2 = "TrySystem";
100
101 static char *chanvar = "SYSTEMSTATUS";
102
103 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
104 {
105         int res = 0;
106         struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
107         
108         if (ast_strlen_zero(data)) {
109                 ast_log(LOG_WARNING, "System requires an argument(command)\n");
110                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
111                 return failmode;
112         }
113
114         ast_autoservice_start(chan);
115
116         /* Do our thing here */
117         ast_str_get_encoded_str(&buf, 0, (char *) data);
118         res = ast_safe_system(ast_str_buffer(buf));
119
120         if ((res < 0) && (errno != ECHILD)) {
121                 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
122                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
123                 res = failmode;
124         } else if (res == 127) {
125                 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
126                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
127                 res = failmode;
128         } else {
129                 if (res < 0) 
130                         res = 0;
131                 if (res != 0)
132                         pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
133                 else
134                         pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
135                 res = 0;
136         } 
137
138         ast_autoservice_stop(chan);
139
140         return res;
141 }
142
143 static int system_exec(struct ast_channel *chan, void *data)
144 {
145         return system_exec_helper(chan, data, -1);
146 }
147
148 static int trysystem_exec(struct ast_channel *chan, void *data)
149 {
150         return system_exec_helper(chan, data, 0);
151 }
152
153 static int unload_module(void)
154 {
155         int res;
156
157         res = ast_unregister_application(app);
158         res |= ast_unregister_application(app2);
159
160         return res;
161 }
162
163 static int load_module(void)
164 {
165         int res;
166
167         res = ast_register_application_xml(app2, trysystem_exec);
168         res |= ast_register_application_xml(app, system_exec);
169
170         return res;
171 }
172
173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");