Project

General

Profile

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