1 |
d776e077
|
Scott Ullrich
|
#!/usr/local/bin/php -f
|
2 |
|
|
|
3 |
|
|
<?php
|
4 |
|
|
|
5 |
8a7712ea
|
Scott Ullrich
|
require_once("globals.inc");
|
6 |
eb95ba84
|
Scott Ullrich
|
echo "Starting the {$g['product_name']} shell system";
|
7 |
d776e077
|
Scott Ullrich
|
$g['booting'] = true;
|
8 |
8a7712ea
|
Scott Ullrich
|
require_once("functions.inc");
|
9 |
d776e077
|
Scott Ullrich
|
echo ".";
|
10 |
8a7712ea
|
Scott Ullrich
|
require_once("config.inc");
|
11 |
d776e077
|
Scott Ullrich
|
echo ".";
|
12 |
8a7712ea
|
Scott Ullrich
|
require_once("util.inc");
|
13 |
415c850d
|
Scott Ullrich
|
echo ".";
|
14 |
d776e077
|
Scott Ullrich
|
$g['booting'] = false;
|
15 |
|
|
|
16 |
7116ab7f
|
Scott Ullrich
|
$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 |
50fa05d1
|
Scott Ullrich
|
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 |
3d3be836
|
Scott Ullrich
|
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 |
cd8ca22f
|
Scott Ullrich
|
function more($text, $count=24) {
|
58 |
5003c48a
|
Scott Ullrich
|
$counter=0;
|
59 |
cfbfd941
|
smos
|
$lines = explode("\n", $text);
|
60 |
5003c48a
|
Scott Ullrich
|
foreach($lines as $line) {
|
61 |
cd8ca22f
|
Scott Ullrich
|
if($counter > $count) {
|
62 |
5003c48a
|
Scott Ullrich
|
echo "Press RETURN to continue ...";
|
63 |
|
|
$fp = fopen('php://stdin', 'r');
|
64 |
|
|
$pressreturn = chop(fgets($fp));
|
65 |
cd8ca22f
|
Scott Ullrich
|
if($pressreturn == "q" || $pressreturn == "quit")
|
66 |
|
|
return;
|
67 |
5003c48a
|
Scott Ullrich
|
fclose($fp);
|
68 |
|
|
$counter = 0;
|
69 |
|
|
}
|
70 |
|
|
echo "{$line}\n";
|
71 |
|
|
$counter++;
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
|
75 |
46d53988
|
Scott Ullrich
|
function show_help() {
|
76 |
60ff8601
|
Scott Ullrich
|
|
77 |
|
|
$show_help_text = <<<EOF
|
78 |
415c850d
|
Scott Ullrich
|
|
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 |
60ff8601
|
Scott Ullrich
|
Example commands:
|
88 |
02105da4
|
Scott Ullrich
|
|
89 |
|
|
startrecording <recordingfilename>
|
90 |
|
|
stoprecording <recordingfilename>
|
91 |
|
|
showrecordings
|
92 |
|
|
|
93 |
60ff8601
|
Scott Ullrich
|
parse_config(true); # reloads the \$config array
|
94 |
5003c48a
|
Scott Ullrich
|
|
95 |
60ff8601
|
Scott Ullrich
|
\$temp = print_r(\$config, true);
|
96 |
|
|
more(\$temp);
|
97 |
5003c48a
|
Scott Ullrich
|
|
98 |
60ff8601
|
Scott Ullrich
|
/* to output a configuration array */
|
99 |
|
|
print_r(\$config);
|
100 |
|
|
|
101 |
4c12ef0a
|
Scott Ullrich
|
/* to output the interfaces configuration portion of config.xml */
|
102 |
60ff8601
|
Scott Ullrich
|
print_r(\$config['interfaces']);
|
103 |
46d53988
|
Scott Ullrich
|
|
104 |
60ff8601
|
Scott Ullrich
|
/* to output the dhcp server configuration */
|
105 |
|
|
print_r(\$config['dhcpd']);
|
106 |
415c850d
|
Scott Ullrich
|
|
107 |
b9e3a295
|
Scott Ullrich
|
/* to exit the php {$g['product_name']} shell */
|
108 |
60ff8601
|
Scott Ullrich
|
exit
|
109 |
46d53988
|
Scott Ullrich
|
|
110 |
60ff8601
|
Scott Ullrich
|
/* to output supported wireless modes for an interface */
|
111 |
|
|
print_r(get_wireless_modes(\"ath0\"));
|
112 |
46d53988
|
Scott Ullrich
|
|
113 |
60ff8601
|
Scott Ullrich
|
/* to enable SSH */
|
114 |
|
|
\$config['system']['enablesshd'] = true;
|
115 |
46d53988
|
Scott Ullrich
|
|
116 |
60ff8601
|
Scott Ullrich
|
/* change OPTX to the OPT interface name such as BACKHAUL */
|
117 |
00ea455f
|
Scott Ullrich
|
\$config['interfaces']['optx']['wireless']['standard'] = "11a";
|
118 |
|
|
\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
|
119 |
|
|
\$config['interfaces']['optx']['wireless']['channel'] = "6";
|
120 |
46d53988
|
Scott Ullrich
|
|
121 |
60ff8601
|
Scott Ullrich
|
/* to enable dhcp server for an optx interface */
|
122 |
|
|
\$config['dhcpd']['optx']['enable'] = true;
|
123 |
00ea455f
|
Scott Ullrich
|
\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
|
124 |
|
|
\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
|
125 |
46d53988
|
Scott Ullrich
|
|
126 |
60ff8601
|
Scott Ullrich
|
/* to disable the firewall filter */
|
127 |
|
|
\$config['system']['disablefilter'] = true;
|
128 |
46d53988
|
Scott Ullrich
|
|
129 |
60ff8601
|
Scott Ullrich
|
/* to enable an interface and set it for dhcp */
|
130 |
|
|
\$config['interfaces']['optx']['disabled'] = false;
|
131 |
00ea455f
|
Scott Ullrich
|
\$config['interfaces']['optx']['ipaddr'] = "dhcp";
|
132 |
46d53988
|
Scott Ullrich
|
|
133 |
60ff8601
|
Scott Ullrich
|
/* to enable an interface and set a static ip address */
|
134 |
|
|
\$config['interfaces']['wan']['disabled'] = false;
|
135 |
00ea455f
|
Scott Ullrich
|
\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
|
136 |
|
|
\$config['interfaces']['wan']['subnet'] = "24";
|
137 |
46d53988
|
Scott Ullrich
|
|
138 |
60ff8601
|
Scott Ullrich
|
/* to save out the new configuration (config.xml) */
|
139 |
|
|
write_config();
|
140 |
46d53988
|
Scott Ullrich
|
|
141 |
60ff8601
|
Scott Ullrich
|
/* to reboot the system after saving */
|
142 |
328ab0ae
|
Scott Ullrich
|
system_reboot_sync();
|
143 |
46d53988
|
Scott Ullrich
|
|
144 |
60ff8601
|
Scott Ullrich
|
EOF;
|
145 |
|
|
|
146 |
|
|
more($show_help_text);
|
147 |
|
|
|
148 |
46d53988
|
Scott Ullrich
|
}
|
149 |
|
|
|
150 |
d776e077
|
Scott Ullrich
|
$fp = fopen('php://stdin', 'r');
|
151 |
|
|
|
152 |
|
|
echo ".\n\n";
|
153 |
|
|
|
154 |
1b8a2f5c
|
Scott Ullrich
|
$pkg_interface='console';
|
155 |
|
|
|
156 |
d776e077
|
Scott Ullrich
|
$shell_active = true;
|
157 |
f6907eb4
|
sullrich
|
$tccommands = array();
|
158 |
|
|
|
159 |
|
|
function completion($string, $index) {
|
160 |
|
|
global $tccommands;
|
161 |
|
|
return $tccommands;
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
readline_completion_function("completion");
|
165 |
d776e077
|
Scott Ullrich
|
|
166 |
415c850d
|
Scott Ullrich
|
if($argc < 2) {
|
167 |
b9e3a295
|
Scott Ullrich
|
echo "Welcome to the {$g['product_name']} php shell system\n";
|
168 |
415c850d
|
Scott Ullrich
|
echo "Written by Scott Ullrich (sullrich@gmail.com)\n";
|
169 |
b1de6b8b
|
Scott Ullrich
|
echo "\nType \"help\" to show common usage scenarios.\n";
|
170 |
|
|
echo "\nAvailable playback commands:\n ";
|
171 |
|
|
$files = scandir("/etc/phpshellsessions/");
|
172 |
f6907eb4
|
sullrich
|
$tccommands[] = "playback";
|
173 |
b1de6b8b
|
Scott Ullrich
|
foreach($files as $file) {
|
174 |
f6907eb4
|
sullrich
|
if($file <> "." and $file <> "..") {
|
175 |
b1de6b8b
|
Scott Ullrich
|
echo $file . " ";
|
176 |
f6907eb4
|
sullrich
|
if(function_exists("readline_add_history")) {
|
177 |
|
|
readline_add_history("playback $file");
|
178 |
|
|
$tccommands[] = "$file";
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
b1de6b8b
|
Scott Ullrich
|
}
|
182 |
|
|
echo "\n\n";
|
183 |
415c850d
|
Scott Ullrich
|
}
|
184 |
d776e077
|
Scott Ullrich
|
|
185 |
34af0cab
|
Scott Ullrich
|
$recording = false;
|
186 |
93a3d242
|
Scott Ullrich
|
$playback_file_split = array();
|
187 |
|
|
$playbackbuffer = "";
|
188 |
|
|
|
189 |
415c850d
|
Scott Ullrich
|
if($argv[1]=="playback" or $argv[1]=="run") {
|
190 |
47643f5b
|
Scott Ullrich
|
if(!file_exists("/etc/phpshellsessions/{$argv[2]}")) {
|
191 |
|
|
echo "Could not locate playback file.";
|
192 |
|
|
exit;
|
193 |
|
|
}
|
194 |
415c850d
|
Scott Ullrich
|
playback_file($argv[2]);
|
195 |
|
|
exit;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
f6907eb4
|
sullrich
|
// 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 |
e788b01d
|
sullrich
|
$tccommands[] = "master";
|
209 |
|
|
$tccommands[] = "RELENG_1_2";
|
210 |
f6907eb4
|
sullrich
|
|
211 |
d776e077
|
Scott Ullrich
|
while($shell_active == true) {
|
212 |
b9e3a295
|
Scott Ullrich
|
$command = readline("{$g['product_name']} shell: ");
|
213 |
415c850d
|
Scott Ullrich
|
readline_add_history($command);
|
214 |
cfbfd941
|
smos
|
$command_split = explode(" ", $command);
|
215 |
415c850d
|
Scott Ullrich
|
$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 |
4c12ef0a
|
Scott Ullrich
|
if($first_command == "exit" or $first_command == "quit")
|
229 |
9f2820d8
|
Scott Ullrich
|
die;
|
230 |
|
|
if($first_command == "help" or $first_command == "?") {
|
231 |
|
|
show_help();
|
232 |
|
|
$playbackbuffer = "";
|
233 |
|
|
continue;
|
234 |
|
|
}
|
235 |
415c850d
|
Scott Ullrich
|
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 |
4c12ef0a
|
Scott Ullrich
|
show_recordings();
|
255 |
|
|
$command = "";
|
256 |
415c850d
|
Scott Ullrich
|
}
|
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 |
34af0cab
|
Scott Ullrich
|
$command = "";
|
274 |
415c850d
|
Scott Ullrich
|
} else {
|
275 |
|
|
$recording = true;
|
276 |
|
|
echo "Recording of {$command_split[1]} started.\n";
|
277 |
34af0cab
|
Scott Ullrich
|
$command = "";
|
278 |
|
|
}
|
279 |
93a3d242
|
Scott Ullrich
|
}
|
280 |
415c850d
|
Scott Ullrich
|
}
|
281 |
|
|
$playbackbuffer .= $command . "\n";
|
282 |
93a3d242
|
Scott Ullrich
|
}
|
283 |
|
|
|
284 |
4c12ef0a
|
Scott Ullrich
|
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 |
93a3d242
|
Scott Ullrich
|
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 |
d776e077
|
Scott Ullrich
|
}
|
311 |
46d53988
|
Scott Ullrich
|
|
312 |
415c850d
|
Scott Ullrich
|
function playback_text($playback_file_contents) {
|
313 |
cfbfd941
|
smos
|
$playback_file_split = explode("\n", $playback_file_contents);
|
314 |
8586a962
|
Scott Ullrich
|
$playback_text = "require_once('functions.inc');\n";
|
315 |
|
|
$playback_text .= "require_once('globals.inc');\n";
|
316 |
|
|
$playback_text .= "require_once('config.inc');\n";
|
317 |
415c850d
|
Scott Ullrich
|
$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 |
cfbfd941
|
smos
|
?>
|