Project

General

Profile

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

    
3
<?php
4

    
5
require_once("globals.inc");
6
echo "Starting the {$g['product_name']} developer shell";
7
require_once("functions.inc");
8
echo ".";
9
require_once("config.inc");
10
echo ".";
11
require_once("util.inc");
12
echo ".";
13

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

    
26
function pipe_cmd($command, $text_to_pipe) {
27
	$descriptorspec = array(
28
		0 => array("pipe", "r"),  // stdin
29
		1 => array("pipe", "w"),  // stdout
30
		2 => array("pipe", "w")); // stderr ?? instead of a file
31

    
32
	$fd = proc_open("$command", $descriptorspec, $pipes);
33
	if (is_resource($fd)) {
34
		fwrite($pipes[0], "{$text_to_pipe}");
35
		fclose($pipes[0]);
36
		while ($s= fgets($pipes[1], 1024)) {
37
			// read from the pipe
38
			$buffer .= $s;
39
		}
40
		fclose($pipes[1]);
41
		fclose($pipes[2]);
42
	}
43
	return $buffer;
44
}
45

    
46
if (!function_exists("readline")) {
47
	function readline() {
48
		$fp = fopen('php://stdin', 'r');
49
		$textinput = chop(fgets($fp));
50
		fclose($fp);
51
		return $textinput;
52
	}
53
}
54

    
55
function more($text, $count=24) {
56
	$counter=0;
57
	$lines = explode("\n", $text);
58
	foreach ($lines as $line) {
59
		if ($counter > $count) {
60
			echo "Press RETURN to continue ...";
61
			$fp = fopen('php://stdin', 'r');
62
			$pressreturn = chop(fgets($fp));
63
			if ($pressreturn == "q" || $pressreturn == "quit") {
64
				return;
65
			}
66
			fclose($fp);
67
			$counter = 0;
68
		}
69
		echo "{$line}\n";
70
		$counter++;
71
	}
72
}
73

    
74
function show_help() {
75

    
76
$show_help_text = <<<EOF
77

    
78
	Enter a series of commands and then execute the set with "exec".
79

    
80
	For example:
81
	echo "foo"; // php command
82
	echo "foo2"; // php command
83
	! echo "heh" # shell command
84
	exec
85

    
86
	Example commands:
87

    
88
	record <recordingfilename>
89
	stoprecording
90
	showrecordings
91

    
92
	parse_config(true);  # reloads the \$config array
93

    
94
	\$temp = print_r(\$config, true);
95
	more(\$temp);
96

    
97
	/* to output a configuration array */
98
	print_r(\$config);
99

    
100
	/* to output the interfaces configuration portion of config.xml */
101
	print_r(\$config['interfaces']);
102

    
103
	/* to output the dhcp server configuration */
104
	print_r(\$config['dhcpd']);
105

    
106
	/* to exit the {$g['product_name']} developer shell */
107
	exit
108

    
109
	/* to output supported wireless modes for an interface */
110
	print_r(get_wireless_modes(\"ath0\"));
111

    
112
	/* to enable SSH */
113
	\$config['system']['enablesshd'] = true;
114

    
115
	/* change OPTX to the OPT interface name such as BACKHAUL */
116
	\$config['interfaces']['optx']['wireless']['standard'] = "11a";
117
	\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
118
	\$config['interfaces']['optx']['wireless']['channel'] = "6";
119

    
120
	/* to enable dhcp server for an optx interface */
121
	\$config['dhcpd']['optx']['enable'] = true;
122
	\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
123
	\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
124

    
125
	/* to disable the firewall filter */
126
	\$config['system']['disablefilter'] = true;
127

    
128
	/* to enable an interface and configure it as a DHCP client */
129
	\$config['interfaces']['optx']['disabled'] = false;
130
	\$config['interfaces']['optx']['ipaddr'] = "dhcp";
131

    
132
	/* to enable an interface and set a static IPv4 address */
133
	\$config['interfaces']['wan']['enable'] = true;
134
	\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
135
	\$config['interfaces']['wan']['subnet'] = "24";
136

    
137
	/* to save out the new configuration (config.xml) */
138
	write_config();
139

    
140
	/* to reboot the system after saving */
141
	system_reboot_sync();
142

    
143
EOF;
144

    
145
	more($show_help_text);
146

    
147
}
148

    
149
$fp = fopen('php://stdin', 'r');
150

    
151
echo ".\n\n";
152

    
153
$pkg_interface='console';
154

    
155
$shell_active = true;
156
$tccommands = array();
157

    
158
function completion($string, $index) {
159
	global $tccommands;
160
	return $tccommands;
161
}
162

    
163
readline_completion_function("completion");
164

    
165
function get_playback_files() {
166
	$playback_files = array();
167
	$files = scandir("/etc/phpshellsessions/");
168
	foreach ($files as $file) {
169
		if ($file <> "." && $file <> "..") {
170
			$playback_files[] = $file;
171
		}
172
	}
173
	return $playback_files;
174
}
175

    
176
if ($argc < 2) {
177
	echo "Welcome to the {$g['product_name']} developer shell\n";
178
	echo "\nType \"help\" to show common usage scenarios.\n";
179
	echo "\nAvailable playback commands:\n     ";
180
	$tccommands[] = "playback";
181
	$playback_files = get_playback_files();
182
	foreach ($playback_files as $pbf) {
183
		echo "{$pbf} ";
184
		if (function_exists("readline_add_history")) {
185
			readline_add_history("playback $pbf");
186
			$tccommands[] = "$pbf";
187
		}
188
	}
189
	echo "\n\n";
190
}
191

    
192
$recording = false;
193
$playback_file_split = array();
194
$playbackbuffer = "";
195

    
196
if ($argv[1]=="playback" or $argv[1]=="run") {
197
	if (empty($argv[2]) || !file_exists("/etc/phpshellsessions/" . basename($argv[2]))) {
198
		echo "Error: Invalid playback file specified.\n\n";
199
		show_recordings();
200
		exit(-1);
201
	}
202
	playback_file(basename($argv[2]));
203
	exit;
204
}
205

    
206
// Define more commands
207
$tccommands[] = "exit";
208
$tccommands[] = "quit";
209
$tccommands[] = "?";
210
$tccommands[] = "exec";
211
$tccommands[] = "stoprecording";
212
$tccommands[] = "showrecordings";
213
$tccommands[] = "record";
214
$tccommands[] = "reset";
215
$tccommands[] = "master";
216
$tccommands[] = "RELENG_1_2";
217

    
218
while ($shell_active == true) {
219
	$command = readline("{$g['product_name']} shell: ");
220
	readline_add_history($command);
221
	$command_split = explode(" ", $command);
222
	$first_command = $command_split[0];
223
	if ($first_command == "playback" || $first_command == "run") {
224
		$playback_file = $command_split[1];
225
		if (!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
226
			$command = "";
227
			echo "Could not locate playback file.\n";
228
		} else {
229
			$command = "";
230
			echo "\nPlayback of file {$command_split[1]} started.\n\n";
231
			playback_file("{$playback_file}");
232
			continue;
233
		}
234
	}
235
	if ($first_command == "exit" or $first_command == "quit") {
236
		die;
237
	}
238
	if ($first_command == "help" or $first_command == "?") {
239
		show_help();
240
		$playbackbuffer = "";
241
		continue;
242
	}
243
	if ($first_command == "exec" or $first_command == "exec;") {
244
		playback_text($playbackbuffer);
245
		$playbackbuffer = "";
246
		continue;
247
	}
248
	if ($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
249
		if ($recording) {
250
			fwrite($recording_fd, $playbackbuffer);
251
			fclose($recording_fd);
252
			$command = "";
253
			conf_mount_ro();
254
			echo "Recording stopped.\n";
255
			$recording = false;
256
		} else {
257
			echo "No recording session in progress.\n";
258
			$command = "";
259
		}
260
	}
261
	if ($first_command == "showrecordings") {
262
		show_recordings();
263
		$command = "";
264
	}
265
	if ($first_command == "reset") {
266
		$playbackbuffer = "";
267
		echo "\nBuffer reset.\n\n";
268
		continue;
269
	}
270
	if ($first_command == "record") {
271
		if (!$command_split[1]) {
272
			echo "usage: record playbackname\n";
273
			echo "\tplaybackname will be created in /etc/phpshellsessions.\n";
274
			$command = "";
275
		} else {
276
			/* time to record */
277
			conf_mount_rw();
278
			safe_mkdir("/etc/phpshellsessions");
279
			$recording_fn = basename($command_split[1]);
280
			$recording_fd = fopen("/etc/phpshellsessions/{$recording_fn}","w");
281
			if (!$recording_fd) {
282
				echo "Could not start recording session.\n";
283
				$command = "";
284
			} else {
285
				$recording = true;
286
				echo "Recording of {$recording_fn} started.\n";
287
				$command = "";
288
			}
289
		}
290
	}
291
	$playbackbuffer .= $command . "\n";
292
}
293

    
294
function show_recordings() {
295
	echo "==> Sessions available for playback are:\n";
296
	$playback_files = get_playback_files();
297
	foreach (get_playback_files() as $pbf) {
298
		echo "{$pbf} ";
299
	}
300
	echo "\n\n";
301
	echo "==> end of list.\n";
302
}
303

    
304
function returnlastchar($command) {
305
	$commandlen = strlen($command);
306
	$endofstring = substr($command, ($commandlen-1));
307
	return $endofstring;
308
}
309

    
310
function returnfirstchar($command) {
311
	$commandlen = strlen($command);
312
	$endofstring = substr($command, 0, 1);
313
	return $endofstring;
314
}
315

    
316
function str_replace_all($search,$replace,$subject) {
317
	while (strpos($subject,$search)!==false) {
318
		$subject = str_replace($search,$replace,$subject);
319
	}
320
	return $subject;
321
}
322

    
323
function playback_text($playback_file_contents) {
324
	$playback_file_split = explode("\n", $playback_file_contents);
325
	$playback_text  = "require_once('functions.inc');\n";
326
	$playback_text .= "require_once('globals.inc');\n";
327
	$playback_text .= "require_once('config.inc');\n";
328
	$toquote = '"';
329
	$toquotereplace = '\\"';
330
	foreach ($playback_file_split as $pfs) {
331
		$firstchar = returnfirstchar($pfs);
332
		$currentline = $pfs;
333
		if ($firstchar == "!") {
334
			/* XXX: encode " in $pfs */
335
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
336
			$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
337
		} else if ($firstchar == "=") {
338
			/* XXX: encode " in $pfs */
339
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
340
			$currentline   .= str_replace("!", "system(\"", $pfsa) . "\");\n";
341
		} else {
342
			$playback_text .= $pfs . "\n";
343
		}
344
	}
345
	global $config;
346
	eval($playback_text);
347
}
348

    
349
function playback_file($playback_file) {
350
	$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
351
	playback_text($playback_file_contents);
352
}
353

    
354
?>
(11-11/23)