Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1 cb7d18d5 Renato Botelho
#!/usr/local/bin/php-cgi -f
2 d776e077 Scott Ullrich
<?php
3 ac24dc24 Renato Botelho
/*
4
 * pfSsh
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7 b8f91b7c Luiz Souza
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
8 ac24dc24 Renato Botelho
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13 ac24dc24 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21 ac24dc24 Renato Botelho
 */
22 d776e077 Scott Ullrich
23 8a7712ea Scott Ullrich
require_once("globals.inc");
24 337822a3 jim-p
if ($argc < 2) {
25
	echo "Starting the {$g['product_name']} developer shell";
26
}
27 8a7712ea Scott Ullrich
require_once("functions.inc");
28 337822a3 jim-p
if ($argc < 2) {
29
	echo ".";
30
}
31 8a7712ea Scott Ullrich
require_once("config.inc");
32 337822a3 jim-p
if ($argc < 2) {
33
	echo ".";
34
}
35 8a7712ea Scott Ullrich
require_once("util.inc");
36 337822a3 jim-p
if ($argc < 2) {
37
	echo ".";
38
}
39 d776e077 Scott Ullrich
40 7d61beba Phil Davis
$shell_cmds = array("alias", "alloc", "bg", "bind", "bindkey", "break",
41
	 "breaksw", "builtins", "case", "cd", "chdir", "command", "complete", "continue", "default",
42
	 "dirs", "do", "done", "echo", "echotc", "elif", "else", "end", "endif", "endsw", "esac", "eval",
43
	 "exec", "exit", "export", "false", "fc", "fg", "filetest", "fi", "for", "foreach", "getopts",
44
	 "glob", "goto", "hash", "hashstat", "history", "hup", "if", "jobid", "jobs", "kill", "limit",
45
	 "local", "log", "login", "logout", "ls-F", "nice", "nohup", "notify", "onintr", "popd",
46
	 "printenv", "pushd", "pwd", "read", "readonly", "rehash", "repeat", "return", "sched", "set",
47
	 "setenv", "settc", "setty", "setvar", "shift", "source", "stop", "suspend", "switch",
48
	 "telltc", "test", "then", "time", "trap", "true", "type", "ulimit", "umask", "unalias",
49
	 "uncomplete", "unhash", "unlimit", "unset", "unsetenv", "until", "wait", "where", "which",
50
	 "while");
