mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
mnet: core libraries and admin pages
This commit is contained in:
parent
10daca92c5
commit
71558f8502
26 changed files with 4213 additions and 0 deletions
225
mnet/xmlrpc/client.php
Normal file
225
mnet/xmlrpc/client.php
Normal file
|
@ -0,0 +1,225 @@
|
|||
<?php
|
||||
/**
|
||||
* An XML-RPC client
|
||||
*
|
||||
* @author Donal McMullan donal@catalyst.net.nz
|
||||
* @version 0.0.1
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package mnet
|
||||
*/
|
||||
|
||||
require_once $CFG->dirroot.'/mnet/lib.php';
|
||||
|
||||
/**
|
||||
* Class representing an XMLRPC request against a remote machine
|
||||
*/
|
||||
class mnet_xmlrpc_client {
|
||||
|
||||
var $method = '';
|
||||
var $params = array();
|
||||
var $timeout = 60;
|
||||
var $error = array();
|
||||
var $response = '';
|
||||
|
||||
/**
|
||||
* Constructor returns true
|
||||
*/
|
||||
function mnet_xmlrpc_client() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow users to override the default timeout
|
||||
* @param int $timeout Request timeout in seconds
|
||||
* $return bool True if param is an integer or integer string
|
||||
*/
|
||||
function set_timeout($timeout) {
|
||||
if (!is_integer($timeout)) {
|
||||
if (is_numeric($timeout)) {
|
||||
$this->timeout = (integer($timeout));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
$this->timeout = $timeout;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the path to the method or function we want to execute on the remote
|
||||
* machine. Examples:
|
||||
* mod/scorm/functionname
|
||||
* auth/mnet/methodname
|
||||
* In the case of auth and enrolment plugins, an object will be created and
|
||||
* the method on that object will be called
|
||||
*/
|
||||
function set_method($xmlrpcpath) {
|
||||
if (is_string($xmlrpcpath)) {
|
||||
$this->method = $xmlrpcpath;
|
||||
$this->params = array();
|
||||
return true;
|
||||
}
|
||||
$this->method = '';
|
||||
$this->params = array();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a parameter to the array of parameters.
|
||||
*
|
||||
* @param string $argument A transport ID, as defined in lib.php
|
||||
* @param string $type The argument type, can be one of:
|
||||
* none
|
||||
* empty
|
||||
* base64
|
||||
* boolean
|
||||
* datetime
|
||||
* double
|
||||
* int
|
||||
* string
|
||||
* array
|
||||
* struct
|
||||
* In its weakly-typed wisdom, PHP will (currently)
|
||||
* ignore everything except datetime and base64
|
||||
* @return bool True on success
|
||||
*/
|
||||
function add_param($argument, $type = 'string') {
|
||||
|
||||
$allowed_types = array('none',
|
||||
'empty',
|
||||
'base64',
|
||||
'boolean',
|
||||
'datetime',
|
||||
'double',
|
||||
'int',
|
||||
'i4',
|
||||
'string',
|
||||
'array',
|
||||
'struct');
|
||||
if (!in_array($type, $allowed_types)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($type != 'datetime' && $type != 'base64') {
|
||||
$this->params[] = $argument;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Note weirdness - The type of $argument gets changed to an object with
|
||||
// value and type properties.
|
||||
// bool xmlrpc_set_type ( string &value, string type )
|
||||
xmlrpc_set_type($argument, $type);
|
||||
$this->params[] = $argument;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the request to the server - decode and return the response
|
||||
*
|
||||
* @param object $mnet_peer A mnet_peer object with details of the
|
||||
* remote host we're connecting to
|
||||
* @return mixed A PHP variable, as returned by the
|
||||
* remote function
|
||||
*/
|
||||
function send($mnet_peer) {
|
||||
global $CFG, $MNET;
|
||||
|
||||
$this->uri = $mnet_peer->wwwroot.
|
||||
'/mnet/xmlrpc/server.php';
|
||||
|
||||
// Initialize with the target URL
|
||||
$ch = curl_init($this->uri);
|
||||
|
||||
$system_methods = array('system/listMethods', 'system/methodSignature', 'system/methodHelp', 'system/listServices');
|
||||
|
||||
if (in_array($this->method, $system_methods) ) {
|
||||
|
||||
// Executing any system method is permitted.
|
||||
|
||||
} else {
|
||||
|
||||
// Find methods that we subscribe to on this host
|
||||
$sql = "
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
{$CFG->prefix}mnet_rpc r,
|
||||
{$CFG->prefix}mnet_service2rpc s2r,
|
||||
{$CFG->prefix}mnet_host2service h2s
|
||||
WHERE
|
||||
r.xmlrpc_path = '{$this->method}' AND
|
||||
s2r.rpcid = r.id AND
|
||||
s2r.serviceid = h2s.serviceid AND
|
||||
h2s.subscribe = '1'";
|
||||
|
||||
$permission = get_record_sql($sql);
|
||||
if ($permission == false) {
|
||||
// TODO: Handle attempt to call not-permitted method
|
||||
echo '<pre>'.$sql.'</pre>';
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
$this->requesttext = xmlrpc_encode_request($this->method, $this->params);
|
||||
$rq = $this->requesttext;
|
||||
$rq = mnet_sign_message($this->requesttext);
|
||||
$this->signedrequest = $rq;
|
||||
$rq = mnet_encrypt_message($rq, $mnet_peer->public_key);
|
||||
$this->encryptedrequest = $rq;
|
||||
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'Moodle');
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $rq);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml charset=UTF-8"));
|
||||
|
||||
$this->rawresponse = curl_exec($ch);
|
||||
if ($this->rawresponse == false) {
|
||||
$this->error[] = array(curl_errno($ch), curl_error($ch));
|
||||
}
|
||||
|
||||
$crypt_parser = new mnet_encxml_parser();
|
||||
$crypt_parser->parse($this->rawresponse);
|
||||
|
||||
if ($crypt_parser->payload_encrypted) {
|
||||
|
||||
$key = array_pop($crypt_parser->cipher);
|
||||
$data = array_pop($crypt_parser->cipher);
|
||||
|
||||
$crypt_parser->free_resource();
|
||||
|
||||
// Initialize payload var
|
||||
$payload = '';
|
||||
|
||||
// &$payload
|
||||
$isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $MNET->get_private_key());
|
||||
|
||||
if (!$isOpen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strpos(substr($payload, 0, 100), '<signedMessage>')) {
|
||||
$sig_parser = new mnet_encxml_parser();
|
||||
$sig_parser->parse($payload);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
$crypt_parser->free_resource();
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->xmlrpcresponse = base64_decode($sig_parser->data_object);
|
||||
$this->response = xmlrpc_decode($this->xmlrpcresponse);
|
||||
curl_close($ch);
|
||||
|
||||
// xmlrpc errors are pushed onto the $this->error stack
|
||||
if (isset($this->response['faultCode'])) {
|
||||
$this->error[] = $this->response['faultCode'] . " : " . $this->response['faultString'];
|
||||
}
|
||||
return empty($this->error);
|
||||
}
|
||||
}
|
||||
?>
|
669
mnet/xmlrpc/server.php
Normal file
669
mnet/xmlrpc/server.php
Normal file
|
@ -0,0 +1,669 @@
|
|||
<?php
|
||||
/**
|
||||
* An XML-RPC server
|
||||
*
|
||||
* @author Donal McMullan donal@catalyst.net.nz
|
||||
* @version 0.0.1
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package mnet
|
||||
*/
|
||||
|
||||
// Make certain that config.php doesn't display any errors, and that it doesn't
|
||||
// override our do-not-display-errors setting:
|
||||
ini_set('display_errors',0);
|
||||
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
|
||||
ini_set('display_errors',0);
|
||||
|
||||
// Include MNET stuff:
|
||||
require_once $CFG->dirroot.'/mnet/lib.php';
|
||||
require_once $CFG->dirroot.'/mnet/remote_client.php';
|
||||
|
||||
// Content type for output is not html:
|
||||
header('Content-type: text/xml');
|
||||
|
||||
if (!empty($CFG->mnet_rpcdebug)) {
|
||||
trigger_error("HTTP_RAW_POST_DATA");
|
||||
trigger_error($HTTP_RAW_POST_DATA);
|
||||
}
|
||||
|
||||
// New global variable which ONLY gets set in this server page, so you know that
|
||||
// if you've been called by a remote Moodle, this should be set:
|
||||
$MNET_REMOTE_CLIENT = new mnet_remote_client();
|
||||
|
||||
// Peek at the message to see if it's an XML-ENC document. If it is, note that
|
||||
// the client connection was encrypted, and strip the xml-encryption and
|
||||
// xml-signature wrappers from the XML-RPC payload
|
||||
if (strpos(substr($HTTP_RAW_POST_DATA, 0, 100), '<encryptedMessage>')) {
|
||||
$MNET_REMOTE_CLIENT->was_encrypted();
|
||||
// Extract the XML-RPC payload from the XML-ENC and XML-SIG wrappers.
|
||||
$payload = mnet_server_strip_wrappers($HTTP_RAW_POST_DATA);
|
||||
} else {
|
||||
$params = xmlrpc_decode_request($HTTP_RAW_POST_DATA, $method);
|
||||
if ($method == 'system.keyswap' ||
|
||||
$method == 'system/keyswap') {
|
||||
|
||||
// OK
|
||||
|
||||
} elseif ($MNET_REMOTE_CLIENT->plaintext_is_ok() == false) {
|
||||
exit(mnet_server_fault(7021, 'forbidden-transport'));
|
||||
}
|
||||
// Looks like plaintext is ok. It is assumed that a plaintext call:
|
||||
// 1. Came from a trusted host on your local network
|
||||
// 2. Is *not* from a Moodle - otherwise why skip encryption/signing?
|
||||
// 3. Is free to execute ANY function in Moodle
|
||||
// 4. Cannot execute any methods (as it can't instantiate a class first)
|
||||
// To execute a method, you'll need to create a wrapper function that first
|
||||
// instantiates the class, and then calls the method.
|
||||
$payload = $HTTP_RAW_POST_DATA;
|
||||
}
|
||||
|
||||
if (!empty($CFG->mnet_rpcdebug)) {
|
||||
trigger_error("XMLRPC Payload");
|
||||
trigger_error(print_r($payload,1));
|
||||
}
|
||||
|
||||
// Parse and action the XML-RPC payload
|
||||
$response = mnet_server_dispatch($payload);
|
||||
|
||||
/**
|
||||
* Strip the encryption (XML-ENC) and signature (XML-SIG) wrappers and return the XML-RPC payload
|
||||
*
|
||||
* IF COMMUNICATION TAKES PLACE OVER UNENCRYPTED HTTP:
|
||||
* The payload will have been encrypted with a symmetric key. This key will
|
||||
* itself have been encrypted using your public key. The key is decrypted using
|
||||
* your private key, and then used to decrypt the XML payload.
|
||||
*
|
||||
* IF COMMUNICATION TAKES PLACE OVER UNENCRYPTED HTTP *OR* ENCRYPTED HTTPS:
|
||||
* In either case, there will be an XML wrapper which contains your XML-RPC doc
|
||||
* as an object element, a signature for that doc, and various standards-
|
||||
* compliant info to aid in verifying the signature.
|
||||
*
|
||||
* This function parses the encryption wrapper, decrypts the contents, parses
|
||||
* the signature wrapper, and if the signature matches the payload, it returns
|
||||
* the payload, which should be an XML-RPC request.
|
||||
* If there is an error, or the signatures don't match, it echoes an XML-RPC
|
||||
* error and exits.
|
||||
*
|
||||
* See the W3C's {@link http://www.w3.org/TR/xmlenc-core/ XML Encryption Syntax and Processing}
|
||||
* and {@link http://www.w3.org/TR/2001/PR-xmldsig-core-20010820/ XML-Signature Syntax and Processing}
|
||||
* guidelines for more detail on the XML.
|
||||
*
|
||||
* -----XML-Envelope---------------------------------
|
||||
* | |
|
||||
* | Encrypted-Symmetric-key---------------- |
|
||||
* | |_____________________________________| |
|
||||
* | |
|
||||
* | Encrypted data------------------------- |
|
||||
* | | | |
|
||||
* | | -XML-Envelope------------------ | |
|
||||
* | | | | | |
|
||||
* | | | --Signature------------- | | |
|
||||
* | | | |______________________| | | |
|
||||
* | | | | | |
|
||||
* | | | --Signed-Payload-------- | | |
|
||||
* | | | | | | | |
|
||||
* | | | | XML-RPC Request | | | |
|
||||
* | | | |______________________| | | |
|
||||
* | | | | | |
|
||||
* | | |_____________________________| | |
|
||||
* | |_____________________________________| |
|
||||
* | |
|
||||
* |________________________________________________|
|
||||
*
|
||||
* @uses $db
|
||||
* @param string $HTTP_RAW_POST_DATA The XML that the client sent
|
||||
* @return string The XMLRPC payload.
|
||||
*/
|
||||
function mnet_server_strip_wrappers($HTTP_RAW_POST_DATA) {
|
||||
global $MNET, $MNET_REMOTE_CLIENT;
|
||||
if (isset($_SERVER)) {
|
||||
|
||||
$crypt_parser = new mnet_encxml_parser();
|
||||
$crypt_parser->parse($HTTP_RAW_POST_DATA);
|
||||
|
||||
if ($crypt_parser->payload_encrypted) {
|
||||
|
||||
$key = array_pop($crypt_parser->cipher);
|
||||
$data = array_pop($crypt_parser->cipher);
|
||||
|
||||
$crypt_parser->free_resource();
|
||||
|
||||
// Initialize payload var
|
||||
$payload = '';
|
||||
|
||||
// &$payload
|
||||
$isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $MNET->get_private_key());
|
||||
|
||||
if (!$isOpen) {
|
||||
exit(mnet_server_fault(7023, 'encryption-invalid'));
|
||||
}
|
||||
|
||||
if (strpos(substr($payload, 0, 100), '<signedMessage>')) {
|
||||
$MNET_REMOTE_CLIENT->was_signed();
|
||||
$sig_parser = new mnet_encxml_parser();
|
||||
$sig_parser->parse($payload);
|
||||
} else {
|
||||
exit(mnet_server_fault(7022, 'verifysignature-error'));
|
||||
}
|
||||
|
||||
} else {
|
||||
exit(mnet_server_fault(7024, 'payload-not-encrypted'));
|
||||
}
|
||||
|
||||
unset($payload);
|
||||
|
||||
$host_record_exists = $MNET_REMOTE_CLIENT->set_wwwroot($sig_parser->remote_wwwroot);
|
||||
|
||||
if (false == $host_record_exists) {
|
||||
exit(mnet_server_fault(7020, 'wrong-wwwroot', $sig_parser->remote_wwwroot));
|
||||
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != $MNET_REMOTE_CLIENT->ip_address) {
|
||||
exit(mnet_server_fault(7017, 'wrong-ip'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the certificate (i.e. public key) from the remote server.
|
||||
*/
|
||||
$certificate = $MNET_REMOTE_CLIENT->public_key;
|
||||
|
||||
if ($certificate == false) {
|
||||
exit(mnet_server_fault(709, 'nosuchpublickey'));
|
||||
}
|
||||
|
||||
$payload = base64_decode($sig_parser->data_object);
|
||||
|
||||
// Does the signature match the data and the public cert?
|
||||
$signature_verified = openssl_verify($payload, base64_decode($sig_parser->signature), $certificate);
|
||||
if ($signature_verified == 1) {
|
||||
// Parse the XML
|
||||
} elseif ($signature_verified == 0) {
|
||||
exit(mnet_server_fault(710, 'verifysignature-invalid'));
|
||||
} else {
|
||||
exit(mnet_server_fault(711, 'verifysignature-error'));
|
||||
}
|
||||
|
||||
$sig_parser->free_resource();
|
||||
|
||||
return $payload;
|
||||
} else {
|
||||
exit(mnet_server_fault(712, "phperror"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the proper XML-RPC content to report an error.
|
||||
*
|
||||
* @param int $code The ID code of the error message
|
||||
* @return string $text The text of the error message
|
||||
*/
|
||||
function mnet_server_fault($code, $text, $param = null) {
|
||||
if (!is_numeric($code)) {
|
||||
$code = 0;
|
||||
}
|
||||
$code = intval($code);
|
||||
|
||||
$text = get_string($text, 'mnet', $param);
|
||||
|
||||
// Replace illegal XML chars - is this already in a lib somewhere?
|
||||
$text = str_replace(array('<','>','&','"',"'"), array('<','>','&','"','''), $text);
|
||||
|
||||
$return = mnet_server_prepare_response('<?xml version="1.0"?>
|
||||
<methodResponse>
|
||||
<fault>
|
||||
<value>
|
||||
<struct>
|
||||
<member>
|
||||
<name>faultCode</name>
|
||||
<value><int>'.$code.'</int></value>
|
||||
</member>
|
||||
<member>
|
||||
<name>faultString</name>
|
||||
<value><string>'.$text.'</string></value>
|
||||
</member>
|
||||
</struct>
|
||||
</value>
|
||||
</fault>
|
||||
</methodResponse>');
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dummy function for the XML-RPC dispatcher - use to call a method on an object
|
||||
* or to call a function
|
||||
*
|
||||
* Translate XML-RPC's strange function call syntax into a more straightforward
|
||||
* PHP-friendly alternative. This dummy function will be called by the
|
||||
* dispatcher, and can be used to call a method on an object, or just a function
|
||||
*
|
||||
* The methodName argument (eg. mnet/testlib/mnet_concatenate_strings)
|
||||
* is ignored.
|
||||
*
|
||||
* @param string $methodname We discard this - see 'functionname'
|
||||
* @param array $argsarray Each element is an argument to the real
|
||||
* function
|
||||
* @param string $functionname The name of the PHP function you want to call
|
||||
* @return mixed The return value will be that of the real
|
||||
* function, whateber it may be.
|
||||
*/
|
||||
function mnet_server_dummy_method($methodname, $argsarray, $functionname) {
|
||||
global $MNET_REMOTE_CLIENT;
|
||||
|
||||
if ($MNET_REMOTE_CLIENT->object_to_call == false) {
|
||||
return @call_user_func_array($functionname, $argsarray);
|
||||
} else {
|
||||
return @call_user_method_array($functionname, $MNET_REMOTE_CLIENT->object_to_call, $argsarray);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Package a response in any required envelope, and return it to the client
|
||||
*
|
||||
* @param string $response The XMLRPC response string
|
||||
* @return string The encoded response string
|
||||
*/
|
||||
function mnet_server_prepare_response($response) {
|
||||
global $MNET_REMOTE_CLIENT;
|
||||
|
||||
if ($MNET_REMOTE_CLIENT->request_was_signed) {
|
||||
$response = mnet_sign_message($response);
|
||||
}
|
||||
|
||||
if ($MNET_REMOTE_CLIENT->request_was_encrypted) {
|
||||
$response = mnet_encrypt_message($response, $MNET_REMOTE_CLIENT->public_key);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* If security checks are passed, dispatch the request to the function/method
|
||||
*
|
||||
* The config variable 'mnet_dispatcher_mode' can be:
|
||||
* strict: Only execute functions that are in specific files
|
||||
* off: The default - don't execute anything
|
||||
*
|
||||
* @param string $payload The XML-RPC request
|
||||
* @return No return val - just echo the response
|
||||
*/
|
||||
function mnet_server_dispatch($payload) {
|
||||
global $CFG, $MNET_REMOTE_CLIENT;
|
||||
// xmlrpc_decode_request returns an array of parameters, and the $method
|
||||
// variable (which is passed by reference) is instantiated with the value from
|
||||
// the methodName tag in the xml payload
|
||||
// xmlrpc_decode_request($xml, &$method)
|
||||
$params = xmlrpc_decode_request($payload, $method);
|
||||
|
||||
// $method is something like: "mod/forum/lib/forum_add_instance"
|
||||
// $params is an array of parameters. A parameter might itself be an array.
|
||||
|
||||
// Whitelist characters that are permitted in a method name
|
||||
// The method name must not begin with a / - avoid absolute paths
|
||||
// A dot character . is only allowed in the filename, i.e. something.php
|
||||
if (0 == preg_match("@^[A-Za-z0-9]+/[A-Za-z0-9/_-]+(\.php/)?[A-Za-z0-9_-]+$@",$method)) {
|
||||
exit(mnet_server_fault(713, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
$callstack = explode('/', $method);
|
||||
// callstack will look like array('mod', 'forum', 'lib', 'forum_add_instance');
|
||||
|
||||
/**
|
||||
* What has the site administrator chosen as his dispatcher setting?
|
||||
* strict: Only execute functions that are in specific files
|
||||
* off: The default - don't execute anything
|
||||
*/
|
||||
////////////////////////////////////// OFF
|
||||
if (!isset($CFG->mnet_dispatcher_mode) ) {
|
||||
set_config('mnet_dispatcher_mode', 'off');
|
||||
exit(mnet_server_fault(704, 'nosuchservice'));
|
||||
} elseif ('off' == $CFG->mnet_dispatcher_mode) {
|
||||
exit(mnet_server_fault(704, 'nosuchservice'));
|
||||
|
||||
////////////////////////////////////// SYSTEM METHODS
|
||||
} elseif ($callstack[0] == 'system') {
|
||||
$functionname = $callstack[1];
|
||||
$xmlrpcserver = xmlrpc_server_create();
|
||||
|
||||
// I'm adding the canonical xmlrpc references here, however we've
|
||||
// already forbidden that the period (.) should be allowed in the call
|
||||
// stack, so if someone tries to access our XMLRPC in the normal way,
|
||||
// they'll already have received a RPC server fault message.
|
||||
|
||||
// Maybe we should allow an easement so that regular XMLRPC clients can
|
||||
// call our system methods, and find out what we have to offer?
|
||||
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system.listMethods', 'mnet_system');
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system/listMethods', 'mnet_system');
|
||||
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system.methodSignature', 'mnet_system');
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system/methodSignature', 'mnet_system');
|
||||
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system.methodHelp', 'mnet_system');
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system/methodHelp', 'mnet_system');
|
||||
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system.listServices', 'mnet_system');
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system/listServices', 'mnet_system');
|
||||
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system.keyswap', 'mnet_keyswap');
|
||||
xmlrpc_server_register_method($xmlrpcserver, 'system/keyswap', 'mnet_keyswap');
|
||||
|
||||
if ($method == 'system.listMethods' ||
|
||||
$method == 'system/listMethods' ||
|
||||
$method == 'system.methodSignature' ||
|
||||
$method == 'system/methodSignature' ||
|
||||
$method == 'system.methodHelp' ||
|
||||
$method == 'system/methodHelp' ||
|
||||
$method == 'system.listServices' ||
|
||||
$method == 'system/listServices' ||
|
||||
$method == 'system.keyswap' ||
|
||||
$method == 'system/keyswap') {
|
||||
|
||||
$response = xmlrpc_server_call_method($xmlrpcserver, $payload, $MNET_REMOTE_CLIENT);
|
||||
$response = mnet_server_prepare_response($response);
|
||||
} else {
|
||||
exit(mnet_server_fault(7018, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
xmlrpc_server_destroy($xmlrpcserver);
|
||||
echo $response;
|
||||
////////////////////////////////////// STRICT AUTH
|
||||
} elseif ($callstack[0] == 'auth') {
|
||||
|
||||
// Break out the callstack into its elements
|
||||
list($base, $plugin, $filename, $methodname) = $callstack;
|
||||
|
||||
// We refuse to include anything that is not auth.php
|
||||
if ($filename == 'auth.php' && is_enabled_auth($plugin)) {
|
||||
$authclass = 'auth_plugin_'.$plugin;
|
||||
$includefile = '/auth/'.$plugin.'/auth.php';
|
||||
$response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $authclass);
|
||||
$response = mnet_server_prepare_response($response);
|
||||
echo $response;
|
||||
} else {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(702, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
////////////////////////////////////// STRICT ENROL
|
||||
} elseif ($callstack[0] == 'enrol') {
|
||||
|
||||
// Break out the callstack into its elements
|
||||
list($base, $plugin, $filename, $methodname) = $callstack;
|
||||
|
||||
if ($filename == 'enrol.php' && is_enabled_enrol($plugin)) {
|
||||
$enrolclass = 'enrolment_plugin_'.$plugin;
|
||||
$includefile = '/enrol/'.$plugin.'/enrol.php';
|
||||
$response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $enrolclass);
|
||||
$response = mnet_server_prepare_response($response);
|
||||
echo $response;
|
||||
} else {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(703, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
////////////////////////////////////// STRICT MOD/*
|
||||
} elseif ($callstack[0] == 'mod' || 'promiscuous' == $CFG->mnet_dispatcher_mode) {
|
||||
list($base, $module, $filename, $functionname) = $callstack;
|
||||
|
||||
////////////////////////////////////// STRICT MOD/*
|
||||
if ($base == 'mod' && $filename == 'rpclib.php') {
|
||||
$includefile = '/mod/'.$module.'/rpclib.php';
|
||||
$response = mnet_server_invoke_method($includefile, $functionname, $method, $payload);
|
||||
$response = mnet_server_prepare_response($response);
|
||||
echo $response;
|
||||
|
||||
////////////////////////////////////// PROMISCUOUS
|
||||
} elseif ('promiscuous' == $CFG->mnet_dispatcher_mode && $MNET_REMOTE_CLIENT->plaintext_is_ok()) {
|
||||
|
||||
$functionname = array_pop($callstack);
|
||||
$filename = array_pop($callstack);
|
||||
|
||||
if ($MNET_REMOTE_CLIENT->plaintext_is_ok()) {
|
||||
|
||||
// The call stack holds the path to any include file
|
||||
$includefile = $CFG->dirroot.'/'.implode('/',$callstack).'/'.$filename.'.php';
|
||||
|
||||
$response = mnet_server_invoke_function($includefile, $functionname, $method, $payload);
|
||||
echo $response;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(7012, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
} else {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(7012, 'nosuchfunction'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the system functions - mostly for introspection
|
||||
*
|
||||
* @param string $method XMLRPC method name, e.g. system.listMethods
|
||||
* @param array $params Array of parameters from the XMLRPC request
|
||||
* @param string $hostinfo Hostinfo object from the mnet_host table
|
||||
* @return mixed Response data - any kind of PHP variable
|
||||
*/
|
||||
function mnet_system($method, $params, $hostinfo) {
|
||||
global $CFG;
|
||||
|
||||
if (empty($hostinfo)) return array();
|
||||
|
||||
$id_list = $hostinfo->id;
|
||||
if (!empty($CFG->mnet_all_hosts_id)) {
|
||||
$id_list .= ', '.$CFG->mnet_all_hosts_id;
|
||||
}
|
||||
|
||||
if ('system.listMethods' == $method || 'system/listMethods' == $method) {
|
||||
if (count($params) == 0) {
|
||||
$query = '
|
||||
SELECT DISTINCT
|
||||
rpc.function_name,
|
||||
rpc.xmlrpc_path,
|
||||
rpc.enabled,
|
||||
rpc.help,
|
||||
rpc.profile
|
||||
FROM
|
||||
'.$CFG->prefix.'mnet_host2service h2s,
|
||||
'.$CFG->prefix.'mnet_service2rpc s2r,
|
||||
'.$CFG->prefix.'mnet_rpc rpc
|
||||
WHERE
|
||||
s2r.rpcid = rpc.id AND
|
||||
h2s.serviceid = s2r.serviceid AND
|
||||
h2s.hostid in ('.$id_list .')
|
||||
ORDER BY
|
||||
rpc.xmlrpc_path ASC';
|
||||
|
||||
} else {
|
||||
$query = '
|
||||
SELECT DISTINCT
|
||||
rpc.function_name,
|
||||
rpc.xmlrpc_path,
|
||||
rpc.enabled,
|
||||
rpc.help,
|
||||
rpc.profile
|
||||
FROM
|
||||
'.$CFG->prefix.'mnet_host2service h2s,
|
||||
'.$CFG->prefix.'mnet_service2rpc s2r,
|
||||
'.$CFG->prefix.'mnet_service svc,
|
||||
'.$CFG->prefix.'mnet_rpc rpc
|
||||
WHERE
|
||||
s2r.rpcid = rpc.id AND
|
||||
h2s.serviceid = s2r.serviceid AND
|
||||
h2s.hostid in ('.$id_list .') AND
|
||||
svc.id = h2s.serviceid AND
|
||||
svc.name = \''.$params[0].'\'
|
||||
ORDER BY
|
||||
rpc.xmlrpc_path ASC';
|
||||
|
||||
}
|
||||
$resultset = array_values(get_records_sql($query));
|
||||
$methods = array();
|
||||
foreach($resultset as $result) {
|
||||
$methods[] = $result->xmlrpc_path;
|
||||
}
|
||||
return $methods;
|
||||
} elseif ('system.methodSignature' == $method || 'system/methodSignature' == $method) {
|
||||
$query = '
|
||||
SELECT DISTINCT
|
||||
rpc.function_name,
|
||||
rpc.xmlrpc_path,
|
||||
rpc.enabled,
|
||||
rpc.help,
|
||||
rpc.profile
|
||||
FROM
|
||||
'.$CFG->prefix.'mnet_host2service h2s,
|
||||
'.$CFG->prefix.'mnet_service2rpc s2r,
|
||||
'.$CFG->prefix.'mnet_rpc rpc
|
||||
WHERE
|
||||
rpc.xmlrpc_path = \''.$params[0].'\' AND
|
||||
s2r.rpcid = rpc.id AND
|
||||
h2s.serviceid = s2r.serviceid AND
|
||||
h2s.hostid in ('.$id_list .')';
|
||||
|
||||
$result = get_records_sql($query);
|
||||
$methodsigs = array();
|
||||
|
||||
if (is_array($result)) {
|
||||
foreach($result as $method) {
|
||||
$methodsigs[] = unserialize($method->profile);
|
||||
}
|
||||
}
|
||||
|
||||
return $methodsigs;
|
||||
} elseif ('system.methodHelp' == $method || 'system/methodHelp' == $method) {
|
||||
$query = '
|
||||
SELECT DISTINCT
|
||||
rpc.function_name,
|
||||
rpc.xmlrpc_path,
|
||||
rpc.enabled,
|
||||
rpc.help,
|
||||
rpc.profile
|
||||
FROM
|
||||
'.$CFG->prefix.'mnet_host2service h2s,
|
||||
'.$CFG->prefix.'mnet_service2rpc s2r,
|
||||
'.$CFG->prefix.'mnet_rpc rpc
|
||||
WHERE
|
||||
rpc.xmlrpc_path = \''.$params[0].'\' AND
|
||||
s2r.rpcid = rpc.id AND
|
||||
h2s.serviceid = s2r.serviceid AND
|
||||
h2s.hostid in ('.$id_list .')';
|
||||
|
||||
$result = get_record_sql($query);
|
||||
|
||||
if (is_object($result)) {
|
||||
return $result->help;
|
||||
}
|
||||
} elseif ('system.listServices' == $method || 'system/listServices' == $method) {
|
||||
$query = '
|
||||
SELECT DISTINCT
|
||||
s.id,
|
||||
s.name,
|
||||
s.apiversion,
|
||||
h2s.publish,
|
||||
h2s.subscribe
|
||||
FROM
|
||||
'.$CFG->prefix.'mnet_host2service h2s,
|
||||
'.$CFG->prefix.'mnet_service s
|
||||
WHERE
|
||||
h2s.serviceid = s.id AND
|
||||
h2s.hostid in ('.$id_list .')
|
||||
ORDER BY
|
||||
s.name ASC';
|
||||
|
||||
$result = get_records_sql($query);
|
||||
$services = array();
|
||||
|
||||
if (is_array($result)) {
|
||||
foreach($result as $service) {
|
||||
$services[] = array('name' => $service->name,
|
||||
'apiversion' => $service->apiversion,
|
||||
'publish' => $service->publish,
|
||||
'subscribe' => $service->subscribe);
|
||||
}
|
||||
}
|
||||
|
||||
return $services;
|
||||
}
|
||||
exit(mnet_server_fault(7019, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the object (if necessary), execute the method or function, and
|
||||
* return the response
|
||||
*
|
||||
* @param string $includefile The file that contains the object definition
|
||||
* @param string $methodname The name of the method to execute
|
||||
* @param string $method The full path to the method
|
||||
* @param string $payload The XML-RPC request payload
|
||||
* @param string $class The name of the class to instantiate (or false)
|
||||
* @return string The XML-RPC response
|
||||
*/
|
||||
function mnet_server_invoke_method($includefile, $methodname, $method, $payload, $class=false) {
|
||||
|
||||
$permission = mnet_permit_rpc_call($includefile, $methodname, $class);
|
||||
|
||||
if (RPC_NOSUCHFILE == $permission) {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(705, 'nosuchfile', $includefile));
|
||||
}
|
||||
|
||||
if (RPC_NOSUCHFUNCTION == $permission) {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(706, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
if (RPC_FORBIDDENFUNCTION == $permission) {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(707, 'forbidden-function'));
|
||||
}
|
||||
|
||||
if (RPC_NOSUCHCLASS == $permission) {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(7013, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
if (RPC_NOSUCHMETHOD == $permission) {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(7014, 'nosuchmethod'));
|
||||
}
|
||||
|
||||
if (RPC_NOSUCHFUNCTION == $permission) {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(7014, 'nosuchmethod'));
|
||||
}
|
||||
|
||||
if (RPC_FORBIDDENMETHOD == $permission) {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(7015, 'nosuchfunction'));
|
||||
}
|
||||
|
||||
if (0 < $permission) {
|
||||
// Generate error response - unable to locate function
|
||||
exit(mnet_server_fault(7019, 'unknownerror'));
|
||||
}
|
||||
|
||||
if (RPC_OK == $permission) {
|
||||
$xmlrpcserver = xmlrpc_server_create();
|
||||
$bool = xmlrpc_server_register_method($xmlrpcserver, $method, 'mnet_server_dummy_method');
|
||||
$response = xmlrpc_server_call_method($xmlrpcserver, $payload, $methodname);
|
||||
$bool = xmlrpc_server_destroy($xmlrpcserver);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
function mnet_keyswap($function, $params) {
|
||||
global $CFG;
|
||||
$return = array();
|
||||
|
||||
if (!empty($CFG->mnet_register_allhosts)) {
|
||||
$mnet_peer = new mnet_peer();
|
||||
$keyok = $mnet_peer->bootstrap($params[0]);
|
||||
if ($keyok) {
|
||||
$mnet_peer->commit();
|
||||
}
|
||||
}
|
||||
$keypair = mnet_get_keypair();
|
||||
return $keypair['certificate'];
|
||||
}
|
||||
?>
|
242
mnet/xmlrpc/xmlparser.php
Normal file
242
mnet/xmlrpc/xmlparser.php
Normal file
|
@ -0,0 +1,242 @@
|
|||
<?php
|
||||
/**
|
||||
* Custom XML parser for signed and/or encrypted XML Docs
|
||||
*
|
||||
* @author Donal McMullan donal@catalyst.net.nz
|
||||
* @version 0.0.1
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package mnet
|
||||
*/
|
||||
|
||||
/**
|
||||
* Custom XML parser class for signed and/or encrypted XML Docs
|
||||
*/
|
||||
class mnet_encxml_parser {
|
||||
/**
|
||||
* Constructor creates and initialises parser resource and calls initialise
|
||||
*
|
||||
* @return bool True
|
||||
*/
|
||||
function mnet_encxml_parser() {
|
||||
return $this->initialise();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default element handlers and initialise properties to empty.
|
||||
*
|
||||
* @return bool True
|
||||
*/
|
||||
function initialise() {
|
||||
$this->parser = xml_parser_create();
|
||||
xml_set_object($this->parser, $this);
|
||||
|
||||
xml_set_element_handler($this->parser, "start_element", "end_element");
|
||||
xml_set_character_data_handler($this->parser, "discard_data");
|
||||
|
||||
$this->tag_number = 0; // Just a unique ID for each tag
|
||||
$this->digest = '';
|
||||
$this->remote_wwwroot = '';
|
||||
$this->signature = '';
|
||||
$this->data_object = '';
|
||||
$this->key_URI = '';
|
||||
$this->payload_encrypted = false;
|
||||
$this->cipher = array();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a block of XML text
|
||||
*
|
||||
* The XML Text will be an XML-RPC request which is wrapped in an XML doc
|
||||
* with a signature from the sender. This envelope may be encrypted and
|
||||
* delivered within another XML envelope with a symmetric key. The parser
|
||||
* should first decrypt this XML, and then place the XML-RPC request into
|
||||
* the data_object property, and the signature into the signature property.
|
||||
*
|
||||
* See the W3C's {@link http://www.w3.org/TR/xmlenc-core/ XML Encryption Syntax and Processing}
|
||||
* and {@link http://www.w3.org/TR/2001/PR-xmldsig-core-20010820/ XML-Signature Syntax and Processing}
|
||||
* guidelines for more detail on the XML.
|
||||
*
|
||||
* -----XML-Envelope---------------------------------
|
||||
* | |
|
||||
* | Symmetric-key-------------------------- |
|
||||
* | |_____________________________________| |
|
||||
* | |
|
||||
* | Encrypted data------------------------- |
|
||||
* | | | |
|
||||
* | | -XML-Envelope------------------ | |
|
||||
* | | | | | |
|
||||
* | | | --Signature------------- | | |
|
||||
* | | | |______________________| | | |
|
||||
* | | | | | |
|
||||
* | | | --Signed-Payload-------- | | |
|
||||
* | | | | | | | |
|
||||
* | | | | XML-RPC Request | | | |
|
||||
* | | | |______________________| | | |
|
||||
* | | | | | |
|
||||
* | | |_____________________________| | |
|
||||
* | |_____________________________________| |
|
||||
* | |
|
||||
* |________________________________________________|
|
||||
*
|
||||
* @uses $MNET
|
||||
* @param string $data The XML that you want to parse
|
||||
* @return bool True on success - false on failure
|
||||
*/
|
||||
function parse($data) {
|
||||
global $MNET, $MNET_REMOTE_CLIENT;
|
||||
|
||||
$p = xml_parse($this->parser, $data);
|
||||
|
||||
if (count($this->cipher) > 0) {
|
||||
$this->payload_encrypted = true;
|
||||
}
|
||||
|
||||
return (bool)$p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the parser and free up any related resource.
|
||||
*/
|
||||
function free_resource() {
|
||||
$free = xml_parser_free($this->parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the character-data handler to the right function for each element
|
||||
*
|
||||
* For each tag (element) name, this function switches the character-data
|
||||
* handler to the function that handles that element. Note that character
|
||||
* data is referred to the handler in blocks of 1024 bytes.
|
||||
*
|
||||
* @param mixed $parser The XML parser
|
||||
* @param string $name The name of the tag, e.g. method_call
|
||||
* @param array $attrs The tag's attributes (if any exist).
|
||||
* @return bool True
|
||||
*/
|
||||
function start_element($parser, $name, $attrs) {
|
||||
$this->tag_number++;
|
||||
$handler = 'discard_data';
|
||||
switch(strtoupper($name)) {
|
||||
case 'DIGESTVALUE':
|
||||
$handler = 'parse_digest';
|
||||
break;
|
||||
case 'SIGNATUREVALUE':
|
||||
$handler = 'parse_signature';
|
||||
break;
|
||||
case 'OBJECT':
|
||||
$handler = 'parse_object';
|
||||
break;
|
||||
case 'RETRIEVALMETHOD':
|
||||
$this->key_URI = $attrs['URI'];
|
||||
break;
|
||||
case 'WWWROOT':
|
||||
$handler = 'parse_wwwroot';
|
||||
break;
|
||||
case 'CIPHERVALUE':
|
||||
$this->cipher[$this->tag_number] = '';
|
||||
$handler = 'parse_cipher';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
xml_set_character_data_handler($this->parser, $handler);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the next chunk of character data to the cipher string for that tag
|
||||
*
|
||||
* The XML parser calls the character-data handler with 1024-character
|
||||
* chunks of data. This means that the handler may be called several times
|
||||
* for a single tag, so we use the concatenate operator (.) to build the
|
||||
* tag content into a string.
|
||||
* We should not encounter more than one of each tag type, except for the
|
||||
* cipher tag. We will often see two of those. We prevent the content of
|
||||
* these two tags being concatenated together by counting each tag, and
|
||||
* using its 'number' as the key to an array of ciphers.
|
||||
*
|
||||
* @param mixed $parser The XML parser
|
||||
* @param string $data The content of the current tag (1024 byte chunk)
|
||||
* @return bool True
|
||||
*/
|
||||
function parse_cipher($parser, $data) {
|
||||
$this->cipher[$this->tag_number] .= $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the next chunk of character data to the remote_wwwroot string
|
||||
*
|
||||
* @param mixed $parser The XML parser
|
||||
* @param string $data The content of the current tag (1024 byte chunk)
|
||||
* @return bool True
|
||||
*/
|
||||
function parse_wwwroot($parser, $data) {
|
||||
$this->remote_wwwroot .= $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the next chunk of character data to the digest string
|
||||
*
|
||||
* @param mixed $parser The XML parser
|
||||
* @param string $data The content of the current tag (1024 byte chunk)
|
||||
* @return bool True
|
||||
*/
|
||||
function parse_digest($parser, $data) {
|
||||
$this->digest .= $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the next chunk of character data to the signature string
|
||||
*
|
||||
* @param mixed $parser The XML parser
|
||||
* @param string $data The content of the current tag (1024 byte chunk)
|
||||
* @return bool True
|
||||
*/
|
||||
function parse_signature($parser, $data) {
|
||||
$this->signature .= $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the next chunk of character data to the data_object string
|
||||
*
|
||||
* @param mixed $parser The XML parser
|
||||
* @param string $data The content of the current tag (1024 byte chunk)
|
||||
* @return bool True
|
||||
*/
|
||||
function parse_object($parser, $data) {
|
||||
$this->data_object .= $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discard the next chunk of character data
|
||||
*
|
||||
* This is used for tags that we're not interested in.
|
||||
*
|
||||
* @param mixed $parser The XML parser
|
||||
* @param string $data The content of the current tag (1024 byte chunk)
|
||||
* @return bool True
|
||||
*/
|
||||
function discard_data($parser, $data) {
|
||||
// Not interested
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch the character-data handler to ignore the next chunk of data
|
||||
*
|
||||
* @param mixed $parser The XML parser
|
||||
* @param string $name The name of the tag, e.g. method_call
|
||||
* @return bool True
|
||||
*/
|
||||
function end_element($parser, $name) {
|
||||
$ok = xml_set_character_data_handler($this->parser, "discard_data");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue