Projet

Général

Profil

Télécharger (8,85 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / zeromq.inc @ 2f650f35

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("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
		return;
73
	}
74
	if($debug)
75
		echo "Creating ZMQSocket()\n";
76
	$server = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REP);
77
	if($debug)
78
		echo "Binding to {$protocol}://{$ipaddress}:{$port}\n";
79
	$server->bind("{$protocol}://{$ipaddress}:{$port}");
80
	if($debug) 
81
		echo "Entering while() loop\n";
82
	while ($msg = $server->recv()) {
83
		// Convert the XML to a PHP array
84
		$message = unserialize($msg);
85
		if($debug) {
86
			echo "Message received:\n";
87
			print_r($message);
88
		}
89
		switch ($message[2]) {
90
			case "pfsense.exec_shell":
91
				$function_to_call = "exec_shell_zeromq";
92
				break;
93
			case "pfsense.exec_php":
94
				$function_to_call = "exec_php_zeromq";
95
				break;
96
			case "pfsense.filter_configure":
97
				$function_to_call = "filter_configure_zeromq";
98
				break;
99
			case "pfsense.interfaces_carp_configure":
100
				$function_to_call = "interfaces_carp_configure_zeromq";
101
				break;
102
			case "pfsense.backup_config_section":
103
				$function_to_call = "backup_config_section_zeromq";
104
				break;
105
			case "pfsense.restore_config_section":
106
				$function_to_call = "restore_config_section_zeromq";
107
				break;
108
			case "pfsense.merge_config_section":
109
				$function_to_call = "merge_config_section_zeromq";
110
				break;
111
			case "pfsense.merge_installedpackages_section_zeromq":
112
				$function_to_call = "merge_installedpackages_section_zeromq";
113
				break;
114
			case "pfsense.check_firmware_version":
115
				$function_to_call = "check_firmware_version_zeromq";
116
				break;
117
			case "pfsense.reboot":
118
				$function_to_call = "reboot_zeromq";
119
				break;
120
			case "pfsense.get_notices":
121
				$function_to_call = "get_notices_zeromq";
122
				break;
123
		}
124
		if(!$function_to_call) {
125
			if($debug)
126
				echo "ERROR:  Could not find a function to call";
127
			return;
128
		} else {
129
			if($debug) 
130
				echo "Invoking function {$message[2]}()\n;";
131
		}
132
		/* Call function that is being invoked */
133
		$result = $function_to_call($message);
134
		/* echo back the result */
135
		$server->send($result);  
136
	}
