71558f85 |
1 | <?php |
2 | /** |
3 | * An XML-RPC server |
4 | * |
5 | * @author Donal McMullan donal@catalyst.net.nz |
6 | * @version 0.0.1 |
7 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
8 | * @package mnet |
9 | */ |
10 | |
11 | // Make certain that config.php doesn't display any errors, and that it doesn't |
12 | // override our do-not-display-errors setting: |
13 | ini_set('display_errors',0); |
14 | require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); |
15 | ini_set('display_errors',0); |
16 | |
17 | // Include MNET stuff: |
18 | require_once $CFG->dirroot.'/mnet/lib.php'; |
19 | require_once $CFG->dirroot.'/mnet/remote_client.php'; |
20 | |
600be062 |
21 | if ($CFG->mnet_dispatcher_mode === 'off') { |
22 | print_error('mnetdisabled', 'mnet'); |
23 | } |
24 | |
71558f85 |
25 | // Content type for output is not html: |
1008dad6 |
26 | header('Content-type: text/xml; charset=utf-8'); |
71558f85 |
27 | |
25202581 |
28 | // PHP 5.2.2: $HTTP_RAW_POST_DATA not populated bug: |
29 | // http://bugs.php.net/bug.php?id=41293 |
30 | if (empty($HTTP_RAW_POST_DATA)) { |
31 | $HTTP_RAW_POST_DATA = file_get_contents('php://input'); |
32 | } |
33 | |
71558f85 |
34 | if (!empty($CFG->mnet_rpcdebug)) { |
35 | trigger_error("HTTP_RAW_POST_DATA"); |
36 | trigger_error($HTTP_RAW_POST_DATA); |
37 | } |
38 | |
39 | // New global variable which ONLY gets set in this server page, so you know that |
40 | // if you've been called by a remote Moodle, this should be set: |
41 | $MNET_REMOTE_CLIENT = new mnet_remote_client(); |
42 | |
43 | // Peek at the message to see if it's an XML-ENC document. If it is, note that |
44 | // the client connection was encrypted, and strip the xml-encryption and |
45 | // xml-signature wrappers from the XML-RPC payload |
46 | if (strpos(substr($HTTP_RAW_POST_DATA, 0, 100), '<encryptedMessage>')) { |
47 | $MNET_REMOTE_CLIENT->was_encrypted(); |
48 | // Extract the XML-RPC payload from the XML-ENC and XML-SIG wrappers. |
49 | $payload = mnet_server_strip_wrappers($HTTP_RAW_POST_DATA); |
50 | } else { |
51 | $params = xmlrpc_decode_request($HTTP_RAW_POST_DATA, $method); |
52 | if ($method == 'system.keyswap' || |
53 | $method == 'system/keyswap') { |
54 | |
55 | // OK |
56 | |
57 | } elseif ($MNET_REMOTE_CLIENT->plaintext_is_ok() == false) { |
58 | exit(mnet_server_fault(7021, 'forbidden-transport')); |
59 | } |
60 | // Looks like plaintext is ok. It is assumed that a plaintext call: |
61 | // 1. Came from a trusted host on your local network |
62 | // 2. Is *not* from a Moodle - otherwise why skip encryption/signing? |
63 | // 3. Is free to execute ANY function in Moodle |
64 | // 4. Cannot execute any methods (as it can't instantiate a class first) |
65 | // To execute a method, you'll need to create a wrapper function that first |
66 | // instantiates the class, and then calls the method. |
67 | $payload = $HTTP_RAW_POST_DATA; |
68 | } |
69 | |
70 | if (!empty($CFG->mnet_rpcdebug)) { |
71 | trigger_error("XMLRPC Payload"); |
72 | trigger_error(print_r($payload,1)); |
73 | } |
74 | |
75 | // Parse and action the XML-RPC payload |
76 | $response = mnet_server_dispatch($payload); |
77 | |
78 | /** |
79 | * Strip the encryption (XML-ENC) and signature (XML-SIG) wrappers and return the XML-RPC payload |
80 | * |
81 | * IF COMMUNICATION TAKES PLACE OVER UNENCRYPTED HTTP: |
82 | * The payload will have been encrypted with a symmetric key. This key will |
83 | * itself have been encrypted using your public key. The key is decrypted using |
84 | * your private key, and then used to decrypt the XML payload. |
85 | * |
86 | * IF COMMUNICATION TAKES PLACE OVER UNENCRYPTED HTTP *OR* ENCRYPTED HTTPS: |
87 | * In either case, there will be an XML wrapper which contains your XML-RPC doc |
88 | * as an object element, a signature for that doc, and various standards- |
89 | * compliant info to aid in verifying the signature. |
90 | * |
91 | * This function parses the encryption wrapper, decrypts the contents, parses |
92 | * the signature wrapper, and if the signature matches the payload, it returns |
93 | * the payload, which should be an XML-RPC request. |
94 | * If there is an error, or the signatures don't match, it echoes an XML-RPC |
95 | * error and exits. |
96 | * |
97 | * See the W3C's {@link http://www.w3.org/TR/xmlenc-core/ XML Encryption Syntax and Processing} |
98 | * and {@link http://www.w3.org/TR/2001/PR-xmldsig-core-20010820/ XML-Signature Syntax and Processing} |
99 | * guidelines for more detail on the XML. |
100 | * |
101 | * -----XML-Envelope--------------------------------- |
102 | * | | |
103 | * | Encrypted-Symmetric-key---------------- | |
104 | * | |_____________________________________| | |
105 | * | | |
106 | * | Encrypted data------------------------- | |
107 | * | | | | |
108 | * | | -XML-Envelope------------------ | | |
109 | * | | | | | | |
110 | * | | | --Signature------------- | | | |
111 | * | | | |______________________| | | | |
112 | * | | | | | | |
113 | * | | | --Signed-Payload-------- | | | |
114 | * | | | | | | | | |
115 | * | | | | XML-RPC Request | | | | |
116 | * | | | |______________________| | | | |
117 | * | | | | | | |
118 | * | | |_____________________________| | | |
119 | * | |_____________________________________| | |
120 | * | | |
121 | * |________________________________________________| |
122 | * |
71558f85 |
123 | * @param string $HTTP_RAW_POST_DATA The XML that the client sent |
124 | * @return string The XMLRPC payload. |
125 | */ |
126 | function mnet_server_strip_wrappers($HTTP_RAW_POST_DATA) { |
127 | global $MNET, $MNET_REMOTE_CLIENT; |
2a75520f |
128 | if (!isset($_SERVER)) { |
129 | exit(mnet_server_fault(712, "phperror")); |
130 | } |
71558f85 |
131 | |
2a75520f |
132 | $crypt_parser = new mnet_encxml_parser(); |
133 | $crypt_parser->parse($HTTP_RAW_POST_DATA); |
71558f85 |
134 | |
2a75520f |
135 | // Make sure we know who we're talking to |
136 | $host_record_exists = $MNET_REMOTE_CLIENT->set_wwwroot($crypt_parser->remote_wwwroot); |
9dd0e611 |
137 | |
2a75520f |
138 | if (false == $host_record_exists) { |
139 | exit(mnet_server_fault(7020, 'wrong-wwwroot', $crypt_parser->remote_wwwroot)); |
140 | } |
9dd0e611 |
141 | |
2a75520f |
142 | if ($crypt_parser->payload_encrypted) { |
71558f85 |
143 | |
2a75520f |
144 | $key = array_pop($crypt_parser->cipher); // This key is Symmetric |
145 | $data = array_pop($crypt_parser->cipher); |
71558f85 |
146 | |
2a75520f |
147 | $crypt_parser->free_resource(); |
71558f85 |
148 | |
2a75520f |
149 | $payload = ''; // Initialize payload var |
150 | $push_current_key = false; // True if we need to push a fresh key to the peer |
71558f85 |
151 | |
2a75520f |
152 | // &$payload |
153 | $isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $MNET->get_private_key()); |
71558f85 |
154 | |
2a75520f |
155 | if (!$isOpen) { |
156 | // Decryption failed... let's try our archived keys |
157 | $openssl_history = get_config('mnet', 'openssl_history'); |
158 | if(empty($openssl_history)) { |
159 | $openssl_history = array(); |
160 | set_config('openssl_history', serialize($openssl_history), 'mnet'); |
161 | } else { |
162 | $openssl_history = unserialize($openssl_history); |
735c7beb |
163 | } |
2a75520f |
164 | foreach($openssl_history as $keyset) { |
165 | $keyresource = openssl_pkey_get_private($keyset['keypair_PEM']); |
166 | $isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $keyresource); |
167 | if ($isOpen) { |
168 | // It's an older code, sir, but it checks out |
c7c55615 |
169 | |
170 | // The peer used one of our public keys that have expired, we will return a |
171 | // signed/encrypted error message containing our new public key |
172 | // Sign message with our old key, and encrypt to the peer's private key. |
173 | |
174 | // Fabricate 'was_signed' |
175 | // Set here so that we sign the response containing the new public key. |
176 | $MNET_REMOTE_CLIENT->was_signed(); |
177 | |
178 | // 'Was_encrypted' is mostly true |
179 | // Set here so that the response is encrypted to the remote peer's private key. |
180 | $MNET_REMOTE_CLIENT->was_encrypted(); |
181 | |
182 | // nb 'srvr_fault_xml' used to avoid use of get_string on our new public_key |
183 | exit(mnet_server_fault_xml(7025, $MNET->public_key, $keyresource)); |
2a75520f |
184 | } |
71558f85 |
185 | } |
2a75520f |
186 | } |
71558f85 |
187 | |
2a75520f |
188 | if (!$isOpen) { |
189 | exit(mnet_server_fault(7023, 'encryption-invalid')); |
190 | } |
71558f85 |
191 | |
2a75520f |
192 | if (strpos(substr($payload, 0, 100), '<signedMessage>')) { |
193 | $MNET_REMOTE_CLIENT->was_signed(); |
194 | $sig_parser = new mnet_encxml_parser(); |
195 | $sig_parser->parse($payload); |
71558f85 |
196 | } else { |
2a75520f |
197 | exit(mnet_server_fault(7022, 'verifysignature-error')); |
71558f85 |
198 | } |
199 | |
2a75520f |
200 | } else { |
201 | exit(mnet_server_fault(7024, 'payload-not-encrypted')); |
202 | } |
71558f85 |
203 | |
2a75520f |
204 | unset($payload); |
71558f85 |
205 | |
2a75520f |
206 | /** |
207 | * Get the certificate (i.e. public key) from the remote server. |
208 | */ |
209 | $certificate = $MNET_REMOTE_CLIENT->public_key; |
71558f85 |
210 | |
2a75520f |
211 | if ($certificate == false) { |
212 | exit(mnet_server_fault(709, 'nosuchpublickey')); |
213 | } |
214 | |
215 | $payload = base64_decode($sig_parser->data_object); |
216 | |
217 | // Does the signature match the data and the public cert? |
218 | $signature_verified = openssl_verify($payload, base64_decode($sig_parser->signature), $certificate); |
219 | if ($signature_verified == 1) { |
220 | $MNET_REMOTE_CLIENT->touch(); |
221 | // Parse the XML |
222 | } elseif ($signature_verified == 0) { |
223 | $currkey = mnet_get_public_key($MNET_REMOTE_CLIENT->wwwroot, $MNET_REMOTE_CLIENT->application); |
224 | if($currkey != $certificate) { |
225 | // Has the server updated its certificate since our last |
226 | // handshake? |
227 | if(!$MNET_REMOTE_CLIENT->refresh_key()) { |
228 | exit(mnet_server_fault(7026, 'verifysignature-invalid')); |
9dd0e611 |
229 | } |
71558f85 |
230 | } else { |
2a75520f |
231 | exit(mnet_server_fault(710, 'verifysignature-invalid')); |
71558f85 |
232 | } |
71558f85 |
233 | } else { |
2a75520f |
234 | exit(mnet_server_fault(711, 'verifysignature-error')); |
71558f85 |
235 | } |
2a75520f |
236 | |
237 | $sig_parser->free_resource(); |
238 | |
239 | return $payload; |
71558f85 |
240 | } |
241 | |
242 | /** |
151a9872 |
243 | * Return the proper XML-RPC content to report an error in the local language. |
71558f85 |
244 | * |
245 | * @param int $code The ID code of the error message |
151a9872 |
246 | * @param string $text The array-key of the error message in the lang file |
ea676d6e |
247 | * or the full string (will be detected by the function |
151a9872 |
248 | * @param string $param The $a param for the error message in the lang file |
71558f85 |
249 | * @return string $text The text of the error message |
250 | */ |
251 | function mnet_server_fault($code, $text, $param = null) { |
151a9872 |
252 | global $MNET_REMOTE_CLIENT; |
71558f85 |
253 | if (!is_numeric($code)) { |
254 | $code = 0; |
255 | } |
256 | $code = intval($code); |
257 | |
ea676d6e |
258 | $string = get_string($text, 'mnet', $param); |
259 | if (strpos($string, '[[') === 0) { |
260 | $string = $text; |
261 | } |
262 | |
263 | return mnet_server_fault_xml($code, $string); |
151a9872 |
264 | } |
71558f85 |
265 | |
151a9872 |
266 | /** |
267 | * Return the proper XML-RPC content to report an error. |
268 | * |
09f0abb2 |
269 | * @param int $code The ID code of the error message |
270 | * @param string $text The error message |
271 | * @param resource $privatekey The private key that should be used to sign the response |
272 | * @return string $text The XML text of the error message |
151a9872 |
273 | */ |
09f0abb2 |
274 | function mnet_server_fault_xml($code, $text, $privatekey = null) { |
573f8b02 |
275 | global $MNET_REMOTE_CLIENT, $CFG; |
71558f85 |
276 | // Replace illegal XML chars - is this already in a lib somewhere? |
277 | $text = str_replace(array('<','>','&','"',"'"), array('<','>','&','"','''), $text); |
278 | |
279 | $return = mnet_server_prepare_response('<?xml version="1.0"?> |
280 | <methodResponse> |
281 | <fault> |
282 | <value> |
283 | <struct> |
284 | <member> |
285 | <name>faultCode</name> |
286 | <value><int>'.$code.'</int></value> |
287 | </member> |
288 | <member> |
289 | <name>faultString</name> |
290 | <value><string>'.$text.'</string></value> |
25202581 |
291 | </member> |
292 | </struct> |
293 | </value> |
294 | </fault> |
09f0abb2 |
295 | </methodResponse>', $privatekey); |
573f8b02 |
296 | |
297 | if (!empty($CFG->mnet_rpcdebug)) { |
3d706b49 |
298 | trigger_error("XMLRPC Error Response $code: $text"); |
573f8b02 |
299 | trigger_error(print_r($return,1)); |
300 | } |
301 | |
71558f85 |
302 | return $return; |
303 | } |
304 | |
305 | /** |
306 | * Dummy function for the XML-RPC dispatcher - use to call a method on an object |
307 | * or to call a function |
308 | * |
309 | * Translate XML-RPC's strange function call syntax into a more straightforward |
310 | * PHP-friendly alternative. This dummy function will be called by the |
311 | * dispatcher, and can be used to call a method on an object, or just a function |
312 | * |
313 | * The methodName argument (eg. mnet/testlib/mnet_concatenate_strings) |
314 | * is ignored. |
315 | * |
316 | * @param string $methodname We discard this - see 'functionname' |
317 | * @param array $argsarray Each element is an argument to the real |
318 | * function |
319 | * @param string $functionname The name of the PHP function you want to call |
320 | * @return mixed The return value will be that of the real |
321 | * function, whateber it may be. |
322 | */ |
323 | function mnet_server_dummy_method($methodname, $argsarray, $functionname) { |
324 | global $MNET_REMOTE_CLIENT; |
325 | |
b91b274b |
326 | if (is_object($MNET_REMOTE_CLIENT->object_to_call)) { |
71558f85 |
327 | return @call_user_method_array($functionname, $MNET_REMOTE_CLIENT->object_to_call, $argsarray); |
b91b274b |
328 | } else if (!empty($MNET_REMOTE_CLIENT->static_location)) { |
329 | return @call_user_func_array(array($MNET_REMOTE_CLIENT->static_location, $functionname), $argsarray); |
330 | } else { |
331 | return @call_user_func_array($functionname, $argsarray); |
71558f85 |
332 | } |
333 | } |
334 | |
335 | /** |
336 | * Package a response in any required envelope, and return it to the client |
337 | * |
09f0abb2 |
338 | * @param string $response The XMLRPC response string |
339 | * @param resource $privatekey The private key to sign the response with |
340 | * @return string The encoded response string |
71558f85 |
341 | */ |
09f0abb2 |
342 | function mnet_server_prepare_response($response, $privatekey = null) { |
71558f85 |
343 | global $MNET_REMOTE_CLIENT; |
344 | |
345 | if ($MNET_REMOTE_CLIENT->request_was_signed) { |
09f0abb2 |
346 | $response = mnet_sign_message($response, $privatekey); |
71558f85 |
347 | } |
348 | |
349 | if ($MNET_REMOTE_CLIENT->request_was_encrypted) { |
350 | $response = mnet_encrypt_message($response, $MNET_REMOTE_CLIENT->public_key); |
351 | } |
352 | |
353 | return $response; |
354 | } |
355 | |
356 | /** |
357 | * If security checks are passed, dispatch the request to the function/method |
358 | * |
359 | * The config variable 'mnet_dispatcher_mode' can be: |
360 | * strict: Only execute functions that are in specific files |
361 | * off: The default - don't execute anything |
362 | * |
363 | * @param string $payload The XML-RPC request |
364 | * @return No return val - just echo the response |
365 | */ |
366 | function mnet_server_dispatch($payload) { |
367 | global $CFG, $MNET_REMOTE_CLIENT; |
368 | // xmlrpc_decode_request returns an array of parameters, and the $method |
369 | // variable (which is passed by reference) is instantiated with the value from |
370 | // the methodName tag in the xml payload |
371 | // xmlrpc_decode_request($xml, &$method) |
372 | $params = xmlrpc_decode_request($payload, $method); |
373 | |
b26a847b |
374 | // $method is something like: "mod/forum/lib.php/forum_add_instance" |
71558f85 |
375 | // $params is an array of parameters. A parameter might itself be an array. |
376 | |
377 | // Whitelist characters that are permitted in a method name |
378 | // The method name must not begin with a / - avoid absolute paths |
379 | // A dot character . is only allowed in the filename, i.e. something.php |
380 | if (0 == preg_match("@^[A-Za-z0-9]+/[A-Za-z0-9/_-]+(\.php/)?[A-Za-z0-9_-]+$@",$method)) { |
381 | exit(mnet_server_fault(713, 'nosuchfunction')); |
382 | } |
383 | |
4fc66605 |
384 | if(preg_match("/^system\./", $method)) { |
b26a847b |
385 | $callstack = explode('.', $method); |
386 | } else { |
387 | $callstack = explode('/', $method); |
388 | // callstack will look like array('mod', 'forum', 'lib.php', 'forum_add_instance'); |
389 | } |
71558f85 |
390 | |
391 | /** |
392 | * What has the site administrator chosen as his dispatcher setting? |
393 | * strict: Only execute functions that are in specific files |
394 | * off: The default - don't execute anything |
395 | */ |
396 | ////////////////////////////////////// OFF |
397 | if (!isset($CFG->mnet_dispatcher_mode) ) { |
398 | set_config('mnet_dispatcher_mode', 'off'); |
399 | exit(mnet_server_fault(704, 'nosuchservice')); |
400 | } elseif ('off' == $CFG->mnet_dispatcher_mode) { |
401 | exit(mnet_server_fault(704, 'nosuchservice')); |
402 | |
403 | ////////////////////////////////////// SYSTEM METHODS |
404 | } elseif ($callstack[0] == 'system') { |
405 | $functionname = $callstack[1]; |
406 | $xmlrpcserver = xmlrpc_server_create(); |
407 | |
408 | // I'm adding the canonical xmlrpc references here, however we've |
409 | // already forbidden that the period (.) should be allowed in the call |
410 | // stack, so if someone tries to access our XMLRPC in the normal way, |
411 | // they'll already have received a RPC server fault message. |
412 | |
413 | // Maybe we should allow an easement so that regular XMLRPC clients can |
414 | // call our system methods, and find out what we have to offer? |
415 | |
416 | xmlrpc_server_register_method($xmlrpcserver, 'system.listMethods', 'mnet_system'); |
417 | xmlrpc_server_register_method($xmlrpcserver, 'system/listMethods', 'mnet_system'); |
418 | |
419 | xmlrpc_server_register_method($xmlrpcserver, 'system.methodSignature', 'mnet_system'); |
420 | xmlrpc_server_register_method($xmlrpcserver, 'system/methodSignature', 'mnet_system'); |
421 | |
422 | xmlrpc_server_register_method($xmlrpcserver, 'system.methodHelp', 'mnet_system'); |
423 | xmlrpc_server_register_method($xmlrpcserver, 'system/methodHelp', 'mnet_system'); |
424 | |
425 | xmlrpc_server_register_method($xmlrpcserver, 'system.listServices', 'mnet_system'); |
426 | xmlrpc_server_register_method($xmlrpcserver, 'system/listServices', 'mnet_system'); |
427 | |
428 | xmlrpc_server_register_method($xmlrpcserver, 'system.keyswap', 'mnet_keyswap'); |
429 | xmlrpc_server_register_method($xmlrpcserver, 'system/keyswap', 'mnet_keyswap'); |
430 | |
431 | if ($method == 'system.listMethods' || |
432 | $method == 'system/listMethods' || |
433 | $method == 'system.methodSignature' || |
434 | $method == 'system/methodSignature' || |
435 | $method == 'system.methodHelp' || |
436 | $method == 'system/methodHelp' || |
437 | $method == 'system.listServices' || |
438 | $method == 'system/listServices' || |
439 | $method == 'system.keyswap' || |
440 | $method == 'system/keyswap') { |
441 | |
1008dad6 |
442 | $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $MNET_REMOTE_CLIENT, array("encoding" => "utf-8")); |
71558f85 |
443 | $response = mnet_server_prepare_response($response); |
444 | } else { |
445 | exit(mnet_server_fault(7018, 'nosuchfunction')); |
446 | } |
447 | |
448 | xmlrpc_server_destroy($xmlrpcserver); |
449 | echo $response; |
450 | ////////////////////////////////////// STRICT AUTH |
451 | } elseif ($callstack[0] == 'auth') { |
452 | |
453 | // Break out the callstack into its elements |
454 | list($base, $plugin, $filename, $methodname) = $callstack; |
455 | |
456 | // We refuse to include anything that is not auth.php |
457 | if ($filename == 'auth.php' && is_enabled_auth($plugin)) { |
458 | $authclass = 'auth_plugin_'.$plugin; |
459 | $includefile = '/auth/'.$plugin.'/auth.php'; |
460 | $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $authclass); |
461 | $response = mnet_server_prepare_response($response); |
462 | echo $response; |
463 | } else { |
464 | // Generate error response - unable to locate function |
465 | exit(mnet_server_fault(702, 'nosuchfunction')); |
466 | } |
467 | |
468 | ////////////////////////////////////// STRICT ENROL |
469 | } elseif ($callstack[0] == 'enrol') { |
470 | |
471 | // Break out the callstack into its elements |
472 | list($base, $plugin, $filename, $methodname) = $callstack; |
473 | |
474 | if ($filename == 'enrol.php' && is_enabled_enrol($plugin)) { |
475 | $enrolclass = 'enrolment_plugin_'.$plugin; |
476 | $includefile = '/enrol/'.$plugin.'/enrol.php'; |
477 | $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $enrolclass); |
478 | $response = mnet_server_prepare_response($response); |
479 | echo $response; |
480 | } else { |
481 | // Generate error response - unable to locate function |
482 | exit(mnet_server_fault(703, 'nosuchfunction')); |
483 | } |
484 | |
b91b274b |
485 | } else if ($callstack[0] == 'portfolio') { |
486 | // Break out the callstack into its elements |
487 | list($base, $plugin, $filename, $methodname) = $callstack; |
488 | |
489 | if ($filename == 'lib.php') { |
490 | $pluginclass = 'portfolio_plugin_' . $plugin; |
491 | $includefile = '/portfolio/type/'.$plugin.'/lib.php'; |
492 | $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $pluginclass); |
493 | $response = mnet_server_prepare_response($response); |
494 | echo $response; |
495 | } else { |
496 | // Generate error response - unable to locate function |
497 | exit(mnet_server_fault(7012, 'nosuchfunction')); |
498 | } |
499 | |
71558f85 |
500 | ////////////////////////////////////// STRICT MOD/* |
68bcd355 |
501 | } elseif ($callstack[0] == 'mod' || 'dangerous' == $CFG->mnet_dispatcher_mode) { |
71558f85 |
502 | list($base, $module, $filename, $functionname) = $callstack; |
503 | |
504 | ////////////////////////////////////// STRICT MOD/* |
505 | if ($base == 'mod' && $filename == 'rpclib.php') { |
506 | $includefile = '/mod/'.$module.'/rpclib.php'; |
507 | $response = mnet_server_invoke_method($includefile, $functionname, $method, $payload); |
508 | $response = mnet_server_prepare_response($response); |
509 | echo $response; |
510 | |
68bcd355 |
511 | ////////////////////////////////////// DANGEROUS |
512 | } elseif ('dangerous' == $CFG->mnet_dispatcher_mode && $MNET_REMOTE_CLIENT->plaintext_is_ok()) { |
71558f85 |
513 | |
514 | $functionname = array_pop($callstack); |
71558f85 |
515 | |
516 | if ($MNET_REMOTE_CLIENT->plaintext_is_ok()) { |
517 | |
1bef950e |
518 | $filename = clean_param(implode('/',$callstack), PARAM_PATH); |
519 | if (0 == preg_match("/php$/", $filename)) { |
520 | // Filename doesn't end in 'php'; possible attack? |
521 | // Generate error response - unable to locate function |
522 | exit(mnet_server_fault(7012, 'nosuchfunction')); |
523 | } |
524 | |
71558f85 |
525 | // The call stack holds the path to any include file |
1bef950e |
526 | $includefile = $CFG->dirroot.'/'.$filename; |
71558f85 |
527 | |
b26a847b |
528 | $response = mnet_server_invoke_method($includefile, $functionname, $method, $payload); |
71558f85 |
529 | echo $response; |
530 | } |
531 | |
532 | } else { |
533 | // Generate error response - unable to locate function |
534 | exit(mnet_server_fault(7012, 'nosuchfunction')); |
535 | } |
536 | |
537 | } else { |
538 | // Generate error response - unable to locate function |
539 | exit(mnet_server_fault(7012, 'nosuchfunction')); |
540 | } |
541 | } |
542 | |
543 | /** |
544 | * Execute the system functions - mostly for introspection |
545 | * |
546 | * @param string $method XMLRPC method name, e.g. system.listMethods |
547 | * @param array $params Array of parameters from the XMLRPC request |
548 | * @param string $hostinfo Hostinfo object from the mnet_host table |
549 | * @return mixed Response data - any kind of PHP variable |
550 | */ |
551 | function mnet_system($method, $params, $hostinfo) { |
cc38ff5d |
552 | global $CFG, $DB; |
71558f85 |
553 | |
554 | if (empty($hostinfo)) return array(); |
555 | |
556 | $id_list = $hostinfo->id; |
557 | if (!empty($CFG->mnet_all_hosts_id)) { |
558 | $id_list .= ', '.$CFG->mnet_all_hosts_id; |
559 | } |
560 | |
561 | if ('system.listMethods' == $method || 'system/listMethods' == $method) { |
562 | if (count($params) == 0) { |
563 | $query = ' |
564 | SELECT DISTINCT |
565 | rpc.function_name, |
566 | rpc.xmlrpc_path, |
567 | rpc.enabled, |
568 | rpc.help, |
569 | rpc.profile |
570 | FROM |
cc38ff5d |
571 | {mnet_host2service} h2s, |
572 | {mnet_service2rpc} s2r, |
573 | {mnet_rpc} rpc |
71558f85 |
574 | WHERE |
575 | s2r.rpcid = rpc.id AND |
576 | h2s.serviceid = s2r.serviceid AND |
f633d85d |
577 | h2s.hostid in ('.$id_list .') AND |
578 | h2s.publish =\'1\' |
71558f85 |
579 | ORDER BY |
580 | rpc.xmlrpc_path ASC'; |
cc38ff5d |
581 | $params = array(); |
71558f85 |
582 | |
583 | } else { |
584 | $query = ' |
585 | SELECT DISTINCT |
586 | rpc.function_name, |
587 | rpc.xmlrpc_path, |
588 | rpc.enabled, |
589 | rpc.help, |
590 | rpc.profile |
591 | FROM |
cc38ff5d |
592 | {mnet_host2service} h2s, |
593 | {mnet_service2rpc} s2r, |
594 | {mnet_service} svc, |
595 | {mnet_rpc} rpc |
71558f85 |
596 | WHERE |
597 | s2r.rpcid = rpc.id AND |
598 | h2s.serviceid = s2r.serviceid AND |
599 | h2s.hostid in ('.$id_list .') AND |
f633d85d |
600 | h2s.publish =\'1\' AND |
71558f85 |
601 | svc.id = h2s.serviceid AND |
cc38ff5d |
602 | svc.name = ? |
71558f85 |
603 | ORDER BY |
604 | rpc.xmlrpc_path ASC'; |
cc38ff5d |
605 | $params = array($params[0]); |
71558f85 |
606 | |
607 | } |
cc38ff5d |
608 | $resultset = array_values($DB->get_records_sql($query, $params)); |
71558f85 |
609 | $methods = array(); |
610 | foreach($resultset as $result) { |
611 | $methods[] = $result->xmlrpc_path; |
612 | } |
613 | return $methods; |
614 | } elseif ('system.methodSignature' == $method || 'system/methodSignature' == $method) { |
615 | $query = ' |
616 | SELECT DISTINCT |
617 | rpc.function_name, |
618 | rpc.xmlrpc_path, |
619 | rpc.enabled, |
620 | rpc.help, |
621 | rpc.profile |
622 | FROM |
cc38ff5d |
623 | {mnet_host2service} h2s, |
624 | {mnet_service2rpc} s2r, |
625 | {mnet_rpc rpc} |
71558f85 |
626 | WHERE |
cc38ff5d |
627 | rpc.xmlrpc_path = ? AND |
71558f85 |
628 | s2r.rpcid = rpc.id AND |
629 | h2s.serviceid = s2r.serviceid AND |
f633d85d |
630 | h2s.publish =\'1\' AND |
71558f85 |
631 | h2s.hostid in ('.$id_list .')'; |
cc38ff5d |
632 | $params = array($params[0]); |
71558f85 |
633 | |
cc38ff5d |
634 | $result = $DB->get_records_sql($query, $params); |
71558f85 |
635 | $methodsigs = array(); |
636 | |
637 | if (is_array($result)) { |
638 | foreach($result as $method) { |
639 | $methodsigs[] = unserialize($method->profile); |
640 | } |
641 | } |
642 | |
643 | return $methodsigs; |
644 | } elseif ('system.methodHelp' == $method || 'system/methodHelp' == $method) { |
645 | $query = ' |
646 | SELECT DISTINCT |
647 | rpc.function_name, |
648 | rpc.xmlrpc_path, |
649 | rpc.enabled, |
650 | rpc.help, |
651 | rpc.profile |
652 | FROM |
cc38ff5d |
653 | {mnet_host2service} h2s, |
654 | {mnet_service2rpc} s2r, |
655 | {mnet_rpc} rpc |
71558f85 |
656 | WHERE |
cc38ff5d |
657 | rpc.xmlrpc_path = ? AND |
71558f85 |
658 | s2r.rpcid = rpc.id AND |
f633d85d |
659 | h2s.publish =\'1\' AND |
71558f85 |
660 | h2s.serviceid = s2r.serviceid AND |
661 | h2s.hostid in ('.$id_list .')'; |
cc38ff5d |
662 | $params = array($params[0]); |
71558f85 |
663 | |
cc38ff5d |
664 | $result = $DB->get_record_sql($query, $params); |
71558f85 |
665 | |
666 | if (is_object($result)) { |
667 | return $result->help; |
668 | } |
669 | } elseif ('system.listServices' == $method || 'system/listServices' == $method) { |
670 | $query = ' |
671 | SELECT DISTINCT |
672 | s.id, |
673 | s.name, |
674 | s.apiversion, |
675 | h2s.publish, |
676 | h2s.subscribe |
677 | FROM |
cc38ff5d |
678 | {mnet_host2service} h2s, |
679 | {mnet_service} s |
71558f85 |
680 | WHERE |
681 | h2s.serviceid = s.id AND |
f633d85d |
682 | (h2s.publish =\'1\' OR h2s.subscribe =\'1\') AND |
71558f85 |
683 | h2s.hostid in ('.$id_list .') |
684 | ORDER BY |
685 | s.name ASC'; |
cc38ff5d |
686 | $params = array(); |
71558f85 |
687 | |
cc38ff5d |
688 | $result = $DB->get_records_sql($query, $params); |
71558f85 |
689 | $services = array(); |
690 | |
691 | if (is_array($result)) { |
692 | foreach($result as $service) { |
693 | $services[] = array('name' => $service->name, |
694 | 'apiversion' => $service->apiversion, |
695 | 'publish' => $service->publish, |
696 | 'subscribe' => $service->subscribe); |
697 | } |
698 | } |
699 | |
700 | return $services; |
701 | } |
702 | exit(mnet_server_fault(7019, 'nosuchfunction')); |
703 | } |
704 | |
705 | /** |
706 | * Initialize the object (if necessary), execute the method or function, and |
707 | * return the response |
708 | * |
709 | * @param string $includefile The file that contains the object definition |
710 | * @param string $methodname The name of the method to execute |
711 | * @param string $method The full path to the method |
712 | * @param string $payload The XML-RPC request payload |
713 | * @param string $class The name of the class to instantiate (or false) |
714 | * @return string The XML-RPC response |
715 | */ |
716 | function mnet_server_invoke_method($includefile, $methodname, $method, $payload, $class=false) { |
717 | |
718 | $permission = mnet_permit_rpc_call($includefile, $methodname, $class); |
719 | |
720 | if (RPC_NOSUCHFILE == $permission) { |
721 | // Generate error response - unable to locate function |
722 | exit(mnet_server_fault(705, 'nosuchfile', $includefile)); |
723 | } |
724 | |
725 | if (RPC_NOSUCHFUNCTION == $permission) { |
726 | // Generate error response - unable to locate function |
727 | exit(mnet_server_fault(706, 'nosuchfunction')); |
728 | } |
729 | |
730 | if (RPC_FORBIDDENFUNCTION == $permission) { |
731 | // Generate error response - unable to locate function |
732 | exit(mnet_server_fault(707, 'forbidden-function')); |
733 | } |
734 | |
735 | if (RPC_NOSUCHCLASS == $permission) { |
736 | // Generate error response - unable to locate function |
737 | exit(mnet_server_fault(7013, 'nosuchfunction')); |
738 | } |
739 | |
740 | if (RPC_NOSUCHMETHOD == $permission) { |
741 | // Generate error response - unable to locate function |
742 | exit(mnet_server_fault(7014, 'nosuchmethod')); |
743 | } |
744 | |
745 | if (RPC_NOSUCHFUNCTION == $permission) { |
746 | // Generate error response - unable to locate function |
747 | exit(mnet_server_fault(7014, 'nosuchmethod')); |
748 | } |
749 | |
750 | if (RPC_FORBIDDENMETHOD == $permission) { |
751 | // Generate error response - unable to locate function |
752 | exit(mnet_server_fault(7015, 'nosuchfunction')); |
753 | } |
754 | |
755 | if (0 < $permission) { |
756 | // Generate error response - unable to locate function |
757 | exit(mnet_server_fault(7019, 'unknownerror')); |
758 | } |
759 | |
760 | if (RPC_OK == $permission) { |
761 | $xmlrpcserver = xmlrpc_server_create(); |
762 | $bool = xmlrpc_server_register_method($xmlrpcserver, $method, 'mnet_server_dummy_method'); |
1008dad6 |
763 | $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $methodname, array("encoding" => "utf-8")); |
71558f85 |
764 | $bool = xmlrpc_server_destroy($xmlrpcserver); |
765 | return $response; |
766 | } |
767 | } |
768 | |
85d2d959 |
769 | /** |
770 | * Accepts a public key from a new remote host and returns the public key for |
771 | * this host. If 'register all hosts' is turned on, it will bootstrap a record |
772 | * for the remote host in the mnet_host table (if it's not already there) |
773 | * |
774 | * @param string $function XML-RPC requires this but we don't... discard! |
775 | * @param array $params Array of parameters |
776 | * $params[0] is the remote wwwroot |
777 | * $params[1] is the remote public key |
778 | * @return string The XML-RPC response |
779 | */ |
71558f85 |
780 | function mnet_keyswap($function, $params) { |
735c7beb |
781 | global $CFG, $MNET; |
71558f85 |
782 | $return = array(); |
783 | |
784 | if (!empty($CFG->mnet_register_allhosts)) { |
785 | $mnet_peer = new mnet_peer(); |
25202581 |
786 | @list($wwwroot, $pubkey, $application) = each($params); |
787 | $keyok = $mnet_peer->bootstrap($wwwroot, $pubkey, $application); |
71558f85 |
788 | if ($keyok) { |
789 | $mnet_peer->commit(); |
790 | } |
791 | } |
735c7beb |
792 | return $MNET->public_key; |
71558f85 |
793 | } |
85d2d959 |
794 | |
71558f85 |
795 | ?> |