Project

General

Profile

Download (11.8 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2

    
3
<?php
4
/*
5
 * pfSsh
6
 *
7
 * part of pfSense (https://www.pfsense.org)
8
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions are met:
13
 *
14
 * 1. Redistributions of source code must retain the above copyright notice,
15
 *    this list of conditions and the following disclaimer.
16
 *
17
 * 2. Redistributions in binary form must reproduce the above copyright
18
 *    notice, this list of conditions and the following disclaimer in
19
 *    the documentation and/or other materials provided with the
20
 *    distribution.
21
 *
22
 * 3. All advertising materials mentioning features or use of this software
23
 *    must display the following acknowledgment:
24
 *    "This product includes software developed by the pfSense Project
25
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
26
 *
27
 * 4. The names "pfSense" and "pfSense Project" must not be used to
28
 *    endorse or promote products derived from this software without
29
 *    prior written permission. For written permission, please contact
30
 *    coreteam@pfsense.org.
31
 *
32
 * 5. Products derived from this software may not be called "pfSense"
33
 *    nor may "pfSense" appear in their names without prior written
34
 *    permission of the Electric Sheep Fencing, LLC.
35
 *
36
 * 6. Redistributions of any form whatsoever must retain the following
37
 *    acknowledgment:
38
 *
39
 * "This product includes software developed by the pfSense Project
40
 * for use in the pfSense software distribution (http://www.pfsense.org/).
41
 *
42
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 * OF THE POSSIBILITY OF SUCH DAMAGE.
54
 */
55

    
56
require_once("globals.inc");
57
echo "Starting the {$g['product_name']} developer shell";
58
require_once("functions.inc");
59
echo ".";
60
require_once("config.inc");
61
echo ".";
62
require_once("util.inc");
63
echo ".";
64

    
65
$shell_cmds = array("alias", "alloc", "bg", "bind", "bindkey", "break",
66
	 "breaksw", "builtins", "case", "cd", "chdir", "command", "complete", "continue", "default",
67
	 "dirs", "do", "done", "echo", "echotc", "elif", "else", "end", "endif", "endsw", "esac", "eval",
68
	 "exec", "exit", "export", "false", "fc", "fg", "filetest", "fi", "for", "foreach", "getopts",
69
	 "glob", "goto", "hash", "hashstat", "history", "hup", "if", "jobid", "jobs", "kill", "limit",
70
	 "local", "log", "login", "logout", "ls-F", "nice", "nohup", "notify", "onintr", "popd",
71
	 "printenv", "pushd", "pwd", "read", "readonly", "rehash", "repeat", "return", "sched", "set",
72
	 "setenv", "settc", "setty", "setvar", "shift", "source", "stop", "suspend", "switch",
73
	 "telltc", "test", "then", "time", "trap", "true", "type", "ulimit", "umask", "unalias",
74
	 "uncomplete", "unhash", "unlimit", "unset", "unsetenv", "until", "wait", "where", "which",
75
	 "while");
76

    
77
function pipe_cmd($command, $text_to_pipe) {
78
	$descriptorspec = array(
79
		0 => array("pipe", "r"),  // stdin
80
		1 => array("pipe", "w"),  // stdout
81
		2 => array("pipe", "w")); // stderr ?? instead of a file
82

    
83
	$fd = proc_open("$command", $descriptorspec, $pipes);
84
	if (is_resource($fd)) {
85
		fwrite($pipes[0], "{$text_to_pipe}");
86
		fclose($pipes[0]);
87
		while ($s= fgets($pipes[1], 1024)) {
88
			// read from the pipe
89
			$buffer .= $s;
90
		}
91
		fclose($pipes[1]);
92
		fclose($pipes[2]);
93
	}
94
	return $buffer;
95
}
96

    
97
if (!function_exists("readline")) {
98
	function readline() {
99
		$fp = fopen('php://stdin', 'r');
100
		$textinput = chop(fgets($fp));
101
		fclose($fp);
102
		return $textinput;
103
	}
104
}
105

    
106
function more($text, $count=24) {
107
	$counter=0;
108
	$lines = explode("\n", $text);
109
	foreach ($lines as $line) {
110
		if ($counter > $count) {
111
			echo "Press RETURN to continue ...";
112
			$fp = fopen('php://stdin', 'r');
113
			$pressreturn = chop(fgets($fp));
114
			if ($pressreturn == "q" || $pressreturn == "quit") {
115
				return;
116
			}
117
			fclose($fp);
118
			$counter = 0;
119
		}
120
		echo "{$line}\n";
121
		$counter++;
122
	}
123
}
124

    
125
function show_help() {
126

    
127
$show_help_text = <<<EOF
128

    
129
	Enter a series of commands and then execute the set with "exec".
130

    
131
	For example:
132
	echo "foo"; // php command
133
	echo "foo2"; // php command
134
	! echo "heh" # shell command
135
	exec
136

    
137
	Example commands:
138

    
139
	record <recordingfilename>
140
	stoprecording
141
	showrecordings
142

    
143
	parse_config(true);  # reloads the \$config array
144

    
145
	\$temp = print_r(\$config, true);
146
	more(\$temp);
147

    
148
	/* to output a configuration array */
149
	print_r(\$config);
150

    
151
	/* to output the interfaces configuration portion of config.xml */
152
	print_r(\$config['interfaces']);
153

    
154
	/* to output the dhcp server configuration */
155
	print_r(\$config['dhcpd']);
156

    
157
	/* to exit the {$g['product_name']} developer shell */
158
	exit
159

    
160
	/* to output supported wireless modes for an interface */
161
	print_r(get_wireless_modes(\"ath0\"));
162

    
163
	/* to enable SSH */
164
	\$config['system']['enablesshd'] = true;
165

    
166
	/* change OPTX to the OPT interface name such as BACKHAUL */
167
	\$config['interfaces']['optx']['wireless']['standard'] = "11a";
168
	\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
169
	\$config['interfaces']['optx']['wireless']['channel'] = "6";
170

    
171
	/* to enable dhcp server for an optx interface */
172
	\$config['dhcpd']['optx']['enable'] = true;
173
	\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
174
	\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
175

    
176
	/* to disable the firewall filter */
177
	\$config['system']['disablefilter'] = true;
178

    
179
	/* to enable an interface and configure it as a DHCP client */
180
	\$config['interfaces']['optx']['disabled'] = false;
181
	\$config['interfaces']['optx']['ipaddr'] = "dhcp";
182

    
183
	/* to enable an interface and set a static IPv4 address */
184
	\$config['interfaces']['wan']['enable'] = true;
185
	\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
186
	\$config['interfaces']['wan']['subnet'] = "24";
187

    
188
	/* to save out the new configuration (config.xml) */
189
	write_config();
190

    
191
	/* to reboot the system after saving */
192
	system_reboot_sync();
193

    
194
EOF;
195

    
196
	more($show_help_text);
197

    
198
}
199

    
200
$fp = fopen('php://stdin', 'r');
201

    
202
echo ".\n\n";
203

    
204
$pkg_interface='console';
205

    
206
$shell_active = true;
207
$tccommands = array();
208

    
209
function completion($string, $index) {
210
	global $tccommands;
211
	return $tccommands;
212
}
213

    
214
readline_completion_function("completion");
215

    
216
function get_playback_files() {
217
	$playback_files = array();
218
	$files = scandir("/etc/phpshellsessions/");
219
	foreach ($files as $file) {
220
		if ($file <> "." && $file <> "..") {
221
			$playback_files[] = $file;
222
		}
223
	}
224
	return $playback_files;
225
}
226

    
227
if ($argc < 2) {
228
	echo "Welcome to the {$g['product_name']} developer shell\n";
229
	echo "\nType \"help\" to show common usage scenarios.\n";
230
	echo "\nAvailable playback commands:\n     ";
231
	$tccommands[] = "playback";
232
	$playback_files = get_playback_files();
233
	foreach ($playback_files as $pbf) {
234
		echo "{$pbf} ";
235
		if (function_exists("readline_add_history")) {
236
			readline_add_history("playback $pbf");
237
			$tccommands[] = "$pbf";
238
		}
239
	}
240
	echo "\n\n";
241
}
242

    
243
$recording = false;
244
$playback_file_split = array();
245
$playbackbuffer = "";
246

    
247
if ($argv[1]=="playback" or $argv[1]=="run") {
248
	if (empty($argv[2]) || !file_exists("/etc/phpshellsessions/" . basename($argv[2]))) {
249
		echo "Error: Invalid playback file specified.\n\n";
250
		show_recordings();
251
		exit(-1);
252
	}
253
	playback_file(basename($argv[2]));
254
	exit;
255
}
256

    
257
// Define more commands
258
$tccommands[] = "exit";
259
$tccommands[] = "quit";
260
$tccommands[] = "?";
261
$tccommands[] = "exec";
262
$tccommands[] = "stoprecording";
263
$tccommands[] = "showrecordings";
264
$tccommands[] = "record";
265
$tccommands[] = "reset";
266
$tccommands[] = "master";
267
$tccommands[] = "RELENG_1_2";
268

    
269
while ($shell_active == true) {
270
	$command = readline("{$g['product_name']} shell: ");
271
	readline_add_history($command);
272
	$command_split = explode(" ", $command);
273
	$first_command = $command_split[0];
274
	if ($first_command == "playback" || $first_command == "run") {
275
		$playback_file = $command_split[1];
276
		if (!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
277
			$command = "";
278
			echo "Could not locate playback file.\n";
279
		} else {
280
			$command = "";
281
			echo "\nPlayback of file {$command_split[1]} started.\n\n";
282
			playback_file("{$playback_file}");
283
			continue;
284
		}
285
	}
286
	if ($first_command == "exit" or $first_command == "quit") {
287
		die;
288
	}
289
	if ($first_command == "help" or $first_command == "?") {
290
		show_help();
291
		$playbackbuffer = "";
292
		continue;
293
	}
294
	if ($first_command == "exec" or $first_command == "exec;") {
295
		playback_text($playbackbuffer);
296
		$playbackbuffer = "";
297
		continue;
298
	}
299
	if ($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
300
		if ($recording) {
301
			fwrite($recording_fd, $playbackbuffer);
302
			fclose($recording_fd);
303
			$command = "";
304
			conf_mount_ro();
305
			echo "Recording stopped.\n";
306
			$recording = false;
307
		} else {
308
			echo "No recording session in progress.\n";
309
			$command = "";
310
		}
311
	}
312
	if ($first_command == "showrecordings") {
313
		show_recordings();
314
		$command = "";
315
	}
316
	if ($first_command == "reset") {
317
		$playbackbuffer = "";
318
		echo "\nBuffer reset.\n\n";
319
		continue;
320
	}
321
	if ($first_command == "record") {
322
		if (!$command_split[1]) {
323
			echo "usage: record playbackname\n";
324
			echo "\tplaybackname will be created in /etc/phpshellsessions.\n";
325
			$command = "";
326
		} else {
327
			/* time to record */
328
			conf_mount_rw();
329
			safe_mkdir("/etc/phpshellsessions");
330
			$recording_fn = basename($command_split[1]);
331
			$recording_fd = fopen("/etc/phpshellsessions/{$recording_fn}","w");
332
			if (!$recording_fd) {
333
				echo "Could not start recording session.\n";
334
				$command = "";
335
			} else {
336
				$recording = true;
337
				echo "Recording of {$recording_fn} started.\n";
338
				$command = "";
339
			}
340
		}
341
	}
342
	$playbackbuffer .= $command . "\n";
343
}
344

    
345
function show_recordings() {
346
	echo "==> Sessions available for playback are:\n";
347
	$playback_files = get_playback_files();
348
	foreach (get_playback_files() as $pbf) {
349
		echo "{$pbf} ";
350
	}
351
	echo "\n\n";
352
	echo "==> end of list.\n";
353
}
354

    
355
function returnlastchar($command) {
356
	$commandlen = strlen($command);
357
	$endofstring = substr($command, ($commandlen-1));
358
	return $endofstring;
359
}
360

    
361
function returnfirstchar($command) {
362
	$commandlen = strlen($command);
363
	$endofstring = substr($command, 0, 1);
364
	return $endofstring;
365
}
366

    
367
function str_replace_all($search,$replace,$subject) {
368
	while (strpos($subject,$search)!==false) {
369
		$subject = str_replace($search,$replace,$subject);
370
	}
371
	return $subject;
372
}
373

    
374
function playback_text($playback_file_contents) {
375
	$playback_file_split = explode("\n", $playback_file_contents);
376
	$playback_text  = "require_once('functions.inc');\n";
377
	$playback_text .= "require_once('globals.inc');\n";
378
	$playback_text .= "require_once('config.inc');\n";
379
	$toquote = '"';
380
	$toquotereplace = '\\"';
381
	foreach ($playback_file_split as $pfs) {
382
		$firstchar = returnfirstchar($pfs);
383
		$currentline = $pfs;
384
		if ($firstchar == "!") {
385
			/* XXX: encode " in $pfs */
386
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
387
			$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
388
		} else if ($firstchar == "=") {
389
			/* XXX: encode " in $pfs */
390
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
391
			$currentline   .= str_replace("!", "system(\"", $pfsa) . "\");\n";
392
		} else {
393
			$playback_text .= $pfs . "\n";
394
		}
395
	}
396
	global $config;
397
	eval($playback_text);
398
}
399

    
400
function playback_file($playback_file) {
401
	$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
402
	playback_text($playback_file_contents);
403
}
404

    
405
?>
(10-10/22)