Project

General

Profile

Download (12.6 KB) Statistics
| Branch: | Tag: | Revision:
1 91bf75df Scott Ullrich
<?php
2 2900e518 Scott Ullrich
/*
3 36a844df Stephen Beaver
	diag_command.php
4 2900e518 Scott Ullrich
*/
5 fd9ebcd5 Stephen Beaver
/* ====================================================================
6 6aef15f8 Stephen Beaver
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7 cb41dd63 Renato Botelho
 *
8
 *	Exec+ v1.02-000 - Copyright 2001-2003, All rights reserved
9 fd9ebcd5 Stephen Beaver
 *	Created by technologEase (http://www.technologEase.com)
10
 *	(modified for m0n0wall by Manuel Kasper <mk@neon1.net>)\
11
 *
12 cb41dd63 Renato Botelho
 *	Some or all of this file is based on the m0n0wall project which is
13
 *	Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
14 9da2cf1c Stephen Beaver
 *
15 6aef15f8 Stephen Beaver
 *	Redistribution and use in source and binary forms, with or without modification,
16
 *	are permitted provided that the following conditions are met:
17 fd9ebcd5 Stephen Beaver
 *
18 6aef15f8 Stephen Beaver
 *	1. Redistributions of source code must retain the above copyright notice,
19
 *		this list of conditions and the following disclaimer.
20 fd9ebcd5 Stephen Beaver
 *
21 6aef15f8 Stephen Beaver
 *	2. Redistributions in binary form must reproduce the above copyright
22
 *		notice, this list of conditions and the following disclaimer in
23
 *		the documentation and/or other materials provided with the
24
 *		distribution.
25 fd9ebcd5 Stephen Beaver
 *
26 6aef15f8 Stephen Beaver
 *	3. All advertising materials mentioning features or use of this software
27
 *		must display the following acknowledgment:
28
 *		"This product includes software developed by the pfSense Project
29
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
30 fd9ebcd5 Stephen Beaver
 *
31 6aef15f8 Stephen Beaver
 *	4. The names "pfSense" and "pfSense Project" must not be used to
32
 *		 endorse or promote products derived from this software without
33
 *		 prior written permission. For written permission, please contact
34
 *		 coreteam@pfsense.org.
35 fd9ebcd5 Stephen Beaver
 *
36 6aef15f8 Stephen Beaver
 *	5. Products derived from this software may not be called "pfSense"
37
 *		nor may "pfSense" appear in their names without prior written
38
 *		permission of the Electric Sheep Fencing, LLC.
39 fd9ebcd5 Stephen Beaver
 *
40 6aef15f8 Stephen Beaver
 *	6. Redistributions of any form whatsoever must retain the following
41
 *		acknowledgment:
42 fd9ebcd5 Stephen Beaver
 *
43 6aef15f8 Stephen Beaver
 *	"This product includes software developed by the pfSense Project
44
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
45 919d91f9 Phil Davis
 *
46 6aef15f8 Stephen Beaver
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
47
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
50
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
57
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
58 fd9ebcd5 Stephen Beaver
 *
59 6aef15f8 Stephen Beaver
 *	====================================================================
60 fd9ebcd5 Stephen Beaver
 *
61
 */
