Project

General

Profile

Download (8.61 KB) Statistics
| Branch: | Tag: | Revision:
1 5da3430e Scott Ullrich
<?php 
2
/*
3
	zeromq.inc
4
	part of the pfSense project (http://www.pfsense.com)
5
	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 666d84c1 Scott Ullrich
require("auth.inc");
36 5da3430e Scott Ullrich
37
/* zeromq_send: Send a message to a member node */
38 2445e851 Scott Ullrich
function zeromq_send($protocol = "tcp", $ipaddress = "127.0.0.1", $port = "8888", 
39
					 $method, $params, $username, $password) {
40 052e65ef Scott Ullrich
	if(!$ipaddress || !$port || !$message || !$username || !$password) 
41 5da3430e Scott Ullrich
		return;
42 2445e851 Scott Ullrich
	if(!is_array($params)) 
43
		return;
44
45 052e65ef Scott Ullrich
	/* Set calling function and auth information */
46 2445e851 Scott Ullrich
	$xmlparams = array(
47
		XML_RPC_encode($username),
48
		XML_RPC_encode($password),
49
		XML_RPC_encode($params)
50
	);
51 052e65ef Scott Ullrich
	
52 2445e851 Scott Ullrich
	/* Create the XML message with params and credentials */
53
	$msg = new XML_RPC_Message($method, $xmlparams);
54 5da3430e Scott Ullrich
55
	/* Create new queue object */
56
	$queue = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, "MySock1");
57
	$queue->connect("{$protocol}://{$ipaddress}:{$port}");
58
59
	/* Assign socket 1 to the queue, send and receive */
60 2445e851 Scott Ullrich
	$result = $queue->send($msg)->recv();
61 5da3430e Scott Ullrich
62 2445e851 Scott Ullrich
	/* xmlrpc_params_to_php() the result and return */
63
	$unserializedresult = xmlrpc_params_to_php($result);
64 5da3430e Scott Ullrich
	
65
	/* Return the result to the caller */
66
	return $unserializedresult;
67
}
68
69 2445e851 Scott Ullrich
function zeromq_server($protocol = "tcp", $ipaddress = "127.0.0.1", $port = "8888") {
70
	global $debug;
71
	if(!$ipaddress || !$port) {
72
		if($debug) 
73
			echo "ERROR: You must pass, proto, ipaddress and port\n";
74 5da3430e Scott Ullrich
		return;
75 2445e851 Scott Ullrich
	}
76
	if($debug)
77
		echo "Creating ZMQSocket()\n";
78 5da3430e Scott Ullrich
	$server = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REP);
79 2445e851 Scott Ullrich
	if($debug)
80
		echo "Binding to {$protocol}://{$ipaddress}:{$port}\n";
81 5da3430e Scott Ullrich
	$server->bind("{$protocol}://{$ipaddress}:{$port}");
82 2445e851 Scott Ullrich
	if($debug) 
83
		echo "Entering while() loop\n";
