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