Merge "Add semantic tags to license info text"
[lhc/web/wiklou.git] / includes / shell / Shell.php
1 <?php
2 /**
3 * Class used for executing shell commands
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Shell;
24
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * Executes shell commands
29 *
30 * @since 1.30
31 *
32 * Use call chaining with this class for expressiveness:
33 * $result = Shell::command( 'some command' )
34 * ->input( 'foo' )
35 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
36 * ->limits( [ 'time' => 300 ] )
37 * ->execute();
38 *
39 * ... = $result->getExitCode();
40 * ... = $result->getStdout();
41 * ... = $result->getStderr();
42 */
43 class Shell {
44
45 /**
46 * Apply a default set of restrictions for improved
47 * security out of the box.
48 *
49 * Equal to NO_ROOT | SECCOMP | PRIVATE_DEV | NO_LOCALSETTINGS
50 *
51 * @note This value will change over time to provide increased security
52 * by default, and is not guaranteed to be backwards-compatible.
53 * @since 1.31
54 */
55 const RESTRICT_DEFAULT = 39;
56
57 /**
58 * Disallow any root access. Any setuid binaries
59 * will be run without elevated access.
60 *
61 * @since 1.31
62 */
63 const NO_ROOT = 1;
64
65 /**
66 * Use seccomp to block dangerous syscalls
67 * @see <https://en.wikipedia.org/wiki/seccomp>
68 *
69 * @since 1.31
70 */
71 const SECCOMP = 2;
72
73 /**
74 * Create a private /dev
75 *
76 * @since 1.31
77 */
78 const PRIVATE_DEV = 4;
79
80 /**
81 * Restrict the request to have no
82 * network access
83 *
84 * @since 1.31
85 */
86 const NO_NETWORK = 8;
87
88 /**
89 * Deny execve syscall with seccomp
90 * @see <https://en.wikipedia.org/wiki/exec_(system_call)>
91 *
92 * @since 1.31
93 */
94 const NO_EXECVE = 16;
95
96 /**
97 * Deny access to LocalSettings.php (MW_CONFIG_FILE)
98 *
99 * @since 1.31
100 */
101 const NO_LOCALSETTINGS = 32;
102
103 /**
104 * Returns a new instance of Command class
105 *
106 * @param string|string[] $command String or array of strings representing the command to
107 * be executed, each value will be escaped.
108 * Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
109 * @return Command
110 */
111 public static function command( $command ) {
112 $args = func_get_args();
113 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
114 // If only one argument has been passed, and that argument is an array,
115 // treat it as a list of arguments
116 $args = reset( $args );
117 }
118 $command = MediaWikiServices::getInstance()
119 ->getShellCommandFactory()
120 ->create();
121
122 return $command->params( $args );
123 }
124
125 /**
126 * Check if this class is effectively disabled via php.ini config
127 *
128 * @return bool
129 */
130 public static function isDisabled() {
131 static $disabled = null;
132
133 if ( is_null( $disabled ) ) {
134 if ( !function_exists( 'proc_open' ) ) {
135 wfDebug( "proc_open() is disabled\n" );
136 $disabled = true;
137 } else {
138 $disabled = false;
139 }
140 }
141
142 return $disabled;
143 }
144
145 /**
146 * Version of escapeshellarg() that works better on Windows.
147 *
148 * Originally, this fixed the incorrect use of single quotes on Windows
149 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
150 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
151 *
152 * @param string $args,... strings to escape and glue together, or a single array of
153 * strings parameter. Null values are ignored.
154 * @return string
155 */
156 public static function escape( /* ... */ ) {
157 $args = func_get_args();
158 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
159 // If only one argument has been passed, and that argument is an array,
160 // treat it as a list of arguments
161 $args = reset( $args );
162 }
163
164 $first = true;
165 $retVal = '';
166 foreach ( $args as $arg ) {
167 if ( $arg === null ) {
168 continue;
169 }
170 if ( !$first ) {
171 $retVal .= ' ';
172 } else {
173 $first = false;
174 }
175
176 if ( wfIsWindows() ) {
177 // Escaping for an MSVC-style command line parser and CMD.EXE
178 // Refs:
179 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
180 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
181 // * T15518
182 // * CR r63214
183 // Double the backslashes before any double quotes. Escape the double quotes.
184 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
185 $arg = '';
186 $iteration = 0;
187 foreach ( $tokens as $token ) {
188 if ( $iteration % 2 == 1 ) {
189 // Delimiter, a double quote preceded by zero or more slashes
190 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
191 } elseif ( $iteration % 4 == 2 ) {
192 // ^ in $token will be outside quotes, need to be escaped
193 $arg .= str_replace( '^', '^^', $token );
194 } else { // $iteration % 4 == 0
195 // ^ in $token will appear inside double quotes, so leave as is
196 $arg .= $token;
197 }
198 $iteration++;
199 }
200 // Double the backslashes before the end of the string, because
201 // we will soon add a quote
202 $m = [];
203 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
204 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
205 }
206
207 // Add surrounding quotes
208 $retVal .= '"' . $arg . '"';
209 } else {
210 $retVal .= escapeshellarg( $arg );
211 }
212 }
213 return $retVal;
214 }
215 }