Project

General

Profile

Download (11.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	exec.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *	Exec+ v1.02-000 - Copyright 2001-2003, All rights reserved
9
 *	Created by technologEase (http://www.technologEase.com)
10
 *	(modified for m0n0wall by Manuel Kasper <mk@neon1.net>)\
11
 *
12
 *	Some or all of this file is based on the m0n0wall project which is
13
 *	Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
14
 *
15
 *	Redistribution and use in source and binary forms, with or without modification,
16
 *	are permitted provided that the following conditions are met:
17
 *
18
 *	1. Redistributions of source code must retain the above copyright notice,
19
 *		this list of conditions and the following disclaimer.
20
 *
21
 *	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
 *
26
 *	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
 *
31
 *	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
 *
36
 *	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
 *
40
 *	6. Redistributions of any form whatsoever must retain the following
41
 *		acknowledgment:
42
 *
43
 *	"This product includes software developed by the pfSense Project
44
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
45
 *
46
 *	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
 *
59
 *	====================================================================
60
 *
61
 */
62

    
63
##|+PRIV
64
##|*IDENT=page-diagnostics-command
65
##|*NAME=Diagnostics: Command
66
##|*DESCR=Allow access to the 'Diagnostics: Command' page.
67
##|*MATCH=exec.php*
68
##|-PRIV
69

    
70
$allowautocomplete = true;
71

    
72
require("guiconfig.inc");
73

    
74
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
	header("Content-Disposition: attachment; filename=\"" .
80
		trim(htmlentities(basename($_POST['dlPath']))) . "\"");
81
	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

    
89
	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

    
97
if ($_POST) {
98
	conf_mount_rw();
99
}
100

    
101
// Function: is Blank
102
// Returns true or false depending on blankness of argument.
103

    
104
function isBlank($arg) {
105
	return preg_match("/^\s*$/", $arg);
106
}
107

    
108
// Function: Puts
109
// Put string, Ruby-style.
110

    
111
function puts($arg) {
112
	echo "$arg\n";
113
}
114

    
115
// "Constants".
116

    
117
$Version = '';
118
$ScriptName = $REQUEST['SCRIPT_NAME'];
119

    
120
// Get year.
121

    
122
$arrDT = localtime();
123
$intYear = $arrDT[5] + 1900;
124

    
125
$closehead = false;
126
$pgtitle = array(gettext("Diagnostics"), gettext("Execute command"));
127
include("head.inc");
128
?>
129
<script type="text/javascript">
130
//<![CDATA[
131
	// Create recall buffer array (of encoded strings).
132
<?php
133

    
134
if (isBlank($_POST['txtRecallBuffer'])) {
135
	puts("	 var arrRecallBuffer = new Array;");
136
} else {
137
	puts("	 var arrRecallBuffer = new Array(");
138
	$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
139
	for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
140
		puts("		'" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
141
	}
142
	puts("		'" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
143
	puts("	 );");
144
}
145
?>
146

    
147
	// Set pointer to end of recall buffer.
148
	var intRecallPtr = arrRecallBuffer.length-1;
149

    
150
	// Functions to extend String class.
151
	function str_encode() { return escape( this ) }
152
	function str_decode() { return unescape( this ) }
153

    
154
	// Extend string class to include encode() and decode() functions.
155
	String.prototype.encode = str_encode
156
	String.prototype.decode = str_decode
157

    
158
	// Function: is Blank
159
	// Returns boolean true or false if argument is blank.
160
	function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
161

    
162
	// Function: frmExecPlus onSubmit (event handler)
163
	// Builds the recall buffer from the command string on submit.
164
	function frmExecPlus_onSubmit( form ) {
165

    
166
		if (!isBlank(form.txtCommand.value)) {
167
			// If this command is repeat of last command, then do not store command.
168
			if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
169

    
170
			// Stuff encoded command string into the recall buffer.
171
			if (isBlank(form.txtRecallBuffer.value)) {
172
				form.txtRecallBuffer.value = form.txtCommand.value.encode();
173
			} else {
174
				form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
175
			}
176
		}
177

    
178
		return true;
179
	}
180

    
181
	// Function: btnRecall onClick (event handler)
182
	// Recalls command buffer going either up or down.
183
	function btnRecall_onClick( form, n ) {
184

    
185
		// If nothing in recall buffer, then error.
186
		if (!arrRecallBuffer.length) {
187
			alert('<?=gettext("Nothing to recall"); ?>!');
188
			form.txtCommand.focus();
189
			return;
190
		}
191

    
192
		// Increment recall buffer pointer in positive or negative direction
193
		// according to <n>.
194
		intRecallPtr += n;
195

    
196
		// Make sure the buffer stays circular.
197
		if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
198
		if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
199

    
200
		// Recall the command.
201
		form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
202
	}
203

    
204
	// Function: Reset onClick (event handler)
205
	// Resets form on reset button click event.
206
	function Reset_onClick( form ) {
207

    
208
		// Reset recall buffer pointer.
209
		intRecallPtr = arrRecallBuffer.length;
210

    
211
		// Clear form (could have spaces in it) and return focus ready for cmd.
212
		form.txtCommand.value = '';
213
		form.txtCommand.focus();
214

    
215
		return true;
216
	}
217
//]]>
218
</script>
219
<?php
220

    
221
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg)) {
222
	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>');
223
}
224

    
225
if (!isBlank($_POST['txtCommand'])):?>
226
	<div class="panel panel-success responsive">
227
		<div class="panel-heading"><h2 class="panel-title">Shell Output - <?=htmlspecialchars($_POST['txtCommand'])?></h2></div>
228
		<div class="panel-body">
229
			<div class="content">
230
				<pre>
231
<?php
232
	putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
233
	putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
234
	$output = array();
235
	exec($_POST['txtCommand'] . ' 2>&1', $output);
236
	foreach ($output as $line) {
237
		print(htmlspecialchars($line) . "\r\n");
238
	}
239
?>
240
				</pre>
241
			</div>
242
		</div>
243
	</div>
244
<? endif ?>
245

    
246
<form action="exec.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
247
	<div class="panel panel-default">
248
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
249
		<div class="panel-body">
250
			<div class="content">
251
				<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-4"	 value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
252
				<br /><br />
253
				<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
254
				<input type="button" class="btn btn-default btn-sm" name="btnRecallPrev" value="<" onclick="btnRecall_onClick( this.form, -1 );" />
255
				<button type="submit" class="btn btn-default btn-sm" value="EXEC"><?=gettext("Execute"); ?></button>
256
				<input type="button" class="btn btn-default btn-sm" name="btnRecallNext" value=">" onclick="btnRecall_onClick( this.form,  1 );" />
257
				<input type="button" class="btn btn-default btn-sm" value="<?=gettext("Clear"); ?>" onclick="return Reset_onClick( this.form );" />
258
			</div>
259
		</div>
260
	</div>
261

    
262
	<div class="panel panel-default">
263
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download file')?></h2></div>
264
		<div class="panel-body">
265
			<div class="content">
266
				<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?php echo htmlspecialchars($_GET['dlPath']) ?>"/>
267
				<br /><br />
268
				<button name="submit" type="submit" class="btn btn-default btn-sm" id="download" value="DOWNLOAD"><?=gettext("Download")?></button>
269
			</div>
270
		</div>
271
	</div>
272

    
273
<?php
274
	if ($ulmsg) {
275
		print('<div class="alert alert-success" role="alert">' . $ulmsg .'</div>');
276
	}
277
?>
278
	<div class="panel panel-default">
279
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload file')?></h2></div>
280
		<div class="panel-body">
281
			<div class="content">
282
				<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
283
				<br />
284
				<button name="submit" type="submit" class="btn btn-default btn-sm" id="upload" value="UPLOAD"><?=gettext("Upload")?></button>
285
			</div>
286
		</div>
287
	</div>
288
<?php
289
	// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
290
	// This is intended to prevent bad code from breaking the GUI
291
	if (!isBlank($_POST['txtPHPCommand'])) {
292
		puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\">PHP response</div>");
293
		puts("<pre>");
294
		$tmpname = tempnam("/tmp", "");
295
		$phpfile = fopen($tmpname, "w");
296
		fwrite($phpfile, "<?php\n");
297
		fwrite($phpfile, "require_once(\"/etc/inc/config.inc\");\n");
298
		fwrite($phpfile, "require_once(\"/etc/inc/functions.inc\");\n\n");
299
		fwrite($phpfile, $_POST['txtPHPCommand'] . "\n");
300
		fwrite($phpfile, "?>\n");
301
		fclose($phpfile);
302

    
303
		exec("/usr/local/bin/php " . $tmpname, $output);
304

    
305
		for ($i=0; $i < count($output); $i++) {
306
			print($output[$i] . "\n");
307
		}
308

    
309
		unlink($tmpname);
310

    
311
//		echo eval($_POST['txtPHPCommand']);
312
		puts("&nbsp;</pre>");
313
		puts("</div>");
314
?>
315
<script type="text/javascript">
316
//<![CDATA[
317
	events.push(function() {
318
		// Scroll to the bottom of the page to more easily see the results of a PHP exec command
319
		$("html, body").animate({ scrollTop: $(document).height() }, 1000);
320
	});
321
//]]>
322
</script>
323
<?php
324
}
325
?>
326
	<div class="panel panel-default responsive">
327
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
328
		<div class="panel-body">
329
			<div class="content">
330
				<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
331
				<br />
332
				<input type="submit" class="btn btn-default btn-sm" value="<?=gettext("Execute")?>" />
333
				<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
334
			</div>
335
		</div>
336
	</div>
337
</form>
338

    
339
<?php
340
include("foot.inc");
341

    
342
if ($_POST) {
343
	conf_mount_ro();
344
}
(34-34/228)