Skip to content

Commit

Permalink
mnet MDL-21294 change errors to exceptions in mnet server
Browse files Browse the repository at this point in the history
  • Loading branch information
Penny Leach committed Jan 13, 2010
1 parent e039552 commit 939ea0b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 51 deletions.
16 changes: 6 additions & 10 deletions auth/mnet/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,17 @@ function user_authorise($token, $useragent) {

$mnet_session = $DB->get_record('mnet_session', array('token'=>$token, 'useragent'=>$useragent));
if (empty($mnet_session)) {
echo mnet_server_fault(1, get_string('authfail_nosessionexists', 'mnet'));
exit;
throw new mnet_server_exception(1, get_string('authfail_nosessionexists', 'mnet'));
}

// check session confirm timeout
if ($mnet_session->confirm_timeout < time()) {
echo mnet_server_fault(2, get_string('authfail_sessiontimedout', 'mnet'));
exit;
throw new mnet_server_exception(2, get_string('authfail_sessiontimedout', 'mnet'));
}

// session okay, try getting the user
if (!$user = $DB->get_record('user', array('id'=>$mnet_session->userid))) {
echo mnet_server_fault(3, get_string('authfail_usermismatch', 'mnet'));
exit;
throw new mnet_server_exception(3, get_string('authfail_usermismatch', 'mnet'));
}

$userdata = array();
Expand Down Expand Up @@ -480,10 +477,9 @@ function update_enrolments($username, $courses) {
}
// make sure it is a user we have an in active session
// with that host...
$userid = $DB->get_field('mnet_session', 'userid',
array('username'=>$username, 'mnethostid'=>$MNET_REMOTE_CLIENT->id));
if (!$userid) {
return false;
if (!$userid = $DB->get_field('mnet_session', 'userid',
array('username'=>$username, 'mnethostid'=>$MNET_REMOTE_CLIENT->id))) {
throw new mnet_server_exception(1, get_string('authfail_nosessionexists', 'mnet'));
}

if (empty($courses)) { // no courses? clear out quickly
Expand Down
21 changes: 21 additions & 0 deletions mnet/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,24 @@ function mnet_get_app_jumppath ($applicationid) {
}
return $appjumppaths[$applicationid];
}

/**
* mnet server exception. extends moodle_exception, but takes slightly different arguments.
* error strings are always in mnet, so $module is not necessary,
* and unlike the rest of moodle, the actual int error code is used.
* this exception should only be used during an xmlrpc server request, ie, not for client requests.
*/
class mnet_server_exception extends moodle_exception {

/**
* @param int $intcode the numerical error associated with this fault. this is <b>not</b> the string errorcode
* @param string $languagekey the key to the language string for the error message, or the text of the message (mnet_server_fault figures it out for you) if you're not using mnet error string file
* @param mixed $a params for get_string
*/
public function __construct($intcode, $languagekey, $a=null) {
parent::__construct($languagekey, 'mnet', '', $a);
$this->code = $intcode;
$this->message = $languagekey; // override this because mnet_server_fault (our handler) uses this directly

}
}
93 changes: 58 additions & 35 deletions mnet/xmlrpc/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@
// if you've been called by a remote Moodle, this should be set:
$MNET_REMOTE_CLIENT = new mnet_remote_client();

$plaintextmessage = mnet_server_strip_encryption($HTTP_RAW_POST_DATA);
$xmlrpcrequest = mnet_server_strip_signature($plaintextmessage);
try {
$plaintextmessage = mnet_server_strip_encryption($HTTP_RAW_POST_DATA);
$xmlrpcrequest = mnet_server_strip_signature($plaintextmessage);
catch (Exception $e {
exit(mnet_server_fault($e->getCode(), $e->getMessage(), $e->a));
}


if($MNET_REMOTE_CLIENT->pushkey == true) {
// The peer used one of our older public keys, we will return a
Expand All @@ -67,7 +72,11 @@
if ((($MNET_REMOTE_CLIENT->request_was_encrypted == true) && ($MNET_REMOTE_CLIENT->signatureok == true))
|| (($method == 'system.keyswap') || ($method == 'system/keyswap'))
|| (($MNET_REMOTE_CLIENT->signatureok == true) && ($MNET_REMOTE_CLIENT->plaintext_is_ok() == true))) {
$response = mnet_server_dispatch($xmlrpcrequest);
try {
$response = mnet_server_dispatch($xmlrpcrequest);
catch (Exception $e) {
exit(mnet_server_fault($e->getCode(), $e->getMessage(), $e->a));
}
} else {
if (($MNET_REMOTE_CLIENT->request_was_encrypted == false) && ($MNET_REMOTE_CLIENT->plaintext_is_ok() == false)) {
exit(mnet_server_fault(7021, 'forbidden-transport'));
Expand Down Expand Up @@ -118,6 +127,9 @@
/* Strip encryption envelope (if present) and decrypt data
*
* @param string $HTTP_RAW_POST_DATA The XML that the client sent
*
* @throws mnet_server_exception
*
* @return string XML with any encryption envolope removed
*/
function mnet_server_strip_encryption($HTTP_RAW_POST_DATA) {
Expand All @@ -133,7 +145,7 @@ function mnet_server_strip_encryption($HTTP_RAW_POST_DATA) {
$host_record_exists = $MNET_REMOTE_CLIENT->set_wwwroot($crypt_parser->remote_wwwroot);

if (false == $host_record_exists) {
exit(mnet_server_fault(7020, 'wrong-wwwroot', $crypt_parser->remote_wwwroot));
throw new mnet_server_exception(7020, 'wrong-wwwroot', $crypt_parser->remote_wwwroot);
}

// This key is symmetric, and is itself encrypted. Can be decrypted using our private key
Expand Down Expand Up @@ -173,12 +185,13 @@ function mnet_server_strip_encryption($HTTP_RAW_POST_DATA) {
}

//If after all that we still couldn't decrypt the message, error out.
exit(mnet_server_fault(7023, 'encryption-invalid'));
throw new mnet_server_exception(7023, 'encryption-invalid');
}

/* Strip signature envelope (if present), try to verify any signature using our record of remote peer's public key.
*
* @param string $plaintextmessage XML envelope containing XMLRPC request and signature
*
* @return string XMLRPC request
*/
function mnet_server_strip_signature($plaintextmessage) {
Expand Down Expand Up @@ -306,6 +319,8 @@ function mnet_server_fault_xml($code, $text, $privatekey = null) {
* The methodName argument (eg. mnet/testlib/mnet_concatenate_strings)
* is ignored.
*
* @throws mnet_server_exception
*
* @param string $methodname We discard this - see 'functionname'
* @param array $argsarray Each element is an argument to the real
* function
Expand All @@ -316,12 +331,16 @@ function mnet_server_fault_xml($code, $text, $privatekey = null) {
function mnet_server_dummy_method($methodname, $argsarray, $functionname) {
global $MNET_REMOTE_CLIENT;

if (is_object($MNET_REMOTE_CLIENT->object_to_call)) {
return @call_user_func_array(array($MNET_REMOTE_CLIENT->object_to_call,$functionname), $argsarray);
} else if (!empty($MNET_REMOTE_CLIENT->static_location)) {
return @call_user_func_array(array($MNET_REMOTE_CLIENT->static_location, $functionname), $argsarray);
} else {
return @call_user_func_array($functionname, $argsarray);
try {
if (is_object($MNET_REMOTE_CLIENT->object_to_call)) {
return @call_user_func_array(array($MNET_REMOTE_CLIENT->object_to_call,$functionname), $argsarray);
} else if (!empty($MNET_REMOTE_CLIENT->static_location)) {
return @call_user_func_array(array($MNET_REMOTE_CLIENT->static_location, $functionname), $argsarray);
} else {
return @call_user_func_array($functionname, $argsarray);
}
} catch (Exception $e) {
exit(mnet_server_fault($e->getCode(), $e->getMessage()));
}
}

Expand Down Expand Up @@ -354,6 +373,9 @@ function mnet_server_prepare_response($response, $privatekey = null) {
* off: The default - don't execute anything
*
* @param string $payload The XML-RPC request
*
* @throws mnet_server_exception
*
* @return No return val - just echo the response
*/
function mnet_server_dispatch($payload) {
Expand All @@ -371,7 +393,7 @@ function mnet_server_dispatch($payload) {
// 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'));
throw new mnet_server_exception(713, 'nosuchfunction');
}

if(preg_match("/^system\./", $method)) {
Expand All @@ -389,9 +411,9 @@ function mnet_server_dispatch($payload) {
////////////////////////////////////// OFF
if (!isset($CFG->mnet_dispatcher_mode) ) {
set_config('mnet_dispatcher_mode', 'off');
exit(mnet_server_fault(704, 'nosuchservice'));
throw new mnet_server_exception(704, 'nosuchservice');
} elseif ('off' == $CFG->mnet_dispatcher_mode) {
exit(mnet_server_fault(704, 'nosuchservice'));
throw new mnet_server_exception(704, 'nosuchservice');

////////////////////////////////////// SYSTEM METHODS
} elseif ($callstack[0] == 'system') {
Expand Down Expand Up @@ -445,7 +467,7 @@ function mnet_server_dispatch($payload) {
$response = xmlrpc_server_call_method($xmlrpcserver, $payload, $MNET_REMOTE_CLIENT, array("encoding" => "utf-8"));
$response = mnet_server_prepare_response($response);
} else {
exit(mnet_server_fault(7018, 'nosuchfunction'));
throw new mnet_server_exception(7018, 'nosuchfunction');
}

xmlrpc_server_destroy($xmlrpcserver);
Expand All @@ -465,7 +487,7 @@ function mnet_server_dispatch($payload) {
echo $response;
} else {
// Generate error response - unable to locate function
exit(mnet_server_fault(702, 'nosuchfunction'));
throw new mnet_server_exception(702, 'nosuchfunction');
}

////////////////////////////////////// STRICT ENROL
Expand All @@ -482,7 +504,7 @@ function mnet_server_dispatch($payload) {
echo $response;
} else {
// Generate error response - unable to locate function
exit(mnet_server_fault(703, 'nosuchfunction'));
throw new mnet_server_exception(703, 'nosuchfunction');
}

} else if ($callstack[0] == 'portfolio') {
Expand All @@ -497,7 +519,7 @@ function mnet_server_dispatch($payload) {
echo $response;
} else {
// Generate error response - unable to locate function
exit(mnet_server_fault(7012, 'nosuchfunction'));
throw new mnet_server_exception(7012, 'nosuchfunction');
}

} else if ($callstack[0] == 'repository') {
Expand All @@ -512,7 +534,7 @@ function mnet_server_dispatch($payload) {
echo $response;
} else {
// Generate error response - unable to locate function
exit(mnet_server_fault(7012, 'nosuchfunction'));
throw new mnet_server_exception(7012, 'nosuchfunction');
}

////////////////////////////////////// STRICT MOD/*
Expand All @@ -537,7 +559,7 @@ function mnet_server_dispatch($payload) {
if (0 == preg_match("/php$/", $filename)) {
// Filename doesn't end in 'php'; possible attack?
// Generate error response - unable to locate function
exit(mnet_server_fault(7012, 'nosuchfunction'));
throw new mnet_server_exception(7012, 'nosuchfunction');
}

// The call stack holds the path to any include file
Expand All @@ -549,12 +571,12 @@ function mnet_server_dispatch($payload) {

} else {
// Generate error response - unable to locate function
exit(mnet_server_fault(7012, 'nosuchfunction'));
throw new mnet_server_exception(7012, 'nosuchfunction');
}

} else {
// Generate error response - unable to locate function
exit(mnet_server_fault(7012, 'nosuchfunction'));
throw new mnet_server_exception(7012, 'nosuchfunction');
}
}

Expand All @@ -564,6 +586,9 @@ function mnet_server_dispatch($payload) {
* @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
*
* @throws mnet_server_exception
*
* @return mixed Response data - any kind of PHP variable
*/
function mnet_system($method, $params, $hostinfo) {
Expand Down Expand Up @@ -717,7 +742,7 @@ function mnet_system($method, $params, $hostinfo) {

return $services;
}
exit(mnet_server_fault(7019, 'nosuchfunction'));
throw new mnet_server_exception(7019, 'nosuchfunction');
}

/**
Expand All @@ -729,6 +754,9 @@ function mnet_system($method, $params, $hostinfo) {
* @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)
*
* @throws mnet_server_exception
*
* @return string The XML-RPC response
*/
function mnet_server_invoke_method($includefile, $methodname, $method, $payload, $class=false) {
Expand All @@ -737,42 +765,37 @@ function mnet_server_invoke_method($includefile, $methodname, $method, $payload,

if (RPC_NOSUCHFILE == $permission) {
// Generate error response - unable to locate function
exit(mnet_server_fault(705, 'nosuchfile', $includefile));
throw new mnet_server_exception(705, 'nosuchfile', $includefile);
}

if (RPC_NOSUCHFUNCTION == $permission) {
// Generate error response - unable to locate function
exit(mnet_server_fault(706, 'nosuchfunction'));
throw new mnet_server_exception(706, 'nosuchfunction');
}

if (RPC_FORBIDDENFUNCTION == $permission) {
// Generate error response - unable to locate function
exit(mnet_server_fault(707, 'forbidden-function'));
throw new mnet_server_exception(707, 'forbidden-function');
}

if (RPC_NOSUCHCLASS == $permission) {
// Generate error response - unable to locate function
exit(mnet_server_fault(7013, 'nosuchfunction'));
throw new mnet_server_exception(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'));
throw new mnet_server_exception(7014, 'nosuchmethod');
}

if (RPC_FORBIDDENMETHOD == $permission) {
// Generate error response - unable to locate function
exit(mnet_server_fault(7015, 'nosuchfunction'));
throw new mnet_server_exception(7015, 'nosuchfunction');
}

if (0 < $permission) {
// Generate error response - unable to locate function
exit(mnet_server_fault(7019, 'unknownerror'));
throw new mnet_server_exception(7019, 'unknownerror');
}

if (RPC_OK == $permission) {
Expand Down
12 changes: 6 additions & 6 deletions portfolio/mahara/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,30 +323,30 @@ public static function fetch_file($token) {
global $DB, $MNET_REMOTE_CLIENT;;
try {
if (!$transferid = $DB->get_field('portfolio_mahara_queue', 'transferid', array('token' => $token))) {
exit(mnet_server_fault(8009, get_string('mnet_notoken', 'portfolio_mahara')));
throw new mnet_server_exception(8009, get_string('mnet_notoken', 'portfolio_mahara'));
}
$exporter = portfolio_exporter::rewaken_object($transferid);
} catch (portfolio_exception $e) {
exit(mnet_server_fault(8010, get_string('mnet_noid', 'portfolio_mahara')));
throw new mnet_server_exception(8010, get_string('mnet_noid', 'portfolio_mahara'));
}
if ($exporter->get('instance')->get_config('mnethostid') != $MNET_REMOTE_CLIENT->id) {
exit(mnet_server_fault(8011, get_string('mnet_wronghost', 'portfolio_mahara')));
throw new mnet_server_exception(8011, get_string('mnet_wronghost', 'portfolio_mahara'));
}
global $CFG;
try {
$i = $exporter->get('instance');
$f = $i->get('file');
if (empty($f) || !($f instanceof stored_file)) {
exit(mnet_server_fault(8012, get_string('mnet_nofile', 'portfolio_mahara')));
throw new mnet_server_exception(8012, get_string('mnet_nofile', 'portfolio_mahara'));
}
try {
$c = $f->get_content();
} catch (file_exception $e) {
exit(mnet_server_fault(8013, get_string('mnet_nofilecontents', 'portfolio_mahara', $e->getMessage())));
throw new mnet_server_exception(8013, get_string('mnet_nofilecontents', 'portfolio_mahara', $e->getMessage()));
}
$contents = base64_encode($c);
} catch (Exception $e) {
exit(mnet_server_fault(8013, get_string('mnet_nofile', 'portfolio_mahara')));
throw new mnet_server_exception(8013, get_string('mnet_nofile', 'portfolio_mahara'));
}
$exporter->log_transfer();
$exporter->process_stage_cleanup(true);
Expand Down

0 comments on commit 939ea0b

Please sign in to comment.