51 7116ab7f Scott Ullrich
52 50fa05d1 Scott Ullrich
function pipe_cmd($command, $text_to_pipe) {
53
	$descriptorspec = array(
54 7d61beba Phil Davis
		0 => array("pipe", "r"),  // stdin
55
		1 => array("pipe", "w"),  // stdout
56
		2 => array("pipe", "w")); // stderr ?? instead of a file
57
58 50fa05d1 Scott Ullrich
	$fd = proc_open("$command", $descriptorspec, $pipes);
59
	if (is_resource($fd)) {
60 7d61beba Phil Davis
		fwrite($pipes[0], "{$text_to_pipe}");
61
		fclose($pipes[0]);
62
		while ($s= fgets($pipes[1], 1024)) {
63
			// read from the pipe
64
			$buffer .= $s;
65
		}
66
		fclose($pipes[1]);
67
		fclose($pipes[2]);
68 50fa05d1 Scott Ullrich
	}
69
	return $buffer;
70
}
71
72 7d61beba Phil Davis
if (!function_exists("readline")) {
73 3d3be836 Scott Ullrich
	function readline() {
74
		$fp = fopen('php://stdin', 'r');
75
		$textinput = chop(fgets($fp));
76
		fclose($fp);
77 6fee314b Phil Davis
		return $textinput;
78 3d3be836 Scott Ullrich
	}
79
}
80
81 cd8ca22f Scott Ullrich
function more($text, $count=24) {
82 7d61beba Phil Davis
	$counter=0;
83
	$lines = explode("\n", $text);
84
	foreach ($lines as $line) {
85
		if ($counter > $count) {
86
			echo "Press RETURN to continue ...";
87
			$fp = fopen('php://stdin', 'r');
88
			$pressreturn = chop(fgets($fp));
89
			if ($pressreturn == "q" || $pressreturn == "quit") {
90
				return;
91
			}
92
			fclose($fp);
93
			$counter = 0;
94
		}
95
		echo "{$line}\n";
96
		$counter++;
97
	}
98 5003c48a Scott Ullrich
}
99
100 46d53988 Scott Ullrich
function show_help() {
101 60ff8601 Scott Ullrich
102
$show_help_text = <<<EOF
103 415c850d Scott Ullrich
104
	Enter a series of commands and then execute the set with "exec".
105 7d61beba Phil Davis
106 415c850d Scott Ullrich
	For example:
107
	echo "foo"; // php command
108
	echo "foo2"; // php command
109
	! echo "heh" # shell command
110
	exec
111
112 60ff8601 Scott Ullrich
	Example commands:
113 02105da4 Scott Ullrich
114 d17af2a5 jim-p
	record <recordingfilename>
115
	stoprecording
116 02105da4 Scott Ullrich
	showrecordings
117
118 60ff8601 Scott Ullrich
	parse_config(true);  # reloads the \$config array
119 5003c48a Scott Ullrich
120 60ff8601 Scott Ullrich
	\$temp = print_r(\$config, true);
121
	more(\$temp);
122 5003c48a Scott Ullrich
123 60ff8601 Scott Ullrich
	/* to output a configuration array */
124
	print_r(\$config);
125 7d61beba Phil Davis
126 4c12ef0a Scott Ullrich
	/* to output the interfaces configuration portion of config.xml */
127 60ff8601 Scott Ullrich
	print_r(\$config['interfaces']);
128 7d61beba Phil Davis
129 60ff8601 Scott Ullrich
	/* to output the dhcp server configuration */
130
	print_r(\$config['dhcpd']);
131 415c850d Scott Ullrich
132 f643a1f1 Chris Buechler
	/* to exit the {$g['product_name']} developer shell */
133 60ff8601 Scott Ullrich
	exit
134 7d61beba Phil Davis
135 60ff8601 Scott Ullrich
	/* to output supported wireless modes for an interface */
136
	print_r(get_wireless_modes(\"ath0\"));
137 7d61beba Phil Davis
138 60ff8601 Scott Ullrich
	/* to enable SSH */
139
	\$config['system']['enablesshd'] = true;
140 7d61beba Phil Davis
141 60ff8601 Scott Ullrich
	/* change OPTX to the OPT interface name such as BACKHAUL */
142 00ea455f Scott Ullrich
	\$config['interfaces']['optx']['wireless']['standard'] = "11a";
143
	\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
144
	\$config['interfaces']['optx']['wireless']['channel'] = "6";
145 7d61beba Phil Davis
146 60ff8601 Scott Ullrich
	/* to enable dhcp server for an optx interface */
147
	\$config['dhcpd']['optx']['enable'] = true;
148 00ea455f Scott Ullrich
	\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
149
	\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
150 7d61beba Phil Davis
151 60ff8601 Scott Ullrich
	/* to disable the firewall filter */
152
	\$config['system']['disablefilter'] = true;
153 7d61beba Phil Davis
154 51b24a30 Chris Buechler
	/* to enable an interface and configure it as a DHCP client */
155 60ff8601 Scott Ullrich
	\$config['interfaces']['optx']['disabled'] = false;
156 00ea455f Scott Ullrich
	\$config['interfaces']['optx']['ipaddr'] = "dhcp";
157 7d61beba Phil Davis
158 51b24a30 Chris Buechler
	/* to enable an interface and set a static IPv4 address */
159
	\$config['interfaces']['wan']['enable'] = true;
160 00ea455f Scott Ullrich
	\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
161
	\$config['interfaces']['wan']['subnet'] = "24";
162 7d61beba Phil Davis
163 60ff8601 Scott Ullrich
	/* to save out the new configuration (config.xml) */
164
	write_config();
165 7d61beba Phil Davis
166 60ff8601 Scott Ullrich
	/* to reboot the system after saving */
167 328ab0ae Scott Ullrich
	system_reboot_sync();
168 7d61beba Phil Davis
169 60ff8601 Scott Ullrich
EOF;
170
171
	more($show_help_text);
172 7d61beba Phil Davis
173 46d53988 Scott Ullrich
}
174
175 d776e077 Scott Ullrich
$fp = fopen('php://stdin', 'r');
176
177 337822a3 jim-p
if ($argc < 2) {
178
	echo ".\n\n";
179
}
180 d776e077 Scott Ullrich
181 1b8a2f5c Scott Ullrich
$pkg_interface='console';
182
183 d776e077 Scott Ullrich
$shell_active = true;
184 f6907eb4 sullrich
$tccommands = array();
185
186
function completion($string, $index) {
187
	global $tccommands;
188
	return $tccommands;
189
}
190
191
readline_completion_function("completion");
192 d776e077 Scott Ullrich
193 36cb313b jim-p
function get_playback_files() {
194
	$playback_files = array();
195
	$files = scandir("/etc/phpshellsessions/");
196
	foreach ($files as $file) {
197 d17af2a5 jim-p
		if ($file <> "." && $file <> "..") {
198 36cb313b jim-p
			$playback_files[] = $file;
199
		}
200
	}
201
	return $playback_files;
202
}
203
204 7d61beba Phil Davis
if ($argc < 2) {
205 f643a1f1 Chris Buechler
	echo "Welcome to the {$g['product_name']} developer shell\n";
206 b1de6b8b Scott Ullrich
	echo "\nType \"help\" to show common usage scenarios.\n";
207
	echo "\nAvailable playback commands:\n     ";
208 f6907eb4 sullrich
	$tccommands[] = "playback";
209 36cb313b jim-p
	$playback_files = get_playback_files();
210
	foreach ($playback_files as $pbf) {
211
		echo "{$pbf} ";
212
		if (function_exists("readline_add_history")) {
213 9e4c0b1f NOYB
			readline_add_history("playback $pbf");
214
			$tccommands[] = "$pbf";
215 f6907eb4 sullrich
		}
216 b1de6b8b Scott Ullrich
	}
217
	echo "\n\n";
218 415c850d Scott Ullrich
}
219 d776e077 Scott Ullrich
220 34af0cab Scott Ullrich
$recording = false;
221 93a3d242 Scott Ullrich
$playback_file_split = array();
222
$playbackbuffer = "";
223
224 7d61beba Phil Davis
if ($argv[1]=="playback" or $argv[1]=="run") {
225 36cb313b jim-p
	if (empty($argv[2]) || !file_exists("/etc/phpshellsessions/" . basename($argv[2]))) {
226
		echo "Error: Invalid playback file specified.\n\n";
227 e296be60 Chris Buechler
		show_recordings();
228 36cb313b jim-p
		exit(-1);
229 47643f5b Scott Ullrich
	}
230 36cb313b jim-p
	playback_file(basename($argv[2]));
231 415c850d Scott Ullrich
	exit;
232
}
233
234 f6907eb4 sullrich
// Define more commands
235
$tccommands[] = "exit";
236
$tccommands[] = "quit";
237
$tccommands[] = "?";
238
$tccommands[] = "exec";
239
$tccommands[] = "stoprecording";
240
$tccommands[] = "showrecordings";
241
$tccommands[] = "record";
242
$tccommands[] = "reset";
243 e788b01d sullrich
$tccommands[] = "master";
244
$tccommands[] = "RELENG_1_2";
245 f6907eb4 sullrich
246 7d61beba Phil Davis
while ($shell_active == true) {
247 b9e3a295 Scott Ullrich
	$command = readline("{$g['product_name']} shell: ");
248 415c850d Scott Ullrich
	readline_add_history($command);
249 7d61beba Phil Davis
	$command_split = explode(" ", $command);
250
	$first_command = $command_split[0];
251
	if ($first_command == "playback" || $first_command == "run") {
252 415c850d Scott Ullrich
		$playback_file = $command_split[1];
253 7d61beba Phil Davis
		if (!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
254 415c850d Scott Ullrich
			$command = "";
255
			echo "Could not locate playback file.\n";
256
		} else {
257
			$command = "";
258
			echo "\nPlayback of file {$command_split[1]} started.\n\n";
259
			playback_file("{$playback_file}");
260
			continue;
261
		}
262
	}
263 7d61beba Phil Davis
	if ($first_command == "exit" or $first_command == "quit") {
264 9f2820d8 Scott Ullrich
		die;
265 7d61beba Phil Davis
	}
266
	if ($first_command == "help" or $first_command == "?") {
267 9f2820d8 Scott Ullrich
		show_help();
268
		$playbackbuffer = "";
269
		continue;
270
	}
271 7d61beba Phil Davis
	if ($first_command == "exec" or $first_command == "exec;") {
272 415c850d Scott Ullrich
		playback_text($playbackbuffer);
273
		$playbackbuffer = "";
274
		continue;
275
	}
276 7d61beba Phil Davis
	if ($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
277
		if ($recording) {
278 415c850d Scott Ullrich
			fwrite($recording_fd, $playbackbuffer);
279
			fclose($recording_fd);
280
			$command = "";
281
			echo "Recording stopped.\n";
282 7d61beba Phil Davis
			$recording = false;
283 415c850d Scott Ullrich
		} else {
284
			echo "No recording session in progress.\n";
285
			$command = "";
286
		}
287
	}
288 7d61beba Phil Davis
	if ($first_command == "showrecordings") {
289 4c12ef0a Scott Ullrich
		show_recordings();
290 7d61beba Phil Davis
		$command = "";
291 415c850d Scott Ullrich
	}
292 7d61beba Phil Davis
	if ($first_command == "reset") {
293 415c850d Scott Ullrich
		$playbackbuffer = "";
294
		echo "\nBuffer reset.\n\n";
295
		continue;
296
	}
297 7d61beba Phil Davis
	if ($first_command == "record") {
298
		if (!$command_split[1]) {
299 415c850d Scott Ullrich
			echo "usage: record playbackname\n";
300 d17af2a5 jim-p
			echo "\tplaybackname will be created in /etc/phpshellsessions.\n";
301 415c850d Scott Ullrich
			$command = "";
302
		} else {
303
			/* time to record */
304
			safe_mkdir("/etc/phpshellsessions");
305 d17af2a5 jim-p
			$recording_fn = basename($command_split[1]);
306
			$recording_fd = fopen("/etc/phpshellsessions/{$recording_fn}","w");
307 7d61beba Phil Davis
			if (!$recording_fd) {
308 415c850d Scott Ullrich
				echo "Could not start recording session.\n";
309 34af0cab Scott Ullrich
				$command = "";
310 7d61beba Phil Davis
			} else {
311 415c850d Scott Ullrich
				$recording = true;
312 d17af2a5 jim-p
				echo "Recording of {$recording_fn} started.\n";
313 34af0cab Scott Ullrich
				$command = "";
314
			}
315 93a3d242 Scott Ullrich
		}
316 415c850d Scott Ullrich
	}
317
	$playbackbuffer .= $command . "\n";
318 93a3d242 Scott Ullrich
}
319
320 4c12ef0a Scott Ullrich
function show_recordings() {
321
	echo "==> Sessions available for playback are:\n";
322 d17af2a5 jim-p
	$playback_files = get_playback_files();
323
	foreach (get_playback_files() as $pbf) {
324
		echo "{$pbf} ";
325
	}
326
	echo "\n\n";
327 7d61beba Phil Davis
	echo "==> end of list.\n";
328 4c12ef0a Scott Ullrich
}
329
330 93a3d242 Scott Ullrich
function returnlastchar($command) {
331
	$commandlen = strlen($command);
332
	$endofstring = substr($command, ($commandlen-1));
333 7d61beba Phil Davis
	return $endofstring;
334 93a3d242 Scott Ullrich
}
335
336
function returnfirstchar($command) {
337
	$commandlen = strlen($command);
338
	$endofstring = substr($command, 0, 1);
339 7d61beba Phil Davis
	return $endofstring;
340 93a3d242 Scott Ullrich
}
341
342
function str_replace_all($search,$replace,$subject) {
343 7d61beba Phil Davis
	while (strpos($subject,$search)!==false) {
344 93a3d242 Scott Ullrich
		$subject = str_replace($search,$replace,$subject);
345 7d61beba Phil Davis
	}
346 93a3d242 Scott Ullrich
	return $subject;
347 d776e077 Scott Ullrich
}
348 46d53988 Scott Ullrich
349 415c850d Scott Ullrich
function playback_text($playback_file_contents) {
350 cfbfd941 smos
	$playback_file_split = explode("\n", $playback_file_contents);
351 8586a962 Scott Ullrich
	$playback_text  = "require_once('functions.inc');\n";
352
	$playback_text .= "require_once('globals.inc');\n";
353
	$playback_text .= "require_once('config.inc');\n";
354 415c850d Scott Ullrich
	$toquote = '"';
355 7d61beba Phil Davis
	$toquotereplace = '\\"';
356
	foreach ($playback_file_split as $pfs) {
357 415c850d Scott Ullrich
		$firstchar = returnfirstchar($pfs);
358
		$currentline = $pfs;
359 7d61beba Phil Davis
		if ($firstchar == "!") {
360 415c850d Scott Ullrich
			/* XXX: encode " in $pfs */
361
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
362
			$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
363
		} else if ($firstchar == "=") {
364
			/* XXX: encode " in $pfs */
365
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
366
			$currentline   .= str_replace("!", "system(\"", $pfsa) . "\");\n";
367
		} else {
368
			$playback_text .= $pfs . "\n";
369
		}
370
	}
371 bde982dd jim-p
	global $config;
372 415c850d Scott Ullrich
	eval($playback_text);
373
}
374
375
function playback_file($playback_file) {
376
	$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
377
	playback_text($playback_file_contents);
378
}
379
380 cfbfd941 smos
?>