Project

General

Profile

Download (10.1 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 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
require_once("globals.inc");
25
echo "Starting the {$g['product_name']} developer shell";
26
require_once("functions.inc");
27
echo ".";
28
require_once("config.inc");
29
echo ".";
30
require_once("util.inc");
31
echo ".";
32

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

    
45
function pipe_cmd($command, $text_to_pipe) {
46
	$descriptorspec = array(
47
		0 => array("pipe", "r"),  // stdin
48
		1 => array("pipe", "w"),  // stdout
49
		2 => array("pipe", "w")); // stderr ?? instead of a file
50

    
51
	$fd = proc_open("$command", $descriptorspec, $pipes);
52
	if (is_resource($fd)) {
53
		fwrite($pipes[0], "{$text_to_pipe}");
54
		fclose($pipes[0]);
55
		while ($s= fgets($pipes[1], 1024)) {
56
			// read from the pipe
57
			$buffer .= $s;
58
		}
59
		fclose($pipes[1]);
60
		fclose($pipes[2]);
61
	}
62
	return $buffer;
63
}
64

    
65
if (!function_exists("readline")) {
66
	function readline() {
67
		$fp = fopen('php://stdin', 'r');
68
		$textinput = chop(fgets($fp));
69
		fclose($fp);
70
		return $textinput;
71
	}
72
}
73

    
74
function more($text, $count=24) {
75
	$counter=0;
76
	$lines = explode("\n", $text);
77
	foreach ($lines as $line) {
78
		if ($counter > $count) {
79
			echo "Press RETURN to continue ...";
80
			$fp = fopen('php://stdin', 'r');
81
			$pressreturn = chop(fgets($fp));
82
			if ($pressreturn == "q" || $pressreturn == "quit") {
83
				return;
84
			}
85
			fclose($fp);
86
			$counter = 0;
87
		}
88
		echo "{$line}\n";
89
		$counter++;
90
	}
91
}
92

    
93
function show_help() {
94

    
95
$show_help_text = <<<EOF
96

    
97
	Enter a series of commands and then execute the set with "exec".
98

    
99
	For example:
100
	echo "foo"; // php command
101
	echo "foo2"; // php command
102
	! echo "heh" # shell command
103
	exec
104

    
105
	Example commands:
106

    
107
	record <recordingfilename>
108
	stoprecording
109
	showrecordings
110

    
111
	parse_config(true);  # reloads the \$config array
112

    
113
	\$temp = print_r(\$config, true);
114
	more(\$temp);
115

    
116
	/* to output a configuration array */
117
	print_r(\$config);
118

    
119
	/* to output the interfaces configuration portion of config.xml */
120
	print_r(\$config['interfaces']);
121

    
122
	/* to output the dhcp server configuration */
123
	print_r(\$config['dhcpd']);
124

    
125
	/* to exit the {$g['product_name']} developer shell */
126
	exit
127

    
128
	/* to output supported wireless modes for an interface */
129
	print_r(get_wireless_modes(\"ath0\"));
130

    
131
	/* to enable SSH */
132
	\$config['system']['enablesshd'] = true;
133

    
134
	/* change OPTX to the OPT interface name such as BACKHAUL */
135
	\$config['interfaces']['optx']['wireless']['standard'] = "11a";
136
	\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
137
	\$config['interfaces']['optx']['wireless']['channel'] = "6";
138

    
139
	/* to enable dhcp server for an optx interface */
140
	\$config['dhcpd']['optx']['enable'] = true;
141
	\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
142
	\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
143

    
144
	/* to disable the firewall filter */
145
	\$config['system']['disablefilter'] = true;
146

    
147
	/* to enable an interface and configure it as a DHCP client */
148
	\$config['interfaces']['optx']['disabled'] = false;
149
	\$config['interfaces']['optx']['ipaddr'] = "dhcp";
150

    
151
	/* to enable an interface and set a static IPv4 address */
152
	\$config['interfaces']['wan']['enable'] = true;
153
	\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
154
	\$config['interfaces']['wan']['subnet'] = "24";
155

    
156
	/* to save out the new configuration (config.xml) */
157
	write_config();
158

    
159
	/* to reboot the system after saving */
160
	system_reboot_sync();
161

    
162
EOF;
163

    
164
	more($show_help_text);
165

    
166
}
167

    
168
$fp = fopen('php://stdin', 'r');
169

    
170
echo ".\n\n";
171

    
172
$pkg_interface='console';
173

    
174
$shell_active = true;
175
$tccommands = array();
176

    
177
function completion($string, $index) {
178
	global $tccommands;
179
	return $tccommands;
180
}
181

    
182
readline_completion_function("completion");
183

    
184
function get_playback_files() {
185
	$playback_files = array();
186
	$files = scandir("/etc/phpshellsessions/");
187
	foreach ($files as $file) {
188
		if ($file <> "." && $file <> "..") {
189
			$playback_files[] = $file;
190
		}
191
	}
192
	return $playback_files;
193
}
194

    
195
if ($argc < 2) {
196
	echo "Welcome to the {$g['product_name']} developer shell\n";
197
	echo "\nType \"help\" to show common usage scenarios.\n";
198
	echo "\nAvailable playback commands:\n     ";
199
	$tccommands[] = "playback";
200
	$playback_files = get_playback_files();
201
	foreach ($playback_files as $pbf) {
202
		echo "{$pbf} ";
203
		if (function_exists("readline_add_history")) {
204
			readline_add_history("playback $pbf");
205
			$tccommands[] = "$pbf";
206
		}
207
	}
208
	echo "\n\n";
209
}
210

    
211
$recording = false;
212
$playback_file_split = array();
213
$playbackbuffer = "";
214

    
215
if ($argv[1]=="playback" or $argv[1]=="run") {
216
	if (empty($argv[2]) || !file_exists("/etc/phpshellsessions/" . basename($argv[2]))) {
217
		echo "Error: Invalid playback file specified.\n\n";
218
		show_recordings();
219
		exit(-1);
220
	}
221
	playback_file(basename($argv[2]));
222
	exit;
223
}
224

    
225
// Define more commands
226
$tccommands[] = "exit";
227
$tccommands[] = "quit";
228
$tccommands[] = "?";
229
$tccommands[] = "exec";
230
$tccommands[] = "stoprecording";
231
$tccommands[] = "showrecordings";
232
$tccommands[] = "record";
233
$tccommands[] = "reset";
234
$tccommands[] = "master";
235
$tccommands[] = "RELENG_1_2";
236

    
237
while ($shell_active == true) {
238
	$command = readline("{$g['product_name']} shell: ");
239
	readline_add_history($command);
240
	$command_split = explode(" ", $command);
241
	$first_command = $command_split[0];
242
	if ($first_command == "playback" || $first_command == "run") {
243
		$playback_file = $command_split[1];
244
		if (!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
245
			$command = "";
246
			echo "Could not locate playback file.\n";
247
		} else {
248
			$command = "";
249
			echo "\nPlayback of file {$command_split[1]} started.\n\n";
250
			playback_file("{$playback_file}");
251
			continue;
252
		}
253
	}
254
	if ($first_command == "exit" or $first_command == "quit") {
255
		die;
256
	}
257
	if ($first_command == "help" or $first_command == "?") {
258
		show_help();
259
		$playbackbuffer = "";
260
		continue;
261
	}
262
	if ($first_command == "exec" or $first_command == "exec;") {
263
		playback_text($playbackbuffer);
264
		$playbackbuffer = "";
265
		continue;
266
	}
267
	if ($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
268
		if ($recording) {
269
			fwrite($recording_fd, $playbackbuffer);
270
			fclose($recording_fd);
271
			$command = "";
272
			echo "Recording stopped.\n";
273
			$recording = false;
274
		} else {
275
			echo "No recording session in progress.\n";
276
			$command = "";
277
		}
278
	}
279
	if ($first_command == "showrecordings") {
280
		show_recordings();
281
		$command = "";
282
	}
283
	if ($first_command == "reset") {
284
		$playbackbuffer = "";
285
		echo "\nBuffer reset.\n\n";
286
		continue;
287
	}
288
	if ($first_command == "record") {
289
		if (!$command_split[1]) {
290
			echo "usage: record playbackname\n";
291
			echo "\tplaybackname will be created in /etc/phpshellsessions.\n";
292
			$command = "";
293
		} else {
294
			/* time to record */
295
			safe_mkdir("/etc/phpshellsessions");
296
			$recording_fn = basename($command_split[1]);
297
			$recording_fd = fopen("/etc/phpshellsessions/{$recording_fn}","w");
298
			if (!$recording_fd) {
299
				echo "Could not start recording session.\n";
300
				$command = "";
301
			} else {
302
				$recording = true;
303
				echo "Recording of {$recording_fn} started.\n";
304
				$command = "";
305
			}
306
		}
307
	}
308
	$playbackbuffer .= $command . "\n";
309
}
310

    
311
function show_recordings() {
312
	echo "==> Sessions available for playback are:\n";
313
	$playback_files = get_playback_files();
314
	foreach (get_playback_files() as $pbf) {
315
		echo "{$pbf} ";
316
	}
317
	echo "\n\n";
318
	echo "==> end of list.\n";
319
}
320

    
321
function returnlastchar($command) {
322
	$commandlen = strlen($command);
323
	$endofstring = substr($command, ($commandlen-1));
324
	return $endofstring;
325
}
326

    
327
function returnfirstchar($command) {
328
	$commandlen = strlen($command);
329
	$endofstring = substr($command, 0, 1);
330
	return $endofstring;
331
}
332

    
333
function str_replace_all($search,$replace,$subject) {
334
	while (strpos($subject,$search)!==false) {
335
		$subject = str_replace($search,$replace,$subject);
336
	}
337
	return $subject;
338
}
339

    
340
function playback_text($playback_file_contents) {
341
	$playback_file_split = explode("\n", $playback_file_contents);
342
	$playback_text  = "require_once('functions.inc');\n";
343
	$playback_text .= "require_once('globals.inc');\n";
344
	$playback_text .= "require_once('config.inc');\n";
345
	$toquote = '"';
346
	$toquotereplace = '\\"';
347
	foreach ($playback_file_split as $pfs) {
348
		$firstchar = returnfirstchar($pfs);
349
		$currentline = $pfs;
350
		if ($firstchar == "!") {
351
			/* XXX: encode " in $pfs */
352
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
353
			$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
354
		} else if ($firstchar == "=") {
355
			/* XXX: encode " in $pfs */
356
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
357
			$currentline   .= str_replace("!", "system(\"", $pfsa) . "\");\n";
358
		} else {
359
			$playback_text .= $pfs . "\n";
360
		}
361
	}
362
	global $config;
363
	eval($playback_text);
364
}
365

    
366
function playback_file($playback_file) {
367
	$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
368
	playback_text($playback_file_contents);
369
}
370

    
371
?>
(11-11/23)