Project

General

Profile

Download (9.01 KB) Statistics
| Branch: | Tag: | Revision:
1 6cf2c8b0 Phil Davis
<?php
2 5da3430e Scott Ullrich
/*
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 86084233 Phil Davis
require_once("auth.inc");
36 5da3430e Scott Ullrich
37 6cf2c8b0 Phil Davis
//$debug = true;
38 5da3430e Scott Ullrich
39
/* zeromq_send: Send a message to a member node */
40 6cf2c8b0 Phil Davis
function zeromq_send($protocol = "tcp", $ipaddress = "127.0.0.1", $port = "8888",
41 2445e851 Scott Ullrich
					 $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 6cf2c8b0 Phil Davis
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 6cf2c8b0 Phil Davis
63 5da3430e Scott Ullrich
	/* 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 6cf2c8b0 Phil Davis
	if (!$ipaddress || !$port) {
70
		if ($debug) {
71 2445e851 Scott Ullrich
			echo "ERROR: You must pass, proto, ipaddress and port\n";
72 6cf2c8b0 Phil Davis
		}
73 5da3430e Scott Ullrich
		return;
74 2445e851 Scott Ullrich
	}
75 6cf2c8b0 Phil Davis
	if ($debug) {
76 2445e851 Scott Ullrich
		echo "Creating ZMQSocket()\n";
77 6cf2c8b0 Phil Davis
	}
78 5da3430e Scott Ullrich
	$server = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REP);
79 6cf2c8b0 Phil Davis
	if ($debug) {
80 2445e851 Scott Ullrich
		echo "Binding to {$protocol}://{$ipaddress}:{$port}\n";
81 6cf2c8b0 Phil Davis
	}
82 5da3430e Scott Ullrich
	$server->bind("{$protocol}://{$ipaddress}:{$port}");
83 6cf2c8b0 Phil Davis
	if ($debug) {
84 2445e851 Scott Ullrich
		echo "Entering while() loop\n";
85 6cf2c8b0 Phil Davis
	}
86 5da3430e Scott Ullrich
	while ($msg = $server->recv()) {
87 2445e851 Scott Ullrich
		// Convert the XML to a PHP array
88 1518d578 Scott Ullrich
		$message = unserialize($msg);
89 6cf2c8b0 Phil Davis
		if ($debug) {
90 1518d578 Scott Ullrich
			echo "Message received:\n";
91
			print_r($message);
92
		}
93
		switch ($message[2]) {
94 5da3430e Scott Ullrich
			case "pfsense.exec_shell":
95
				$function_to_call = "exec_shell_zeromq";
96 2445e851 Scott Ullrich
				break;
97 5da3430e Scott Ullrich
			case "pfsense.exec_php":
98
				$function_to_call = "exec_php_zeromq";
99 2445e851 Scott Ullrich
				break;
100 5da3430e Scott Ullrich
			case "pfsense.filter_configure":
101
				$function_to_call = "filter_configure_zeromq";
102 2445e851 Scott Ullrich
				break;
103 5da3430e Scott Ullrich
			case "pfsense.interfaces_carp_configure":
104
				$function_to_call = "interfaces_carp_configure_zeromq";
105 2445e851 Scott Ullrich
				break;
106 5da3430e Scott Ullrich
			case "pfsense.backup_config_section":
107
				$function_to_call = "backup_config_section_zeromq";
108 2445e851 Scott Ullrich
				break;
109 5da3430e Scott Ullrich
			case "pfsense.restore_config_section":
110
				$function_to_call = "restore_config_section_zeromq";
111 2445e851 Scott Ullrich
				break;
112 5da3430e Scott Ullrich
			case "pfsense.merge_config_section":
113
				$function_to_call = "merge_config_section_zeromq";
114 2445e851 Scott Ullrich
				break;
115 5da3430e Scott Ullrich
			case "pfsense.merge_installedpackages_section_zeromq":
116
				$function_to_call = "merge_installedpackages_section_zeromq";
117 2445e851 Scott Ullrich
				break;
118 5da3430e Scott Ullrich
			case "pfsense.check_firmware_version":
119
				$function_to_call = "check_firmware_version_zeromq";
120 2445e851 Scott Ullrich
				break;
121 5da3430e Scott Ullrich
			case "pfsense.reboot":
122
				$function_to_call = "reboot_zeromq";
123 2445e851 Scott Ullrich
				break;
124 5da3430e Scott Ullrich
			case "pfsense.get_notices":
125
				$function_to_call = "get_notices_zeromq";
126 2445e851 Scott Ullrich
				break;
127 5da3430e Scott Ullrich
		}
128 6cf2c8b0 Phil Davis
		if (!$function_to_call) {
129
			if ($debug) {
130 2445e851 Scott Ullrich
				echo "ERROR:  Could not find a function to call";
131 6cf2c8b0 Phil Davis
			}
132 5da3430e Scott Ullrich
			return;
133 1518d578 Scott Ullrich
		} else {
134 6cf2c8b0 Phil Davis
			if ($debug) {
135 1518d578 Scott Ullrich
				echo "Invoking function {$message[2]}()\n;";
136 6cf2c8b0 Phil Davis
			}
137 2445e851 Scott Ullrich
		}
138
		/* Call function that is being invoked */
139 5da3430e Scott Ullrich
		$result = $function_to_call($message);
140 2445e851 Scott Ullrich
		/* echo back the result */
141 6cf2c8b0 Phil Davis
		$server->send($result);
142 5da3430e Scott Ullrich
	}
