summaryrefslogtreecommitdiffstats
path: root/etc/inc/zeromq.inc
blob: f644fe2fe975706d1827b461c72e1cd84d529855 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php 
/*
	zeromq.inc
	part of the pfSense project (http://www.pfsense.com)
	Copyright 2010 Scott Ullrich <sullrich@gmail.com>
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

define('ZEROMQ_AUTH_FAIL', 'authfail');

/* zeromq_send: Send a message to a member node */
function zeromq_send($protocol = "tcp", $ipaddress, $port, $message) {
	if(!$ipaddress || !$port || !$message) 
		return;
	
	/* Serialize the data we are going to send over */
	$serialized = serialize($message);

	/* Create new queue object */
	$queue = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, "MySock1");
	$queue->connect("{$protocol}://{$ipaddress}:{$port}");

	/* Assign socket 1 to the queue, send and receive */
	$result = $queue->send($serialized)->recv();

	/* Unserialize the return and return */
	$unserializedresult = unserialize($result);
	
	/* Return the result to the caller */
	return $unserializedresult;
}

function zeromq_server($protocol = "tcp", $ipaddress, $port) {
	if(!$ipaddress || !$port) 
		return;
	$server = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REP);
	$server->bind("{$protocol}://{$ipaddress}:{$port}");
	/* Loop receiving and echoing back */
	while ($msg = $server->recv()) {
	    $message = unserialize($msg);
		switch ($message['function']) {
			case "pfsense.exec_shell":
				$function_to_call = "exec_shell_zeromq";
			break;
			case "pfsense.exec_php":
				$function_to_call = "exec_php_zeromq";
			break;
			case "pfsense.filter_configure":
				$function_to_call = "filter_configure_zeromq";
			break;
			case "pfsense.interfaces_carp_configure":
				$function_to_call = "interfaces_carp_configure_zeromq";
			break;
			case "pfsense.backup_config_section":
				$function_to_call = "backup_config_section_zeromq";
			break;
			case "pfsense.restore_config_section":
				$function_to_call = "restore_config_section_zeromq";
			break;
			case "pfsense.merge_config_section":
				$function_to_call = "merge_config_section_zeromq";
			break;
			case "pfsense.merge_installedpackages_section_zeromq":
				$function_to_call = "merge_installedpackages_section_zeromq";
			break;
			case "pfsense.check_firmware_version":
				$function_to_call = "check_firmware_version_zeromq";
			break;
			case "pfsense.reboot":
				$function_to_call = "reboot_zeromq";
			break;
			case "pfsense.get_notices":
				$function_to_call = "get_notices_zeromq";
			break;
		}
		if(!$function_to_call) 
			return;
		// Call function that is being invoked
		$result = $function_to_call($message);
	    /* echo back the result */
	    $server->send($result);  
	}
}

function zeromq_auth(&$params) {
	global $config, $g;
	$localpass =  $config['system']['user'][0]['password'];	
	if(crypt($params[0], $localpass) == $localpass) {
		array_shift($params);
		return true;
	} else if(crypt($params['xmlrpcauth'],  $localpass)  !=  $localpass)  {
		unset($params['xmlrpcauth']);
		return false;
	}
	unset($params['xmlrpcauth']);
	return false;
}

function exec_php_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params)) 
		return ZEROMQ_AUTH_FAIL;
	$exec_php = $params[0];
	eval($exec_php);
	if($toreturn) {
		$response = XML_RPC_encode($toreturn);
		return new XML_RPC_Response($response);
	} else
		return $xmlrpc_g['return']['true'];
}

function exec_shell_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params)) 
		return ZEROMQ_AUTH_FAIL;
	$shell_cmd = $params[0];
	mwexec($shell_cmd);
	return $xmlrpc_g['return']['true'];
}

function backup_config_section_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params)) 
		return ZEROMQ_AUTH_FAIL;
	$val = array_intersect_key($config, array_flip($params[0]));
	return new XML_RPC_Response(XML_RPC_encode($val));
}

function restore_config_section_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params)) 
		return ZEROMQ_AUTH_FAIL;
	$config = array_merge($config, $params[0]);
	$mergedkeys = implode(",", array_keys($params[0]));
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
	return $xmlrpc_g['return']['true'];
}

function merge_installedpackages_section_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params)) 
		return ZEROMQ_AUTH_FAIL;
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
	$mergedkeys = implode(",", array_keys($params[0]));
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
	return $xmlrpc_g['return']['true'];
}

function merge_config_section_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params))
	 	return ZEROMQ_AUTH_FAIL;
	$config = array_merge_recursive_unique($config, $params[0]);
	$mergedkeys = implode(",", array_keys($params[0]));
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
	return $xmlrpc_g['return']['true'];
}

function filter_configure_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params))
		return ZEROMQ_AUTH_FAIL;
	filter_configure();
	system_routing_configure();
	setup_gateways_monitor();
	relayd_configure();
	require_once("openvpn.inc");
	openvpn_resync_all();
	services_dhcpd_configure();
	services_dnsmasq_configure();
	local_sync_accounts();
	return $xmlrpc_g['return']['true'];
}

function interfaces_carp_configure_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params)) 
		return ZEROMQ_AUTH_FAIL;
	interfaces_carp_setup();
	interfaces_vips_configure();
	return $xmlrpc_g['return']['true'];
}

function check_firmware_version_zeromq($raw_params) {
	global  $XML_RPC_String;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params)) return ZEROMQ_AUTH_FAIL;
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
}

function reboot_zeromq($raw_params) {
	global $config, $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params))
		return ZEROMQ_AUTH_FAIL;
	mwexec_bg("/etc/rc.reboot");
	return $xmlrpc_g['return']['true'];
}

function get_notices_zeromq($raw_params) {
	global $g;
	$params = xmlrpc_params_to_php($raw_params);
	if(!zeromq_auth($params))
		return ZEROMQ_AUTH_FAIL;
	require("notices.inc");
	if(!$params) {
		$toreturn = get_notices();
	} else {
		$toreturn = get_notices($params);
	}
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
	return $response;
}

?>
OpenPOWER on IntegriCloud