Project

General

Profile

Download (10.6 KB) Statistics
| Branch: | Tag: | Revision:
1 91bf75df Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 2900e518 Scott Ullrich
/*
4 fd9ebcd5 Stephen Beaver
	exec.php
5 2900e518 Scott Ullrich
*/
6 fd9ebcd5 Stephen Beaver
/* ====================================================================
7
 *	Exec+ v1.02-000 - Copyright 2001-2003, All rights reserved
8 6aef15f8 Stephen Beaver
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. 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 6aef15f8 Stephen Beaver
 *	Redistribution and use in source and binary forms, with or without modification,
13
 *	are permitted provided that the following conditions are met:
14 fd9ebcd5 Stephen Beaver
 *
15 6aef15f8 Stephen Beaver
 *	1. Redistributions of source code must retain the above copyright notice,
16
 *		this list of conditions and the following disclaimer.
17 fd9ebcd5 Stephen Beaver
 *
18 6aef15f8 Stephen Beaver
 *	2. Redistributions in binary form must reproduce the above copyright
19
 *		notice, this list of conditions and the following disclaimer in
20
 *		the documentation and/or other materials provided with the
21
 *		distribution.
22 fd9ebcd5 Stephen Beaver
 *
23 6aef15f8 Stephen Beaver
 *	3. All advertising materials mentioning features or use of this software
24
 *		must display the following acknowledgment:
25
 *		"This product includes software developed by the pfSense Project
26
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
27 fd9ebcd5 Stephen Beaver
 *
28 6aef15f8 Stephen Beaver
 *	4. The names "pfSense" and "pfSense Project" must not be used to
29
 *		 endorse or promote products derived from this software without
30
 *		 prior written permission. For written permission, please contact
31
 *		 coreteam@pfsense.org.
32 fd9ebcd5 Stephen Beaver
 *
33 6aef15f8 Stephen Beaver
 *	5. Products derived from this software may not be called "pfSense"
34
 *		nor may "pfSense" appear in their names without prior written
35
 *		permission of the Electric Sheep Fencing, LLC.
36 fd9ebcd5 Stephen Beaver
 *
37 6aef15f8 Stephen Beaver
 *	6. Redistributions of any form whatsoever must retain the following
38
 *		acknowledgment:
39 fd9ebcd5 Stephen Beaver
 *
40 6aef15f8 Stephen Beaver
 *	"This product includes software developed by the pfSense Project
41
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
42 fd9ebcd5 Stephen Beaver
  *
43 6aef15f8 Stephen Beaver
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
44
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
47
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
55 fd9ebcd5 Stephen Beaver
 *
56 6aef15f8 Stephen Beaver
 *	====================================================================
57 fd9ebcd5 Stephen Beaver
 *
58
 */
59 7ac5a4cb Scott Ullrich
/*
60 45d6ada5 Sjon Hortensius
	pfSense_MODULE: shell
61 7ac5a4cb Scott Ullrich
*/
62 2900e518 Scott Ullrich
63 6b07c15a Matthew Grooms
##|+PRIV
64
##|*IDENT=page-diagnostics-command
65
##|*NAME=Diagnostics: Command page
66
##|*DESCR=Allow access to the 'Diagnostics: Command' page.
67
##|*MATCH=exec.php*
68
##|-PRIV
69
70 7c9a30c8 jim-p
$allowautocomplete = true;
71
72 510e86d1 Scott Ullrich
require("guiconfig.inc");
73 458e0e0b Scott Ullrich
74 5b237745 Scott Ullrich
if (($_POST['submit'] == "Download") && file_exists($_POST['dlPath'])) {
75
	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
} else if (($_POST['submit'] == "Upload") && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
92
	move_uploaded_file($_FILES['ulfile']['tmp_name'], "/tmp/" . $_FILES['ulfile']['name']);
93
	$ulmsg = "Uploaded file to /tmp/" . htmlentities($_FILES['ulfile']['name']);
94
	unset($_POST['txtCommand']);
95
}
96 2900e518 Scott Ullrich
97 41b1ff89 Phil Davis
if ($_POST) {
98 61a90ed5 Scott Ullrich
	conf_mount_rw();
99 41b1ff89 Phil Davis
}
100 74285e13 Scott Ullrich
101 5b237745 Scott Ullrich
// Function: is Blank
102
// Returns true or false depending on blankness of argument.
103
104 41b1ff89 Phil Davis
function isBlank($arg) {
105 6c07db48 Phil Davis
	return preg_match("/^\s*$/", $arg);
106 41b1ff89 Phil Davis
}
107 5b237745 Scott Ullrich
108
// Function: Puts
109
// Put string, Ruby-style.
110
111 41b1ff89 Phil Davis
function puts($arg) {
112
	echo "$arg\n";
113
}
114 5b237745 Scott Ullrich
115
// "Constants".
116
117 45d6ada5 Sjon Hortensius
$Version = '';
118 aa205c3b Ermal
$ScriptName = $REQUEST['SCRIPT_NAME'];
119 5b237745 Scott Ullrich
120
// Get year.
121
122 45d6ada5 Sjon Hortensius
$arrDT = localtime();
123 5b237745 Scott Ullrich
$intYear = $arrDT[5] + 1900;
124
125 fa7855f3 Colin Fleming
$closehead = false;
126 6c07db48 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("Execute command"));
127 998abf60 Bill Marquette
include("head.inc");
128 5b237745 Scott Ullrich
?>
129 45d6ada5 Sjon Hortensius
<script>
130
	// Create recall buffer array (of encoded strings).
