1
|
<?php
|
2
|
/*
|
3
|
* diag_command.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
|
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
|
##|*MATCH=diag_command.php*
|
35
|
##|-PRIV
|
36
|
|
37
|
$allowautocomplete = true;
|
38
|
|
39
|
require_once("guiconfig.inc");
|
40
|
|
41
|
if ($_POST['submit'] == "DOWNLOAD" && file_exists($_POST['dlPath'])) {
|
42
|
session_cache_limiter('public');
|
43
|
$fd = fopen($_POST['dlPath'], "rb");
|
44
|
header("Content-Type: application/octet-stream");
|
45
|
header("Content-Length: " . filesize($_POST['dlPath']));
|
46
|
header("Content-Disposition: attachment; filename=\"" .
|
47
|
trim(htmlentities(basename($_POST['dlPath']))) . "\"");
|
48
|
if (isset($_SERVER['HTTPS'])) {
|
49
|
header('Pragma: ');
|
50
|
header('Cache-Control: ');
|
51
|
} else {
|
52
|
header("Pragma: private");
|
53
|
header("Cache-Control: private, must-revalidate");
|
54
|
}
|
55
|
|
56
|
fpassthru($fd);
|
57
|
exit;
|
58
|
} else if ($_POST['submit'] == "UPLOAD" && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
|
59
|
move_uploaded_file($_FILES['ulfile']['tmp_name'], "/tmp/" . $_FILES['ulfile']['name']);
|
60
|
$ulmsg = sprintf(gettext('Uploaded file to /tmp/%s.'), htmlentities($_FILES['ulfile']['name']));
|
61
|
}
|
62
|
|
63
|
if ($_POST) {
|
64
|
conf_mount_rw();
|
65
|
}
|
66
|
|
67
|
// Function: is Blank
|
68
|
// Returns true or false depending on blankness of argument.
|
69
|
|
70
|
function isBlank($arg) {
|
71
|
return preg_match("/^\s*$/", $arg);
|
72
|
}
|
73
|
|
74
|
// Function: Puts
|
75
|
// Put string, Ruby-style.
|
76
|
|
77
|
function puts($arg) {
|
78
|
echo "$arg\n";
|
79
|
}
|
80
|
|
81
|
// "Constants".
|
82
|
|
83
|
$Version = '';
|
84
|
$ScriptName = $REQUEST['SCRIPT_NAME'];
|
85
|
|
86
|
// Get year.
|
87
|
|
88
|
$arrDT = localtime();
|
89
|
$intYear = $arrDT[5] + 1900;
|
90
|
|
91
|
$pgtitle = array(gettext("Diagnostics"), gettext("Command Prompt"));
|
92
|
include("head.inc");
|
93
|
?>
|
94
|
<script type="text/javascript">
|
95
|
//<![CDATA[
|
96
|
// Create recall buffer array (of encoded strings).
|
97
|
<?php
|
98
|
|
99
|
if (isBlank($_POST['txtRecallBuffer'])) {
|
100
|
puts(" var arrRecallBuffer = new Array;");
|
101
|
} else {
|
102
|
puts(" var arrRecallBuffer = new Array(");
|
103
|
$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
|
104
|
for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
|
105
|
puts(" '" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
|
106
|
}
|
107
|
puts(" '" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
|
108
|
puts(" );");
|
109
|
}
|
110
|
?>
|
111
|
|
112
|
// Set pointer to end of recall buffer.
|
113
|
var intRecallPtr = arrRecallBuffer.length-1;
|
114
|
|
115
|
// Functions to extend String class.
|
116
|
function str_encode() { return escape( this ) }
|
117
|
function str_decode() { return unescape( this ) }
|
118
|
|
119
|
// Extend string class to include encode() and decode() functions.
|
120
|
String.prototype.encode = str_encode
|
121
|
String.prototype.decode = str_decode
|
122
|
|
123
|
// Function: is Blank
|
124
|
// Returns boolean true or false if argument is blank.
|
125
|
function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
|
126
|
|
127
|
// Function: frmExecPlus onSubmit (event handler)
|
128
|
// Builds the recall buffer from the command string on submit.
|
129
|
function frmExecPlus_onSubmit( form ) {
|
130
|
|
131
|
if (!isBlank(form.txtCommand.value)) {
|
132
|
// If this command is repeat of last command, then do not store command.
|
133
|
if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
|
134
|
|
135
|
// Stuff encoded command string into the recall buffer.
|
136
|
if (isBlank(form.txtRecallBuffer.value)) {
|
137
|
form.txtRecallBuffer.value = form.txtCommand.value.encode();
|
138
|
} else {
|
139
|
form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
|
140
|
}
|
141
|
}
|
142
|
|
143
|
return true;
|
144
|
}
|
145
|
|
146
|
// Function: btnRecall onClick (event handler)
|
147
|
// Recalls command buffer going either up or down.
|
148
|
function btnRecall_onClick( form, n ) {
|
149
|
|
150
|
// If nothing in recall buffer, then error.
|
151
|
if (!arrRecallBuffer.length) {
|
152
|
alert('<?=gettext("Nothing to recall"); ?>!');
|
153
|
form.txtCommand.focus();
|
154
|
return;
|
155
|
}
|
156
|
|
157
|
// Increment recall buffer pointer in positive or negative direction
|
158
|
// according to <n>.
|
159
|
intRecallPtr += n;
|
160
|
|
161
|
// Make sure the buffer stays circular.
|
162
|
if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
|
163
|
if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
|
164
|
|
165
|
// Recall the command.
|
166
|
form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
|
167
|
}
|
168
|
|
169
|
// Function: Reset onClick (event handler)
|
170
|
// Resets form on reset button click event.
|
171
|
function Reset_onClick( form ) {
|
172
|
|
173
|
// Reset recall buffer pointer.
|
174
|
intRecallPtr = arrRecallBuffer.length;
|
175
|
|
176
|
// Clear form (could have spaces in it) and return focus ready for cmd.
|
177
|
form.txtCommand.value = '';
|
178
|
form.txtCommand.focus();
|
179
|
|
180
|
return true;
|
181
|
}
|
182
|
//]]>
|
183
|
</script>
|
184
|
<?php
|
185
|
|
186
|
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg)) {
|
187
|
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'));
|
188
|
}
|
189
|
|
190
|
if ($_POST['submit'] == "EXEC" && !isBlank($_POST['txtCommand'])):?>
|
191
|
<div class="panel panel-success responsive">
|
192
|
<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Shell Output - %s'), htmlspecialchars($_POST['txtCommand']))?></h2></div>
|
193
|
<div class="panel-body">
|
194
|
<div class="content">
|
195
|
<?php
|
196
|
putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
|
197
|
putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
|
198
|
$output = array();
|
199
|
exec($_POST['txtCommand'] . ' 2>&1', $output);
|
200
|
|
201
|
$output = implode("\n", $output);
|
202
|
print("<pre>" . htmlspecialchars($output) . "</pre>");
|
203
|
?>
|
204
|
</div>
|
205
|
</div>
|
206
|
</div>
|
207
|
<?php endif; ?>
|
208
|
|
209
|
<form action="diag_command.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
|
210
|
<div class="panel panel-default">
|
211
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
|
212
|
<div class="panel-body">
|
213
|
<div class="content">
|
214
|
<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-7" value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
|
215
|
<br /><br />
|
216
|
<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
|
217
|
|
218
|
<div class="btn-group">
|
219
|
<button type="button" class="btn btn-success btn-sm" name="btnRecallPrev" onclick="btnRecall_onClick( this.form, -1 );" title="<?=gettext("Recall Previous Command")?>">
|
220
|
<i class="fa fa-angle-double-left"></i>
|
221
|
</button>
|
222
|
<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXEC" title="<?=gettext("Execute the entered command")?>">
|
223
|
<i class="fa fa-bolt"></i>
|
224
|
<?=gettext("Execute"); ?>
|
225
|
</button>
|
226
|
<button type="button" class="btn btn-success btn-sm" name="btnRecallNext" onclick="btnRecall_onClick( this.form, 1 );" title="<?=gettext("Recall Next Command")?>">
|
227
|
<i class="fa fa-angle-double-right"></i>
|
228
|
</button>
|
229
|
<button style="margin-left: 10px;" type="button" class="btn btn-default btn-sm" onclick="return Reset_onClick( this.form );" title="<?=gettext("Clear command entry")?>">
|
230
|
<i class="fa fa-undo"></i>
|
231
|
<?=gettext("Clear"); ?>
|
232
|
</button>
|
233
|
</div>
|
234
|
</div>
|
235
|
</div>
|
236
|
</div>
|
237
|
|
238
|
<div class="panel panel-default">
|
239
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download File')?></h2></div>
|
240
|
<div class="panel-body">
|
241
|
<div class="content">
|
242
|
<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?=htmlspecialchars($_GET['dlPath']);?>"/>
|
243
|
<br /><br />
|
244
|
<button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">
|
245
|
<i class="fa fa-download icon-embed-btn"></i>
|
246
|
<?=gettext("Download")?>
|
247
|
</button>
|
248
|
</div>
|
249
|
</div>
|
250
|
</div>
|
251
|
|
252
|
<?php
|
253
|
if ($ulmsg) {
|
254
|
print_info_box($ulmsg, 'success', false);
|
255
|
}
|
256
|
?>
|
257
|
<div class="panel panel-default">
|
258
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload File')?></h2></div>
|
259
|
<div class="panel-body">
|
260
|
<div class="content">
|
261
|
<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
|
262
|
<br />
|
263
|
<button name="submit" type="submit" class="btn btn-primary btn-sm" id="upload" value="UPLOAD">
|
264
|
<i class="fa fa-upload icon-embed-btn"></i>
|
265
|
<?=gettext("Upload")?>
|
266
|
</button>
|
267
|
</div>
|
268
|
</div>
|
269
|
</div>
|
270
|
<?php
|
271
|
// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
|
272
|
// This is intended to prevent bad code from breaking the GUI
|
273
|
if ($_POST['submit'] == "EXECPHP" && !isBlank($_POST['txtPHPCommand'])) {
|
274
|
puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\"><h2 class=\"panel-title\">PHP Response</h2></div>");
|
275
|
|
276
|
$tmpname = tempnam("/tmp", "");
|
277
|
$phpfile = fopen($tmpname, "w");
|
278
|
fwrite($phpfile, "<?php\n");
|
279
|
fwrite($phpfile, "require_once(\"/etc/inc/config.inc\");\n");
|
280
|
fwrite($phpfile, "require_once(\"/etc/inc/functions.inc\");\n\n");
|
281
|
fwrite($phpfile, $_POST['txtPHPCommand'] . "\n");
|
282
|
fwrite($phpfile, "?>\n");
|
283
|
fclose($phpfile);
|
284
|
|
285
|
$output = array();
|
286
|
exec("/usr/local/bin/php " . $tmpname, $output);
|
287
|
|
288
|
unlink($tmpname);
|
289
|
|
290
|
$output = implode("\n", $output);
|
291
|
print("<pre>" . htmlspecialchars($output) . "</pre>");
|
292
|
|
293
|
// echo eval($_POST['txtPHPCommand']);
|
294
|
puts("</div>");
|
295
|
?>
|
296
|
<script type="text/javascript">
|
297
|
//<![CDATA[
|
298
|
events.push(function() {
|
299
|
// Scroll to the bottom of the page to more easily see the results of a PHP exec command
|
300
|
$("html, body").animate({ scrollTop: $(document).height() }, 1000);
|
301
|
});
|
302
|
//]]>
|
303
|
</script>
|
304
|
<?php
|
305
|
}
|
306
|
?>
|
307
|
<div class="panel panel-default responsive">
|
308
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
|
309
|
<div class="panel-body">
|
310
|
<div class="content">
|
311
|
<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
|
312
|
<br />
|
313
|
<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXECPHP" title="<?=gettext("Execute this PHP Code")?>">
|
314
|
<i class="fa fa-bolt"></i>
|
315
|
<?=gettext("Execute")?>
|
316
|
</button>
|
317
|
<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
|
318
|
</div>
|
319
|
</div>
|
320
|
</div>
|
321
|
</form>
|
322
|
|
323
|
<?php
|
324
|
include("foot.inc");
|
325
|
|
326
|
if ($_POST) {
|
327
|
conf_mount_ro();
|
328
|
}
|