137
}
138

    
139
function zeromq_auth($params) {
140
	global $config, $g, $debug;	
141

    
142
	$username = $params[0];
143
	$passwd = $params[1];
144
	
145
	$user = getUserEntry($username);
146
	if (!$user) {
147
		if($debug) 
148
			echo "Could not locate user $username with getUserEntry()\n";
149
		return false;
150
	}
151

    
152
	if (is_account_disabled($username) || is_account_expired($username)) {
153
		if($debug) 
154
			echo "Returning account expired/disabled\n";
155
		return false;
156
	}
157
		
158
	if ($user['password']) {
159
		$passwd = crypt($passwd, $user['password']);
160
		if ($passwd == $user['password'])
161
			return true;
162
	}
163

    
164
	if ($user['md5-hash']) {
165
		$passwd = md5($passwd);
166
		if ($passwd == $user['md5-hash'])
167
			return true;
168
	}
169

    
170
	if($debug) 
171
		echo "zeromq_auth() fall through == false\n";
172

    
173
	return false;
174
}
175

    
176
function exec_php_zeromq($raw_params) {
177
	global $config, $g, $debug;
178
	$params = $raw_params;
179
	if(zeromq_auth($raw_params) == false) {
180
		if($debug)
181
			echo "Auth failed in exec_shell_zeromq()\n";
182
		return ZEROMQ_AUTH_FAIL;
183
	}
184
	$exec_php = $params[3];
185
	if($debug) 
186
		echo "Running exec_php_zeromq(): {$exec_php}\n";
187
	eval($exec_php);
188
	if($toreturn) {
189
		return serialize($toreturn);
190
	} else
191
		return ZEROMQ_FASLE;
192
}
193

    
194
function exec_shell_zeromq($raw_params) {
195
	global $config, $g, $debug;
196
	$params = $raw_params;
197
	if(zeromq_auth($raw_params) == false) {
198
		if($debug)
199
			echo "Auth failed in exec_shell_zeromq()\n";
200
		return ZEROMQ_AUTH_FAIL;
201
	}
202
	$shell_cmd = $params[3];
203
	if($debug) 
204
		echo "Running exec_shell_zeromq(): {$shell_cmd}\n";
205
	mwexec($shell_cmd);
206
	return ZEROMQ_FASLE;
207
}
208

    
209
function backup_config_section_zeromq($raw_params) {
210
	global $config, $g, $debug;
211
	$params = $raw_params;
212
	if(zeromq_auth($raw_params) == false)
213
		return ZEROMQ_AUTH_FAIL;
214
	$val = array_intersect_key($config, array_flip($params[3]));
215
	return serialize($val);
216
}
217

    
218
function restore_config_section_zeromq($raw_params) {
219
	global $config, $g, $debug;
220
	$params = $raw_params;
221
	if(zeromq_auth($raw_params) == false) 
222
		return ZEROMQ_AUTH_FAIL;
223
	$config = array_merge($config, $params[3]);
224
	$mergedkeys = implode(",", array_keys($params[3]));
225
	write_config(sprintf(gettext("Merged in config (%s sections) from ZeroMQ client."),$mergedkeys));
226
	return ZEROMQ_FASLE;
227
}
228

    
229
function merge_installedpackages_section_zeromq($raw_params) {
230
	global $config, $g, $debug;
231
	$params = $raw_params;
232
	if(zeromq_auth($raw_params) == false) 
233
		return ZEROMQ_AUTH_FAIL;
234
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
235
	$mergedkeys = implode(",", array_keys($params[3]));
236
	write_config(sprintf(gettext("Merged in config (%s sections) from ZeroMQ client."),$mergedkeys));
237
	return ZEROMQ_FASLE;
238
}
239

    
240
function merge_config_section_zeromq($raw_params) {
241
	global $config, $g, $debug;
242
	$params = $raw_params;
243
	if(zeromq_auth($raw_params) == false)
244
	 	return ZEROMQ_AUTH_FAIL;
245
	$config = array_merge_recursive_unique($config, $params[0]);
246
	$mergedkeys = implode(",", array_keys($params[3]));
247
	write_config("Merged in config ({$mergedkeys} sections) from ZeroMQ client.");
248
	return ZEROMQ_FASLE;
249
}
250

    
251
function filter_configure_zeromq($raw_params) {
252
	global $config, $g, $debug;
253
	$params = $raw_params;
254
	if(zeromq_auth($raw_params) == false)
255
		return ZEROMQ_AUTH_FAIL;
256
	filter_configure();
257
	system_routing_configure();
258
	setup_gateways_monitor();
259
	relayd_configure();
260
	require_once("openvpn.inc");
261
	openvpn_resync_all();
262
	services_dhcpd_configure();
263
	if (isset($config['dnsmasq']['enable']))
264
		services_dnsmasq_configure();
265
	elseif (isset($config['unbound']['enable']))
266
		services_unbound_configure();
267
	local_sync_accounts();
268
	return ZEROMQ_FASLE;
269
}
270

    
271
function interfaces_carp_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
	interfaces_carp_setup();
277
	interfaces_vips_configure();
278
	return ZEROMQ_FASLE;
279
}
280

    
281
function check_firmware_version_zeromq($raw_params) {
282
	global $config, $g, $debug;
283
	$params = $raw_params;
284
	if(zeromq_auth($raw_params) == false) 
285
		return ZEROMQ_AUTH_FAIL;
286
	return serialize(check_firmware_version(false));
287
}
288

    
289
function reboot_zeromq($raw_params) {
290
	global $config, $g, $debug;
291
	$params = $raw_params;
292
	if(zeromq_auth($raw_params) == false)
293
		return ZEROMQ_AUTH_FAIL;
294
	mwexec_bg("/etc/rc.reboot");
295
	return ZEROMQ_FASLE;
296
}
297

    
298
function get_notices_zeromq($raw_params) {
299
	global $config, $g, $debug;
300
	$params = $raw_params;
301
	if(zeromq_auth($raw_params) == false)
302
		return ZEROMQ_AUTH_FAIL;
303
	if(!function_exists("get_notices"))
304
		require("notices.inc");
305
	if(!$params) {
306
		$toreturn = get_notices();
307
	} else {
308
		$toreturn = get_notices($params);
309
	}
310
	return serialize($toreturn);
311
}
312

    
313
?>
(67-67/67)