131 5b237745 Scott Ullrich
<?php
132
133 6c07db48 Phil Davis
if (isBlank($_POST['txtRecallBuffer'])) {
134 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array;");
135 5b237745 Scott Ullrich
} else {
136 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array(");
137 6c07db48 Phil Davis
	$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
138
	for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
139 6aef15f8 Stephen Beaver
		puts("		'" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
140 41b1ff89 Phil Davis
	}
141 6aef15f8 Stephen Beaver
	puts("		'" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
142
	puts("	 );");
143 5b237745 Scott Ullrich
}
144
?>
145 45d6ada5 Sjon Hortensius
146 41b1ff89 Phil Davis
	// Set pointer to end of recall buffer.
147
	var intRecallPtr = arrRecallBuffer.length-1;
148 5b237745 Scott Ullrich
149 45d6ada5 Sjon Hortensius
	// Functions to extend String class.
150
	function str_encode() { return escape( this ) }
151
	function str_decode() { return unescape( this ) }
152
153
	// Extend string class to include encode() and decode() functions.
154
	String.prototype.encode = str_encode
155
	String.prototype.decode = str_decode
156
157
	// Function: is Blank
158
	// Returns boolean true or false if argument is blank.
159
	function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
160
161
	// Function: frmExecPlus onSubmit (event handler)
162
	// Builds the recall buffer from the command string on submit.
163
	function frmExecPlus_onSubmit( form ) {
164 5b237745 Scott Ullrich
165 45d6ada5 Sjon Hortensius
		if (!isBlank(form.txtCommand.value)) {
166
			// If this command is repeat of last command, then do not store command.
167
			if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
168
169
			// Stuff encoded command string into the recall buffer.
170 41b1ff89 Phil Davis
			if (isBlank(form.txtRecallBuffer.value)) {
171 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value = form.txtCommand.value.encode();
172 41b1ff89 Phil Davis
			} else {
173 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
174 41b1ff89 Phil Davis
			}
175 45d6ada5 Sjon Hortensius
		}
176
177
		return true;
178
	}
179 5b237745 Scott Ullrich
180 45d6ada5 Sjon Hortensius
	// Function: btnRecall onClick (event handler)
181
	// Recalls command buffer going either up or down.
182
	function btnRecall_onClick( form, n ) {
183 5b237745 Scott Ullrich
184 45d6ada5 Sjon Hortensius
		// If nothing in recall buffer, then error.
185
		if (!arrRecallBuffer.length) {
186 6c07db48 Phil Davis
			alert('<?=gettext("Nothing to recall"); ?>!');
187 45d6ada5 Sjon Hortensius
			form.txtCommand.focus();
188
			return;
189
		}
190 5b237745 Scott Ullrich
191 45d6ada5 Sjon Hortensius
		// Increment recall buffer pointer in positive or negative direction
192
		// according to <n>.
193
		intRecallPtr += n;
194 5b237745 Scott Ullrich
195 45d6ada5 Sjon Hortensius
		// Make sure the buffer stays circular.
196
		if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
197
		if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
198 5b237745 Scott Ullrich
199 45d6ada5 Sjon Hortensius
		// Recall the command.
200
		form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
201
	}
202 5b237745 Scott Ullrich
203 45d6ada5 Sjon Hortensius
	// Function: Reset onClick (event handler)
204
	// Resets form on reset button click event.
205
	function Reset_onClick( form ) {
206 5b237745 Scott Ullrich
207 45d6ada5 Sjon Hortensius
		// Reset recall buffer pointer.
208
		intRecallPtr = arrRecallBuffer.length;
209 5b237745 Scott Ullrich
210 45d6ada5 Sjon Hortensius
		// Clear form (could have spaces in it) and return focus ready for cmd.
211
		form.txtCommand.value = '';
212
		form.txtCommand.focus();
213 5b237745 Scott Ullrich
214 45d6ada5 Sjon Hortensius
		return true;
215
	}
216 fa7855f3 Colin Fleming
//]]>
217 5b237745 Scott Ullrich
</script>
218 45d6ada5 Sjon Hortensius
<?php
219 5b237745 Scott Ullrich
220 45d6ada5 Sjon Hortensius
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg))
221
	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>');
