Project

General

Profile

Download (11.6 KB) Statistics
| Branch: | Tag: | Revision:
1 91bf75df Scott Ullrich
<?php
2 2900e518 Scott Ullrich
/*
3 fd9ebcd5 Stephen Beaver
	exec.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 7ac5a4cb Scott Ullrich
/*
63 45d6ada5 Sjon Hortensius
	pfSense_MODULE: shell
64 7ac5a4cb Scott Ullrich
*/
65 2900e518 Scott Ullrich
66 6b07c15a Matthew Grooms
##|+PRIV
67
##|*IDENT=page-diagnostics-command
68
##|*NAME=Diagnostics: Command page
69
##|*DESCR=Allow access to the 'Diagnostics: Command' page.
70
##|*MATCH=exec.php*
71
##|-PRIV
72
73 7c9a30c8 jim-p
$allowautocomplete = true;
74
75 510e86d1 Scott Ullrich
require("guiconfig.inc");
76 458e0e0b Scott Ullrich
77 bd8eeef9 Stephen Beaver
if (($_POST['submit'] == "DOWNLOAD") && file_exists($_POST['dlPath'])) {
78 5b237745 Scott Ullrich
	session_cache_limiter('public');
79
	$fd = fopen($_POST['dlPath'], "rb");
80
	header("Content-Type: application/octet-stream");
81
	header("Content-Length: " . filesize($_POST['dlPath']));
82 be4b8e72 Scott Ullrich
	header("Content-Disposition: attachment; filename=\"" .
83 5b237745 Scott Ullrich
		trim(htmlentities(basename($_POST['dlPath']))) . "\"");
84 2d181b70 jim-p
	if (isset($_SERVER['HTTPS'])) {
85
		header('Pragma: ');
86
		header('Cache-Control: ');
87
	} else {
88
		header("Pragma: private");
89
		header("Cache-Control: private, must-revalidate");
90
	}
91 be4b8e72 Scott Ullrich
92 5b237745 Scott Ullrich
	fpassthru($fd);
93
	exit;
94 bd8eeef9 Stephen Beaver
} else if (($_POST['submit'] == "UPLOAD") && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
95 5b237745 Scott Ullrich
	move_uploaded_file($_FILES['ulfile']['tmp_name'], "/tmp/" . $_FILES['ulfile']['name']);
96
	$ulmsg = "Uploaded file to /tmp/" . htmlentities($_FILES['ulfile']['name']);
97
	unset($_POST['txtCommand']);
98
}
99 2900e518 Scott Ullrich
100 41b1ff89 Phil Davis
if ($_POST) {
101 61a90ed5 Scott Ullrich
	conf_mount_rw();
102 41b1ff89 Phil Davis
}
103 74285e13 Scott Ullrich
104 5b237745 Scott Ullrich
// Function: is Blank
105
// Returns true or false depending on blankness of argument.
106
107 41b1ff89 Phil Davis
function isBlank($arg) {
108 6c07db48 Phil Davis
	return preg_match("/^\s*$/", $arg);
109 41b1ff89 Phil Davis
}
110 5b237745 Scott Ullrich
111
// Function: Puts
112
// Put string, Ruby-style.
113
114 41b1ff89 Phil Davis
function puts($arg) {
115
	echo "$arg\n";
116
}
117 5b237745 Scott Ullrich
118
// "Constants".
119
120 45d6ada5 Sjon Hortensius
$Version = '';
121 aa205c3b Ermal
$ScriptName = $REQUEST['SCRIPT_NAME'];
122 5b237745 Scott Ullrich
123
// Get year.
124
125 45d6ada5 Sjon Hortensius
$arrDT = localtime();
126 5b237745 Scott Ullrich
$intYear = $arrDT[5] + 1900;
127
128 fa7855f3 Colin Fleming
$closehead = false;
129 6c07db48 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("Execute command"));
130 998abf60 Bill Marquette
include("head.inc");
131 5b237745 Scott Ullrich
?>
132 45d6ada5 Sjon Hortensius
<script>
133
	// Create recall buffer array (of encoded strings).
