summaryrefslogtreecommitdiffstats
path: root/usr/local/sbin/pfSsh.php
blob: 16edc84a706b397adbddf1824a790ca079000cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/local/bin/php -f
 
<?php

echo "Starting the pfSense shell system";

echo ".";
require_once("globals.inc");
$g['booting'] = true;
require_once("functions.inc");
echo ".";
require_once("config.inc");
echo ".";
require_once("util.inc");
echo ".";
$g['booting'] = false;

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

function pipe_cmd($command, $text_to_pipe) {
	$descriptorspec = array(
	    0 => array("pipe", "r"),  // stdin
	    1 => array("pipe", "w"),  // stdout
	    2 => array("pipe", "w")); // stderr ?? instead of a file
	
	$fd = proc_open("$command", $descriptorspec, $pipes);
	if (is_resource($fd)) {
	        fwrite($pipes[0], "{$text_to_pipe}");
	        fclose($pipes[0]);
	        while($s= fgets($pipes[1], 1024)) {
	          // read from the pipe
	          $buffer .= $s;
	        }
	        fclose($pipes[1]);
	        fclose($pipes[2]);
	}
	return $buffer;
}

if(!function_exists("readline")) {
	function readline() {
		$fp = fopen('php://stdin', 'r');
		$textinput = chop(fgets($fp));
		fclose($fp);
	}
	return $textinput;
}

function more($text, $count=24) {
        $counter=0;
        $lines = split("\n", $text);
        foreach($lines as $line) {
                if($counter > $count) {
                        echo "Press RETURN to continue ...";
                        $fp = fopen('php://stdin', 'r');
                        $pressreturn = chop(fgets($fp));
                        if($pressreturn == "q" || $pressreturn == "quit") 
                        	return; 
                        fclose($fp);
                        $counter = 0;
                }
                echo "{$line}\n";
                $counter++;
        }
}

