MDL-15094 some forgotten conversions
[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) {
25202581 212 $currkey = mnet_get_public_key($MNET_REMOTE_CLIENT->wwwroot, $MNET_REMOTE_CLIENT->application->xmlrpc_server_url);
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) {
522 global $CFG;
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
541 '.$CFG->prefix.'mnet_host2service h2s,
542 '.$CFG->prefix.'mnet_service2rpc s2r,
543 '.$CFG->prefix.'mnet_rpc rpc
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';
551
552 } else {
553 $query = '
554 SELECT DISTINCT
555 rpc.function_name,
556 rpc.xmlrpc_path,
557 rpc.enabled,
558 rpc.help,
559 rpc.profile
560 FROM
561 '.$CFG->prefix.'mnet_host2service h2s,
562 '.$CFG->prefix.'mnet_service2rpc s2r,
563 '.$CFG->prefix.'mnet_service svc,
564 '.$CFG->prefix.'mnet_rpc rpc
565 WHERE
566 s2r.rpcid = rpc.id AND
567 h2s.serviceid = s2r.serviceid AND
568 h2s.hostid in ('.$id_list .') AND
f633d85d 569 h2s.publish =\'1\' AND
71558f85 570 svc.id = h2s.serviceid AND
571 svc.name = \''.$params[0].'\'
572 ORDER BY
573 rpc.xmlrpc_path ASC';
574
575 }
576 $resultset = array_values(get_records_sql($query));
577 $methods = array();
578 foreach($resultset as $result) {
579 $methods[] = $result->xmlrpc_path;
580 }
581 return $methods;
582 } elseif ('system.methodSignature' == $method || 'system/methodSignature' == $method) {
583 $query = '
584 SELECT DISTINCT
585 rpc.function_name,
586 rpc.xmlrpc_path,
587 rpc.enabled,
588 rpc.help,
589 rpc.profile
590 FROM
591 '.$CFG->prefix.'mnet_host2service h2s,
592 '.$CFG->prefix.'mnet_service2rpc s2r,
593 '.$CFG->prefix.'mnet_rpc rpc
594 WHERE
595 rpc.xmlrpc_path = \''.$params[0].'\' AND
596 s2r.rpcid = rpc.id AND
597 h2s.serviceid = s2r.serviceid AND
f633d85d 598 h2s.publish =\'1\' AND
71558f85 599 h2s.hostid in ('.$id_list .')';
600
601 $result = get_records_sql($query);
602 $methodsigs = array();
603
604 if (is_array($result)) {
605 foreach($result as $method) {
606 $methodsigs[] = unserialize($method->profile);
607 }
608 }
609
610 return $methodsigs;
611 } elseif ('system.methodHelp' == $method || 'system/methodHelp' == $method) {
612 $query = '
613 SELECT DISTINCT
614 rpc.function_name,
615 rpc.xmlrpc_path,
616 rpc.enabled,
617 rpc.help,
618 rpc.profile
619 FROM
620 '.$CFG->prefix.'mnet_host2service h2s,
621 '.$CFG->prefix.'mnet_service2rpc s2r,
622 '.$CFG->prefix.'mnet_rpc rpc
623 WHERE
624 rpc.xmlrpc_path = \''.$params[0].'\' AND
625 s2r.rpcid = rpc.id AND
f633d85d 626 h2s.publish =\'1\' AND
71558f85 627 h2s.serviceid = s2r.serviceid AND
628 h2s.hostid in ('.$id_list .')';
629
630 $result = get_record_sql($query);
631
632 if (is_object($result)) {
633 return $result->help;
634 }
635 } elseif ('system.listServices' == $method || 'system/listServices' == $method) {
636 $query = '
637 SELECT DISTINCT
638 s.id,
639 s.name,
640 s.apiversion,
641 h2s.publish,
642 h2s.subscribe
643 FROM
644 '.$CFG->prefix.'mnet_host2service h2s,
645 '.$CFG->prefix.'mnet_service s
646 WHERE
647 h2s.serviceid = s.id AND
f633d85d 648 (h2s.publish =\'1\' OR h2s.subscribe =\'1\') AND
71558f85 649 h2s.hostid in ('.$id_list .')
650 ORDER BY
651 s.name ASC';
652
653 $result = get_records_sql($query);
654 $services = array();
655
656 if (is_array($result)) {
657 foreach($result as $service) {
658 $services[] = array('name' => $service->name,
659 'apiversion' => $service->apiversion,
660 'publish' => $service->publish,
661 'subscribe' => $service->subscribe);
662 }
663 }
664
665 return $services;
666 }
667 exit(mnet_server_fault(7019, 'nosuchfunction'));
668}
669
670/**
671 * Initialize the object (if necessary), execute the method or function, and
672 * return the response
673 *
674 * @param string $includefile The file that contains the object definition
675 * @param string $methodname The name of the method to execute
676 * @param string $method The full path to the method
677 * @param string $payload The XML-RPC request payload
678 * @param string $class The name of the class to instantiate (or false)
679 * @return string The XML-RPC response
680 */
681function mnet_server_invoke_method($includefile, $methodname, $method, $payload, $class=false) {
682
683 $permission = mnet_permit_rpc_call($includefile, $methodname, $class);
684
685 if (RPC_NOSUCHFILE == $permission) {
686 // Generate error response - unable to locate function
687 exit(mnet_server_fault(705, 'nosuchfile', $includefile));
688 }
689
690 if (RPC_NOSUCHFUNCTION == $permission) {
691 // Generate error response - unable to locate function
692 exit(mnet_server_fault(706, 'nosuchfunction'));
693 }
694
695 if (RPC_FORBIDDENFUNCTION == $permission) {
696 // Generate error response - unable to locate function
697 exit(mnet_server_fault(707, 'forbidden-function'));
698 }
699
700 if (RPC_NOSUCHCLASS == $permission) {
701 // Generate error response - unable to locate function
702 exit(mnet_server_fault(7013, 'nosuchfunction'));
703 }
704
705 if (RPC_NOSUCHMETHOD == $permission) {
706 // Generate error response - unable to locate function
707 exit(mnet_server_fault(7014, 'nosuchmethod'));
708 }
709
710 if (RPC_NOSUCHFUNCTION == $permission) {
711 // Generate error response - unable to locate function
712 exit(mnet_server_fault(7014, 'nosuchmethod'));
713 }
714
715 if (RPC_FORBIDDENMETHOD == $permission) {
716 // Generate error response - unable to locate function
717 exit(mnet_server_fault(7015, 'nosuchfunction'));
718 }
719
720 if (0 < $permission) {
721 // Generate error response - unable to locate function
722 exit(mnet_server_fault(7019, 'unknownerror'));
723 }
724
725 if (RPC_OK == $permission) {
726 $xmlrpcserver = xmlrpc_server_create();
727 $bool = xmlrpc_server_register_method($xmlrpcserver, $method, 'mnet_server_dummy_method');
1008dad6 728 $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $methodname, array("encoding" => "utf-8"));
71558f85 729 $bool = xmlrpc_server_destroy($xmlrpcserver);
730 return $response;
731 }
732}
733
85d2d959 734/**
735 * Accepts a public key from a new remote host and returns the public key for
736 * this host. If 'register all hosts' is turned on, it will bootstrap a record
737 * for the remote host in the mnet_host table (if it's not already there)
738 *
739 * @param string $function XML-RPC requires this but we don't... discard!
740 * @param array $params Array of parameters
741 * $params[0] is the remote wwwroot
742 * $params[1] is the remote public key
743 * @return string The XML-RPC response
744 */
71558f85 745function mnet_keyswap($function, $params) {
735c7beb 746 global $CFG, $MNET;
71558f85 747 $return = array();
748
749 if (!empty($CFG->mnet_register_allhosts)) {
750 $mnet_peer = new mnet_peer();
25202581 751 @list($wwwroot, $pubkey, $application) = each($params);
752 $keyok = $mnet_peer->bootstrap($wwwroot, $pubkey, $application);
71558f85 753 if ($keyok) {
754 $mnet_peer->commit();
755 }
756 }
735c7beb 757 return $MNET->public_key;
71558f85 758}
85d2d959 759
71558f85 760?>