62 2900e518 Scott Ullrich
63 6b07c15a Matthew Grooms
##|+PRIV
64
##|*IDENT=page-diagnostics-command
65 5230f468 jim-p
##|*NAME=Diagnostics: Command
66 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Command' page.
67 36a844df Stephen Beaver
##|*MATCH=diag_command.php*
68 6b07c15a Matthew Grooms
##|-PRIV
69
70 7c9a30c8 jim-p
$allowautocomplete = true;
71
72 510e86d1 Scott Ullrich
require("guiconfig.inc");
73 458e0e0b Scott Ullrich
74 59255714 NewEraCracker
if ($_POST['submit'] == "DOWNLOAD" && file_exists($_POST['dlPath'])) {
75 5b237745 Scott Ullrich
	session_cache_limiter('public');
76
	$fd = fopen($_POST['dlPath'], "rb");
77
	header("Content-Type: application/octet-stream");
78
	header("Content-Length: " . filesize($_POST['dlPath']));
79 be4b8e72 Scott Ullrich
	header("Content-Disposition: attachment; filename=\"" .
80 5b237745 Scott Ullrich
		trim(htmlentities(basename($_POST['dlPath']))) . "\"");
81 2d181b70 jim-p
	if (isset($_SERVER['HTTPS'])) {
82
		header('Pragma: ');
83
		header('Cache-Control: ');
84
	} else {
85
		header("Pragma: private");
86
		header("Cache-Control: private, must-revalidate");
87
	}
88 be4b8e72 Scott Ullrich
89 5b237745 Scott Ullrich
	fpassthru($fd);
90
	exit;
91 59255714 NewEraCracker
} else if ($_POST['submit'] == "UPLOAD" && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
92 5b237745 Scott Ullrich
	move_uploaded_file($_FILES['ulfile']['tmp_name'], "/tmp/" . $_FILES['ulfile']['name']);
93 8545adde k-paulius
	$ulmsg = sprintf(gettext('Uploaded file to /tmp/%s.'), htmlentities($_FILES['ulfile']['name']));
94 5b237745 Scott Ullrich
}
95 2900e518 Scott Ullrich
96 41b1ff89 Phil Davis
if ($_POST) {
97 61a90ed5 Scott Ullrich
	conf_mount_rw();
98 41b1ff89 Phil Davis
}
99 74285e13 Scott Ullrich
100 5b237745 Scott Ullrich
// Function: is Blank
101
// Returns true or false depending on blankness of argument.
102
103 41b1ff89 Phil Davis
function isBlank($arg) {
104 6c07db48 Phil Davis
	return preg_match("/^\s*$/", $arg);
105 41b1ff89 Phil Davis
}
106 5b237745 Scott Ullrich
107
// Function: Puts
108
// Put string, Ruby-style.
109
110 41b1ff89 Phil Davis
function puts($arg) {
111
	echo "$arg\n";
112
}
113 5b237745 Scott Ullrich
114
// "Constants".
115
116 45d6ada5 Sjon Hortensius
$Version = '';
117 aa205c3b Ermal
$ScriptName = $REQUEST['SCRIPT_NAME'];
118 5b237745 Scott Ullrich
119
// Get year.
120
121 45d6ada5 Sjon Hortensius
$arrDT = localtime();
122 5b237745 Scott Ullrich
$intYear = $arrDT[5] + 1900;
123
124 a6a6ee00 k-paulius
$pgtitle = array(gettext("Diagnostics"), gettext("Command Prompt"));
125 998abf60 Bill Marquette
include("head.inc");
126 5b237745 Scott Ullrich
?>
127 8fd9052f Colin Fleming
<script type="text/javascript">
128
//<![CDATA[
129 45d6ada5 Sjon Hortensius
	// Create recall buffer array (of encoded strings).
130 5b237745 Scott Ullrich
<?php
131
132 6c07db48 Phil Davis
if (isBlank($_POST['txtRecallBuffer'])) {
133 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array;");
134 5b237745 Scott Ullrich
} else {
135 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array(");
136 6c07db48 Phil Davis
	$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
137
	for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
138 6aef15f8 Stephen Beaver
		puts("		'" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
139 41b1ff89 Phil Davis
	}
140 6aef15f8 Stephen Beaver
	puts("		'" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
141
	puts("	 );");
142 5b237745 Scott Ullrich
}
143
?>
144 45d6ada5 Sjon Hortensius
145 41b1ff89 Phil Davis
	// Set pointer to end of recall buffer.
146
	var intRecallPtr = arrRecallBuffer.length-1;
147 5b237745 Scott Ullrich
148 45d6ada5 Sjon Hortensius
	// Functions to extend String class.
149
	function str_encode() { return escape( this ) }
150
	function str_decode() { return unescape( this ) }
151
152
	// Extend string class to include encode() and decode() functions.
153
	String.prototype.encode = str_encode
154
	String.prototype.decode = str_decode
155
156
	// Function: is Blank
157
	// Returns boolean true or false if argument is blank.
158
	function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
159
160
	// Function: frmExecPlus onSubmit (event handler)
161
	// Builds the recall buffer from the command string on submit.
162
	function frmExecPlus_onSubmit( form ) {
163 5b237745 Scott Ullrich
164 45d6ada5 Sjon Hortensius
		if (!isBlank(form.txtCommand.value)) {
165
			// If this command is repeat of last command, then do not store command.
166
			if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
167
168
			// Stuff encoded command string into the recall buffer.
169 41b1ff89 Phil Davis
			if (isBlank(form.txtRecallBuffer.value)) {
170 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value = form.txtCommand.value.encode();
171 41b1ff89 Phil Davis
			} else {
172 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
173 41b1ff89 Phil Davis
			}
174 45d6ada5 Sjon Hortensius
		}
175
176
		return true;
177
	}
178 5b237745 Scott Ullrich
179 45d6ada5 Sjon Hortensius
	// Function: btnRecall onClick (event handler)
180
	// Recalls command buffer going either up or down.
181
	function btnRecall_onClick( form, n ) {
182 5b237745 Scott Ullrich
183 45d6ada5 Sjon Hortensius
		// If nothing in recall buffer, then error.
184
		if (!arrRecallBuffer.length) {
185 6c07db48 Phil Davis
			alert('<?=gettext("Nothing to recall"); ?>!');
186 45d6ada5 Sjon Hortensius
			form.txtCommand.focus();
187
			return;
188
		}
189 5b237745 Scott Ullrich
190 45d6ada5 Sjon Hortensius
		// Increment recall buffer pointer in positive or negative direction
191
		// according to <n>.
192
		intRecallPtr += n;
193 5b237745 Scott Ullrich
194 45d6ada5 Sjon Hortensius
		// Make sure the buffer stays circular.
195
		if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
196
		if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
197 5b237745 Scott Ullrich
198 45d6ada5 Sjon Hortensius
		// Recall the command.
199
		form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
200
	}
201 5b237745 Scott Ullrich
202 45d6ada5 Sjon Hortensius
	// Function: Reset onClick (event handler)
203
	// Resets form on reset button click event.
204
	function Reset_onClick( form ) {
205 5b237745 Scott Ullrich
206 45d6ada5 Sjon Hortensius
		// Reset recall buffer pointer.
207
		intRecallPtr = arrRecallBuffer.length;
208 5b237745 Scott Ullrich
209 45d6ada5 Sjon Hortensius
		// Clear form (could have spaces in it) and return focus ready for cmd.
210
		form.txtCommand.value = '';
211
		form.txtCommand.focus();
212 5b237745 Scott Ullrich
213 45d6ada5 Sjon Hortensius
		return true;
214
	}
215 fa7855f3 Colin Fleming
//]]>
216 5b237745 Scott Ullrich
</script>
217 45d6ada5 Sjon Hortensius
<?php
218 5b237745 Scott Ullrich
219 947141fd Phil Davis
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg)) {
220 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'));
221 947141fd Phil Davis
}
222 5b237745 Scott Ullrich
223 59255714 NewEraCracker
if ($_POST['submit'] == "EXEC" && !isBlank($_POST['txtCommand'])):?>
224 45d6ada5 Sjon Hortensius
	<div class="panel panel-success responsive">
