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