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