225 b7edfd3c Phil Davis
		<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Shell Output - %s'), htmlspecialchars($_POST['txtCommand']))?></h2></div>
226 45d6ada5 Sjon Hortensius
		<div class="panel-body">
227 7b91ab09 Jared Dillard
			<div class="content">
228 5b237745 Scott Ullrich
<?php
229 45d6ada5 Sjon Hortensius
	putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
230
	putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
231 6aef15f8 Stephen Beaver
	$output = array();
232
	exec($_POST['txtCommand'] . ' 2>&1', $output);
233 5eb9f6ad NewEraCracker
234
	$output = implode("\n", $output);
235
	print("<pre>" . htmlspecialchars($output) . "</pre>");
236 7b91ab09 Jared Dillard
?>
237
			</div>
238 45d6ada5 Sjon Hortensius
		</div>
239
	</div>
240 fa172bc5 NewEraCracker
<?php endif; ?>
241 5b237745 Scott Ullrich
242 36a844df Stephen Beaver
<form action="diag_command.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
243 45d6ada5 Sjon Hortensius
	<div class="panel panel-default">
244 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
245 45d6ada5 Sjon Hortensius
		<div class="panel-body">
246 7b91ab09 Jared Dillard
			<div class="content">
247 37676f4e jim-p
				<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-7"	 value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
248 7b91ab09 Jared Dillard
				<br /><br />
249
				<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
250 37676f4e jim-p
251
				<div class="btn-group">
252 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")?>">
253 37676f4e jim-p
						<i class="fa fa-angle-double-left"></i>
254
					</button>
255 59255714 NewEraCracker
					<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXEC" title="<?=gettext("Execute the entered command")?>">
256 df2aa83e Stephen Beaver
						<i class="fa fa-bolt"></i>
257 37676f4e jim-p
						<?=gettext("Execute"); ?>
258
					</button>
259 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")?>">
260 37676f4e jim-p
						<i class="fa fa-angle-double-right"></i>
261 6fe069a4 jim-p
					</button>
262 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")?>">
263 37676f4e jim-p
						<i class="fa fa-undo"></i>
264
						<?=gettext("Clear"); ?>
265
					</button>
266
				</div>
267 7b91ab09 Jared Dillard
			</div>
268 45d6ada5 Sjon Hortensius
		</div>
269
	</div>
270
271
	<div class="panel panel-default">
272 3d7a8696 k-paulius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download File')?></h2></div>
273 45d6ada5 Sjon Hortensius
		<div class="panel-body">
274 7b91ab09 Jared Dillard
			<div class="content">
275 5c0ab3cd NewEraCracker
				<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?=htmlspecialchars($_GET['dlPath']);?>"/>
276 7b91ab09 Jared Dillard
				<br /><br />