143
}
144
145 052e65ef Scott Ullrich
function zeromq_auth($params) {
146 6cf2c8b0 Phil Davis
	global $config, $g, $debug;
147 052e65ef Scott Ullrich
148 2445e851 Scott Ullrich
	$username = $params[0];
149 1518d578 Scott Ullrich
	$passwd = $params[1];
150 6cf2c8b0 Phil Davis
151 052e65ef Scott Ullrich
	$user = getUserEntry($username);
152 2445e851 Scott Ullrich
	if (!$user) {
153 6cf2c8b0 Phil Davis
		if ($debug) {
154 2445e851 Scott Ullrich
			echo "Could not locate user $username with getUserEntry()\n";
155 6cf2c8b0 Phil Davis
		}
156 5da3430e Scott Ullrich
		return false;
157 2445e851 Scott Ullrich
	}
158 052e65ef Scott Ullrich
159 1518d578 Scott Ullrich
	if (is_account_disabled($username) || is_account_expired($username)) {
160 6cf2c8b0 Phil Davis
		if ($debug) {
161 1518d578 Scott Ullrich
			echo "Returning account expired/disabled\n";
162 6cf2c8b0 Phil Davis
		}
163 052e65ef Scott Ullrich
		return false;
164 1518d578 Scott Ullrich
	}
165 6cf2c8b0 Phil Davis
166 052e65ef Scott Ullrich
	if ($user['password']) {
167
		$passwd = crypt($passwd, $user['password']);
168 6cf2c8b0 Phil Davis
		if ($passwd == $user['password']) {
169 052e65ef Scott Ullrich
			return true;
170 6cf2c8b0 Phil Davis
		}
171 5da3430e Scott Ullrich
	}
172 052e65ef Scott Ullrich
173
	if ($user['md5-hash']) {
174
		$passwd = md5($passwd);
175 6cf2c8b0 Phil Davis
		if ($passwd == $user['md5-hash']) {
176 052e65ef Scott Ullrich
			return true;
177 6cf2c8b0 Phil Davis
		}
178 052e65ef Scott Ullrich
	}
179
180 6cf2c8b0 Phil Davis
	if ($debug) {
181 1518d578 Scott Ullrich
		echo "zeromq_auth() fall through == false\n";
182 6cf2c8b0 Phil Davis
	}
183 1518d578 Scott Ullrich
184 5da3430e Scott Ullrich
	return false;
185
}
186
187
function exec_php_zeromq($raw_params) {
188 1518d578 Scott Ullrich
	global $config, $g, $debug;
189
	$params = $raw_params;
190 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
191
		if ($debug) {
192 1518d578 Scott Ullrich
			echo "Auth failed in exec_shell_zeromq()\n";
193 6cf2c8b0 Phil Davis
		}
194 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
195 1518d578 Scott Ullrich
	}
196
	$exec_php = $params[3];
197 6cf2c8b0 Phil Davis
	if ($debug) {
198 1518d578 Scott Ullrich
		echo "Running exec_php_zeromq(): {$exec_php}\n";
199 6cf2c8b0 Phil Davis
	}
200 5da3430e Scott Ullrich
	eval($exec_php);
201 6cf2c8b0 Phil Davis
	if ($toreturn) {
202 1518d578 Scott Ullrich
		return serialize($toreturn);
203 6cf2c8b0 Phil Davis
	} else {
204 052e65ef Scott Ullrich
		return ZEROMQ_FASLE;
205 6cf2c8b0 Phil Davis
	}
206 5da3430e Scott Ullrich
}
207
208
function exec_shell_zeromq($raw_params) {
209 1518d578 Scott Ullrich
	global $config, $g, $debug;
210
	$params = $raw_params;
211 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
212
		if ($debug) {
213 1518d578 Scott Ullrich
			echo "Auth failed in exec_shell_zeromq()\n";
214 6cf2c8b0 Phil Davis
		}
215 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
216 1518d578 Scott Ullrich
	}
217
	$shell_cmd = $params[3];
218 6cf2c8b0 Phil Davis
	if ($debug) {
219 1518d578 Scott Ullrich
		echo "Running exec_shell_zeromq(): {$shell_cmd}\n";
220 6cf2c8b0 Phil Davis
	}
221 5da3430e Scott Ullrich
	mwexec($shell_cmd);
222 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
223 5da3430e Scott Ullrich
}
224
225
function backup_config_section_zeromq($raw_params) {
226 1518d578 Scott Ullrich
	global $config, $g, $debug;
227
	$params = $raw_params;
228 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
229 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
230 6cf2c8b0 Phil Davis
	}
231 5180d00a Scott Ullrich
	$val = array_intersect_key($config, array_flip($params[3]));
232 1518d578 Scott Ullrich
	return serialize($val);