134 5b237745 Scott Ullrich
<?php
135
136 6c07db48 Phil Davis
if (isBlank($_POST['txtRecallBuffer'])) {
137 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array;");
138 5b237745 Scott Ullrich
} else {
139 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array(");
140 6c07db48 Phil Davis
	$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
141
	for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
142 6aef15f8 Stephen Beaver
		puts("		'" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
143 41b1ff89 Phil Davis
	}
144 6aef15f8 Stephen Beaver
	puts("		'" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
145
	puts("	 );");
146 5b237745 Scott Ullrich
}
147
?>
148 45d6ada5 Sjon Hortensius
149 41b1ff89 Phil Davis
	// Set pointer to end of recall buffer.
150
	var intRecallPtr = arrRecallBuffer.length-1;
151 5b237745 Scott Ullrich
152 45d6ada5 Sjon Hortensius
	// Functions to extend String class.
153
	function str_encode() { return escape( this ) }
154
	function str_decode() { return unescape( this ) }
155
156
	// Extend string class to include encode() and decode() functions.
157
	String.prototype.encode = str_encode
158
	String.prototype.decode = str_decode
159
160
	// Function: is Blank
161
	// Returns boolean true or false if argument is blank.
162
	function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
163
164
	// Function: frmExecPlus onSubmit (event handler)
165
	// Builds the recall buffer from the command string on submit.
166
	function frmExecPlus_onSubmit( form ) {
167 5b237745 Scott Ullrich
168 45d6ada5 Sjon Hortensius
		if (!isBlank(form.txtCommand.value)) {
169
			// If this command is repeat of last command, then do not store command.
170
			if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
171
172
			// Stuff encoded command string into the recall buffer.
173 41b1ff89 Phil Davis
			if (isBlank(form.txtRecallBuffer.value)) {
174 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value = form.txtCommand.value.encode();
175 41b1ff89 Phil Davis
			} else {
176 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
177 41b1ff89 Phil Davis
			}
178 45d6ada5 Sjon Hortensius
		}
179
180
		return true;
181
	}
182 5b237745 Scott Ullrich
183 45d6ada5 Sjon Hortensius
	// Function: btnRecall onClick (event handler)
184
	// Recalls command buffer going either up or down.
185
	function btnRecall_onClick( form, n ) {
186 5b237745 Scott Ullrich
187 45d6ada5 Sjon Hortensius
		// If nothing in recall buffer, then error.
188
		if (!arrRecallBuffer.length) {
189 6c07db48 Phil Davis
			alert('<?=gettext("Nothing to recall"); ?>!');
190 45d6ada5 Sjon Hortensius
			form.txtCommand.focus();
191
			return;
192
		}
193 5b237745 Scott Ullrich
194 45d6ada5 Sjon Hortensius
		// Increment recall buffer pointer in positive or negative direction
195
		// according to <n>.
196
		intRecallPtr += n;
197 5b237745 Scott Ullrich
198 45d6ada5 Sjon Hortensius
		// Make sure the buffer stays circular.
199
		if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
200
		if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
201 5b237745 Scott Ullrich
202 45d6ada5 Sjon Hortensius
		// Recall the command.
203
		form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
204
	}
205 5b237745 Scott Ullrich
206 45d6ada5 Sjon Hortensius
	// Function: Reset onClick (event handler)
207
	// Resets form on reset button click event.
208
	function Reset_onClick( form ) {
209 5b237745 Scott Ullrich
210 45d6ada5 Sjon Hortensius
		// Reset recall buffer pointer.
211
		intRecallPtr = arrRecallBuffer.length;
212 5b237745 Scott Ullrich
213 45d6ada5 Sjon Hortensius
		// Clear form (could have spaces in it) and return focus ready for cmd.
214
		form.txtCommand.value = '';
215
		form.txtCommand.focus();
216 5b237745 Scott Ullrich
217 45d6ada5 Sjon Hortensius
		return true;
218
	}
219 fa7855f3 Colin Fleming
//]]>
220 5b237745 Scott Ullrich
</script>
221 45d6ada5 Sjon Hortensius
<?php
222 5b237745 Scott Ullrich
223 45d6ada5 Sjon Hortensius
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg))
224
	print('<div class="alert alert-warning" role="alert">'.gettext("The capabilities offered here can be dangerous. No support is available. Use them at your own risk!").'</div>');
225 5b237745 Scott Ullrich
226 45d6ada5 Sjon Hortensius
if (!isBlank($_POST['txtCommand'])):?>
227
	<div class="panel panel-success responsive">
228 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title">Shell Output - <?=htmlspecialchars($_POST['txtCommand'])?></h2></div>
229 45d6ada5 Sjon Hortensius
		<div class="panel-body">
230
			<pre>
231 5b237745 Scott Ullrich
<?php
232 45d6ada5 Sjon Hortensius
	putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
233
	putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
234 6aef15f8 Stephen Beaver
	$output = array();
235
	exec($_POST['txtCommand'] . ' 2>&1', $output);
236
	foreach($output as $line)
237
		print(htmlspecialchars($line) . "\r\n");
238 8c4455dd Stephen Beaver
?></pre>
239
240 45d6ada5 Sjon Hortensius
		</div>
241
	</div>
242
<? endif ?>
243 5b237745 Scott Ullrich
244 45d6ada5 Sjon Hortensius
<form action="exec.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
245
	<div class="panel panel-default">
246 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
247 45d6ada5 Sjon Hortensius
		<div class="panel-body">
