084e10e793527bfe7ef326f0918ab18cb6207222
[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 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
35 * ->limits( [ 'time' => 300 ] )
36 * ->execute();
37 *
38 * ... = $result->getExitCode();
39 * ... = $result->getStdout();
40 * ... = $result->getStderr();
41 */
42 class Shell {
43
44 /**
45 * Apply a default set of restrictions for improved
46 * security out of the box.
47 *
48 * Equal to NO_ROOT | SECCOMP | PRIVATE_DEV
49 *
50 * @note This value will change over time to provide increased security
51 * by default, and is not guaranteed to be backwards-compatible.
52 * @since 1.31
53 */
54 const RESTRICT_DEFAULT = 7;
55
56 /**
57 * Disallow any root access. Any setuid binaries
58 * will be run without elevated access.
59 *
60 * @since 1.31
61 */
62 const NO_ROOT = 1;
63
64 /**
65 * Use seccomp to block dangerous syscalls
66 * @see <https://en.wikipedia.org/wiki/seccomp>
67 *
68 * @since 1.31
69 */
70 const SECCOMP = 2;
71
72 /**
73 * Create a private /dev
74 *
75 * @since 1.31
76 */
77 const PRIVATE_DEV = 4;
78
79 /**
80 * Restrict the request to have no
81 * network access
82 *
83 * @since 1.31
84 */
85 const NO_NETWORK = 8;
86
87 /**
88 * Deny execve syscall with seccomp
89 * @see <https://en.wikipedia.org/wiki/exec_(system_call)>
90 *
91 * @since 1.31
92 */
93 const NO_EXECVE = 16;
94
95 /**
96 * Returns a new instance of Command class
97 *
98 * @param string|string[] $command String or array of strings representing the command to
99 * be executed, each value will be escaped.
100 * Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
101 * @return Command
102 */
103 public static function command( $command ) {
104 $args = func_get_args();
105 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
106 // If only one argument has been passed, and that argument is an array,
107 // treat it as a list of arguments
108 $args = reset( $args );
109 }
110 $command = MediaWikiServices::getInstance()
111 ->getShellCommandFactory()
112 ->create();
113
114 return $command->params( $args );
115 }
116
117 /**
118 * Check if this class is effectively disabled via php.ini config
119 *
120 * @return bool
121 */
122 public static function isDisabled() {
123 static $disabled = null;
124
125 if ( is_null( $disabled ) ) {
126 if ( !function_exists( 'proc_open' ) ) {
127 wfDebug( "proc_open() is disabled\n" );
128 $disabled = true;
129 } else {
130 $disabled = false;
131 }
132 }
133
134 return $disabled;
135 }
136
137 /**
138 * Version of escapeshellarg() that works better on Windows.
139 *
140 * Originally, this fixed the incorrect use of single quotes on Windows
141 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
142 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
143 *
144 * @param string $args,... strings to escape and glue together, or a single array of
145 * strings parameter. Null values are ignored.
146 * @return string
147 */
148 public static function escape( /* ... */ ) {
149 $args = func_get_args();
150 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
151 // If only one argument has been passed, and that argument is an array,
152 // treat it as a list of arguments
153 $args = reset( $args );
154 }
155
156 $first = true;
157 $retVal = '';
158 foreach ( $args as $arg ) {
159 if ( $arg === null ) {
160 continue;
161 }
162 if ( !$first ) {
163 $retVal .= ' ';
164 } else {
165 $first = false;
166 }
167
168 if ( wfIsWindows() ) {
169 // Escaping for an MSVC-style command line parser and CMD.EXE
170 // Refs:
171 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
172 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
173 // * T15518
174 // * CR r63214
175 // Double the backslashes before any double quotes. Escape the double quotes.
176 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
177 $arg = '';
178 $iteration = 0;
179 foreach ( $tokens as $token ) {
180 if ( $iteration % 2 == 1 ) {
181 // Delimiter, a double quote preceded by zero or more slashes
182 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
183 } elseif ( $iteration % 4 == 2 ) {
184 // ^ in $token will be outside quotes, need to be escaped
185 $arg .= str_replace( '^', '^^', $token );
186 } else { // $iteration % 4 == 0
187 // ^ in $token will appear inside double quotes, so leave as is
188 $arg .= $token;
189 }
190 $iteration++;
191 }
192 // Double the backslashes before the end of the string, because
193 // we will soon add a quote
194 $m = [];
195 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
196 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
197 }
198
199 // Add surrounding quotes
200 $retVal .= '"' . $arg . '"';
201 } else {
202 $retVal .= escapeshellarg( $arg );
203 }
204 }
205 return $retVal;
206 }
207 }