2007-01-04 02:33:51 +00:00
|
|
|
<?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"));
|
|
|
|
|
2007-01-04 03:36:24 +00:00
|
|
|
$timestamp_send = time();
|
2007-01-04 02:33:51 +00:00
|
|
|
$this->rawresponse = curl_exec($ch);
|
2007-01-04 03:36:24 +00:00
|
|
|
$timestamp_receive = time();
|
|
|
|
|
2007-01-04 02:33:51 +00:00
|
|
|
if ($this->rawresponse == false) {
|
|
|
|
$this->error[] = array(curl_errno($ch), curl_error($ch));
|
2007-01-04 03:36:24 +00:00
|
|
|
return false;
|
2007-01-04 02:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2007-01-04 03:36:24 +00:00
|
|
|
// Margin of error is the time it took the request to complete.
|
|
|
|
$margin_of_error = $timestamp_receive - $timestamp_send;
|
|
|
|
|
|
|
|
// Guess the time gap between sending the request and the remote machine
|
|
|
|
// executing the time() function. Marginally better than nothing.
|
|
|
|
$hysteresis = ($margin_of_error) / 2;
|
|
|
|
|
|
|
|
$remote_timestamp = $sig_parser->remote_timestamp - $hysteresis;
|
|
|
|
$time_offset = $remote_timestamp - $timestamp_send;
|
|
|
|
if ($time_offset > 0) {
|
|
|
|
$result = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'drift_threshold');
|
|
|
|
if(empty($result)) {
|
|
|
|
// We decided 15 seconds was a pretty good arbitrary threshold
|
|
|
|
// for time-drift between servers, but you can customize this in
|
|
|
|
// the config_plugins table. It's not advised though.
|
|
|
|
set_config('drift_threshold', 15, 'mnet');
|
|
|
|
$threshold = 15;
|
|
|
|
} else {
|
|
|
|
$threshold = $result;
|
|
|
|
}
|
|
|
|
if ($time_offset > $threshold) {
|
|
|
|
$this->error[] = 'Time gap with '.$mnet_peer->name.' ('.$time_offset.' seconds) is greater than the permitted maximum of '.$threshold.' seconds';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-04 02:33:51 +00:00
|
|
|
$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'])) {
|
2007-01-04 03:34:14 +00:00
|
|
|
// The faultCode 7025 means we tried to connect with an old SSL key
|
|
|
|
// The faultString is the new key - let's save it and try again
|
|
|
|
// The re_key attribute stops us from getting into a loop
|
|
|
|
if($this->response['faultCode'] == 7025 && empty($mnet_peer->re_key)) {
|
|
|
|
$record = new stdClass();
|
|
|
|
$record->id = $mnet_peer->id;
|
|
|
|
$record->public_key = $this->response['faultString'];
|
|
|
|
$details = openssl_x509_parse($record->public_key);
|
|
|
|
$record->public_key_expires = $details['validTo_time_t'];
|
|
|
|
update_record('mnet_host', $record);
|
|
|
|
$mnet_peer2 = new mnet_peer();
|
|
|
|
$mnet_peer2->set_id($record->id);
|
|
|
|
$mnet_peer2->re_key = true;
|
|
|
|
$this->send($mnet_peer2);
|
|
|
|
} else {
|
|
|
|
$this->error[] = $this->response['faultCode'] . " : " . $this->response['faultString'];
|
|
|
|
}
|
2007-01-04 02:33:51 +00:00
|
|
|
}
|
|
|
|
return empty($this->error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|