Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_command.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * 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
 *
13
 * originally based on m0n0wall (http://m0n0.ch/wall)
14
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
15
 * All rights reserved.
16
 *
17
 * 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
 *
21
 * http://www.apache.org/licenses/LICENSE-2.0
22
 *
23
 * 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
 */
29

    
30
##|+PRIV
31
##|*IDENT=page-diagnostics-command
32
##|*NAME=Diagnostics: Command
33
##|*DESCR=Allow access to the 'Diagnostics: Command' page.
34
##|*MATCH=diag_command.php*
35
##|-PRIV
36

    
37
$allowautocomplete = true;
38

    
39
require_once("guiconfig.inc");
40

    
41
if ($_POST['submit'] == "DOWNLOAD" && file_exists($_POST['dlPath'])) {
42
	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
	header("Content-Disposition: attachment; filename=\"" .
47
		trim(htmlentities(basename($_POST['dlPath']))) . "\"");
48
	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

    
56
	fpassthru($fd);
57
	exit;
58
} else if ($_POST['submit'] == "UPLOAD" && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
59
	move_uploaded_file($_FILES['ulfile']['tmp_name'], "/tmp/" . $_FILES['ulfile']['name']);
60
	$ulmsg = sprintf(gettext('Uploaded file to /tmp/%s.'), htmlentities($_FILES['ulfile']['name']));
61
}
62

    
63
if ($_POST) {
64
}
65

    
66
// Function: is Blank
67
// Returns true or false depending on blankness of argument.
68

    
69
function isBlank($arg) {
70
	return preg_match("/^\s*$/", $arg);
71
}
72

    
73
// Function: Puts
74
// Put string, Ruby-style.
75

    
76
function puts($arg) {
77
	echo "$arg\n";
78
}
79

    
80
// "Constants".
81

    
82
$Version = '';
83
$ScriptName = $REQUEST['SCRIPT_NAME'];
84

    
85
// Get year.
86

    
87
$arrDT = localtime();
88
$intYear = $arrDT[5] + 1900;
89

    
90
$pgtitle = array(gettext("Diagnostics"), gettext("Command Prompt"));
91
include("head.inc");
92
?>
93
<script type="text/javascript">
94
//<![CDATA[
95
	// Create recall buffer array (of encoded strings).
96
<?php
97

    
98
if (isBlank($_POST['txtRecallBuffer'])) {
99
	puts("	 var arrRecallBuffer = new Array;");
100
} else {
101
	puts("	 var arrRecallBuffer = new Array(");
102
	$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
103
	for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
104
		puts("		'" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
105
	}
106
	puts("		'" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
107
	puts("	 );");
108
}
109
?>
110

    
111
	// Set pointer to end of recall buffer.
112
	var intRecallPtr = arrRecallBuffer.length-1;
113

    
114
	// Functions to extend String class.
115
	function str_encode() { return escape( this ) }
116
	function str_decode() { return unescape( this ) }
117

    
118
	// Extend string class to include encode() and decode() functions.
119
	String.prototype.encode = str_encode
120
	String.prototype.decode = str_decode
121

    
122
	// Function: is Blank
123
	// Returns boolean true or false if argument is blank.
124
	function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
125

    
126
	// Function: frmExecPlus onSubmit (event handler)
127
	// Builds the recall buffer from the command string on submit.
128
	function frmExecPlus_onSubmit( form ) {
129

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

    
134
			// Stuff encoded command string into the recall buffer.
135
			if (isBlank(form.txtRecallBuffer.value)) {
136
				form.txtRecallBuffer.value = form.txtCommand.value.encode();
137
			} else {
138
				form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
139
			}
140
		}
141

    
142
		return true;
143
	}
144

    
145
	// Function: btnRecall onClick (event handler)
146
	// Recalls command buffer going either up or down.
147
	function btnRecall_onClick( form, n ) {
148

    
149
		// If nothing in recall buffer, then error.
150
		if (!arrRecallBuffer.length) {
151
			alert('<?=gettext("Nothing to recall"); ?>!');
152
			form.txtCommand.focus();
153
			return;
154
		}
155

    
156
		// Increment recall buffer pointer in positive or negative direction
157
		// according to <n>.
158
		intRecallPtr += n;
159

    
160
		// Make sure the buffer stays circular.
161
		if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
162
		if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
163

    
164
		// Recall the command.
165
		form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
166
	}
167

    
168
	// Function: Reset onClick (event handler)
169
	// Resets form on reset button click event.
170
	function Reset_onClick( form ) {
171

    
172
		// Reset recall buffer pointer.
173
		intRecallPtr = arrRecallBuffer.length;
174

    
175
		// Clear form (could have spaces in it) and return focus ready for cmd.
176
		form.txtCommand.value = '';
177
		form.txtCommand.focus();
178

    
179
		return true;
180
	}
181
//]]>
182
</script>
183
<?php
184

    
185
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg)) {
186
	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'));
187
}
188

    
189
if ($_POST['submit'] == "EXEC" && !isBlank($_POST['txtCommand'])):?>
190
	<div class="panel panel-success responsive">