84 5da3430e Scott Ullrich
	while ($msg = $server->recv()) {
85 2445e851 Scott Ullrich
		//$message = unserialize($msg);
86
		// Convert the XML to a PHP array
87
		$message = xmlrpc_params_to_php($msg);
88
		switch ($message[3]) {
89 5da3430e Scott Ullrich
			case "pfsense.exec_shell":
90
				$function_to_call = "exec_shell_zeromq";
91 2445e851 Scott Ullrich
				break;
92 5da3430e Scott Ullrich
			case "pfsense.exec_php":
93
				$function_to_call = "exec_php_zeromq";
94 2445e851 Scott Ullrich
				break;
95 5da3430e Scott Ullrich
			case "pfsense.filter_configure":
96
				$function_to_call = "filter_configure_zeromq";
97 2445e851 Scott Ullrich
				break;
98 5da3430e Scott Ullrich
			case "pfsense.interfaces_carp_configure":
99
				$function_to_call = "interfaces_carp_configure_zeromq";
100 2445e851 Scott Ullrich
				break;
101 5da3430e Scott Ullrich
			case "pfsense.backup_config_section":
102
				$function_to_call = "backup_config_section_zeromq";
103 2445e851 Scott Ullrich
				break;
104 5da3430e Scott Ullrich
			case "pfsense.restore_config_section":
105
				$function_to_call = "restore_config_section_zeromq";
106 2445e851 Scott Ullrich
				break;
107 5da3430e Scott Ullrich
			case "pfsense.merge_config_section":
108
				$function_to_call = "merge_config_section_zeromq";
109 2445e851 Scott Ullrich
				break;
110 5da3430e Scott Ullrich
			case "pfsense.merge_installedpackages_section_zeromq":
111
				$function_to_call = "merge_installedpackages_section_zeromq";
112 2445e851 Scott Ullrich
				break;
113 5da3430e Scott Ullrich
			case "pfsense.check_firmware_version":
114
				$function_to_call = "check_firmware_version_zeromq";
115 2445e851 Scott Ullrich
				break;
116 5da3430e Scott Ullrich
			case "pfsense.reboot":
117
				$function_to_call = "reboot_zeromq";
118 2445e851 Scott Ullrich
				break;
119 5da3430e Scott Ullrich
			case "pfsense.get_notices":
120
				$function_to_call = "get_notices_zeromq";
121 2445e851 Scott Ullrich
				break;
122 5da3430e Scott Ullrich
		}
123 2445e851 Scott Ullrich
		if(!$function_to_call) {
124
			if($debug)
125
				echo "ERROR:  Could not find a function to call";
126 5da3430e Scott Ullrich
			return;
127 2445e851 Scott Ullrich
		}
128
		/* Call function that is being invoked */
129 5da3430e Scott Ullrich
		$result = $function_to_call($message);
130 2445e851 Scott Ullrich
		/* echo back the result */
131
		$server->send($result);  
132 5da3430e Scott Ullrich
	}
