1
|
<?php
|
2
|
/*
|
3
|
* diag_command.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
|
7
|
* All rights reserved.
|
8
|
*
|
9
|
* Exec+ v1.02-000 - Copyright 2001-2003, All rights reserved
|
10
|
* Created by technologEase (http://www.technologEase.com)
|
11
|
* (modified for m0n0wall by Manuel Kasper <mk@neon1.net>)\
|
12
|
*
|
13
|
* originally based on m0n0wall (http://m0n0.ch/wall)
|
14
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
15
|
* All rights reserved.
|
16
|
*
|
17
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
18
|
* you may not use this file except in compliance with the License.
|
19
|
* You may obtain a copy of the License at
|
20
|
*
|
21
|
* http://www.apache.org/licenses/LICENSE-2.0
|
22
|
*
|
23
|
* Unless required by applicable law or agreed to in writing, software
|
24
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
25
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
26
|
* See the License for the specific language governing permissions and
|
27
|
* limitations under the License.
|
28
|
*/
|
29
|
|
30
|
##|+PRIV
|
31
|
##|*IDENT=page-diagnostics-command
|
32
|
##|*NAME=Diagnostics: Command
|
33
|
##|*DESCR=Allow access to the 'Diagnostics: Command' page.
|
34
|
##|*WARN=standard-warning-root
|
35
|
##|*MATCH=diag_command.php*
|
36
|
##|-PRIV
|
37
|
|
38
|
$allowautocomplete = true;
|
39
|
|
40
|
require_once("guiconfig.inc");
|
41
|
|
42
|
if ($_POST['submit'] == "DOWNLOAD" && file_exists($_POST['dlPath'])) {
|
43
|
session_cache_limiter('public');
|
44
|
$fd = fopen($_POST['dlPath'], "rb");
|
45
|
header("Content-Type: application/octet-stream");
|
46
|
header("Content-Length: " . filesize($_POST['dlPath']));
|
47
|
header("Content-Disposition: attachment; filename=\"" .
|
48
|
trim(htmlentities(basename($_POST['dlPath']))) . "\"");
|
49
|
if (isset($_SERVER['HTTPS'])) {
|
50
|
header('Pragma: ');
|
51
|
header('Cache-Control: ');
|
52
|
} else {
|
53
|
header("Pragma: private");
|
54
|
header("Cache-Control: private, must-revalidate");
|
55
|
}
|
56
|
|
57
|
fpassthru($fd);
|
58
|
exit;
|
59
|
} else if ($_POST['submit'] == "UPLOAD" && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
|
60
|
move_uploaded_file($_FILES['ulfile']['tmp_name'], $g["tmp_path"] . "/" . $_FILES['ulfile']['name']);
|
61
|
$ulmsg = sprintf(gettext('Uploaded file to %s.'), $g["tmp_path"] . "/" . htmlentities($_FILES['ulfile']['name']));
|
62
|
}
|
63
|
|
64
|
// Function: is Blank
|
65
|
// Returns true or false depending on blankness of argument.
|
66
|
|
67
|
function isBlank($arg) {
|
68
|
return preg_match("/^\s*$/", $arg);
|
69
|
}
|
70
|
|
71
|
// Function: Puts
|
72
|
// Put string, Ruby-style.
|
73
|
|
74
|
function puts($arg) {
|
75
|
echo "$arg\n";
|
76
|
}
|
77
|
|
78
|
$pgtitle = array(gettext("Diagnostics"), gettext("Command Prompt"));
|
79
|
include("head.inc");
|
80
|
?>
|
81
|
<script type="text/javascript">
|
82
|
//<![CDATA[
|
83
|
// Create recall buffer array (of encoded strings).
|
84
|
<?php
|
85
|
|
86
|
if (isBlank($_POST['txtRecallBuffer'])) {
|
87
|
puts(" var arrRecallBuffer = new Array;");
|
88
|
} else {
|
89
|
puts(" var arrRecallBuffer = new Array(");
|
90
|
$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
|
91
|
for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
|
92
|
puts(" '" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
|
93
|
}
|
94
|
puts(" '" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
|
95
|
puts(" );");
|
96
|
}
|
97
|
?>
|
98
|
|
99
|
// Set pointer to end of recall buffer.
|
100
|
var intRecallPtr = arrRecallBuffer.length-1;
|
101
|
|
102
|
// Functions to extend String class.
|
103
|
function str_encode() { return escape( this ) }
|
104
|
function str_decode() { return unescape( this ) }
|
105
|
|
106
|
// Extend string class to include encode() and decode() functions.
|
107
|
String.prototype.encode = str_encode
|
108
|
String.prototype.decode = str_decode
|
109
|
|
110
|
// Function: is Blank
|
111
|
// Returns boolean true or false if argument is blank.
|
112
|
function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
|
113
|
|
114
|
// Function: frmExecPlus onSubmit (event handler)
|
115
|
// Builds the recall buffer from the command string on submit.
|
116
|
function frmExecPlus_onSubmit( form ) {
|
117
|
|
118
|
if (!isBlank(form.txtCommand.value)) {
|
119
|
// If this command is repeat of last command, then do not store command.
|
120
|
if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
|
121
|
|
122
|
// Stuff encoded command string into the recall buffer.
|
123
|
if (isBlank(form.txtRecallBuffer.value)) {
|
124
|
form.txtRecallBuffer.value = form.txtCommand.value.encode();
|
125
|
} else {
|
126
|
form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
|
127
|
}
|
128
|
}
|
129
|
|
130
|
return true;
|
131
|
}
|
132
|
|
133
|
// Function: btnRecall onClick (event handler)
|
134
|
// Recalls command buffer going either up or down.
|
135
|
function btnRecall_onClick( form, n ) {
|
136
|
|
137
|
// If nothing in recall buffer, then error.
|
138
|
if (!arrRecallBuffer.length) {
|
139
|
alert('<?=gettext("Nothing to recall"); ?>!');
|
140
|
form.txtCommand.focus();
|
141
|
return;
|
142
|
}
|
143
|
|
144
|
// Increment recall buffer pointer in positive or negative direction
|
145
|
// according to <n>.
|
146
|
intRecallPtr += n;
|
147
|
|
148
|
// Make sure the buffer stays circular.
|
149
|
if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
|
150
|
if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
|
151
|
|
152
|
// Recall the command.
|
153
|
form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
|
154
|
}
|
155
|
|
156
|
// Function: Reset onClick (event handler)
|
157
|
// Resets form on reset button click event.
|
158
|
function Reset_onClick( form ) {
|
159
|
|
160
|
// Reset recall buffer pointer.
|
161
|
intRecallPtr = arrRecallBuffer.length;
|
162
|
|
163
|
// Clear form (could have spaces in it) and return focus ready for cmd.
|
164
|
form.txtCommand.value = '';
|
165
|
form.txtCommand.focus();
|
166
|
|
167
|
return true;
|
168
|
}
|
169
|
//]]>
|
170
|
</script>
|
171
|
<?php
|
172
|
|
173
|
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg)) {
|
174
|
print_callout(gettext("The capabilities offered here can be dangerous. No support is available. Use them at your own risk!"), 'danger', gettext('Advanced Users Only'));
|
175
|
}
|
176
|
|
177
|
if ($_POST['submit'] == "EXEC" && !isBlank($_POST['txtCommand'])):?>
|
178
|
<div class="panel panel-success responsive">
|
179
|
<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Shell Output - %s'), htmlspecialchars($_POST['txtCommand']))?></h2></div>
|
180
|
<div class="panel-body">
|
181
|
<div class="content">
|
182
|
<?php
|
183
|
putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
|
184
|
putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
|
185
|
$output = array();
|
186
|
exec($_POST['txtCommand'] . ' 2>&1', $output);
|
187
|
|
188
|
$output = implode("\n", $output);
|
189
|
print("<pre>" . htmlspecialchars($output) . "</pre>");
|
190
|
?>
|
191
|
</div>
|
192
|
</div>
|
193
|
</div>
|
194
|
<?php endif; ?>
|
195
|
|
196
|
<form action="diag_command.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
|
197
|
<div class="panel panel-default">
|
198
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
|
199
|
<div class="panel-body">
|
200
|
<div class="content">
|
201
|
<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-7" value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
|
202
|
<br /><br />
|
203
|
<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
|
204
|
|
205
|
<div class="btn-group">
|
206
|
<button type="button" class="btn btn-success btn-sm" name="btnRecallPrev" onclick="btnRecall_onClick( this.form, -1 );" title="<?=gettext("Recall Previous Command")?>">
|
207
|
<i class="fa fa-angle-double-left"></i>
|
208
|
</button>
|
209
|
<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXEC" title="<?=gettext("Execute the entered command")?>">
|
210
|
<i class="fa fa-bolt"></i>
|
211
|
<?=gettext("Execute"); ?>
|
212
|
</button>
|
213
|
<button type="button" class="btn btn-success btn-sm" name="btnRecallNext" onclick="btnRecall_onClick( this.form, 1 );" title="<?=gettext("Recall Next Command")?>">
|
214
|
<i class="fa fa-angle-double-right"></i>
|
215
|
</button>
|
216
|
<button style="margin-left: 10px;" type="button" class="btn btn-default btn-sm" onclick="return Reset_onClick( this.form );" title="<?=gettext("Clear command entry")?>">
|
217
|
<i class="fa fa-undo"></i>
|
218
|
<?=gettext("Clear"); ?>
|
219
|
</button>
|
220
|
</div>
|
221
|
</div>
|
222
|
</div>
|
223
|
</div>
|
224
|
|
225
|
<div class="panel panel-default">
|
226
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download File')?></h2></div>
|
227
|
<div class="panel-body">
|
228
|
<div class="content">
|
229
|
<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?=htmlspecialchars($_REQUEST['dlPath']);?>"/>
|
230
|
<br /><br />
|
231
|
<button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">
|
232
|
<i class="fa fa-download icon-embed-btn"></i>
|
233
|
<?=gettext("Download")?>
|
234
|
</button>
|
235
|
</div>
|
236
|
</div>
|
237
|
</div>
|
238
|
|
239
|
<?php
|
240
|
if ($ulmsg) {
|
241
|
print_info_box($ulmsg, 'success', false);
|
242
|
}
|
243
|
?>
|
244
|
<div class="panel panel-default">
|
245
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload File')?></h2></div>
|
246
|
<div class="panel-body">
|
247
|
<div class="content">
|
248
|
<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
|
249
|
<br />
|
250
|
<button name="submit" type="submit" class="btn btn-primary btn-sm" id="upload" value="UPLOAD">
|
251
|
<i class="fa fa-upload icon-embed-btn"></i>
|
252
|
<?=gettext("Upload")?>
|
253
|
</button>
|
254
|
</div>
|
255
|
</div>
|
256
|
</div>
|
257
|
<?php
|
258
|
|
259
|
// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
|
260
|
// This is intended to prevent bad code from breaking the GUI
|
261
|
if ($_POST['submit'] == "EXECPHP" && !isBlank($_POST['txtPHPCommand'])) {
|
262
|
|
263
|
safe_mkdir($g['tmp_path_user_code']); //create if doesn't exist
|
264
|
$tmpfile = tempnam($g['tmp_path_user_code'], "");
|
265
|
$phpcode = <<<END_FILE
|
266
|
<?php
|
267
|
require_once("/etc/inc/config.inc");
|
268
|
require_once("/etc/inc/functions.inc");
|
269
|
|
270
|
// USER CODE STARTS HERE:
|
271
|
|
272
|
%s
|
273
|
?>
|
274
|
END_FILE;
|
275
|
$lineno_correction = 6; // line numbering correction, this should be the number of lines added above, BEFORE the user's code
|
276
|
|
277
|
file_put_contents($tmpfile, sprintf($phpcode, $_POST['txtPHPCommand']));
|
278
|
|
279
|
$output = $matches = array();
|
280
|
$retval = 0;
|
281
|
exec("/usr/local/bin/php -d log_errors=off {$tmpfile}", $output, $retval);
|
282
|
|
283
|
puts('<div class="panel panel-success responsive"><div class="panel-heading"><h2 class="panel-title">PHP Response</h2></div>');
|
284
|
|
285
|
// Help user to find bad code line, if it gave an error
|
286
|
$errmsg_found = preg_match("`error.*:.* (?:in|File:) {$tmpfile}(?:\(| on line |, Line: )(\d+)(?:, Message:|\).* eval\(\)'d code|$)`i", implode("\n", $output), $matches);
|
287
|
if ($retval || $errmsg_found) {
|
288
|
/* Trap failed code - test both retval and output message
|
289
|
* Typical messages as at 2.3.x:
|
290
|
* "Parse error: syntax error, ERR_DETAILS in FILE on line NN"
|
291
|
* "PHP ERROR: Type: NN, File: FILE, Line: NN, Message: ERR_DETAILS"
|
292
|
* "Parse error: syntax error, unexpected end of file in FILE(NN) : eval()'d code on line 1" [the number in (..) is the error line]
|
293
|
*/
|
294
|
if ($matches[1] > $lineno_correction) {
|
295
|
$errline = $matches[1] - $lineno_correction;
|
296
|
$errtext = sprintf(gettext('Line %s appears to have generated an error, and has been highlighted. The full response is below.'), $errline);
|
297
|
} else {
|
298
|
$errline = -1;
|
299
|
$errtext = gettext('The code appears to have generated an error, but the line responsible cannot be identified. The full response is below.');
|
300
|
}
|
301
|
$errtext .= '<br/>' . sprintf(gettext('Note that the line number in the full PHP response will be %s lines too large. Nested code and eval() errors may incorrectly point to "line 1".'), $lineno_correction);
|
302
|
$syntax_output = array();
|
303
|
$html = "";
|
304
|
exec("/usr/local/bin/php -s -d log_errors=off {$tmpfile}", $syntax_output);
|
305
|
// Lines 0, 2 and 3 are CSS wrapper for the syntax highlighted code which is at line 1 <br> separated.
|
306
|
$syntax_output = explode("<br />", $syntax_output[1]);
|
307
|
$margin_layout = '%3s %' . strlen(count($syntax_output)) . 'd:';
|
308
|
for ($lineno = 1; $lineno < count($syntax_output) - $lineno_correction; $lineno++) {
|
309
|
$margin = str_replace(' ', ' ', sprintf($margin_layout, ($lineno == $errline ? '>>>' : ''), $lineno));
|
310
|
$html .= "<span style='color:black;backgroundcolor:lightgrey'><tt>{$margin}</tt></span> {$syntax_output[$lineno + $lineno_correction - 1]}<br/>\n";
|
311
|
}
|
312
|
print_info_box($errtext, 'danger');
|
313
|
print "<div style='margin:20px'><b>" . gettext("Error locator:") . "</b>\n";
|
314
|
print "<div id='errdiv' style='height:7em; width:60%; overflow:auto; white-space: nowrap; border:darkgrey solid 1px; margin-top: 20px'>\n";
|
315
|
print $html . "\n</div></div>\n";
|
316
|
}
|
317
|
|
318
|
$output = implode("\n", $output);
|
319
|
print("<pre>" . htmlspecialchars($output) . "</pre>");
|
320
|
|
321
|
// echo eval($_POST['txtPHPCommand']);
|
322
|
|
323
|
puts("</div>");
|
324
|
|
325
|
unlink($tmpfile);
|
326
|
?>
|
327
|
<script type="text/javascript">
|
328
|
//<![CDATA[
|
329
|
events.push(function() {
|
330
|
// scroll error locator if needed (does nothing if no error)
|
331
|
$('#errdiv').scrollTop(<?=max($errline - ($lineno_correction - 3.5), 0);?> * parseFloat($('#errdiv').css('line-height')));
|
332
|
|
333
|
// Scroll to the bottom of the page to more easily see the results of a PHP exec command
|
334
|
$("html, body").animate({ scrollTop: $(document).height() }, 1000);
|
335
|
});
|
336
|
//]]>
|
337
|
</script>
|
338
|
<?php
|
339
|
}
|
340
|
?>
|
341
|
<div class="panel panel-default responsive">
|
342
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
|
343
|
<div class="panel-body">
|
344
|
<div class="content">
|
345
|
<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
|
346
|
<br />
|
347
|
<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXECPHP" title="<?=gettext("Execute this PHP Code")?>">
|
348
|
<i class="fa fa-bolt"></i>
|
349
|
<?=gettext("Execute")?>
|
350
|
</button>
|
351
|
<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
|
352
|
</div>
|
353
|
</div>
|
354
|
</div>
|
355
|
</form>
|
356
|
|
357
|
<?php
|
358
|
include("foot.inc");
|
359
|
|
360
|
if ($_POST) {
|
361
|
}
|