1
|
<?php
|
2
|
/*
|
3
|
* diag_command.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2016 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
|
// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
|
259
|
// This is intended to prevent bad code from breaking the GUI
|
260
|
if ($_POST['submit'] == "EXECPHP" && !isBlank($_POST['txtPHPCommand'])) {
|
261
|
puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\"><h2 class=\"panel-title\">PHP Response</h2></div>");
|
262
|
|
263
|
$tmpname = tempnam("/tmp", "");
|
264
|
$phpfile = fopen($tmpname, "w");
|
265
|
fwrite($phpfile, "<?php\n");
|
266
|
fwrite($phpfile, "require_once(\"/etc/inc/config.inc\");\n");
|
267
|
fwrite($phpfile, "require_once(\"/etc/inc/functions.inc\");\n\n");
|
268
|
fwrite($phpfile, $_POST['txtPHPCommand'] . "\n");
|
269
|
fwrite($phpfile, "?>\n");
|
270
|
fclose($phpfile);
|
271
|
|
272
|
$output = array();
|
273
|
exec("/usr/local/bin/php -d log_errors=off " . $tmpname, $output);
|
274
|
|
275
|
unlink($tmpname);
|
276
|
|
277
|
$output = implode("\n", $output);
|
278
|
print("<pre>" . htmlspecialchars($output) . "</pre>");
|
279
|
|
280
|
// echo eval($_POST['txtPHPCommand']);
|
281
|
puts("</div>");
|
282
|
?>
|
283
|
<script type="text/javascript">
|
284
|
//<![CDATA[
|
285
|
events.push(function() {
|
286
|
// Scroll to the bottom of the page to more easily see the results of a PHP exec command
|
287
|
$("html, body").animate({ scrollTop: $(document).height() }, 1000);
|
288
|
});
|
289
|
//]]>
|
290
|
</script>
|
291
|
<?php
|
292
|
}
|
293
|
?>
|
294
|
<div class="panel panel-default responsive">
|
295
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
|
296
|
<div class="panel-body">
|
297
|
<div class="content">
|
298
|
<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
|
299
|
<br />
|
300
|
<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXECPHP" title="<?=gettext("Execute this PHP Code")?>">
|
301
|
<i class="fa fa-bolt"></i>
|
302
|
<?=gettext("Execute")?>
|
303
|
</button>
|
304
|
<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
|
305
|
</div>
|
306
|
</div>
|
307
|
</div>
|
308
|
</form>
|
309
|
|
310
|
<?php
|
311
|
include("foot.inc");
|
312
|
|
313
|
if ($_POST) {
|
314
|
}
|