Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	exec.php
5
*/
6
/* ====================================================================
7
 *	Exec+ v1.02-000 - Copyright 2001-2003, All rights reserved
8
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
9
 *	Created by technologEase (http://www.technologEase.com)
10
 *	(modified for m0n0wall by Manuel Kasper <mk@neon1.net>)\
11
 *
12
 *  Redistribution and use in source and binary forms, with or without modification, 
13
 *  are permitted provided that the following conditions are met: 
14
 *
15
 *  1. Redistributions of source code must retain the above copyright notice,
16
 *      this list of conditions and the following disclaimer.
17
 *
18
 *  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
 *
23
 *  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
 *
28
 *  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
 *
33
 *  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
 *
37
 *  6. Redistributions of any form whatsoever must retain the following
38
 *      acknowledgment:
39
 *
40
 *  "This product includes software developed by the pfSense Project
41
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
42
  *
43
 *  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
 *
56
 *  ====================================================================
57
 *
58
 */
59
/*
60
	pfSense_MODULE: shell
61
*/
62

    
63
##|+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
$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>
130
	// Create recall buffer array (of encoded strings).
131
<?php
132

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

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

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

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

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

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

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

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

    
179
		return true;
180
	}
181

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

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

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

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

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

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

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

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

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

    
222
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg))
223
	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>');
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
			<pre>
230
<?php
231
	putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
232
	putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
233
	print htmlspecialchars(system($_POST['txtCommand'].' 2>&1'));
234
?>
235
			</pre>
236
		</div>
237
	</div>
238
<? endif ?>
239

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

    
254
	<div class="panel panel-default">
255
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download file')?></h2></div>
256
		<div class="panel-body">
257
			<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4"/>
258
			<br /><br />
259
			<input name="submit" type="submit"	class="btn btn-default btn-sm" id="download" value="<?=gettext("Download"); ?>" />
260
		</div>
261
	</div>
262

    
263
<?php
264
	if ($ulmsg)
265
		print('<div class="alert alert-success" role="alert">' . $ulmsg .'</div>');
266
?>
267
	<div class="panel panel-default">
268
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload a file')?></h2></div>
269
		<div class="panel-body">
270
			<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
271
			<br />
272
			<input name="submit" type="submit" class="btn btn-default btn-sm pull-left" id="upload" value="<?=gettext("Upload"); ?>" />
273

    
274
		</div>
275
	</div>
276
<?php
277
	if (!isBlank($_POST['txtPHPCommand'])) {
278
		puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\">PHP response</div>");
279
		puts("<pre>");
280
		require_once("config.inc");
281
		require_once("functions.inc");
282
		echo eval($_POST['txtPHPCommand']);
283
		puts("&nbsp;</pre>");
284
		puts("</div>");
285
}
286
?>
287
	<div class="panel panel-default responsive">
288
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
289
		<div class="panel-body">
290
			<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
291
			<br />
292
			<input type="submit" class="btn btn-default btn-sm" value="<?=gettext("Execute")?>" />
293
			<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
294
		</div>
295
	</div>
296
</form>
297

    
298
<?php
299
include("foot.inc");
300

    
301
if($_POST)
302
	conf_mount_ro();
(46-46/237)