1 |
cb7d18d5
|
Renato Botelho
|
#!/usr/local/bin/php-cgi -f
|
2 |
7d61beba
|
Phil Davis
|
|
3 |
d776e077
|
Scott Ullrich
|
<?php
|
4 |
|
|
|
5 |
8a7712ea
|
Scott Ullrich
|
require_once("globals.inc");
|
6 |
f643a1f1
|
Chris Buechler
|
echo "Starting the {$g['product_name']} developer shell";
|
7 |
8a7712ea
|
Scott Ullrich
|
require_once("functions.inc");
|
8 |
d776e077
|
Scott Ullrich
|
echo ".";
|
9 |
8a7712ea
|
Scott Ullrich
|
require_once("config.inc");
|
10 |
d776e077
|
Scott Ullrich
|
echo ".";
|
11 |
8a7712ea
|
Scott Ullrich
|
require_once("util.inc");
|
12 |
415c850d
|
Scott Ullrich
|
echo ".";
|
13 |
d776e077
|
Scott Ullrich
|
|
14 |
7d61beba
|
Phil Davis
|
$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 |
7116ab7f
|
Scott Ullrich
|
|
26 |
50fa05d1
|
Scott Ullrich
|
function pipe_cmd($command, $text_to_pipe) {
|
27 |
|
|
$descriptorspec = array(
|
28 |
7d61beba
|
Phil Davis
|
0 => array("pipe", "r"), // stdin
|
29 |
|
|
1 => array("pipe", "w"), // stdout
|
30 |
|
|
2 => array("pipe", "w")); // stderr ?? instead of a file
|
31 |
|
|
|
32 |
50fa05d1
|
Scott Ullrich
|
$fd = proc_open("$command", $descriptorspec, $pipes);
|
33 |
|
|
if (is_resource($fd)) {
|
34 |
7d61beba
|
Phil Davis
|
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 |
50fa05d1
|
Scott Ullrich
|
}
|
43 |
|
|
return $buffer;
|
44 |
|
|
}
|
45 |
|
|
|
46 |
7d61beba
|
Phil Davis
|
if (!function_exists("readline")) {
|
47 |
3d3be836
|
Scott Ullrich
|
function readline() {
|
48 |
|
|
$fp = fopen('php://stdin', 'r');
|
49 |
|
|
$textinput = chop(fgets($fp));
|
50 |
|
|
fclose($fp);
|
51 |
6fee314b
|
Phil Davis
|
return $textinput;
|
52 |
3d3be836
|
Scott Ullrich
|
}
|
53 |
|
|
}
|
54 |
|
|
|
55 |
cd8ca22f
|
Scott Ullrich
|
function more($text, $count=24) {
|
56 |
7d61beba
|
Phil Davis
|
$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 |
5003c48a
|
Scott Ullrich
|
}
|
73 |
|
|
|
74 |
46d53988
|
Scott Ullrich
|
function show_help() {
|
75 |
60ff8601
|
Scott Ullrich
|
|
76 |
|
|
$show_help_text = <<<EOF
|
77 |
415c850d
|
Scott Ullrich
|
|
78 |
|
|
Enter a series of commands and then execute the set with "exec".
|
79 |
7d61beba
|
Phil Davis
|
|
80 |
415c850d
|
Scott Ullrich
|
For example:
|
81 |
|
|
echo "foo"; // php command
|
82 |
|
|
echo "foo2"; // php command
|
83 |
|
|
! echo "heh" # shell command
|
84 |
|
|
exec
|
85 |
|
|
|
86 |
60ff8601
|
Scott Ullrich
|
Example commands:
|
87 |
02105da4
|
Scott Ullrich
|
|
88 |
d17af2a5
|
jim-p
|
record <recordingfilename>
|
89 |
|
|
stoprecording
|
90 |
02105da4
|
Scott Ullrich
|
showrecordings
|
91 |
|
|
|
92 |
60ff8601
|
Scott Ullrich
|
parse_config(true); # reloads the \$config array
|
93 |
5003c48a
|
Scott Ullrich
|
|
94 |
60ff8601
|
Scott Ullrich
|
\$temp = print_r(\$config, true);
|
95 |
|
|
more(\$temp);
|
96 |
5003c48a
|
Scott Ullrich
|
|
97 |
60ff8601
|
Scott Ullrich
|
/* to output a configuration array */
|
98 |
|
|
print_r(\$config);
|
99 |
7d61beba
|
Phil Davis
|
|
100 |
4c12ef0a
|
Scott Ullrich
|
/* to output the interfaces configuration portion of config.xml */
|
101 |
60ff8601
|
Scott Ullrich
|
print_r(\$config['interfaces']);
|
102 |
7d61beba
|
Phil Davis
|
|
103 |
60ff8601
|
Scott Ullrich
|
/* to output the dhcp server configuration */
|
104 |
|
|
print_r(\$config['dhcpd']);
|
105 |
415c850d
|
Scott Ullrich
|
|
106 |
f643a1f1
|
Chris Buechler
|
/* to exit the {$g['product_name']} developer shell */
|
107 |
60ff8601
|
Scott Ullrich
|
exit
|
108 |
7d61beba
|
Phil Davis
|
|
109 |
60ff8601
|
Scott Ullrich
|
/* to output supported wireless modes for an interface */
|
110 |
|
|
print_r(get_wireless_modes(\"ath0\"));
|
111 |
7d61beba
|
Phil Davis
|
|
112 |
60ff8601
|
Scott Ullrich
|
/* to enable SSH */
|
113 |
|
|
\$config['system']['enablesshd'] = true;
|
114 |
7d61beba
|
Phil Davis
|
|
115 |
60ff8601
|
Scott Ullrich
|
/* change OPTX to the OPT interface name such as BACKHAUL */
|
116 |
00ea455f
|
Scott Ullrich
|
\$config['interfaces']['optx']['wireless']['standard'] = "11a";
|
117 |
|
|
\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
|
118 |
|
|
\$config['interfaces']['optx']['wireless']['channel'] = "6";
|
119 |
7d61beba
|
Phil Davis
|
|
120 |
60ff8601
|
Scott Ullrich
|
/* to enable dhcp server for an optx interface */
|
121 |
|
|
\$config['dhcpd']['optx']['enable'] = true;
|
122 |
00ea455f
|
Scott Ullrich
|
\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
|
123 |
|
|
\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
|
124 |
7d61beba
|
Phil Davis
|
|
125 |
60ff8601
|
Scott Ullrich
|
/* to disable the firewall filter */
|
126 |
|
|
\$config['system']['disablefilter'] = true;
|
127 |
7d61beba
|
Phil Davis
|
|
128 |
51b24a30
|
Chris Buechler
|
/* to enable an interface and configure it as a DHCP client */
|
129 |
60ff8601
|
Scott Ullrich
|
\$config['interfaces']['optx']['disabled'] = false;
|
130 |
00ea455f
|
Scott Ullrich
|
\$config['interfaces']['optx']['ipaddr'] = "dhcp";
|
131 |
7d61beba
|
Phil Davis
|
|
132 |
51b24a30
|
Chris Buechler
|
/* to enable an interface and set a static IPv4 address */
|
133 |
|
|
\$config['interfaces']['wan']['enable'] = true;
|
134 |
00ea455f
|
Scott Ullrich
|
\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
|
135 |
|
|
\$config['interfaces']['wan']['subnet'] = "24";
|
136 |
7d61beba
|
Phil Davis
|
|
137 |
60ff8601
|
Scott Ullrich
|
/* to save out the new configuration (config.xml) */
|
138 |
|
|
write_config();
|
139 |
7d61beba
|
Phil Davis
|
|
140 |
60ff8601
|
Scott Ullrich
|
/* to reboot the system after saving */
|
141 |
328ab0ae
|
Scott Ullrich
|
system_reboot_sync();
|
142 |
7d61beba
|
Phil Davis
|
|
143 |
60ff8601
|
Scott Ullrich
|
EOF;
|
144 |
|
|
|
145 |
|
|
more($show_help_text);
|
146 |
7d61beba
|
Phil Davis
|
|
147 |
46d53988
|
Scott Ullrich
|
}
|
148 |
|
|
|
149 |
d776e077
|
Scott Ullrich
|
$fp = fopen('php://stdin', 'r');
|
150 |
|
|
|
151 |
|
|
echo ".\n\n";
|
152 |
|
|
|
153 |
1b8a2f5c
|
Scott Ullrich
|
$pkg_interface='console';
|
154 |
|
|
|
155 |
d776e077
|
Scott Ullrich
|
$shell_active = true;
|
156 |
f6907eb4
|
sullrich
|
$tccommands = array();
|
157 |
|
|
|
158 |
|
|
function completion($string, $index) {
|
159 |
|
|
global $tccommands;
|
160 |
|
|
return $tccommands;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
readline_completion_function("completion");
|
164 |
d776e077
|
Scott Ullrich
|
|
165 |
36cb313b
|
jim-p
|
function get_playback_files() {
|
166 |
|
|
$playback_files = array();
|
167 |
|
|
$files = scandir("/etc/phpshellsessions/");
|
168 |
|
|
foreach ($files as $file) {
|
169 |
d17af2a5
|
jim-p
|
if ($file <> "." && $file <> "..") {
|
170 |
36cb313b
|
jim-p
|
$playback_files[] = $file;
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
return $playback_files;
|
174 |
|
|
}
|
175 |
|
|
|
176 |
7d61beba
|
Phil Davis
|
if ($argc < 2) {
|
177 |
f643a1f1
|
Chris Buechler
|
echo "Welcome to the {$g['product_name']} developer shell\n";
|
178 |
b1de6b8b
|
Scott Ullrich
|
echo "\nType \"help\" to show common usage scenarios.\n";
|
179 |
|
|
echo "\nAvailable playback commands:\n ";
|
180 |
f6907eb4
|
sullrich
|
$tccommands[] = "playback";
|
181 |
36cb313b
|
jim-p
|
$playback_files = get_playback_files();
|
182 |
|
|
foreach ($playback_files as $pbf) {
|
183 |
|
|
echo "{$pbf} ";
|
184 |
|
|
if (function_exists("readline_add_history")) {
|
185 |
9e4c0b1f
|
NOYB
|
readline_add_history("playback $pbf");
|
186 |
|
|
$tccommands[] = "$pbf";
|
187 |
f6907eb4
|
sullrich
|
}
|
188 |
b1de6b8b
|
Scott Ullrich
|
}
|
189 |
|
|
echo "\n\n";
|
190 |
415c850d
|
Scott Ullrich
|
}
|
191 |
d776e077
|
Scott Ullrich
|
|
192 |
34af0cab
|
Scott Ullrich
|
$recording = false;
|
193 |
93a3d242
|
Scott Ullrich
|
$playback_file_split = array();
|
194 |
|
|
$playbackbuffer = "";
|
195 |
|
|
|
196 |
7d61beba
|
Phil Davis
|
if ($argv[1]=="playback" or $argv[1]=="run") {
|
197 |
36cb313b
|
jim-p
|
if (empty($argv[2]) || !file_exists("/etc/phpshellsessions/" . basename($argv[2]))) {
|
198 |
|
|
echo "Error: Invalid playback file specified.\n\n";
|
199 |
e296be60
|
Chris Buechler
|
show_recordings();
|
200 |
36cb313b
|
jim-p
|
exit(-1);
|
201 |
47643f5b
|
Scott Ullrich
|
}
|
202 |
36cb313b
|
jim-p
|
playback_file(basename($argv[2]));
|
203 |
415c850d
|
Scott Ullrich
|
exit;
|
204 |
|
|
}
|
205 |
|
|
|
206 |
f6907eb4
|
sullrich
|
// 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 |
e788b01d
|
sullrich
|
$tccommands[] = "master";
|
216 |
|
|
$tccommands[] = "RELENG_1_2";
|
217 |
f6907eb4
|
sullrich
|
|
218 |
7d61beba
|
Phil Davis
|
while ($shell_active == true) {
|
219 |
b9e3a295
|
Scott Ullrich
|
$command = readline("{$g['product_name']} shell: ");
|
220 |
415c850d
|
Scott Ullrich
|
readline_add_history($command);
|
221 |
7d61beba
|
Phil Davis
|
$command_split = explode(" ", $command);
|
222 |
|
|
$first_command = $command_split[0];
|
223 |
|
|
if ($first_command == "playback" || $first_command == "run") {
|
224 |
415c850d
|
Scott Ullrich
|
$playback_file = $command_split[1];
|
225 |
7d61beba
|
Phil Davis
|
if (!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
|
226 |
415c850d
|
Scott Ullrich
|
$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 |
7d61beba
|
Phil Davis
|
if ($first_command == "exit" or $first_command == "quit") {
|
236 |
9f2820d8
|
Scott Ullrich
|
die;
|
237 |
7d61beba
|
Phil Davis
|
}
|
238 |
|
|
if ($first_command == "help" or $first_command == "?") {
|
239 |
9f2820d8
|
Scott Ullrich
|
show_help();
|
240 |
|
|
$playbackbuffer = "";
|
241 |
|
|
continue;
|
242 |
|
|
}
|
243 |
7d61beba
|
Phil Davis
|
if ($first_command == "exec" or $first_command == "exec;") {
|
244 |
415c850d
|
Scott Ullrich
|
playback_text($playbackbuffer);
|
245 |
|
|
$playbackbuffer = "";
|
246 |
|
|
continue;
|
247 |
|
|
}
|
248 |
7d61beba
|
Phil Davis
|
if ($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
|
249 |
|
|
if ($recording) {
|
250 |
415c850d
|
Scott Ullrich
|
fwrite($recording_fd, $playbackbuffer);
|
251 |
|
|
fclose($recording_fd);
|
252 |
|
|
$command = "";
|
253 |
|
|
conf_mount_ro();
|
254 |
|
|
echo "Recording stopped.\n";
|
255 |
7d61beba
|
Phil Davis
|
$recording = false;
|
256 |
415c850d
|
Scott Ullrich
|
} else {
|
257 |
|
|
echo "No recording session in progress.\n";
|
258 |
|
|
$command = "";
|
259 |
|
|
}
|
260 |
|
|
}
|
261 |
7d61beba
|
Phil Davis
|
if ($first_command == "showrecordings") {
|
262 |
4c12ef0a
|
Scott Ullrich
|
show_recordings();
|
263 |
7d61beba
|
Phil Davis
|
$command = "";
|
264 |
415c850d
|
Scott Ullrich
|
}
|
265 |
7d61beba
|
Phil Davis
|
if ($first_command == "reset") {
|
266 |
415c850d
|
Scott Ullrich
|
$playbackbuffer = "";
|
267 |
|
|
echo "\nBuffer reset.\n\n";
|
268 |
|
|
continue;
|
269 |
|
|
}
|
270 |
7d61beba
|
Phil Davis
|
if ($first_command == "record") {
|
271 |
|
|
if (!$command_split[1]) {
|
272 |
415c850d
|
Scott Ullrich
|
echo "usage: record playbackname\n";
|
273 |
d17af2a5
|
jim-p
|
echo "\tplaybackname will be created in /etc/phpshellsessions.\n";
|
274 |
415c850d
|
Scott Ullrich
|
$command = "";
|
275 |
|
|
} else {
|
276 |
|
|
/* time to record */
|
277 |
|
|
conf_mount_rw();
|
278 |
|
|
safe_mkdir("/etc/phpshellsessions");
|
279 |
d17af2a5
|
jim-p
|
$recording_fn = basename($command_split[1]);
|
280 |
|
|
$recording_fd = fopen("/etc/phpshellsessions/{$recording_fn}","w");
|
281 |
7d61beba
|
Phil Davis
|
if (!$recording_fd) {
|
282 |
415c850d
|
Scott Ullrich
|
echo "Could not start recording session.\n";
|
283 |
34af0cab
|
Scott Ullrich
|
$command = "";
|
284 |
7d61beba
|
Phil Davis
|
} else {
|
285 |
415c850d
|
Scott Ullrich
|
$recording = true;
|
286 |
d17af2a5
|
jim-p
|
echo "Recording of {$recording_fn} started.\n";
|
287 |
34af0cab
|
Scott Ullrich
|
$command = "";
|
288 |
|
|
}
|
289 |
93a3d242
|
Scott Ullrich
|
}
|
290 |
415c850d
|
Scott Ullrich
|
}
|
291 |
|
|
$playbackbuffer .= $command . "\n";
|
292 |
93a3d242
|
Scott Ullrich
|
}
|
293 |
|
|
|
294 |
4c12ef0a
|
Scott Ullrich
|
function show_recordings() {
|
295 |
|
|
echo "==> Sessions available for playback are:\n";
|
296 |
d17af2a5
|
jim-p
|
$playback_files = get_playback_files();
|
297 |
|
|
foreach (get_playback_files() as $pbf) {
|
298 |
|
|
echo "{$pbf} ";
|
299 |
|
|
}
|
300 |
|
|
echo "\n\n";
|
301 |
7d61beba
|
Phil Davis
|
echo "==> end of list.\n";
|
302 |
4c12ef0a
|
Scott Ullrich
|
}
|
303 |
|
|
|
304 |
93a3d242
|
Scott Ullrich
|
function returnlastchar($command) {
|
305 |
|
|
$commandlen = strlen($command);
|
306 |
|
|
$endofstring = substr($command, ($commandlen-1));
|
307 |
7d61beba
|
Phil Davis
|
return $endofstring;
|
308 |
93a3d242
|
Scott Ullrich
|
}
|
309 |
|
|
|
310 |
|
|
function returnfirstchar($command) {
|
311 |
|
|
$commandlen = strlen($command);
|
312 |
|
|
$endofstring = substr($command, 0, 1);
|
313 |
7d61beba
|
Phil Davis
|
return $endofstring;
|
314 |
93a3d242
|
Scott Ullrich
|
}
|
315 |
|
|
|
316 |
|
|
function str_replace_all($search,$replace,$subject) {
|
317 |
7d61beba
|
Phil Davis
|
while (strpos($subject,$search)!==false) {
|
318 |
93a3d242
|
Scott Ullrich
|
$subject = str_replace($search,$replace,$subject);
|
319 |
7d61beba
|
Phil Davis
|
}
|
320 |
93a3d242
|
Scott Ullrich
|
return $subject;
|
321 |
d776e077
|
Scott Ullrich
|
}
|
322 |
46d53988
|
Scott Ullrich
|
|
323 |
415c850d
|
Scott Ullrich
|
function playback_text($playback_file_contents) {
|
324 |
cfbfd941
|
smos
|
$playback_file_split = explode("\n", $playback_file_contents);
|
325 |
8586a962
|
Scott Ullrich
|
$playback_text = "require_once('functions.inc');\n";
|
326 |
|
|
$playback_text .= "require_once('globals.inc');\n";
|
327 |
|
|
$playback_text .= "require_once('config.inc');\n";
|
328 |
415c850d
|
Scott Ullrich
|
$toquote = '"';
|
329 |
7d61beba
|
Phil Davis
|
$toquotereplace = '\\"';
|
330 |
|
|
foreach ($playback_file_split as $pfs) {
|
331 |
415c850d
|
Scott Ullrich
|
$firstchar = returnfirstchar($pfs);
|
332 |
|
|
$currentline = $pfs;
|
333 |
7d61beba
|
Phil Davis
|
if ($firstchar == "!") {
|
334 |
415c850d
|
Scott Ullrich
|
/* 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 |
bde982dd
|
jim-p
|
global $config;
|
346 |
415c850d
|
Scott Ullrich
|
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 |
cfbfd941
|
smos
|
?>
|