248
			<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-4"	 value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
249
			<br /><br />
250
			<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
251
			<input type="button" class="btn btn-default btn-sm" name="btnRecallPrev" value="<" onclick="btnRecall_onClick( this.form, -1 );" />
252 bd8eeef9 Stephen Beaver
			<button type="submit" class="btn btn-default btn-sm" value="EXEC"><?=gettext("Execute"); ?></button>
253 45d6ada5 Sjon Hortensius
			<input type="button" class="btn btn-default btn-sm" name="btnRecallNext" value=">" onclick="btnRecall_onClick( this.form,  1 );" />
254 bd8eeef9 Stephen Beaver
			<input type="button" class="btn btn-default btn-sm" value="<?=gettext("Clear"); ?>" onclick="return Reset_onClick( this.form );" />
255 45d6ada5 Sjon Hortensius
		</div>
256
	</div>
257
258
	<div class="panel panel-default">
259 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download file')?></h2></div>
260 45d6ada5 Sjon Hortensius
		<div class="panel-body">
261 2ccb418a jim-p
			<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?php echo htmlspecialchars($_GET['dlPath']) ?>"/>
262 45d6ada5 Sjon Hortensius
			<br /><br />
263 bd8eeef9 Stephen Beaver
			<button name="submit" type="submit" class="btn btn-default btn-sm" id="download" value="DOWNLOAD"><?=gettext("Download")?></button>
264 45d6ada5 Sjon Hortensius
		</div>
265
	</div>
266 fbcf0037 Scott Ullrich
267 45d6ada5 Sjon Hortensius
<?php
268
	if ($ulmsg)
269
		print('<div class="alert alert-success" role="alert">' . $ulmsg .'</div>');
270
?>
271
	<div class="panel panel-default">
272 06831ceb Phil Davis
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload file')?></h2></div>
273 45d6ada5 Sjon Hortensius
		<div class="panel-body">
274
			<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
275
			<br />
276 bd8eeef9 Stephen Beaver
			<button name="submit" type="submit" class="btn btn-default btn-sm pull-left" id="upload" value="UPLOAD"><?=gettext("Upload")?></button>
277 45d6ada5 Sjon Hortensius
278
		</div>
279
	</div>
280
<?php
281 f5ba2db3 Stephen Beaver
	// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
282
	// This is intended to prevent bad code from breaking the GUI
283 45d6ada5 Sjon Hortensius
	if (!isBlank($_POST['txtPHPCommand'])) {
284
		puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\">PHP response</div>");
285
		puts("<pre>");
286 fc8b158f Stephen Beaver
		$tmpname = tempnam("/tmp", "");
287
		$phpfile = fopen($tmpname, "w");
288 3e115dbf Stephen Beaver
		fwrite($phpfile, "<?php\n");
289
		fwrite($phpfile, "require_once(\"/etc/inc/config.inc\");\n");
290
		fwrite($phpfile, "require_once(\"/etc/inc/functions.inc\");\n\n");
291
		fwrite($phpfile, $_POST['txtPHPCommand'] . "\n");
292
		fwrite($phpfile, "?>\n");
293
		fclose($phpfile);
294
295 fc8b158f Stephen Beaver
		exec("/usr/local/bin/php " . $tmpname, $output);
296 3e115dbf Stephen Beaver
297
		for ($i=0; $i < count($output); $i++) {
298
			print($output[$i] . "\n");
299
		}
300
301 fc8b158f Stephen Beaver
		unlink($tmpname);
302 3e115dbf Stephen Beaver
303
//		echo eval($_POST['txtPHPCommand']);
304 45d6ada5 Sjon Hortensius
		puts("&nbsp;</pre>");
305
		puts("</div>");
306 bd8eeef9 Stephen Beaver
?>
307
<script>
308
	events.push(function(){
309
		// Scroll to the bottom of the page to more easily see the results of a PHP exec command
310
		$("html, body").animate({ scrollTop: $(document).height() }, 1000);
311
	});
312
</script>
313
<?php
314 fbcf0037 Scott Ullrich
}
315 5b237745 Scott Ullrich
?>
316 45d6ada5 Sjon Hortensius
	<div class="panel panel-default responsive">
317 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
318 45d6ada5 Sjon Hortensius
		<div class="panel-body">
319
			<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
320
			<br />
321
			<input type="submit" class="btn btn-default btn-sm" value="<?=gettext("Execute")?>" />
322
			<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
323
		</div>
324
	</div>
325 fa7855f3 Colin Fleming
</form>
326 74285e13 Scott Ullrich
327
<?php
328 45d6ada5 Sjon Hortensius
include("foot.inc");
329 74285e13 Scott Ullrich
330 61a90ed5 Scott Ullrich
if($_POST)
331 0bbbafc4 Phil Davis
	conf_mount_ro();