1
|
<?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
|
define('ZEROMQ_TRUE', 'true');
|
32
|
define('ZEROMQ_FASLE', 'false');
|
33
|
|
34
|
$do_not_include_config_gui_inc = true;
|
35
|
require("auth.inc");
|
36
|
|
37
|
/* zeromq_send: Send a message to a member node */
|
38
|
function zeromq_send($protocol = "tcp", $ipaddress = "127.0.0.1", $port = "8888",
|
39
|
$method, $params, $username, $password) {
|
40
|
if(!$ipaddress || !$port || !$message || !$username || !$password)
|
41
|
return;
|
42
|
if(!is_array($params))
|
43
|
return;
|
44
|
|
45
|
/* Set calling function and auth information */
|
46
|
$xmlparams = array(
|
47
|
XML_RPC_encode($username),
|
48
|
XML_RPC_encode($password),
|
49
|
XML_RPC_encode($params)
|
50
|
);
|
51
|
|
52
|
/* Create the XML message with params and credentials */
|
53
|
$msg = new XML_RPC_Message($method, $xmlparams);
|
54
|
|
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
|
$result = $queue->send($msg)->recv();
|
61
|
|
62
|
/* xmlrpc_params_to_php() the result and return */
|
63
|
$unserializedresult = xmlrpc_params_to_php($result);
|
64
|
|
65
|
/* Return the result to the caller */
|
66
|
return $unserializedresult;
|
67
|
}
|
68
|
|
69
|
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
|
return;
|
75
|
}
|
76
|
if($debug)
|
77
|
echo "Creating ZMQSocket()\n";
|
78
|
$server = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REP);
|
79
|
if($debug)
|
80
|
echo "Binding to {$protocol}://{$ipaddress}:{$port}\n";
|
81
|
$server->bind("{$protocol}://{$ipaddress}:{$port}");
|
82
|
if($debug)
|
83
|
echo "Entering while() loop\n";
|
84
|
while ($msg = $server->recv()) {
|
85
|
//$message = unserialize($msg);
|
86
|
// Convert the XML to a PHP array
|
87
|
$message = xmlrpc_params_to_php($msg);
|
88
|
switch ($message[3]) {
|
89
|
case "pfsense.exec_shell":
|
90
|
$function_to_call = "exec_shell_zeromq";
|
91
|
break;
|
92
|
case "pfsense.exec_php":
|
93
|
$function_to_call = "exec_php_zeromq";
|
94
|
break;
|
95
|
case "pfsense.filter_configure":
|
96
|
$function_to_call = "filter_configure_zeromq";
|
97
|
break;
|
98
|
case "pfsense.interfaces_carp_configure":
|
99
|
$function_to_call = "interfaces_carp_configure_zeromq";
|
100
|
break;
|
101
|
case "pfsense.backup_config_section":
|
102
|
$function_to_call = "backup_config_section_zeromq";
|
103
|
break;
|
104
|
case "pfsense.restore_config_section":
|
105
|
$function_to_call = "restore_config_section_zeromq";
|
106
|
break;
|
107
|
case "pfsense.merge_config_section":
|
108
|
$function_to_call = "merge_config_section_zeromq";
|
109
|
break;
|
110
|
case "pfsense.merge_installedpackages_section_zeromq":
|
111
|
$function_to_call = "merge_installedpackages_section_zeromq";
|
112
|
break;
|
113
|
case "pfsense.check_firmware_version":
|
114
|
$function_to_call = "check_firmware_version_zeromq";
|
115
|
break;
|
116
|
case "pfsense.reboot":
|
117
|
$function_to_call = "reboot_zeromq";
|
118
|
break;
|
119
|
case "pfsense.get_notices":
|
120
|
$function_to_call = "get_notices_zeromq";
|
121
|
break;
|
122
|
}
|
123
|
if(!$function_to_call) {
|
124
|
if($debug)
|
125
|
echo "ERROR: Could not find a function to call";
|
126
|
return;
|
127
|
}
|
128
|
/* Call function that is being invoked */
|
129
|
$result = $function_to_call($message);
|
130
|
/* echo back the result */
|
131
|
$server->send($result);
|
132
|
}
|
133
|
}
|
134
|
|
135
|
function zeromq_auth($params) {
|
136
|
global $config, $g, $debug;
|
137
|
|
138
|
$username = $params[0];
|
139
|
$password = $params[1];
|
140
|
|
141
|
$user = getUserEntry($username);
|
142
|
if (!$user) {
|
143
|
if($debug)
|
144
|
echo "Could not locate user $username with getUserEntry()\n";
|
145
|
return false;
|
146
|
}
|
147
|
|
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
|
}
|
156
|
|
157
|
if ($user['md5-hash']) {
|
158
|
$passwd = md5($passwd);
|
159
|
if ($passwd == $user['md5-hash'])
|
160
|
return true;
|
161
|
}
|
162
|
|
163
|
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
|
return ZEROMQ_FASLE;
|
178
|
}
|
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
|
return ZEROMQ_FASLE;
|
188
|
}
|
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
|
return ZEROMQ_FASLE;
|
208
|
}
|
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
|
return ZEROMQ_FASLE;
|
219
|
}
|
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
|
return ZEROMQ_FASLE;
|
230
|
}
|
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
|
return ZEROMQ_FASLE;
|
247
|
}
|
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
|
return ZEROMQ_FASLE;
|
257
|
}
|
258
|
|
259
|
function check_firmware_version_zeromq($raw_params) {
|
260
|
global $XML_RPC_String;
|
261
|
$params = xmlrpc_params_to_php($raw_params);
|
262
|
if(!zeromq_auth($params))
|
263
|
return ZEROMQ_AUTH_FAIL;
|
264
|
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
|
return ZEROMQ_FASLE;
|
274
|
}
|
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
|
?>
|