function show_help() {

$show_help_text = <<<EOF

	Enter a series of commands and then execute the set with "exec".
	
	For example:
	echo "foo"; // php command
	echo "foo2"; // php command
	! echo "heh" # shell command
	exec

	Example commands:

	startrecording <recordingfilename>
	stoprecording <recordingfilename>
	showrecordings

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

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

	/* to output a configuration array */
	print_r(\$config);
	
	/* to output the interfaces configuration portion of the configuration */
	print_r(\$config['interfaces']);
	
	/* to output the dhcp server configuration */
	print_r(\$config['dhcpd']);

	/* to exit the php pfSense shell */
	exit
	
	/* to output supported wireless modes for an interface */
	print_r(get_wireless_modes(\"ath0\"));
	
	/* to enable SSH */
	\$config['system']['enablesshd'] = true;
	
	/* change OPTX to the OPT interface name such as BACKHAUL */
	\$config['interfaces']['optx']['wireless']['standard'] = "11a";
	\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
	\$config['interfaces']['optx']['wireless']['channel'] = "6";
	
	/* to enable dhcp server for an optx interface */
	\$config['dhcpd']['optx']['enable'] = true;
	\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
	\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
	
	/* to disable the firewall filter */
	\$config['system']['disablefilter'] = true;
	
	/* to enable an interface and set it for dhcp */
	\$config['interfaces']['optx']['disabled'] = false;
	\$config['interfaces']['optx']['ipaddr'] = "dhcp";
	
	/* to enable an interface and set a static ip address */
	\$config['interfaces']['wan']['disabled'] = false;
	\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
	\$config['interfaces']['wan']['subnet'] = "24";
	
	/* to save out the new configuration (config.xml) */
	write_config();
	
	/* to reboot the system after saving */
	system_reboot_sync();
	
EOF;

	more($show_help_text);
 	
}

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

echo ".\n\n";

$pkg_interface='console';

$shell_active = true;

if($argc < 2) {
	echo "Welcome to the pfSense php shell system\n";
	echo "Written by Scott Ullrich (sullrich@gmail.com)\n";
	echo "\nType \"help\" to show common usage scenarios.\n\n";
}

$recording = false;
$playback_file_split = array();
$playbackbuffer = "";

if($argv[1]=="playback" or $argv[1]=="run") { 
	if(!file_exists("/etc/phpshellsessions/{$argv[2]}")) {
		echo "Could not locate playback file.";
		exit;
	}
	playback_file($argv[2]);
	exit;
}

while($shell_active == true) {
	$command = readline("pfSense shell: ");
	readline_add_history($command);
    $command_split = split(" ", $command);
    $first_command = $command_split[0];	
	if($first_command == "playback" || $first_command == "run") {
		$playback_file = $command_split[1];
		if(!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
			$command = "";
			echo "Could not locate playback file.\n";
		} else {
			$command = "";
			echo "\nPlayback of file {$command_split[1]} started.\n\n";
			playback_file("{$playback_file}");
			continue;
		}
	}
	if($first_command == "exit" or $first_command == "quit") {
		die;
	}
	if($first_command == "help" or $first_command == "?") {
		show_help();
		$playbackbuffer = "";
		continue;
	}
	if($first_command == "exec" or $first_command == "exec;") {
		playback_text($playbackbuffer);
		$playbackbuffer = "";
		continue;
	}
	if($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
		if($recording) {
			fwrite($recording_fd, $playbackbuffer);
			fclose($recording_fd);
			$command = "";
			conf_mount_ro();
			echo "Recording stopped.\n";
			$recording = false; 
		} else {
			echo "No recording session in progress.\n";
			$command = "";
		}
	}
	if($first_command == "showrecordings") {
		conf_mount_rw();
		safe_mkdir("/etc/phpshellsessions");
		if($recording) 
			conf_mount_ro();
		echo "==> Sessions available for playback are:\n";
		system("cd /etc/phpshellsessions && ls /etc/phpshellsessions");
		echo "==> end of list.\n";
		$command = "";
	}
	if($first_command == "reset") {
		$playbackbuffer = "";
		echo "\nBuffer reset.\n\n";
		continue;
	}
	if($first_command == "record") {
		if(!$command_split[1]) {
			echo "usage: record playbackname\n";
			$command = "";
		} else {
			/* time to record */
			conf_mount_rw();
			safe_mkdir("/etc/phpshellsessions");
			$recording_fd = fopen("/etc/phpshellsessions/{$command_split[1]}","w");
			if(!$recording_fd) {
				echo "Could not start recording session.\n";
				$command = "";
			} else { 
				$recording = true;
				echo "Recording of {$command_split[1]} started.\n";
				$command = "";
			}
		}
	}
	$playbackbuffer .= $command . "\n";
}

function returnlastchar($command) {
	$commandlen = strlen($command);
	$endofstring = substr($command, ($commandlen-1));
	return $endofstring; 
}

function returnfirstchar($command) {
	$commandlen = strlen($command);
	$endofstring = substr($command, 0, 1);
	return $endofstring; 
}

function str_replace_all($search,$replace,$subject) {
	while(strpos($subject,$search)!==false) 
		$subject = str_replace($search,$replace,$subject);
	return $subject;
}

function playback_text($playback_file_contents) {
	$playback_file_split = split("\n", $playback_file_contents);
	$playback_text  = "require_once('functions.inc');\n";
	$playback_text .= "require_once('globals.inc');\n";
	$playback_text .= "require_once('config.inc');\n";
	$toquote = '"';
	$toquotereplace = '\\"';	
	foreach($playback_file_split as $pfs) {
		$firstchar = returnfirstchar($pfs);
		$currentline = $pfs;
		if($firstchar == "!") {
			/* XXX: encode " in $pfs */
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
			$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
		} else if ($firstchar == "=") {
			/* XXX: encode " in $pfs */
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
			$currentline   .= str_replace("!", "system(\"", $pfsa) . "\");\n";
		} else {
			$playback_text .= $pfs . "\n";
		}
	}
	eval($playback_text);
}

function playback_file($playback_file) {
	$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
	playback_text($playback_file_contents);
}

OpenPOWER on IntegriCloud