233 5da3430e Scott Ullrich
}
234
235
function restore_config_section_zeromq($raw_params) {
236 1518d578 Scott Ullrich
	global $config, $g, $debug;
237
	$params = $raw_params;
238 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
239 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
240 6cf2c8b0 Phil Davis
	}
241 5180d00a Scott Ullrich
	$config = array_merge($config, $params[3]);
242
	$mergedkeys = implode(",", array_keys($params[3]));
243 086cf944 Phil Davis
	write_config(sprintf(gettext("Merged in config (%s sections) from ZeroMQ client."), $mergedkeys));
244 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
245 5da3430e Scott Ullrich
}
246
247
function merge_installedpackages_section_zeromq($raw_params) {
248 1518d578 Scott Ullrich
	global $config, $g, $debug;
249
	$params = $raw_params;
250 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
251 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
252 6cf2c8b0 Phil Davis
	}
253 5da3430e Scott Ullrich
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
254 5180d00a Scott Ullrich
	$mergedkeys = implode(",", array_keys($params[3]));
255 086cf944 Phil Davis
	write_config(sprintf(gettext("Merged in config (%s sections) from ZeroMQ client."), $mergedkeys));
256 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
257 5da3430e Scott Ullrich
}
258
259
function merge_config_section_zeromq($raw_params) {
260 1518d578 Scott Ullrich
	global $config, $g, $debug;
261
	$params = $raw_params;
262 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
263
		return ZEROMQ_AUTH_FAIL;
264
	}
265 5da3430e Scott Ullrich
	$config = array_merge_recursive_unique($config, $params[0]);
266 5180d00a Scott Ullrich
	$mergedkeys = implode(",", array_keys($params[3]));
267 bc87e826 Scott Ullrich
	write_config("Merged in config ({$mergedkeys} sections) from ZeroMQ client.");
268 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
269 5da3430e Scott Ullrich
}
270
271
function filter_configure_zeromq($raw_params) {
272 1518d578 Scott Ullrich
	global $config, $g, $debug;
273
	$params = $raw_params;
274 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
275 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
276 6cf2c8b0 Phil Davis
	}
277 5da3430e Scott Ullrich
	filter_configure();
278
	system_routing_configure();
279
	setup_gateways_monitor();
280
	relayd_configure();
281
	require_once("openvpn.inc");
282
	openvpn_resync_all();
283
	services_dhcpd_configure();
284 6cf2c8b0 Phil Davis
	if (isset($config['dnsmasq']['enable'])) {
285 50b2851b Warren Baker
		services_dnsmasq_configure();
286 6cf2c8b0 Phil Davis
	} elseif (isset($config['unbound']['enable'])) {
287 50b2851b Warren Baker
		services_unbound_configure();
288 6cf2c8b0 Phil Davis
	}
289 5da3430e Scott Ullrich
	local_sync_accounts();
290 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
291 5da3430e Scott Ullrich
}
292
293
function interfaces_carp_configure_zeromq($raw_params) {
294 1518d578 Scott Ullrich
	global $config, $g, $debug;
295
	$params = $raw_params;
296 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
297 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
298 6cf2c8b0 Phil Davis
	}
299 8ff85c39 Ermal
	interfaces_sync_setup();
300 5da3430e Scott Ullrich
	interfaces_vips_configure();
301 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
302 5da3430e Scott Ullrich
}
303
304
function check_firmware_version_zeromq($raw_params) {
305 bc87e826 Scott Ullrich
	global $config, $g, $debug;
306 1518d578 Scott Ullrich
	$params = $raw_params;
307 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
308 2445e851 Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
309 6cf2c8b0 Phil Davis
	}
310 1518d578 Scott Ullrich
	return serialize(check_firmware_version(false));
311 5da3430e Scott Ullrich
}
312
313
function reboot_zeromq($raw_params) {
314 1518d578 Scott Ullrich
	global $config, $g, $debug;
315
	$params = $raw_params;
316 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
317 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
318 6cf2c8b0 Phil Davis
	}
319 5da3430e Scott Ullrich
	mwexec_bg("/etc/rc.reboot");
320 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
321 5da3430e Scott Ullrich
}
322
323
function get_notices_zeromq($raw_params) {
324 bc87e826 Scott Ullrich
	global $config, $g, $debug;
325 1518d578 Scott Ullrich
	$params = $raw_params;
326 6cf2c8b0 Phil Davis
	if (zeromq_auth($raw_params) == false) {
327 5da3430e Scott Ullrich
		return ZEROMQ_AUTH_FAIL;
328 6cf2c8b0 Phil Davis
	}
329
	if (!function_exists("get_notices")) {
330 2f650f35 Phil Davis
		require("notices.inc");
331 6cf2c8b0 Phil Davis
	}
332
	if (!$params) {
333 5da3430e Scott Ullrich
		$toreturn = get_notices();
334
	} else {
335
		$toreturn = get_notices($params);
336
	}
337 1518d578 Scott Ullrich
	return serialize($toreturn);
338 5da3430e Scott Ullrich
}
339
340 62bf5cd0 Renato Botelho
?>