Project

General

Profile

Download (8.85 KB) Statistics
| Branch: | Tag: | Revision:
1 5da3430e Scott Ullrich
<?php 
2
/*
3
	zeromq.inc
4 5721595b Chris Buechler
	part of the pfSense project (https://www.pfsense.org)
5 5da3430e Scott Ullrich
	Copyright 2010 Scott Ullrich <sullrich@gmail.com>
6
	All rights reserved.
7
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
define('ZEROMQ_AUTH_FAIL', 'authfail');
31 052e65ef Scott Ullrich
define('ZEROMQ_TRUE', 'true');
32
define('ZEROMQ_FASLE', 'false');
33
34
$do_not_include_config_gui_inc = true;
35 603a97cb Phil Davis
require_once("auth.inc");
36 5da3430e Scott Ullrich
37 4614d447 Scott Ullrich
//$debug = true; 
38 5da3430e Scott Ullrich
39
/* zeromq_send: Send a message to a member node */
40 2445e851 Scott Ullrich
function zeromq_send($protocol = "tcp", $ipaddress = "127.0.0.1", $port = "8888", 
41
					 $method, $params, $username, $password) {
42 1518d578 Scott Ullrich
43
	global $debug;
44 2445e851 Scott Ullrich
45 052e65ef Scott Ullrich
	/* Set calling function and auth information */
46 2445e851 Scott Ullrich
	$xmlparams = array(
47 1518d578 Scott Ullrich
		$username,
48
		$password,
49
		$method,
50
		$params
51 2445e851 Scott Ullrich
	);
52 052e65ef Scott Ullrich
	
53 5da3430e Scott Ullrich
	/* Create new queue object */
54
	$queue = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, "MySock1");
55
	$queue->connect("{$protocol}://{$ipaddress}:{$port}");
56
57
	/* Assign socket 1 to the queue, send and receive */
58 1518d578 Scott Ullrich
	$result = $queue->send(serialize($xmlparams))->recv();
59 5da3430e Scott Ullrich
60 2445e851 Scott Ullrich
	/* xmlrpc_params_to_php() the result and return */
61 5da3430e Scott Ullrich
	$unserializedresult = unserialize($result);
62
	
63
	/* Return the result to the caller */
64
	return $unserializedresult;
65
}
66
67 2445e851 Scott Ullrich
function zeromq_server($protocol = "tcp", $ipaddress = "127.0.0.1", $port = "8888") {
68
	global $debug;
69
	if(!$ipaddress || !$port) {
70
		if($debug) 
71
			echo "ERROR: You must pass, proto, ipaddress and port\n";
72 5da3430e Scott Ullrich
		return;
73 2445e851 Scott Ullrich
	}
74
	if($debug)
75
		echo "Creating ZMQSocket()\n";
76 5da3430e Scott Ullrich
	$server = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REP);
77 2445e851 Scott Ullrich
	if($debug)
78
		echo "Binding to {$protocol}://{$ipaddress}:{$port}\n";
79 5da3430e Scott Ullrich
	$server->bind("{$protocol}://{$ipaddress}:{$port}");
80 2445e851 Scott Ullrich
	if($debug) 
81
		echo "Entering while() loop\n";
82 5da3430e Scott Ullrich
	while ($msg = $server->recv()) {
83 2445e851 Scott Ullrich
		// Convert the XML to a PHP array
84 1518d578 Scott Ullrich
		$message = unserialize($msg);
85
		if($debug) {
86
			echo "Message received:\n";
87
			print_r($message);
88
		}
89
		switch ($message[2]) {
90 5da3430e Scott Ullrich
			case "pfsense.exec_shell":
91
				$function_to_call = "exec_shell_zeromq";
92 2445e851 Scott Ullrich
				break;
93 5da3430e Scott Ullrich
			case "pfsense.exec_php":
94
				$function_to_call = "exec_php_zeromq";
95 2445e851 Scott Ullrich
				break;
96 5da3430e Scott Ullrich
			case "pfsense.filter_configure":
97
				$function_to_call = "filter_configure_zeromq";
98 2445e851 Scott Ullrich
				break;
99 5da3430e Scott Ullrich
			case "pfsense.interfaces_carp_configure":
100
				$function_to_call = "interfaces_carp_configure_zeromq";
101 2445e851 Scott Ullrich
				break;
102 5da3430e Scott Ullrich
			case "pfsense.backup_config_section":
103
				$function_to_call = "backup_config_section_zeromq";
104 2445e851 Scott Ullrich
				break;
105 5da3430e Scott Ullrich
			case "pfsense.restore_config_section":
106
				$function_to_call = "restore_config_section_zeromq";
107 2445e851 Scott Ullrich
				break;
108 5da3430e Scott Ullrich
			case "pfsense.merge_config_section":
109
				$function_to_call = "merge_config_section_zeromq";
110 2445e851 Scott Ullrich
				break;
111 5da3430e Scott Ullrich
			case "pfsense.merge_installedpackages_section_zeromq":
112
				$function_to_call = "merge_installedpackages_section_zeromq";
113 2445e851 Scott Ullrich
				break;
114 5da3430e Scott Ullrich
			case "pfsense.check_firmware_version":
115
				$function_to_call = "check_firmware_version_zeromq";
116 2445e851 Scott Ullrich
				break;
117 5da3430e Scott Ullrich
			case "pfsense.reboot":
118
				$function_to_call = "reboot_zeromq";
119 2445e851 Scott Ullrich
				break;
120 5da3430e Scott Ullrich
			case "pfsense.get_notices":
121
				$function_to_call = "get_notices_zeromq";
122 2445e851 Scott Ullrich
				break;
123 5da3430e Scott Ullrich
		}
124 2445e851 Scott Ullrich
		if(!$function_to_call) {
125
			if($debug)
126
				echo "ERROR:  Could not find a function to call";
127 5da3430e Scott Ullrich
			return;
128 1518d578 Scott Ullrich
		} else {
129
			if($debug) 
130
				echo "Invoking function {$message[2]}()\n;";
131 2445e851 Scott Ullrich
		}
132
		/* Call function that is being invoked */
133 5da3430e Scott Ullrich
		$result = $function_to_call($message);
134 2445e851 Scott Ullrich
		/* echo back the result */
135
		$server->send($result);  
136 5da3430e Scott Ullrich
	}
