Project

General

Profile

Download (9.21 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
	startrecording <recordingfilename>
89
	stoprecording <recordingfilename>
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
if ($argc < 2) {
166
	echo "Welcome to the {$g['product_name']} developer shell\n";
167
	echo "\nType \"help\" to show common usage scenarios.\n";
168
	echo "\nAvailable playback commands:\n     ";
169
	$files = scandir("/etc/phpshellsessions/");
170
	$tccommands[] = "playback";
171
	foreach ($files as $file) {
172
		if ($file <> "." and $file <> "..") {
173
			echo $file . " ";
174
			if (function_exists("readline_add_history")) {
175
				readline_add_history("playback $file");
176
				$tccommands[] = "$file";
177
			}
178
		}
179
	}
180
	echo "\n\n";
181
}
182

    
183
$recording = false;
184
$playback_file_split = array();
185
$playbackbuffer = "";
186

    
187
if ($argv[1]=="playback" or $argv[1]=="run") {
188
	if (!file_exists("/etc/phpshellsessions/{$argv[2]}")) {
189
		echo "Could not locate playback file.";
190
		exit;
191
	}
192
	playback_file($argv[2]);
193
	exit;
194
}
195

    
196
// Define more commands
197
$tccommands[] = "exit";
198
$tccommands[] = "quit";
199
$tccommands[] = "?";
200
$tccommands[] = "exec";
201
$tccommands[] = "startrecording";
202
$tccommands[] = "stoprecording";
203
$tccommands[] = "showrecordings";
204
$tccommands[] = "record";
205
$tccommands[] = "reset";
206
$tccommands[] = "master";
207
$tccommands[] = "RELENG_1_2";
208

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

    
283
function show_recordings() {
284
	conf_mount_rw();
285
	safe_mkdir("/etc/phpshellsessions");
286
	if ($recording) {
287
		conf_mount_ro();
288
	}
289
	echo "==> Sessions available for playback are:\n";
290
	system("cd /etc/phpshellsessions && ls /etc/phpshellsessions");
291
	echo "==> end of list.\n";
292
}
293

    
294
function returnlastchar($command) {
295
	$commandlen = strlen($command);
296
	$endofstring = substr($command, ($commandlen-1));
297
	return $endofstring;
298
}
299

    
300
function returnfirstchar($command) {
301
	$commandlen = strlen($command);
302
	$endofstring = substr($command, 0, 1);
303
	return $endofstring;
304
}
305

    
306
function str_replace_all($search,$replace,$subject) {
307
	while (strpos($subject,$search)!==false) {
308
		$subject = str_replace($search,$replace,$subject);
309
	}
310
	return $subject;
311
}
312

    
313
function playback_text($playback_file_contents) {
314
	$playback_file_split = explode("\n", $playback_file_contents);
315
	$playback_text  = "require_once('functions.inc');\n";
316
	$playback_text .= "require_once('globals.inc');\n";
317
	$playback_text .= "require_once('config.inc');\n";
318
	$toquote = '"';
319
	$toquotereplace = '\\"';
320
	foreach ($playback_file_split as $pfs) {
321
		$firstchar = returnfirstchar($pfs);
322
		$currentline = $pfs;
323
		if ($firstchar == "!") {
324
			/* XXX: encode " in $pfs */
325
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
326
			$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
327
		} else if ($firstchar == "=") {
328
			/* XXX: encode " in $pfs */
329
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
330
			$currentline   .= str_replace("!", "system(\"", $pfsa) . "\");\n";
331
		} else {
332
			$playback_text .= $pfs . "\n";
333
		}
334
	}
335
	global $config;
336
	eval($playback_text);
337
}
338

    
339
function playback_file($playback_file) {
340
	$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
341
	playback_text($playback_file_contents);
342
}
343

    
344
?>
(11-11/23)