Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1 91bf75df Scott Ullrich
<?php
2 2900e518 Scott Ullrich
/*
3 c5d81585 Renato Botelho
 * diag_command.php
4 cb41dd63 Renato Botelho
 *
5 c5d81585 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 c5d81585 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 c5d81585 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 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
18
 * you may not use this file except in compliance with the License.
19
 * You may obtain a copy of the License at
20 fd9ebcd5 Stephen Beaver
 *
21 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
22 fd9ebcd5 Stephen Beaver
 *
23 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
24
 * distributed under the License is distributed on an "AS IS" BASIS,
25
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26
 * See the License for the specific language governing permissions and
27
 * limitations under the License.
28 fd9ebcd5 Stephen Beaver
 */
29 2900e518 Scott Ullrich
30 6b07c15a Matthew Grooms
##|+PRIV
31
##|*IDENT=page-diagnostics-command
32 5230f468 jim-p
##|*NAME=Diagnostics: Command
33 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Command' page.
34 36a844df Stephen Beaver
##|*MATCH=diag_command.php*
35 6b07c15a Matthew Grooms
##|-PRIV
36
37 7c9a30c8 jim-p
$allowautocomplete = true;
38
39 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
40 458e0e0b Scott Ullrich
41 59255714 NewEraCracker
if ($_POST['submit'] == "DOWNLOAD" && file_exists($_POST['dlPath'])) {
42 5b237745 Scott Ullrich
	session_cache_limiter('public');
43
	$fd = fopen($_POST['dlPath'], "rb");
44
	header("Content-Type: application/octet-stream");
45
	header("Content-Length: " . filesize($_POST['dlPath']));
46 be4b8e72 Scott Ullrich
	header("Content-Disposition: attachment; filename=\"" .
47 5b237745 Scott Ullrich
		trim(htmlentities(basename($_POST['dlPath']))) . "\"");
48 2d181b70 jim-p
	if (isset($_SERVER['HTTPS'])) {
49
		header('Pragma: ');
50
		header('Cache-Control: ');
51
	} else {
52
		header("Pragma: private");
53
		header("Cache-Control: private, must-revalidate");
54
	}
55 be4b8e72 Scott Ullrich
56 5b237745 Scott Ullrich
	fpassthru($fd);
57
	exit;
58 59255714 NewEraCracker
} else if ($_POST['submit'] == "UPLOAD" && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
59 5b237745 Scott Ullrich
	move_uploaded_file($_FILES['ulfile']['tmp_name'], "/tmp/" . $_FILES['ulfile']['name']);
60 8545adde k-paulius
	$ulmsg = sprintf(gettext('Uploaded file to /tmp/%s.'), htmlentities($_FILES['ulfile']['name']));
61 5b237745 Scott Ullrich
}
62 2900e518 Scott Ullrich
63 41b1ff89 Phil Davis
if ($_POST) {
64 61a90ed5 Scott Ullrich
	conf_mount_rw();
65 41b1ff89 Phil Davis
}
66 74285e13 Scott Ullrich
67 5b237745 Scott Ullrich
// Function: is Blank
68
// Returns true or false depending on blankness of argument.
69
70 41b1ff89 Phil Davis
function isBlank($arg) {
71 6c07db48 Phil Davis
	return preg_match("/^\s*$/", $arg);
72 41b1ff89 Phil Davis
}
73 5b237745 Scott Ullrich
74
// Function: Puts
75
// Put string, Ruby-style.
76
77 41b1ff89 Phil Davis
function puts($arg) {
78
	echo "$arg\n";
79
}
80 5b237745 Scott Ullrich
81
// "Constants".
82
83 45d6ada5 Sjon Hortensius
$Version = '';
84 aa205c3b Ermal
$ScriptName = $REQUEST['SCRIPT_NAME'];
85 5b237745 Scott Ullrich
86
// Get year.
87
88 45d6ada5 Sjon Hortensius
$arrDT = localtime();
89 5b237745 Scott Ullrich
$intYear = $arrDT[5] + 1900;
90
91 a6a6ee00 k-paulius
$pgtitle = array(gettext("Diagnostics"), gettext("Command Prompt"));
92 998abf60 Bill Marquette
include("head.inc");
93 5b237745 Scott Ullrich
?>
94 8fd9052f Colin Fleming
<script type="text/javascript">
95
//<![CDATA[
96 45d6ada5 Sjon Hortensius
	// Create recall buffer array (of encoded strings).
