Project

General

Profile

Download (9.57 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2
 
3
<?php
4

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

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

    
28
function pipe_cmd($command, $text_to_pipe) {
29
	$descriptorspec = array(
30
	    0 => array("pipe", "r"),  // stdin
31
	    1 => array("pipe", "w"),  // stdout
32
	    2 => array("pipe", "w")); // stderr ?? instead of a file
33
	
34
	$fd = proc_open("$command", $descriptorspec, $pipes);
35
	if (is_resource($fd)) {
36
	        fwrite($pipes[0], "{$text_to_pipe}");
37
	        fclose($pipes[0]);
38
	        while($s= fgets($pipes[1], 1024)) {
39
	          // read from the pipe
40
	          $buffer .= $s;
41
	        }
42
	        fclose($pipes[1]);
43
	        fclose($pipes[2]);
44
	}
45
	return $buffer;
46
}
47

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

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

    
75
function show_help() {
76

    
77
$show_help_text = <<<EOF
78

    
79
	Enter a series of commands and then execute the set with "exec".
80
	
81
	For example:
82
	echo "foo"; // php command
83
	echo "foo2"; // php command
84
	! echo "heh" # shell command
85
	exec
86

    
87
	Example commands:
88

    
89
	startrecording <recordingfilename>
90
	stoprecording <recordingfilename>
91
	showrecordings
92

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

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

    
98
	/* to output a configuration array */
99
	print_r(\$config);
100
	
101
	/* to output the interfaces configuration portion of config.xml */
102
	print_r(\$config['interfaces']);
103
	
104
	/* to output the dhcp server configuration */
105
	print_r(\$config['dhcpd']);
106

    
107
	/* to exit the php {$g['product_name']} shell */
108
	exit
109
	
110
	/* to output supported wireless modes for an interface */
111
	print_r(get_wireless_modes(\"ath0\"));
112
	
113
	/* to enable SSH */
114
	\$config['system']['enablesshd'] = true;
115
	
116
	/* change OPTX to the OPT interface name such as BACKHAUL */
117
	\$config['interfaces']['optx']['wireless']['standard'] = "11a";
118
	\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
119
	\$config['interfaces']['optx']['wireless']['channel'] = "6";
120
	
121
	/* to enable dhcp server for an optx interface */
122
	\$config['dhcpd']['optx']['enable'] = true;
123
	\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
124
	\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
125
	
126
	/* to disable the firewall filter */
127
	\$config['system']['disablefilter'] = true;
128
	
129
	/* to enable an interface and set it for dhcp */
130
	\$config['interfaces']['optx']['disabled'] = false;
131
	\$config['interfaces']['optx']['ipaddr'] = "dhcp";
132
	
133
	/* to enable an interface and set a static ip address */
134
	\$config['interfaces']['wan']['disabled'] = false;
135
	\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
136
	\$config['interfaces']['wan']['subnet'] = "24";
137
	
138
	/* to save out the new configuration (config.xml) */
139
	write_config();
140
	
141
	/* to reboot the system after saving */
142
	system_reboot_sync();
143
	
144
EOF;
145

    
146
	more($show_help_text);
147
 	
148
}
149

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

    
152
echo ".\n\n";
153

    
154
$pkg_interface='console';
155

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

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

    
164
readline_completion_function("completion");
165

    
166
if($argc < 2) {
167
	echo "Welcome to the {$g['product_name']} php shell system\n";
168
	echo "Written by Scott Ullrich (sullrich@gmail.com)\n";
169
	echo "\nType \"help\" to show common usage scenarios.\n";
170
	echo "\nAvailable playback commands:\n     ";
171
	$files = scandir("/etc/phpshellsessions/");
172
	$tccommands[] = "playback";
173
	foreach($files as $file) {
174
		if($file <> "." and $file <> "..") {
175
			echo $file . " ";
176
			if(function_exists("readline_add_history")) {
177
				readline_add_history("playback $file");
178
				$tccommands[] = "$file";
179
			}
180
		}
181
	}
182
	echo "\n\n";
183
}
184

    
185
$recording = false;
186
$playback_file_split = array();
187
$playbackbuffer = "";
188

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

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

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

    
284
function show_recordings() {
285
	conf_mount_rw();
286
	safe_mkdir("/etc/phpshellsessions");
287
	if($recording) 
288
		conf_mount_ro();
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
	return $subject;
310
}
311

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

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

    
342
?>
(6-6/13)