1
|
#!/usr/local/bin/php -f
|
2
|
|
3
|
<?php
|
4
|
|
5
|
echo "Starting the pfSense shell system";
|
6
|
|
7
|
echo ".";
|
8
|
require("globals.inc");
|
9
|
$g['booting'] = true;
|
10
|
require("functions.inc");
|
11
|
echo ".";
|
12
|
require("config.inc");
|
13
|
echo ".";
|
14
|
require("util.inc");
|
15
|
echo ".";
|
16
|
$g['booting'] = false;
|
17
|
|
18
|
$shell_cmds = array("alias", "alloc", "bg", "bind", "bindkey", "break",
|
19
|
"breaksw", "builtins", "case", "cd", "chdir", "command", "complete", "continue", "default",
|
20
|
"dirs", "do", "done", "echo", "echotc", "elif", "else", "end", "endif", "endsw", "esac", "eval",
|
21
|
"exec", "exit", "export", "false", "fc", "fg", "filetest", "fi", "for", "foreach", "getopts",
|
22
|
"glob", "goto", "hash", "hashstat", "history", "hup", "if", "jobid", "jobs", "kill", "limit",
|
23
|
"local", "log", "login", "logout", "ls-F", "nice", "nohup", "notify", "onintr", "popd",
|
24
|
"printenv", "pushd", "pwd", "read", "readonly", "rehash", "repeat", "return", "sched", "set",
|
25
|
"setenv", "settc", "setty", "setvar", "shift", "source", "stop", "suspend", "switch",
|
26
|
"telltc", "test", "then", "time", "trap", "true", "type", "ulimit", "umask", "unalias",
|
27
|
"uncomplete", "unhash", "unlimit", "unset", "unsetenv", "until", "wait", "where", "which",
|
28
|
"while");
|
29
|
|
30
|
function pipe_cmd($command, $text_to_pipe) {
|
31
|
$descriptorspec = array(
|
32
|
0 => array("pipe", "r"), // stdin
|
33
|
1 => array("pipe", "w"), // stdout
|
34
|
2 => array("pipe", "w")); // stderr ?? instead of a file
|
35
|
|
36
|
$fd = proc_open("$command", $descriptorspec, $pipes);
|
37
|
if (is_resource($fd)) {
|
38
|
fwrite($pipes[0], "{$text_to_pipe}");
|
39
|
fclose($pipes[0]);
|
40
|
while($s= fgets($pipes[1], 1024)) {
|
41
|
// read from the pipe
|
42
|
$buffer .= $s;
|
43
|
}
|
44
|
fclose($pipes[1]);
|
45
|
fclose($pipes[2]);
|
46
|
}
|
47
|
return $buffer;
|
48
|
}
|
49
|
|
50
|
if(!function_exists("readline")) {
|
51
|
function readline() {
|
52
|
$fp = fopen('php://stdin', 'r');
|
53
|
$textinput = chop(fgets($fp));
|
54
|
fclose($fp);
|
55
|
}
|
56
|
return $textinput;
|
57
|
}
|
58
|
|
59
|
function more($text, $count=24) {
|
60
|
$counter=0;
|
61
|
$lines = split("\n", $text);
|
62
|
foreach($lines as $line) {
|
63
|
if($counter > $count) {
|
64
|
echo "Press RETURN to continue ...";
|
65
|
$fp = fopen('php://stdin', 'r');
|
66
|
$pressreturn = chop(fgets($fp));
|
67
|
if($pressreturn == "q" || $pressreturn == "quit")
|
68
|
return;
|
69
|
fclose($fp);
|
70
|
$counter = 0;
|
71
|
}
|
72
|
echo "{$line}\n";
|
73
|
$counter++;
|
74
|
}
|
75
|
}
|
76
|
|
77
|
function show_help() {
|
78
|
|
79
|
$show_help_text = <<<EOF
|
80
|
|
81
|
Enter a series of commands and then execute the set with "exec".
|
82
|
|
83
|
For example:
|
84
|
echo "foo"; // php command
|
85
|
echo "foo2"; // php command
|
86
|
! echo "heh" # shell command
|
87
|
exec
|
88
|
|
89
|
Example commands:
|
90
|
|
91
|
startrecording <recordingfilename>
|
92
|
stoprecording <recordingfilename>
|
93
|
showrecordings
|
94
|
|
95
|
parse_config(true); # reloads the \$config array
|
96
|
|
97
|
\$temp = print_r(\$config, true);
|
98
|
more(\$temp);
|
99
|
|
100
|
/* to output a configuration array */
|
101
|
print_r(\$config);
|
102
|
|
103
|
/* to output the interfaces configuration portion of the configuration */
|
104
|
print_r(\$config['interfaces']);
|
105
|
|
106
|
/* to output the dhcp server configuration */
|
107
|
print_r(\$config['dhcpd']);
|
108
|
|
109
|
/* to exit the php pfSense shell */
|
110
|
exit
|
111
|
|
112
|
/* to output supported wireless modes for an interface */
|
113
|
print_r(get_wireless_modes(\"ath0\"));
|
114
|
|
115
|
/* to enable SSH */
|
116
|
\$config['system']['enablesshd'] = true;
|
117
|
|
118
|
/* change OPTX to the OPT interface name such as BACKHAUL */
|
119
|
\$config['interfaces']['optx']['wireless']['standard'] = "11a";
|
120
|
\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
|
121
|
\$config['interfaces']['optx']['wireless']['channel'] = "6";
|
122
|
|
123
|
/* to enable dhcp server for an optx interface */
|
124
|
\$config['dhcpd']['optx']['enable'] = true;
|
125
|
\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
|
126
|
\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
|
127
|
|
128
|
/* to disable the firewall filter */
|
129
|
\$config['system']['disablefilter'] = true;
|
130
|
|
131
|
/* to enable an interface and set it for dhcp */
|
132
|
\$config['interfaces']['optx']['disabled'] = false;
|
133
|
\$config['interfaces']['optx']['ipaddr'] = "dhcp";
|
134
|
|
135
|
/* to enable an interface and set a static ip address */
|
136
|
\$config['interfaces']['wan']['disabled'] = false;
|
137
|
\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
|
138
|
\$config['interfaces']['wan']['subnet'] = "24";
|
139
|
|
140
|
/* to save out the new configuration (config.xml) */
|
141
|
write_config();
|
142
|
|
143
|
/* to reboot the system after saving */
|
144
|
system_reboot_sync();
|
145
|
|
146
|
EOF;
|
147
|
|
148
|
more($show_help_text);
|
149
|
|
150
|
}
|
151
|
|
152
|
$fp = fopen('php://stdin', 'r');
|
153
|
|
154
|
echo ".\n\n";
|
155
|
|
156
|
$pkg_interface='console';
|
157
|
|
158
|
$shell_active = true;
|
159
|
|
160
|
if($argc < 2) {
|
161
|
echo "Welcome to the pfSense php shell system\n";
|
162
|
echo "Written by Scott Ullrich (sullrich@gmail.com)\n";
|
163
|
echo "\nType \"help\" to show common usage scenarios.\n";
|
164
|
echo "\nAvailable playback commands:\n ";
|
165
|
$files = scandir("/etc/phpshellsessions/");
|
166
|
foreach($files as $file) {
|
167
|
if($file <> "." and $file <> "..")
|
168
|
echo $file . " ";
|
169
|
}
|
170
|
echo "\n\n";
|
171
|
}
|
172
|
|
173
|
$recording = false;
|
174
|
$playback_file_split = array();
|
175
|
$playbackbuffer = "";
|
176
|
|
177
|
if($argv[1]=="playback" or $argv[1]=="run") {
|
178
|
if(!file_exists("/etc/phpshellsessions/{$argv[2]}")) {
|
179
|
echo "Could not locate playback file.";
|
180
|
exit;
|
181
|
}
|
182
|
playback_file($argv[2]);
|
183
|
exit;
|
184
|
}
|
185
|
|
186
|
while($shell_active == true) {
|
187
|
$command = readline("pfSense shell: ");
|
188
|
readline_add_history($command);
|
189
|
$command_split = split(" ", $command);
|
190
|
$first_command = $command_split[0];
|
191
|
if($first_command == "playback" || $first_command == "run") {
|
192
|
$playback_file = $command_split[1];
|
193
|
if(!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
|
194
|
$command = "";
|
195
|
echo "Could not locate playback file.\n";
|
196
|
} else {
|
197
|
$command = "";
|
198
|
echo "\nPlayback of file {$command_split[1]} started.\n\n";
|
199
|
playback_file("{$playback_file}");
|
200
|
continue;
|
201
|
}
|
202
|
}
|
203
|
if($first_command == "exit" or $first_command == "quit") {
|
204
|
die;
|
205
|
}
|
206
|
if($first_command == "help" or $first_command == "?") {
|
207
|
show_help();
|
208
|
$playbackbuffer = "";
|
209
|
continue;
|
210
|
}
|
211
|
if($first_command == "exec" or $first_command == "exec;") {
|
212
|
playback_text($playbackbuffer);
|
213
|
$playbackbuffer = "";
|
214
|
continue;
|
215
|
}
|
216
|
if($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
|
217
|
if($recording) {
|
218
|
fwrite($recording_fd, $playbackbuffer);
|
219
|
fclose($recording_fd);
|
220
|
$command = "";
|
221
|
conf_mount_ro();
|
222
|
echo "Recording stopped.\n";
|
223
|
$recording = false;
|
224
|
} else {
|
225
|
echo "No recording session in progress.\n";
|
226
|
$command = "";
|
227
|
}
|
228
|
}
|
229
|
if($first_command == "showrecordings") {
|
230
|
conf_mount_rw();
|
231
|
safe_mkdir("/etc/phpshellsessions");
|
232
|
if($recording)
|
233
|
conf_mount_ro();
|
234
|
echo "==> Sessions available for playback are:\n";
|
235
|
system("cd /etc/phpshellsessions && ls /etc/phpshellsessions");
|
236
|
echo "==> end of list.\n";
|
237
|
$command = "";
|
238
|
}
|
239
|
if($first_command == "reset") {
|
240
|
$playbackbuffer = "";
|
241
|
echo "\nBuffer reset.\n\n";
|
242
|
continue;
|
243
|
}
|
244
|
if($first_command == "record") {
|
245
|
if(!$command_split[1]) {
|
246
|
echo "usage: record playbackname\n";
|
247
|
$command = "";
|
248
|
} else {
|
249
|
/* time to record */
|
250
|
conf_mount_rw();
|
251
|
safe_mkdir("/etc/phpshellsessions");
|
252
|
$recording_fd = fopen("/etc/phpshellsessions/{$command_split[1]}","w");
|
253
|
if(!$recording_fd) {
|
254
|
echo "Could not start recording session.\n";
|
255
|
$command = "";
|
256
|
} else {
|
257
|
$recording = true;
|
258
|
echo "Recording of {$command_split[1]} started.\n";
|
259
|
$command = "";
|
260
|
}
|
261
|
}
|
262
|
}
|
263
|
$playbackbuffer .= $command . "\n";
|
264
|
}
|
265
|
|
266
|
function returnlastchar($command) {
|
267
|
$commandlen = strlen($command);
|
268
|
$endofstring = substr($command, ($commandlen-1));
|
269
|
return $endofstring;
|
270
|
}
|
271
|
|
272
|
function returnfirstchar($command) {
|
273
|
$commandlen = strlen($command);
|
274
|
$endofstring = substr($command, 0, 1);
|
275
|
return $endofstring;
|
276
|
}
|
277
|
|
278
|
function str_replace_all($search,$replace,$subject) {
|
279
|
while(strpos($subject,$search)!==false)
|
280
|
$subject = str_replace($search,$replace,$subject);
|
281
|
return $subject;
|
282
|
}
|
283
|
|
284
|
function playback_text($playback_file_contents) {
|
285
|
$playback_file_split = split("\n", $playback_file_contents);
|
286
|
$playback_text = "";
|
287
|
$toquote = '"';
|
288
|
$toquotereplace = '\\"';
|
289
|
foreach($playback_file_split as $pfs) {
|
290
|
$firstchar = returnfirstchar($pfs);
|
291
|
$currentline = $pfs;
|
292
|
if($firstchar == "!") {
|
293
|
/* XXX: encode " in $pfs */
|
294
|
$pfsa = str_replace($toquote, $toquotereplace, $currentline);
|
295
|
$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
|
296
|
} else if ($firstchar == "=") {
|
297
|
/* XXX: encode " in $pfs */
|
298
|
$pfsa = str_replace($toquote, $toquotereplace, $currentline);
|
299
|
$currentline .= str_replace("!", "system(\"", $pfsa) . "\");\n";
|
300
|
} else {
|
301
|
$playback_text .= $pfs . "\n";
|
302
|
}
|
303
|
}
|
304
|
eval($playback_text);
|
305
|
}
|
306
|
|
307
|
function playback_file($playback_file) {
|
308
|
$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
|
309
|
playback_text($playback_file_contents);
|
310
|
}
|
311
|
|
312
|
?>
|