Project

General

Profile

Download (8.94 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

    
158
if($argc < 2) {
159
	echo "Welcome to the {$g['product_name']} php shell system\n";
160
	echo "Written by Scott Ullrich (sullrich@gmail.com)\n";
161
	echo "\nType \"help\" to show common usage scenarios.\n";
162
	echo "\nAvailable playback commands:\n     ";
163
	$files = scandir("/etc/phpshellsessions/");
164
	foreach($files as $file) {
165
		if($file <> "." and $file <> "..")
166
			echo $file . " ";
167
	}
168
	echo "\n\n";
169
}
170

    
171
$recording = false;
172
$playback_file_split = array();
173
$playbackbuffer = "";
174

    
175
if($argv[1]=="playback" or $argv[1]=="run") { 
176
	if(!file_exists("/etc/phpshellsessions/{$argv[2]}")) {
177
		echo "Could not locate playback file.";
178
		exit;
179
	}
180
	playback_file($argv[2]);
181
	exit;
182
}
183

    
184
while($shell_active == true) {
185
	$command = readline("{$g['product_name']} shell: ");
186
	readline_add_history($command);
187
    $command_split = split(" ", $command);
188
    $first_command = $command_split[0];	
189
	if($first_command == "playback" || $first_command == "run") {
190
		$playback_file = $command_split[1];
191
		if(!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
192
			$command = "";
193
			echo "Could not locate playback file.\n";
194
		} else {
195
			$command = "";
196
			echo "\nPlayback of file {$command_split[1]} started.\n\n";
197
			playback_file("{$playback_file}");
198
			continue;
199
		}
200
	}
201
	if($first_command == "exit" or $first_command == "quit") 
202
		die;
203
	if($first_command == "help" or $first_command == "?") {
204
		show_help();
205
		$playbackbuffer = "";
206
		continue;
207
	}
208
	if($first_command == "exec" or $first_command == "exec;") {
209
		playback_text($playbackbuffer);
210
		$playbackbuffer = "";
211
		continue;
212
	}
213
	if($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
214
		if($recording) {
215
			fwrite($recording_fd, $playbackbuffer);
216
			fclose($recording_fd);
217
			$command = "";
218
			conf_mount_ro();
219
			echo "Recording stopped.\n";
220
			$recording = false; 
221
		} else {
222
			echo "No recording session in progress.\n";
223
			$command = "";
224
		}
225
	}
226
	if($first_command == "showrecordings") {
227
		show_recordings();
228
		$command = "";		
229
	}
230
	if($first_command == "reset") {
231
		$playbackbuffer = "";
232
		echo "\nBuffer reset.\n\n";
233
		continue;
234
	}
235
	if($first_command == "record") {
236
		if(!$command_split[1]) {
237
			echo "usage: record playbackname\n";
238
			$command = "";
239
		} else {
240
			/* time to record */
241
			conf_mount_rw();
242
			safe_mkdir("/etc/phpshellsessions");
243
			$recording_fd = fopen("/etc/phpshellsessions/{$command_split[1]}","w");
244
			if(!$recording_fd) {
245
				echo "Could not start recording session.\n";
246
				$command = "";
247
			} else { 
248
				$recording = true;
249
				echo "Recording of {$command_split[1]} started.\n";
250
				$command = "";
251
			}
252
		}
253
	}
254
	$playbackbuffer .= $command . "\n";
255
}
256

    
257
function show_recordings() {
258
	conf_mount_rw();
259
	safe_mkdir("/etc/phpshellsessions");
260
	if($recording) 
261
		conf_mount_ro();
262
	echo "==> Sessions available for playback are:\n";
263
	system("cd /etc/phpshellsessions && ls /etc/phpshellsessions");
264
	echo "==> end of list.\n";	
265
}
266

    
267
function returnlastchar($command) {
268
	$commandlen = strlen($command);
269
	$endofstring = substr($command, ($commandlen-1));
270
	return $endofstring; 
271
}
272

    
273
function returnfirstchar($command) {
274
	$commandlen = strlen($command);
275
	$endofstring = substr($command, 0, 1);
276
	return $endofstring; 
277
}
278

    
279
function str_replace_all($search,$replace,$subject) {
280
	while(strpos($subject,$search)!==false) 
281
		$subject = str_replace($search,$replace,$subject);
282
	return $subject;
283
}
284

    
285
function playback_text($playback_file_contents) {
286
	$playback_file_split = split("\n", $playback_file_contents);
287
	$playback_text  = "require_once('functions.inc');\n";
288
	$playback_text .= "require_once('globals.inc');\n";
289
	$playback_text .= "require_once('config.inc');\n";
290
	$toquote = '"';
291
	$toquotereplace = '\\"';	
292
	foreach($playback_file_split as $pfs) {
293
		$firstchar = returnfirstchar($pfs);
294
		$currentline = $pfs;
295
		if($firstchar == "!") {
296
			/* XXX: encode " in $pfs */
297
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
298
			$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
299
		} else if ($firstchar == "=") {
300
			/* XXX: encode " in $pfs */
301
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
302
			$currentline   .= str_replace("!", "system(\"", $pfsa) . "\");\n";
303
		} else {
304
			$playback_text .= $pfs . "\n";
305
		}
306
	}
307
	eval($playback_text);
308
}
309

    
310
function playback_file($playback_file) {
311
	$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
312
	playback_text($playback_file_contents);
313
}
314

    
315
?>
(8-8/15)