97 5b237745 Scott Ullrich
<?php
98
99 6c07db48 Phil Davis
if (isBlank($_POST['txtRecallBuffer'])) {
100 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array;");
101 5b237745 Scott Ullrich
} else {
102 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array(");
103 6c07db48 Phil Davis
	$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
104
	for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
105 6aef15f8 Stephen Beaver
		puts("		'" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
106 41b1ff89 Phil Davis
	}
107 6aef15f8 Stephen Beaver
	puts("		'" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
108
	puts("	 );");
109 5b237745 Scott Ullrich
}
110
?>
111 45d6ada5 Sjon Hortensius
112 41b1ff89 Phil Davis
	// Set pointer to end of recall buffer.
113
	var intRecallPtr = arrRecallBuffer.length-1;
114 5b237745 Scott Ullrich
115 45d6ada5 Sjon Hortensius
	// Functions to extend String class.
116
	function str_encode() { return escape( this ) }
117
	function str_decode() { return unescape( this ) }
118
119
	// Extend string class to include encode() and decode() functions.
120
	String.prototype.encode = str_encode
121
	String.prototype.decode = str_decode
122
123
	// Function: is Blank
124
	// Returns boolean true or false if argument is blank.
125
	function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
126
127
	// Function: frmExecPlus onSubmit (event handler)
128
	// Builds the recall buffer from the command string on submit.
129
	function frmExecPlus_onSubmit( form ) {
130 5b237745 Scott Ullrich
131 45d6ada5 Sjon Hortensius
		if (!isBlank(form.txtCommand.value)) {
132
			// If this command is repeat of last command, then do not store command.
133
			if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
134
135
			// Stuff encoded command string into the recall buffer.
136 41b1ff89 Phil Davis
			if (isBlank(form.txtRecallBuffer.value)) {
137 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value = form.txtCommand.value.encode();
138 41b1ff89 Phil Davis
			} else {
139 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
140 41b1ff89 Phil Davis
			}
141 45d6ada5 Sjon Hortensius
		}
142
143
		return true;
144
	}
145 5b237745 Scott Ullrich
146 45d6ada5 Sjon Hortensius
	// Function: btnRecall onClick (event handler)
147
	// Recalls command buffer going either up or down.
148
	function btnRecall_onClick( form, n ) {
149 5b237745 Scott Ullrich
150 45d6ada5 Sjon Hortensius
		// If nothing in recall buffer, then error.
151
		if (!arrRecallBuffer.length) {
152 6c07db48 Phil Davis
			alert('<?=gettext("Nothing to recall"); ?>!');
153 45d6ada5 Sjon Hortensius
			form.txtCommand.focus();
154
			return;
155
		}
156 5b237745 Scott Ullrich
157 45d6ada5 Sjon Hortensius
		// Increment recall buffer pointer in positive or negative direction
158
		// according to <n>.
159
		intRecallPtr += n;
160 5b237745 Scott Ullrich
161 45d6ada5 Sjon Hortensius
		// Make sure the buffer stays circular.
162
		if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
163
		if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
164 5b237745 Scott Ullrich
165 45d6ada5 Sjon Hortensius
		// Recall the command.
166
		form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
167
	}
168 5b237745 Scott Ullrich
169 45d6ada5 Sjon Hortensius
	// Function: Reset onClick (event handler)
170
	// Resets form on reset button click event.
171
	function Reset_onClick( form ) {
172 5b237745 Scott Ullrich
173 45d6ada5 Sjon Hortensius
		// Reset recall buffer pointer.
174
		intRecallPtr = arrRecallBuffer.length;
175 5b237745 Scott Ullrich
176 45d6ada5 Sjon Hortensius
		// Clear form (could have spaces in it) and return focus ready for cmd.
177
		form.txtCommand.value = '';
178
		form.txtCommand.focus();
179 5b237745 Scott Ullrich
180 45d6ada5 Sjon Hortensius
		return true;
181
	}
182 fa7855f3 Colin Fleming
//]]>
183 5b237745 Scott Ullrich
</script>
184 45d6ada5 Sjon Hortensius
<?php
185 5b237745 Scott Ullrich
186 947141fd Phil Davis
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg)) {
187 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'));
188 947141fd Phil Davis
}
189 5b237745 Scott Ullrich
190 59255714 NewEraCracker
if ($_POST['submit'] == "EXEC" && !isBlank($_POST['txtCommand'])):?>
191 45d6ada5 Sjon Hortensius
	<div class="panel panel-success responsive">