133
}
134
135 052e65ef Scott Ullrich
function zeromq_auth($params) {
136 2445e851 Scott Ullrich
	global $config, $g, $debug;	
137 052e65ef Scott Ullrich
138 2445e851 Scott Ullrich
	$username = $params[0];
139
	$password = $params[1];
140 052e65ef Scott Ullrich
	
141
	$user = getUserEntry($username);
142 2445e851 Scott Ullrich
	if (!$user) {
143
		if($debug) 
144
			echo "Could not locate user $username with getUserEntry()\n";
145 5da3430e Scott Ullrich
		return false;
146 2445e851 Scott Ullrich
	}
147 052e65ef Scott Ullrich
148
	if (is_account_disabled($username) || is_account_expired($username))
149
		return false;
150
151
	if ($user['password']) {
152
		$passwd = crypt($passwd, $user['password']);
153
		if ($passwd == $user['password'])
154
			return true;
155 5da3430e Scott Ullrich
	}
156 052e65ef Scott Ullrich
157
	if ($user['md5-hash']) {
158
		$passwd = md5($passwd);
159
		if ($passwd == $user['md5-hash'])
160
			return true;
161
	}
162
163 5da3430e Scott Ullrich
	return false;
164
}
165
166
function exec_php_zeromq($raw_params) {
167
	global $config, $g;
168
	$params = xmlrpc_params_to_php($raw_params);
169
	if(!zeromq_auth($params)) 
170
		return ZEROMQ_AUTH_FAIL;
171
	$exec_php = $params[0];
172
	eval($exec_php);
173
	if($toreturn) {
174
		$response = XML_RPC_encode($toreturn);
175
		return new XML_RPC_Response($response);
176
	} else
177 052e65ef Scott Ullrich
		return ZEROMQ_FASLE;
178 5da3430e Scott Ullrich
}
179
180
function exec_shell_zeromq($raw_params) {
181
	global $config, $g;
182
	$params = xmlrpc_params_to_php($raw_params);
183
	if(!zeromq_auth($params)) 
184
		return ZEROMQ_AUTH_FAIL;
185
	$shell_cmd = $params[0];
186
	mwexec($shell_cmd);
187 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
188 5da3430e Scott Ullrich
}
189
190
function backup_config_section_zeromq($raw_params) {
191
	global $config, $g;
192
	$params = xmlrpc_params_to_php($raw_params);
193
	if(!zeromq_auth($params)) 
194
		return ZEROMQ_AUTH_FAIL;
195
	$val = array_intersect_key($config, array_flip($params[0]));
196
	return new XML_RPC_Response(XML_RPC_encode($val));
197
}
198
199
function restore_config_section_zeromq($raw_params) {
200
	global $config, $g;
201
	$params = xmlrpc_params_to_php($raw_params);
202
	if(!zeromq_auth($params)) 
203
		return ZEROMQ_AUTH_FAIL;
204
	$config = array_merge($config, $params[0]);
205
	$mergedkeys = implode(",", array_keys($params[0]));
206
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
207 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
208 5da3430e Scott Ullrich
}
209
210
function merge_installedpackages_section_zeromq($raw_params) {
211
	global $config, $g;
212
	$params = xmlrpc_params_to_php($raw_params);
213
	if(!zeromq_auth($params)) 
214
		return ZEROMQ_AUTH_FAIL;
215
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
216
	$mergedkeys = implode(",", array_keys($params[0]));
217
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
218 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
219 5da3430e Scott Ullrich
}
220
221
function merge_config_section_zeromq($raw_params) {
222
	global $config, $g;
223
	$params = xmlrpc_params_to_php($raw_params);
224
	if(!zeromq_auth($params))
225
	 	return ZEROMQ_AUTH_FAIL;
226
	$config = array_merge_recursive_unique($config, $params[0]);
227
	$mergedkeys = implode(",", array_keys($params[0]));
228
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
229 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
230 5da3430e Scott Ullrich
}
231
232
function filter_configure_zeromq($raw_params) {
233
	global $config, $g;
234
	$params = xmlrpc_params_to_php($raw_params);
235
	if(!zeromq_auth($params))
236
		return ZEROMQ_AUTH_FAIL;
237
	filter_configure();
238
	system_routing_configure();
239
	setup_gateways_monitor();
240
	relayd_configure();
241
	require_once("openvpn.inc");
242
	openvpn_resync_all();
243
	services_dhcpd_configure();
244
	services_dnsmasq_configure();
245
	local_sync_accounts();
246 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
247 5da3430e Scott Ullrich
}
248
249
function interfaces_carp_configure_zeromq($raw_params) {
250
	global $config, $g;
251
	$params = xmlrpc_params_to_php($raw_params);
252
	if(!zeromq_auth($params)) 
253
		return ZEROMQ_AUTH_FAIL;
254
	interfaces_carp_setup();
255
	interfaces_vips_configure();
256 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
257 5da3430e Scott Ullrich
}
258
259
function check_firmware_version_zeromq($raw_params) {
260
	global  $XML_RPC_String;
261
	$params = xmlrpc_params_to_php($raw_params);
262 2445e851 Scott Ullrich
	if(!zeromq_auth($params)) 
263
		return ZEROMQ_AUTH_FAIL;
264 5da3430e Scott Ullrich
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
265
}
266
267
function reboot_zeromq($raw_params) {
268
	global $config, $g;
269
	$params = xmlrpc_params_to_php($raw_params);
270
	if(!zeromq_auth($params))
271
		return ZEROMQ_AUTH_FAIL;
272
	mwexec_bg("/etc/rc.reboot");
273 052e65ef Scott Ullrich
	return ZEROMQ_FASLE;
274 5da3430e Scott Ullrich
}
275
276
function get_notices_zeromq($raw_params) {
277
	global $g;
278
	$params = xmlrpc_params_to_php($raw_params);
279
	if(!zeromq_auth($params))
280
		return ZEROMQ_AUTH_FAIL;
281
	require("notices.inc");
282
	if(!$params) {
283
		$toreturn = get_notices();
284
	} else {
285
		$toreturn = get_notices($params);
286
	}
287
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
288
	return $response;
289
}
290
291
?>