137
}
138
139 052e65ef Scott Ullrich
function zeromq_auth($params) {
140 2445e851 Scott Ullrich
	global $config, $g, $debug;	
141 052e65ef Scott Ullrich
142 2445e851 Scott Ullrich
	$username = $params[0];
143 1518d578 Scott Ullrich
	$passwd = $params[1];
144 052e65ef Scott Ullrich
	
145
	$user = getUserEntry($username);
146 2445e851 Scott Ullrich
	if (!$user) {
147
		if($debug) 
148
			echo "Could not locate user $username with getUserEntry()\n";
149 5da3430e Scott Ullrich
		return false;
150 2445e851 Scott Ullrich
	}
151 052e65ef Scott Ullrich
152 1518d578 Scott Ullrich
	if (is_account_disabled($username) || is_account_expired($username)) {
153
		if($debug) 
154
			echo "Returning account expired/disabled\n";
155 052e65ef Scott Ullrich
		return false;
156 1518d578 Scott Ullrich
	}
157
		
158 052e65ef Scott Ullrich
	if ($user['password']) {
159
		$passwd = crypt($passwd, $user['password']);
160
		if ($passwd == $user['password'])
161
			return true;
162 5da3430e Scott Ullrich
	}
163 052e65ef Scott Ullrich
164
	if ($user['md5-hash']) {
165
		$passwd = md5($passwd);
166
		if ($passwd == $user['md5-hash'])
167
			return true;
168
	}
169
170 1518d578 Scott Ullrich
	if($debug) 
171
		echo "zeromq_auth() fall through == false\n";
172
173 5da3430e Scott Ullrich
	return false;
174
}
175
176
function exec_php_zeromq($raw_params) {
177 1518d578 Scott Ullrich
	global $config, $g, $debug;
178
	$params = $raw_params;
179
	if(zeromq_auth($raw_params) == false) {
180
		if($debug)
181
			echo "Auth failed in exec_shell_zeromq()\n";
182 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
183 1518d578 Scott Ullrich
	}
184
	$exec_php = $params[3];
185
	if($debug) 
186
		echo "Running exec_php_zeromq(): {$exec_php}\n";
187 5da3430e Scott Ullrich
	eval($exec_php);
188
	if($toreturn) {
189 1518d578 Scott Ullrich
		return serialize($toreturn);
190 5da3430e Scott Ullrich
	} else
191 052e65ef Scott Ullrich
		return ZEROMQ_FASLE;
192 5da3430e Scott Ullrich
}
193
194
function exec_shell_zeromq($raw_params) {
195 1518d578 Scott Ullrich
	global $config, $g, $debug;
196
	$params = $raw_params;
197
	if(zeromq_auth($raw_params) == false) {
198
		if($debug)
199
			echo "Auth failed in exec_shell_zeromq()\n";
200 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
201 1518d578 Scott Ullrich
	}
202
	$shell_cmd = $params[3];
203
	if($debug) 
204
		echo "Running exec_shell_zeromq(): {$shell_cmd}\n";
205 5da3430e Scott Ullrich
	mwexec($shell_cmd);
206 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
207 5da3430e Scott Ullrich
}
208
209
function backup_config_section_zeromq($raw_params) {
210 1518d578 Scott Ullrich
	global $config, $g, $debug;
211
	$params = $raw_params;
212
	if(zeromq_auth($raw_params) == false)
213 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
214 5180d00a Scott Ullrich
	$val = array_intersect_key($config, array_flip($params[3]));
215 1518d578 Scott Ullrich
	return serialize($val);
216 5da3430e Scott Ullrich
}
217
218
function restore_config_section_zeromq($raw_params) {
219 1518d578 Scott Ullrich
	global $config, $g, $debug;
220
	$params = $raw_params;
221
	if(zeromq_auth($raw_params) == false) 
222 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
223 5180d00a Scott Ullrich
	$config = array_merge($config, $params[3]);
224
	$mergedkeys = implode(",", array_keys($params[3]));
225 bc87e826 Scott Ullrich
	write_config(sprintf(gettext("Merged in config (%s sections) from ZeroMQ client."),$mergedkeys));
226 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
227 5da3430e Scott Ullrich
}
228
229
function merge_installedpackages_section_zeromq($raw_params) {
230 1518d578 Scott Ullrich
	global $config, $g, $debug;
231
	$params = $raw_params;
232
	if(zeromq_auth($raw_params) == false) 
233 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
234
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
235 5180d00a Scott Ullrich
	$mergedkeys = implode(",", array_keys($params[3]));
236 bc87e826 Scott Ullrich
	write_config(sprintf(gettext("Merged in config (%s sections) from ZeroMQ client."),$mergedkeys));
237 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
238 5da3430e Scott Ullrich
}
239
240
function merge_config_section_zeromq($raw_params) {
241 1518d578 Scott Ullrich
	global $config, $g, $debug;
242
	$params = $raw_params;
243
	if(zeromq_auth($raw_params) == false)
244 5da3430e Scott Ullrich
	 	return ZEROMQ_AUTH_FAIL;
245
	$config = array_merge_recursive_unique($config, $params[0]);
246 5180d00a Scott Ullrich
	$mergedkeys = implode(",", array_keys($params[3]));
247 bc87e826 Scott Ullrich
	write_config("Merged in config ({$mergedkeys} sections) from ZeroMQ client.");
248 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
249 5da3430e Scott Ullrich
}
250
251
function filter_configure_zeromq($raw_params) {
252 1518d578 Scott Ullrich
	global $config, $g, $debug;
253
	$params = $raw_params;
254
	if(zeromq_auth($raw_params) == false)
255 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
256
	filter_configure();
257
	system_routing_configure();
258
	setup_gateways_monitor();
259
	relayd_configure();
260
	require_once("openvpn.inc");
261
	openvpn_resync_all();
262
	services_dhcpd_configure();
263 50b2851b Warren Baker
	if (isset($config['dnsmasq']['enable']))
264
		services_dnsmasq_configure();
265
	elseif (isset($config['unbound']['enable']))
266
		services_unbound_configure();
267 5da3430e Scott Ullrich
	local_sync_accounts();
268 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
269 5da3430e Scott Ullrich
}
270
271
function interfaces_carp_configure_zeromq($raw_params) {
272 1518d578 Scott Ullrich
	global $config, $g, $debug;
273
	$params = $raw_params;
274
	if(zeromq_auth($raw_params) == false) 
275 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
276 8ff85c39 Ermal
	interfaces_sync_setup();
277 5da3430e Scott Ullrich
	interfaces_vips_configure();
278 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
279 5da3430e Scott Ullrich
}
280
281
function check_firmware_version_zeromq($raw_params) {
282 bc87e826 Scott Ullrich
	global $config, $g, $debug;
283 1518d578 Scott Ullrich
	$params = $raw_params;
284
	if(zeromq_auth($raw_params) == false) 
285 2445e851 Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
286 1518d578 Scott Ullrich
	return serialize(check_firmware_version(false));
287 5da3430e Scott Ullrich
}
288
289
function reboot_zeromq($raw_params) {
290 1518d578 Scott Ullrich
	global $config, $g, $debug;
291
	$params = $raw_params;
292
	if(zeromq_auth($raw_params) == false)
293 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
294
	mwexec_bg("/etc/rc.reboot");
295 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
296 5da3430e Scott Ullrich
}
297
298
function get_notices_zeromq($raw_params) {
299 bc87e826 Scott Ullrich
	global $config, $g, $debug;
300 1518d578 Scott Ullrich
	$params = $raw_params;
301
	if(zeromq_auth($raw_params) == false)
302 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
303 2f650f35 Phil Davis
	if(!function_exists("get_notices"))
304
		require("notices.inc");
305 5da3430e Scott Ullrich
	if(!$params) {
306
		$toreturn = get_notices();
307
	} else {
308
		$toreturn = get_notices($params);
309
	}
310 1518d578 Scott Ullrich
	return serialize($toreturn);
311 5da3430e Scott Ullrich
}
312
313 62bf5cd0 Renato Botelho
?>