192 b7edfd3c Phil Davis
		<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Shell Output - %s'), htmlspecialchars($_POST['txtCommand']))?></h2></div>
193 45d6ada5 Sjon Hortensius
		<div class="panel-body">
194 7b91ab09 Jared Dillard
			<div class="content">
195 5b237745 Scott Ullrich
<?php
196 45d6ada5 Sjon Hortensius
	putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
197
	putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
198 6aef15f8 Stephen Beaver
	$output = array();
199
	exec($_POST['txtCommand'] . ' 2>&1', $output);
200 5eb9f6ad NewEraCracker
201
	$output = implode("\n", $output);
202
	print("<pre>" . htmlspecialchars($output) . "</pre>");
203 7b91ab09 Jared Dillard
?>
204
			</div>
205 45d6ada5 Sjon Hortensius
		</div>
206
	</div>
207 fa172bc5 NewEraCracker
<?php endif; ?>
208 5b237745 Scott Ullrich
209 36a844df Stephen Beaver
<form action="diag_command.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
210 45d6ada5 Sjon Hortensius
	<div class="panel panel-default">
211 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
212 45d6ada5 Sjon Hortensius
		<div class="panel-body">
213 7b91ab09 Jared Dillard
			<div class="content">
214 37676f4e jim-p
				<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-7"	 value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
215 7b91ab09 Jared Dillard
				<br /><br />
216
				<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
217 37676f4e jim-p
218
				<div class="btn-group">
219 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")?>">
220 37676f4e jim-p
						<i class="fa fa-angle-double-left"></i>
221
					</button>
222 59255714 NewEraCracker
					<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXEC" title="<?=gettext("Execute the entered command")?>">
223 df2aa83e Stephen Beaver
						<i class="fa fa-bolt"></i>
224 37676f4e jim-p
						<?=gettext("Execute"); ?>
225
					</button>
226 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")?>">
227 37676f4e jim-p
						<i class="fa fa-angle-double-right"></i>
228 6fe069a4 jim-p
					</button>
229 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")?>">
230 37676f4e jim-p
						<i class="fa fa-undo"></i>
231
						<?=gettext("Clear"); ?>
232
					</button>
233
				</div>
234 7b91ab09 Jared Dillard
			</div>
235 45d6ada5 Sjon Hortensius
		</div>
236
	</div>
237
238
	<div class="panel panel-default">
239 3d7a8696 k-paulius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download File')?></h2></div>
240 45d6ada5 Sjon Hortensius
		<div class="panel-body">
241 7b91ab09 Jared Dillard
			<div class="content">
242 5c0ab3cd NewEraCracker
				<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?=htmlspecialchars($_GET['dlPath']);?>"/>
243 7b91ab09 Jared Dillard
				<br /><br />
244 37676f4e jim-p
				<button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">
245
					<i class="fa fa-download icon-embed-btn"></i>
246
					<?=gettext("Download")?>
247
				</button>
248 7b91ab09 Jared Dillard
			</div>
249 45d6ada5 Sjon Hortensius
		</div>
250
	</div>
251 fbcf0037 Scott Ullrich
252 45d6ada5 Sjon Hortensius
<?php
253 947141fd Phil Davis
	if ($ulmsg) {
254 7c945f74 k-paulius
		print_info_box($ulmsg, 'success', false);
255 947141fd Phil Davis
	}
256 45d6ada5 Sjon Hortensius
?>
257
	<div class="panel panel-default">
