Updated the HEAD build version to 20080830
[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
243 * @param string $param The $a param for the error message in the lang file
71558f85 244 * @return string $text The text of the error message
245 */
246function mnet_server_fault($code, $text, $param = null) {
151a9872 247 global $MNET_REMOTE_CLIENT;
71558f85 248 if (!is_numeric($code)) {
249 $code = 0;
250 }
251 $code = intval($code);
252
253 $text = get_string($text, 'mnet', $param);
151a9872 254 return mnet_server_fault_xml($code, $text);
255}
71558f85 256
151a9872 257/**
258 * Return the proper XML-RPC content to report an error.
259 *
09f0abb2 260 * @param int $code The ID code of the error message
261 * @param string $text The error message
262 * @param resource $privatekey The private key that should be used to sign the response
263 * @return string $text The XML text of the error message
151a9872 264 */
09f0abb2 265function mnet_server_fault_xml($code, $text, $privatekey = null) {
573f8b02 266 global $MNET_REMOTE_CLIENT, $CFG;
71558f85 267 // Replace illegal XML chars - is this already in a lib somewhere?
268 $text = str_replace(array('<','>','&','"',"'"), array('&lt;','&gt;','&amp;','&quot;','&apos;'), $text);
269
270 $return = mnet_server_prepare_response('<?xml version="1.0"?>
271<methodResponse>
272 <fault>
273 <value>
274 <struct>
275 <member>
276 <name>faultCode</name>
277 <value><int>'.$code.'</int></value>
278 </member>
279 <member>
280 <name>faultString</name>
281 <value><string>'.$text.'</string></value>
25202581 282 </member>
283 </struct>
284 </value>
285 </fault>
09f0abb2 286</methodResponse>', $privatekey);
573f8b02 287
288 if (!empty($CFG->mnet_rpcdebug)) {
3d706b49 289 trigger_error("XMLRPC Error Response $code: $text");
573f8b02 290 trigger_error(print_r($return,1));
291 }
292
71558f85 293 return $return;
294}
295
296/**
297 * Dummy function for the XML-RPC dispatcher - use to call a method on an object
298 * or to call a function
299 *
300 * Translate XML-RPC's strange function call syntax into a more straightforward
301 * PHP-friendly alternative. This dummy function will be called by the
302 * dispatcher, and can be used to call a method on an object, or just a function
303 *
304 * The methodName argument (eg. mnet/testlib/mnet_concatenate_strings)
305 * is ignored.
306 *
307 * @param string $methodname We discard this - see 'functionname'
308 * @param array $argsarray Each element is an argument to the real
309 * function
310 * @param string $functionname The name of the PHP function you want to call
311 * @return mixed The return value will be that of the real
312 * function, whateber it may be.
313 */
314function mnet_server_dummy_method($methodname, $argsarray, $functionname) {
315 global $MNET_REMOTE_CLIENT;
316
5c2e5bf2 317 if (!is_object($MNET_REMOTE_CLIENT->object_to_call)) {
71558f85 318 return @call_user_func_array($functionname, $argsarray);
319 } else {
320 return @call_user_method_array($functionname, $MNET_REMOTE_CLIENT->object_to_call, $argsarray);
321 }
322}
323
324/**
325 * Package a response in any required envelope, and return it to the client
326 *
09f0abb2 327 * @param string $response The XMLRPC response string
328 * @param resource $privatekey The private key to sign the response with
329 * @return string The encoded response string
71558f85 330 */
09f0abb2 331function mnet_server_prepare_response($response, $privatekey = null) {
71558f85 332 global $MNET_REMOTE_CLIENT;
333
334 if ($MNET_REMOTE_CLIENT->request_was_signed) {
09f0abb2 335 $response = mnet_sign_message($response, $privatekey);
71558f85 336 }
337
338 if ($MNET_REMOTE_CLIENT->request_was_encrypted) {
339 $response = mnet_encrypt_message($response, $MNET_REMOTE_CLIENT->public_key);
340 }
341
342 return $response;
343}
344
345/**
346 * If security checks are passed, dispatch the request to the function/method
347 *
348 * The config variable 'mnet_dispatcher_mode' can be:
349 * strict: Only execute functions that are in specific files
350 * off: The default - don't execute anything
351 *
352 * @param string $payload The XML-RPC request
353 * @return No return val - just echo the response
354 */
355function mnet_server_dispatch($payload) {
356 global $CFG, $MNET_REMOTE_CLIENT;
357 // xmlrpc_decode_request returns an array of parameters, and the $method
358 // variable (which is passed by reference) is instantiated with the value from
359 // the methodName tag in the xml payload
360 // xmlrpc_decode_request($xml, &$method)
361 $params = xmlrpc_decode_request($payload, $method);
362
b26a847b 363 // $method is something like: "mod/forum/lib.php/forum_add_instance"
71558f85 364 // $params is an array of parameters. A parameter might itself be an array.
365
366 // Whitelist characters that are permitted in a method name
367 // The method name must not begin with a / - avoid absolute paths
368 // A dot character . is only allowed in the filename, i.e. something.php
369 if (0 == preg_match("@^[A-Za-z0-9]+/[A-Za-z0-9/_-]+(\.php/)?[A-Za-z0-9_-]+$@",$method)) {
370 exit(mnet_server_fault(713, 'nosuchfunction'));
371 }
372
4fc66605 373 if(preg_match("/^system\./", $method)) {
b26a847b 374 $callstack = explode('.', $method);
375 } else {
376 $callstack = explode('/', $method);
377 // callstack will look like array('mod', 'forum', 'lib.php', 'forum_add_instance');
378 }
71558f85 379
380 /**
381 * What has the site administrator chosen as his dispatcher setting?
382 * strict: Only execute functions that are in specific files
383 * off: The default - don't execute anything
384 */
385 ////////////////////////////////////// OFF
386 if (!isset($CFG->mnet_dispatcher_mode) ) {
387 set_config('mnet_dispatcher_mode', 'off');
388 exit(mnet_server_fault(704, 'nosuchservice'));
389 } elseif ('off' == $CFG->mnet_dispatcher_mode) {
390 exit(mnet_server_fault(704, 'nosuchservice'));
391
392 ////////////////////////////////////// SYSTEM METHODS
393 } elseif ($callstack[0] == 'system') {
394 $functionname = $callstack[1];
395 $xmlrpcserver = xmlrpc_server_create();
396
397 // I'm adding the canonical xmlrpc references here, however we've
398 // already forbidden that the period (.) should be allowed in the call
399 // stack, so if someone tries to access our XMLRPC in the normal way,
400 // they'll already have received a RPC server fault message.
401
402 // Maybe we should allow an easement so that regular XMLRPC clients can
403 // call our system methods, and find out what we have to offer?
404
405 xmlrpc_server_register_method($xmlrpcserver, 'system.listMethods', 'mnet_system');
406 xmlrpc_server_register_method($xmlrpcserver, 'system/listMethods', 'mnet_system');
407
408 xmlrpc_server_register_method($xmlrpcserver, 'system.methodSignature', 'mnet_system');
409 xmlrpc_server_register_method($xmlrpcserver, 'system/methodSignature', 'mnet_system');
410
411 xmlrpc_server_register_method($xmlrpcserver, 'system.methodHelp', 'mnet_system');
412 xmlrpc_server_register_method($xmlrpcserver, 'system/methodHelp', 'mnet_system');
413
414 xmlrpc_server_register_method($xmlrpcserver, 'system.listServices', 'mnet_system');
415 xmlrpc_server_register_method($xmlrpcserver, 'system/listServices', 'mnet_system');
416
417 xmlrpc_server_register_method($xmlrpcserver, 'system.keyswap', 'mnet_keyswap');
418 xmlrpc_server_register_method($xmlrpcserver, 'system/keyswap', 'mnet_keyswap');
419
420 if ($method == 'system.listMethods' ||
421 $method == 'system/listMethods' ||
422 $method == 'system.methodSignature' ||
423 $method == 'system/methodSignature' ||
424 $method == 'system.methodHelp' ||
425 $method == 'system/methodHelp' ||
426 $method == 'system.listServices' ||
427 $method == 'system/listServices' ||
428 $method == 'system.keyswap' ||
429 $method == 'system/keyswap') {
430
1008dad6 431 $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $MNET_REMOTE_CLIENT, array("encoding" => "utf-8"));
71558f85 432 $response = mnet_server_prepare_response($response);
433 } else {
434 exit(mnet_server_fault(7018, 'nosuchfunction'));
435 }
436
437 xmlrpc_server_destroy($xmlrpcserver);
438 echo $response;
439 ////////////////////////////////////// STRICT AUTH
440 } elseif ($callstack[0] == 'auth') {
441
442 // Break out the callstack into its elements
443 list($base, $plugin, $filename, $methodname) = $callstack;
444
445 // We refuse to include anything that is not auth.php
446 if ($filename == 'auth.php' && is_enabled_auth($plugin)) {
447 $authclass = 'auth_plugin_'.$plugin;
448 $includefile = '/auth/'.$plugin.'/auth.php';
449 $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $authclass);
450 $response = mnet_server_prepare_response($response);
451 echo $response;
452 } else {
453 // Generate error response - unable to locate function
454 exit(mnet_server_fault(702, 'nosuchfunction'));
455 }
456
457 ////////////////////////////////////// STRICT ENROL
458 } elseif ($callstack[0] == 'enrol') {
459
460 // Break out the callstack into its elements
461 list($base, $plugin, $filename, $methodname) = $callstack;
462
463 if ($filename == 'enrol.php' && is_enabled_enrol($plugin)) {
464 $enrolclass = 'enrolment_plugin_'.$plugin;
465 $includefile = '/enrol/'.$plugin.'/enrol.php';
466 $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $enrolclass);
467 $response = mnet_server_prepare_response($response);
468 echo $response;
469 } else {
470 // Generate error response - unable to locate function
471 exit(mnet_server_fault(703, 'nosuchfunction'));
472 }
473
474 ////////////////////////////////////// STRICT MOD/*
68bcd355 475 } elseif ($callstack[0] == 'mod' || 'dangerous' == $CFG->mnet_dispatcher_mode) {
71558f85 476 list($base, $module, $filename, $functionname) = $callstack;
477
478 ////////////////////////////////////// STRICT MOD/*
479 if ($base == 'mod' && $filename == 'rpclib.php') {
480 $includefile = '/mod/'.$module.'/rpclib.php';
481 $response = mnet_server_invoke_method($includefile, $functionname, $method, $payload);
482 $response = mnet_server_prepare_response($response);
483 echo $response;
484
68bcd355 485 ////////////////////////////////////// DANGEROUS
486 } elseif ('dangerous' == $CFG->mnet_dispatcher_mode && $MNET_REMOTE_CLIENT->plaintext_is_ok()) {
71558f85 487
488 $functionname = array_pop($callstack);
71558f85 489
490 if ($MNET_REMOTE_CLIENT->plaintext_is_ok()) {
491
1bef950e 492 $filename = clean_param(implode('/',$callstack), PARAM_PATH);
493 if (0 == preg_match("/php$/", $filename)) {
494 // Filename doesn't end in 'php'; possible attack?
495 // Generate error response - unable to locate function
496 exit(mnet_server_fault(7012, 'nosuchfunction'));
497 }
498
71558f85 499 // The call stack holds the path to any include file
1bef950e 500 $includefile = $CFG->dirroot.'/'.$filename;
71558f85 501
b26a847b 502 $response = mnet_server_invoke_method($includefile, $functionname, $method, $payload);
71558f85 503 echo $response;
504 }
505
506 } else {
507 // Generate error response - unable to locate function
508 exit(mnet_server_fault(7012, 'nosuchfunction'));
509 }
510
511 } else {
512 // Generate error response - unable to locate function
513 exit(mnet_server_fault(7012, 'nosuchfunction'));
514 }
515}
516
517/**
518 * Execute the system functions - mostly for introspection
519 *
520 * @param string $method XMLRPC method name, e.g. system.listMethods
521 * @param array $params Array of parameters from the XMLRPC request
522 * @param string $hostinfo Hostinfo object from the mnet_host table
523 * @return mixed Response data - any kind of PHP variable
524 */
525function mnet_system($method, $params, $hostinfo) {
cc38ff5d 526 global $CFG, $DB;
71558f85 527
528 if (empty($hostinfo)) return array();
529
530 $id_list = $hostinfo->id;
531 if (!empty($CFG->mnet_all_hosts_id)) {
532 $id_list .= ', '.$CFG->mnet_all_hosts_id;
533 }
534
535 if ('system.listMethods' == $method || 'system/listMethods' == $method) {
536 if (count($params) == 0) {
537 $query = '
538 SELECT DISTINCT
539 rpc.function_name,
540 rpc.xmlrpc_path,
541 rpc.enabled,
542 rpc.help,
543 rpc.profile
544 FROM
cc38ff5d 545 {mnet_host2service} h2s,
546 {mnet_service2rpc} s2r,
547 {mnet_rpc} rpc
71558f85 548 WHERE
549 s2r.rpcid = rpc.id AND
550 h2s.serviceid = s2r.serviceid AND
f633d85d 551 h2s.hostid in ('.$id_list .') AND
552 h2s.publish =\'1\'
71558f85 553 ORDER BY
554 rpc.xmlrpc_path ASC';
cc38ff5d 555 $params = array();
71558f85 556
557 } else {
558 $query = '
559 SELECT DISTINCT
560 rpc.function_name,
561 rpc.xmlrpc_path,
562 rpc.enabled,
563 rpc.help,
564 rpc.profile
565 FROM
cc38ff5d 566 {mnet_host2service} h2s,
567 {mnet_service2rpc} s2r,
568 {mnet_service} svc,
569 {mnet_rpc} rpc
71558f85 570 WHERE
571 s2r.rpcid = rpc.id AND
572 h2s.serviceid = s2r.serviceid AND
573 h2s.hostid in ('.$id_list .') AND
f633d85d 574 h2s.publish =\'1\' AND
71558f85 575 svc.id = h2s.serviceid AND
cc38ff5d 576 svc.name = ?
71558f85 577 ORDER BY
578 rpc.xmlrpc_path ASC';
cc38ff5d 579 $params = array($params[0]);
71558f85 580
581 }
cc38ff5d 582 $resultset = array_values($DB->get_records_sql($query, $params));
71558f85 583 $methods = array();
584 foreach($resultset as $result) {
585 $methods[] = $result->xmlrpc_path;
586 }
587 return $methods;
588 } elseif ('system.methodSignature' == $method || 'system/methodSignature' == $method) {
589 $query = '
590 SELECT DISTINCT
591 rpc.function_name,
592 rpc.xmlrpc_path,
593 rpc.enabled,
594 rpc.help,
595 rpc.profile
596 FROM
cc38ff5d 597 {mnet_host2service} h2s,
598 {mnet_service2rpc} s2r,
599 {mnet_rpc rpc}
71558f85 600 WHERE
cc38ff5d 601 rpc.xmlrpc_path = ? AND
71558f85 602 s2r.rpcid = rpc.id AND
603 h2s.serviceid = s2r.serviceid AND
f633d85d 604 h2s.publish =\'1\' AND
71558f85 605 h2s.hostid in ('.$id_list .')';
cc38ff5d 606 $params = array($params[0]);
71558f85 607
cc38ff5d 608 $result = $DB->get_records_sql($query, $params);
71558f85 609 $methodsigs = array();
610
611 if (is_array($result)) {
612 foreach($result as $method) {
613 $methodsigs[] = unserialize($method->profile);
614 }
615 }
616
617 return $methodsigs;
618 } elseif ('system.methodHelp' == $method || 'system/methodHelp' == $method) {
619 $query = '
620 SELECT DISTINCT
621 rpc.function_name,
622 rpc.xmlrpc_path,
623 rpc.enabled,
624 rpc.help,
625 rpc.profile
626 FROM
cc38ff5d 627 {mnet_host2service} h2s,
628 {mnet_service2rpc} s2r,
629 {mnet_rpc} rpc
71558f85 630 WHERE
cc38ff5d 631 rpc.xmlrpc_path = ? AND
71558f85 632 s2r.rpcid = rpc.id AND
f633d85d 633 h2s.publish =\'1\' AND
71558f85 634 h2s.serviceid = s2r.serviceid AND
635 h2s.hostid in ('.$id_list .')';
cc38ff5d 636 $params = array($params[0]);
71558f85 637
cc38ff5d 638 $result = $DB->get_record_sql($query, $params);
71558f85 639
640 if (is_object($result)) {
641 return $result->help;
642 }
643 } elseif ('system.listServices' == $method || 'system/listServices' == $method) {
644 $query = '
645 SELECT DISTINCT
646 s.id,
647 s.name,
648 s.apiversion,
649 h2s.publish,
650 h2s.subscribe
651 FROM
cc38ff5d 652 {mnet_host2service} h2s,
653 {mnet_service} s
71558f85 654 WHERE
655 h2s.serviceid = s.id AND
f633d85d 656 (h2s.publish =\'1\' OR h2s.subscribe =\'1\') AND
71558f85 657 h2s.hostid in ('.$id_list .')
658 ORDER BY
659 s.name ASC';
cc38ff5d 660 $params = array();
71558f85 661
cc38ff5d 662 $result = $DB->get_records_sql($query, $params);
71558f85 663 $services = array();
664
665 if (is_array($result)) {
666 foreach($result as $service) {
667 $services[] = array('name' => $service->name,
668 'apiversion' => $service->apiversion,
669 'publish' => $service->publish,
670 'subscribe' => $service->subscribe);
671 }
672 }
673
674 return $services;
675 }
676 exit(mnet_server_fault(7019, 'nosuchfunction'));
677}
678
679/**
680 * Initialize the object (if necessary), execute the method or function, and
681 * return the response
682 *
683 * @param string $includefile The file that contains the object definition
684 * @param string $methodname The name of the method to execute
685 * @param string $method The full path to the method
686 * @param string $payload The XML-RPC request payload
687 * @param string $class The name of the class to instantiate (or false)
688 * @return string The XML-RPC response
689 */
690function mnet_server_invoke_method($includefile, $methodname, $method, $payload, $class=false) {
691
692 $permission = mnet_permit_rpc_call($includefile, $methodname, $class);
693
694 if (RPC_NOSUCHFILE == $permission) {
695 // Generate error response - unable to locate function
696 exit(mnet_server_fault(705, 'nosuchfile', $includefile));
697 }
698
699 if (RPC_NOSUCHFUNCTION == $permission) {
700 // Generate error response - unable to locate function
701 exit(mnet_server_fault(706, 'nosuchfunction'));
702 }
703
704 if (RPC_FORBIDDENFUNCTION == $permission) {
705 // Generate error response - unable to locate function
706 exit(mnet_server_fault(707, 'forbidden-function'));
707 }
708
709 if (RPC_NOSUCHCLASS == $permission) {
710 // Generate error response - unable to locate function
711 exit(mnet_server_fault(7013, 'nosuchfunction'));
712 }
713
714 if (RPC_NOSUCHMETHOD == $permission) {
715 // Generate error response - unable to locate function
716 exit(mnet_server_fault(7014, 'nosuchmethod'));
717 }
718
719 if (RPC_NOSUCHFUNCTION == $permission) {
720 // Generate error response - unable to locate function
721 exit(mnet_server_fault(7014, 'nosuchmethod'));
722 }
723
724 if (RPC_FORBIDDENMETHOD == $permission) {
725 // Generate error response - unable to locate function
726 exit(mnet_server_fault(7015, 'nosuchfunction'));
727 }
728
729 if (0 < $permission) {
730 // Generate error response - unable to locate function
731 exit(mnet_server_fault(7019, 'unknownerror'));
732 }
733
734 if (RPC_OK == $permission) {
735 $xmlrpcserver = xmlrpc_server_create();
736 $bool = xmlrpc_server_register_method($xmlrpcserver, $method, 'mnet_server_dummy_method');
1008dad6 737 $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $methodname, array("encoding" => "utf-8"));
71558f85 738 $bool = xmlrpc_server_destroy($xmlrpcserver);
739 return $response;
740 }
741}
742
85d2d959 743/**
744 * Accepts a public key from a new remote host and returns the public key for
745 * this host. If 'register all hosts' is turned on, it will bootstrap a record
746 * for the remote host in the mnet_host table (if it's not already there)
747 *
748 * @param string $function XML-RPC requires this but we don't... discard!
749 * @param array $params Array of parameters
750 * $params[0] is the remote wwwroot
751 * $params[1] is the remote public key
752 * @return string The XML-RPC response
753 */
71558f85 754function mnet_keyswap($function, $params) {
735c7beb 755 global $CFG, $MNET;
71558f85 756 $return = array();
757
758 if (!empty($CFG->mnet_register_allhosts)) {
759 $mnet_peer = new mnet_peer();
25202581 760 @list($wwwroot, $pubkey, $application) = each($params);
761 $keyok = $mnet_peer->bootstrap($wwwroot, $pubkey, $application);
71558f85 762 if ($keyok) {
763 $mnet_peer->commit();
764 }
765 }
735c7beb 766 return $MNET->public_key;
71558f85 767}
85d2d959 768
71558f85 769?>