191
		<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Shell Output - %s'), htmlspecialchars($_POST['txtCommand']))?></h2></div>
192
		<div class="panel-body">
193
			<div class="content">
194
<?php
195
	putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
196
	putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
197
	$output = array();
198
	exec($_POST['txtCommand'] . ' 2>&1', $output);
199

    
200
	$output = implode("\n", $output);
201
	print("<pre>" . htmlspecialchars($output) . "</pre>");
202
?>
203
			</div>
204
		</div>
205
	</div>
206
<?php endif; ?>
207

    
208
<form action="diag_command.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
209
	<div class="panel panel-default">
210
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
211
		<div class="panel-body">
212
			<div class="content">
213
				<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-7"	 value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
214
				<br /><br />
215
				<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
216

    
217
				<div class="btn-group">
218
					<button type="button" class="btn btn-success btn-sm" name="btnRecallPrev" onclick="btnRecall_onClick( this.form, -1 );" title="<?=gettext("Recall Previous Command")?>">
219
						<i class="fa fa-angle-double-left"></i>
220
					</button>
221
					<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXEC" title="<?=gettext("Execute the entered command")?>">
222
						<i class="fa fa-bolt"></i>
223
						<?=gettext("Execute"); ?>
224
					</button>
225
					<button type="button" class="btn btn-success btn-sm" name="btnRecallNext" onclick="btnRecall_onClick( this.form,  1 );" title="<?=gettext("Recall Next Command")?>">
226
						<i class="fa fa-angle-double-right"></i>
227
					</button>
228
					<button style="margin-left: 10px;" type="button" class="btn btn-default btn-sm" onclick="return Reset_onClick( this.form );" title="<?=gettext("Clear command entry")?>">
229
						<i class="fa fa-undo"></i>
230
						<?=gettext("Clear"); ?>
231
					</button>
232
				</div>
233
			</div>
234
		</div>
235
	</div>
236

    
237
	<div class="panel panel-default">
238
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download File')?></h2></div>
239
		<div class="panel-body">
240
			<div class="content">
241
				<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?=htmlspecialchars($_GET['dlPath']);?>"/>
242
				<br /><br />
243
				<button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">
244
					<i class="fa fa-download icon-embed-btn"></i>
245
					<?=gettext("Download")?>
246
				</button>
247
			</div>
248
		</div>
249
	</div>
250

    
251
<?php
252
	if ($ulmsg) {
253
		print_info_box($ulmsg, 'success', false);
254
	}
255
?>
256
	<div class="panel panel-default">
257
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload File')?></h2></div>
258
		<div class="panel-body">
259
			<div class="content">
260
				<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
261
				<br />
262
				<button name="submit" type="submit" class="btn btn-primary btn-sm" id="upload" value="UPLOAD">
263
					<i class="fa fa-upload icon-embed-btn"></i>
264
					<?=gettext("Upload")?>
265
				</button>
266
			</div>
267
		</div>
268
	</div>
269
<?php
270
	// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
271
	// This is intended to prevent bad code from breaking the GUI
272
	if ($_POST['submit'] == "EXECPHP" && !isBlank($_POST['txtPHPCommand'])) {
273
		puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\"><h2 class=\"panel-title\">PHP Response</h2></div>");
274

    
275
		$tmpname = tempnam("/tmp", "");
276
		$phpfile = fopen($tmpname, "w");
277
		fwrite($phpfile, "<?php\n");
278
		fwrite($phpfile, "require_once(\"/etc/inc/config.inc\");\n");
279
		fwrite($phpfile, "require_once(\"/etc/inc/functions.inc\");\n\n");
280
		fwrite($phpfile, $_POST['txtPHPCommand'] . "\n");
281
		fwrite($phpfile, "?>\n");
282
		fclose($phpfile);
283

    
284
		$output = array();
285
		exec("/usr/local/bin/php -d log_errors=off " . $tmpname, $output);
286

    
287
		unlink($tmpname);
288

    
289
		$output = implode("\n", $output);
290
		print("<pre>" . htmlspecialchars($output) . "</pre>");
291

    
292
//		echo eval($_POST['txtPHPCommand']);
293
		puts("</div>");
294
?>
295
<script type="text/javascript">
296
//<![CDATA[
297
	events.push(function() {
298
		// Scroll to the bottom of the page to more easily see the results of a PHP exec command
299
		$("html, body").animate({ scrollTop: $(document).height() }, 1000);
300
	});
301
//]]>
302
</script>
303
<?php
304
}
305
?>
306
	<div class="panel panel-default responsive">
307
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
308
		<div class="panel-body">
309
			<div class="content">
310
				<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
311
				<br />
312
				<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXECPHP" title="<?=gettext("Execute this PHP Code")?>">
313
					<i class="fa fa-bolt"></i>
314
					<?=gettext("Execute")?>
315
				</button>
316
				<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
317
			</div>
318
		</div>
319
	</div>
320
</form>
321

    
322
<?php
323
include("foot.inc");
324

    
325
if ($_POST) {
326
}
(7-7/225)