258 3d7a8696 k-paulius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload File')?></h2></div>
259 45d6ada5 Sjon Hortensius
		<div class="panel-body">
260 7b91ab09 Jared Dillard
			<div class="content">
261
				<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
262
				<br />
263 37676f4e jim-p
				<button name="submit" type="submit" class="btn btn-primary btn-sm" id="upload" value="UPLOAD">
264
					<i class="fa fa-upload icon-embed-btn"></i>
265
					<?=gettext("Upload")?>
266
				</button>
267 7b91ab09 Jared Dillard
			</div>
268 45d6ada5 Sjon Hortensius
		</div>
269
	</div>
270
<?php
271 f5ba2db3 Stephen Beaver
	// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
272
	// This is intended to prevent bad code from breaking the GUI
273 59255714 NewEraCracker
	if ($_POST['submit'] == "EXECPHP" && !isBlank($_POST['txtPHPCommand'])) {
274 3d7a8696 k-paulius
		puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\"><h2 class=\"panel-title\">PHP Response</h2></div>");
275 5eb9f6ad NewEraCracker
276 fc8b158f Stephen Beaver
		$tmpname = tempnam("/tmp", "");
277
		$phpfile = fopen($tmpname, "w");
278 3e115dbf Stephen Beaver
		fwrite($phpfile, "<?php\n");
279
		fwrite($phpfile, "require_once(\"/etc/inc/config.inc\");\n");
280
		fwrite($phpfile, "require_once(\"/etc/inc/functions.inc\");\n\n");
281
		fwrite($phpfile, $_POST['txtPHPCommand'] . "\n");
282
		fwrite($phpfile, "?>\n");
283
		fclose($phpfile);
284
285 5eb9f6ad NewEraCracker
		$output = array();
286 fc8b158f Stephen Beaver
		exec("/usr/local/bin/php " . $tmpname, $output);
287 3e115dbf Stephen Beaver
288 fc8b158f Stephen Beaver
		unlink($tmpname);
289 3e115dbf Stephen Beaver
290 5eb9f6ad NewEraCracker
		$output = implode("\n", $output);
291
		print("<pre>" . htmlspecialchars($output) . "</pre>");
292
293 3e115dbf Stephen Beaver
//		echo eval($_POST['txtPHPCommand']);
294 45d6ada5 Sjon Hortensius
		puts("</div>");
295 bd8eeef9 Stephen Beaver
?>
296 8fd9052f Colin Fleming
<script type="text/javascript">
297
//<![CDATA[
298 947141fd Phil Davis
	events.push(function() {
299 bd8eeef9 Stephen Beaver
		// Scroll to the bottom of the page to more easily see the results of a PHP exec command
300
		$("html, body").animate({ scrollTop: $(document).height() }, 1000);
301
	});
302 8fd9052f Colin Fleming
//]]>
303 bd8eeef9 Stephen Beaver
</script>
304
<?php
305 fbcf0037 Scott Ullrich
}
306 5b237745 Scott Ullrich
?>
307 45d6ada5 Sjon Hortensius
	<div class="panel panel-default responsive">
308 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
309 45d6ada5 Sjon Hortensius
		<div class="panel-body">
310 7b91ab09 Jared Dillard
			<div class="content">
311
				<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
312
				<br />
313 59255714 NewEraCracker
				<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXECPHP" title="<?=gettext("Execute this PHP Code")?>">
314 df2aa83e Stephen Beaver
					<i class="fa fa-bolt"></i>
315 37676f4e jim-p
					<?=gettext("Execute")?>
316
				</button>
317 7b91ab09 Jared Dillard
				<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
318
			</div>
319 45d6ada5 Sjon Hortensius
		</div>
320
	</div>
321 fa7855f3 Colin Fleming
</form>
322 74285e13 Scott Ullrich
323
<?php
324 45d6ada5 Sjon Hortensius
include("foot.inc");
325 74285e13 Scott Ullrich
326 947141fd Phil Davis
if ($_POST) {
327 0bbbafc4 Phil Davis
	conf_mount_ro();
328 947141fd Phil Davis
}