Project

General

Profile

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