From 415c850d1a391a4a1af589e182cbc6a2c8155c2c Mon Sep 17 00:00:00 2001 From: Scott Ullrich Date: Fri, 23 Nov 2007 06:07:38 +0000 Subject: Allow arguments to be passed to the pfSense php shell. --- usr/local/sbin/pfSsh.php | 291 +++++++++++++++++++---------------------------- 1 file changed, 117 insertions(+), 174 deletions(-) (limited to 'usr/local/sbin/pfSsh.php') diff --git a/usr/local/sbin/pfSsh.php b/usr/local/sbin/pfSsh.php index d5d7490..2aa9a23 100755 --- a/usr/local/sbin/pfSsh.php +++ b/usr/local/sbin/pfSsh.php @@ -11,6 +11,8 @@ require("functions.inc"); echo "."; require("config.inc"); echo "."; +require("util.inc"); +echo "."; $g['booting'] = false; $shell_cmds = array("alias", "alloc", "bg", "bind", "bindkey", "break", @@ -75,6 +77,15 @@ function more($text, $count=24) { function show_help() { $show_help_text = << @@ -94,10 +105,7 @@ $show_help_text = << Sessions available for playback are:\n\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 = ""; - echo "Could not locate playback file.\n"; - } else { - $playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}"); - $playback_file_split = split("\n", $playback_file_contents); - $playbackinprogress = true; - $dontunsetplaybacksplit = true; + } else { + $recording = true; + echo "Recording of {$command_split[1]} started.\n"; $command = ""; - echo "Playback of file {$command_split[1]} started.\n\n"; } } - // add command to playback area - if($command) - $playback_file_split[] = $command; - // check for multiline - $lastchar = returnlastchar($command); - if($lastchar == "\\") { - continue; - } - if(is_array($playback_file_split)) - foreach($playback_file_split as $pfc) { - $command = $pfc; - $lastchar = returnlastchar($command); - if($lastchar == "\\") { - $playbackbuffer .= $command; - continue; - } - if($playbackbuffer) { - $firstchar = returnfirstchar($playbackbuffer); - if($firstchar == "!") { - $commandstr = $playbackbuffer . "\n" . $command; - $command = str_replace_all("!", "\n", $commandstr); - $commandstr = str_replace_all("\\", "", $command); - file_put_contents("/tmp/phpSh.sh", $commandstr); - exec("chmod a+rx /tmp/phpSh.sh"); - system("/tmp/phpSh.sh"); - $playbackbuffer = ""; - $command = ""; - continue; - } - if($firstchar == "=") { - $commandstr = $playbackbuffer . "\n" . $command; - $command = str_replace_all("!", "\n", $commandstr); - $commandstr = str_replace_all("\\", "", $command); - file_put_contents("/tmp/phpSh.sh", $commandstr); - exec("chmod a+rx /tmp/phpSh.sh"); - system("/tmp/phpSh.sh"); - $playbackbuffer = ""; - $command = ""; - continue; - } - $playbackbuffernew = $playbackbuffer . "\n" . $command; - eval($playbackbuffernew); - $playbackbuffer = ""; - $playbackbuffernew = ""; - continue; - } - if($command == "exit") { - $shell_active = false; - echo "\n"; - break; - } - readline_add_history($command); - $command_split = split(" ", $command); - $first_command = $command_split[0]; - switch($first_command) { - case "=": - $newcmd = ""; - $counter = 0; - foreach($command_split as $cs) { - if($counter > 0) - $newcmd .= " {$cs}"; - $counter++; - } - if($playbackinprogress) - echo "pfSense shell: {$command}\n\n"; - if($recording) - fwrite($recording_fd, $command . "\n"); - file_put_contents("/tmp/phpSh.sh", $newcmd); - exec("chmod a+rx /tmp/phpSh.sh"); - system("/tmp/phpSh.sh"); - if($command_split[1] == "cd") { - echo "Changing working directory to {$command_split[2]}.\n"; - chdir($command_split[2]); - } - $command = ""; - echo "\n"; - break; - case "!": - $newcmd = ""; - $counter = 0; - foreach($command_split as $cs) { - if($counter > 0) - $newcmd .= " {$cs}"; - $counter++; - } - file_put_contents("/tmp/phpSh.sh", $newcmd); - exec("chmod a+rx /tmp/phpSh.sh"); - system("/tmp/phpSh.sh"); - echo "\n"; - $command = ""; - break; - } - if($command == "help") { - show_help(); - $command = ""; - } - if($command_split[0] == "stoprecording" || $command_split[0] == "stoprecord" || $command_split[0] == "stop") { - if($recording) { - fclose($recording_fd); - $command = ""; - conf_mount_ro(); - echo "Recording stopped.\n"; - $recording = false; - } else { - echo "No recording session in progress.\n"; - $command = ""; - } - } - if($command_split[0] == "showrecordings") { - conf_mount_rw(); - safe_mkdir("/etc/phpshellsessions"); - if($recording) - conf_mount_ro(); - echo "==> Sessions available for playback are:\n\n"; - system("cd /etc/phpshellsessions && ls /etc/phpshellsessions"); - echo "==> end of list.\n"; - $command = ""; - } - if($command_split[0] == "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 = ""; - } - } - } - if($command) { - eval($command); - if($playbackinprogress) - echo "pfSense shell: {$command}\n\n"; - if($recording) - fwrite($recording_fd, $command . "\n"); - } - } - unset($playback_file_split); - unset($playback_file_contents); - unset($playback); - unset($command); + } + $playbackbuffer .= $command . "\n"; } function returnlastchar($command) { @@ -349,3 +264,31 @@ function str_replace_all($search,$replace,$subject) { return $subject; } +function playback_text($playback_file_contents) { + $playback_file_split = split("\n", $playback_file_contents); + $playback_text = ""; + $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); +} + -- cgit v1.1