summaryrefslogtreecommitdiffstats
path: root/usr/local/sbin/pfSsh.php
blob: edc69be79add1434554c0d34702f2b5855ce6039 (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
#!/usr/local/bin/php -f
 
<?php

echo "Starting the pfSense shell system";

echo ".";
require("globals.inc");
$g['booting'] = true;
require("functions.inc");
echo ".";
require("config.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() {
	echo "\nExample commands:\n";
	
	echo "\nparse_config(true);  # reloads the \$config array\n";

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

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

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

echo ".\n\n";

$pkg_interface='console';

$shell_active = true;

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";

while($shell_active == true) {
        $command = readline("\npfSense shell: ");
        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++;
        		}
        		system("$newcmd");
        		if($command_split[1] == "cd") {
        			echo "\nChanging working directory to {$command_split[2]}.\n";
        			chdir($command_split[2]);
        		}
        		$command = "";
				break;
        	case "!":
        		system("$newcmd");
        		$command = "";
				break;				
        }
	    if($command == "help") {
	    	show_help();
	    	$command = "";
	    }
		if($command == "multiline" or $command == "ml") {
			echo "\nmultiline mode enabled.  enter EOF on a blank line to execute.\n\n";
			$command = "";
			$mlcommand = "";
			$xxxyzyz = 0;
			while($command <> "EOF") {
				echo "pfSense multiline shell[$xxxyzyz]> ";
		        $command = readline("Command: ");
		        readline_add_history($command);
		        if($command == "help") 
		        	show_help();
		        if($command == "exit") 
		        	die;
		        if($command <> "EOF") 
		        	$mlcommand .= $command;
		        $xxxyzyz++;
			}
			$command = $mlcommand;
		}
		if($command) {
	        eval($command); 
	    }
}

OpenPOWER on IntegriCloud