222 5b237745 Scott Ullrich
223 45d6ada5 Sjon Hortensius
if (!isBlank($_POST['txtCommand'])):?>
224
	<div class="panel panel-success responsive">
225 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title">Shell Output - <?=htmlspecialchars($_POST['txtCommand'])?></h2></div>
226 45d6ada5 Sjon Hortensius
		<div class="panel-body">
227
			<pre>
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
	foreach($output as $line)
234
		print(htmlspecialchars($line) . "\r\n");
235 8c4455dd Stephen Beaver
?></pre>
236
237 45d6ada5 Sjon Hortensius
		</div>
238
	</div>
239
<? endif ?>
240 5b237745 Scott Ullrich
241 45d6ada5 Sjon Hortensius
<form action="exec.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
242
	<div class="panel panel-default">
243 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
244 45d6ada5 Sjon Hortensius
		<div class="panel-body">
245
			<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-4"	 value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
246
			<br /><br />
247
			<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
248
			<input type="button" class="btn btn-default btn-sm" name="btnRecallPrev" value="<" onclick="btnRecall_onClick( this.form, -1 );" />
249
			<input type="submit" class="btn btn-default btn-sm" value="<?=gettext("Execute"); ?>" />
250
			<input type="button" class="btn btn-default btn-sm" name="btnRecallNext" value=">" onclick="btnRecall_onClick( this.form,  1 );" />
251
			<input type="button"  class="btn btn-default btn-sm" value="<?=gettext("Clear"); ?>" onclick="return Reset_onClick( this.form );" />
252
		</div>
253
	</div>
254
255
	<div class="panel panel-default">
256 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download file')?></h2></div>
257 45d6ada5 Sjon Hortensius
		<div class="panel-body">
258 2ccb418a jim-p
			<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?php echo htmlspecialchars($_GET['dlPath']) ?>"/>
259 45d6ada5 Sjon Hortensius
			<br /><br />
260
			<input name="submit" type="submit"	class="btn btn-default btn-sm" id="download" value="<?=gettext("Download"); ?>" />
261
		</div>
262
	</div>
263 fbcf0037 Scott Ullrich
264 45d6ada5 Sjon Hortensius
<?php
265
	if ($ulmsg)
266
		print('<div class="alert alert-success" role="alert">' . $ulmsg .'</div>');
267
?>
268
	<div class="panel panel-default">
269 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload a file')?></h2></div>
270 45d6ada5 Sjon Hortensius
		<div class="panel-body">
271
			<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
272
			<br />
273
			<input name="submit" type="submit" class="btn btn-default btn-sm pull-left" id="upload" value="<?=gettext("Upload"); ?>" />
274
275
		</div>
276
	</div>
277
<?php
278
	if (!isBlank($_POST['txtPHPCommand'])) {
279
		puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\">PHP response</div>");
280
		puts("<pre>");
281
		require_once("config.inc");
282
		require_once("functions.inc");
283
		echo eval($_POST['txtPHPCommand']);
284
		puts("&nbsp;</pre>");
285
		puts("</div>");
286 fbcf0037 Scott Ullrich
}
287 5b237745 Scott Ullrich
?>
288 45d6ada5 Sjon Hortensius
	<div class="panel panel-default responsive">
289 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
290 45d6ada5 Sjon Hortensius
		<div class="panel-body">
291
			<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
292
			<br />
293
			<input type="submit" class="btn btn-default btn-sm" value="<?=gettext("Execute")?>" />
294
			<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
295
		</div>
296
	</div>
297 fa7855f3 Colin Fleming
</form>
298 74285e13 Scott Ullrich
299
<?php
300 45d6ada5 Sjon Hortensius
include("foot.inc");
301 74285e13 Scott Ullrich
302 61a90ed5 Scott Ullrich
if($_POST)
303 0bbbafc4 Phil Davis
	conf_mount_ro();