277 37676f4e jim-p
				<button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">
278
					<i class="fa fa-download icon-embed-btn"></i>
279
					<?=gettext("Download")?>
280
				</button>
281 7b91ab09 Jared Dillard
			</div>
282 45d6ada5 Sjon Hortensius
		</div>
283
	</div>
284 fbcf0037 Scott Ullrich
285 45d6ada5 Sjon Hortensius
<?php
286 947141fd Phil Davis
	if ($ulmsg) {
287 7c945f74 k-paulius
		print_info_box($ulmsg, 'success', false);
288 947141fd Phil Davis
	}
289 45d6ada5 Sjon Hortensius
?>
290
	<div class="panel panel-default">
291 3d7a8696 k-paulius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload File')?></h2></div>
292 45d6ada5 Sjon Hortensius
		<div class="panel-body">
293 7b91ab09 Jared Dillard
			<div class="content">
294
				<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
295
				<br />
296 37676f4e jim-p
				<button name="submit" type="submit" class="btn btn-primary btn-sm" id="upload" value="UPLOAD">
297
					<i class="fa fa-upload icon-embed-btn"></i>
298
					<?=gettext("Upload")?>
299
				</button>
300 7b91ab09 Jared Dillard
			</div>
301 45d6ada5 Sjon Hortensius
		</div>
302
	</div>
303
<?php
304 f5ba2db3 Stephen Beaver
	// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
305
	// This is intended to prevent bad code from breaking the GUI
306 59255714 NewEraCracker
	if ($_POST['submit'] == "EXECPHP" && !isBlank($_POST['txtPHPCommand'])) {
307 3d7a8696 k-paulius
		puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\"><h2 class=\"panel-title\">PHP Response</h2></div>");
308 5eb9f6ad NewEraCracker
309 fc8b158f Stephen Beaver
		$tmpname = tempnam("/tmp", "");
310
		$phpfile = fopen($tmpname, "w");
311 3e115dbf Stephen Beaver
		fwrite($phpfile, "<?php\n");
312
		fwrite($phpfile, "require_once(\"/etc/inc/config.inc\");\n");
313
		fwrite($phpfile, "require_once(\"/etc/inc/functions.inc\");\n\n");
314
		fwrite($phpfile, $_POST['txtPHPCommand'] . "\n");
315
		fwrite($phpfile, "?>\n");
316
		fclose($phpfile);
317
318 5eb9f6ad NewEraCracker
		$output = array();
319 fc8b158f Stephen Beaver
		exec("/usr/local/bin/php " . $tmpname, $output);
320 3e115dbf Stephen Beaver
321 fc8b158f Stephen Beaver
		unlink($tmpname);
322 3e115dbf Stephen Beaver
323 5eb9f6ad NewEraCracker
		$output = implode("\n", $output);
324
		print("<pre>" . htmlspecialchars($output) . "</pre>");
325
326 3e115dbf Stephen Beaver
//		echo eval($_POST['txtPHPCommand']);
327 45d6ada5 Sjon Hortensius
		puts("</div>");
328 bd8eeef9 Stephen Beaver
?>
329 8fd9052f Colin Fleming
<script type="text/javascript">
330
//<![CDATA[
331 947141fd Phil Davis
	events.push(function() {
332 bd8eeef9 Stephen Beaver
		// Scroll to the bottom of the page to more easily see the results of a PHP exec command
333
		$("html, body").animate({ scrollTop: $(document).height() }, 1000);
334
	});
335 8fd9052f Colin Fleming
//]]>
336 bd8eeef9 Stephen Beaver
</script>
337
<?php
338 fbcf0037 Scott Ullrich
}
339 5b237745 Scott Ullrich
?>
340 45d6ada5 Sjon Hortensius
	<div class="panel panel-default responsive">
341 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
342 45d6ada5 Sjon Hortensius
		<div class="panel-body">
343 7b91ab09 Jared Dillard
			<div class="content">
344
				<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
345
				<br />
346 59255714 NewEraCracker
				<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXECPHP" title="<?=gettext("Execute this PHP Code")?>">
347 df2aa83e Stephen Beaver
					<i class="fa fa-bolt"></i>
348 37676f4e jim-p
					<?=gettext("Execute")?>
349
				</button>
350 7b91ab09 Jared Dillard
				<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
351
			</div>
352 45d6ada5 Sjon Hortensius
		</div>
353
	</div>
354 fa7855f3 Colin Fleming
</form>
355 74285e13 Scott Ullrich
356
<?php
357 45d6ada5 Sjon Hortensius
include("foot.inc");
358 74285e13 Scott Ullrich
359 947141fd Phil Davis
if ($_POST) {
360 0bbbafc4 Phil Davis
	conf_mount_ro();
361 947141fd Phil Davis
}