Skip to content

Commit 71f61c4

Browse files
author
Penny Leach
committed
mnet MDL-15505 added new mnet_debug function and started migrating to it
1 parent 48fb394 commit 71f61c4

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

mnet/lib.php

+29
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,32 @@ function mnet_get_app_jumppath ($applicationid) {
521521
return $appjumppaths[$applicationid];
522522
}
523523

524+
525+
function mnet_debug($debugdata, $debuglevel=1) {
526+
global $CFG;
527+
if ($CFG->mnet_rpcdebug < $debuglevel) {
528+
return;
529+
}
530+
if (is_object($debugdata)) {
531+
$debugdata = (array)$debugdata;
532+
}
533+
if (is_array($debugdata)) {
534+
mnet_debug('DUMPING ARRAY');
535+
foreach ($debugdata as $key => $value) {
536+
mnet_debug("$key: $value");
537+
}
538+
mnet_debug('END DUMPING ARRAY');
539+
return;
540+
}
541+
$prefix = 'MNET DEBUG ';
542+
if (defined('MNET_SERVER')) {
543+
$prefix .= " (server $CFG->wwwroot";
544+
if ($peer = get_mnet_remote_client() && !empty($peer->wwwroot)) {
545+
$prefix .= ", remote peer " . $peer->wwwroot;
546+
}
547+
$prefix .= ')';
548+
} else {
549+
$prefix .= " (client $CFG->wwwroot) ";
550+
}
551+
error_log("$prefix $debugdata");
552+
}

mnet/remote_client.php

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function plaintext_is_ok() {
6868
}
6969

7070
function refresh_key() {
71+
mnet_debug("remote client refreshing key");
7172
global $CFG;
7273
// set up an RPC request
7374
require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
@@ -77,6 +78,7 @@ function refresh_key() {
7778

7879
// Do RPC call and store response
7980
if ($mnetrequest->send($this) === true) {
81+
mnet_debug("refresh key request complete");
8082
// Ok - we actually don't care about the result
8183
$temp = new mnet_peer();
8284
$temp->set_id($this->id);

mnet/xmlrpc/client.php

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function send($mnet_peer) {
129129

130130

131131
if (!$this->permission_to_call($mnet_peer)) {
132+
mnet_debug("tried and wasn't allowed to call a method on $mnet_peer->wwwroot");
132133
return false;
133134
}
134135

@@ -140,7 +141,9 @@ function send($mnet_peer) {
140141
curl_setopt($httprequest, CURLOPT_POSTFIELDS, $this->encryptedrequest);
141142

142143
$timestamp_send = time();
144+
mnet_debug("about to send the curl request");
143145
$this->rawresponse = curl_exec($httprequest);
146+
mnet_debug("managed to complete a curl request");
144147
$timestamp_receive = time();
145148

146149
if ($this->rawresponse === false) {
@@ -257,6 +260,7 @@ function send($mnet_peer) {
257260
// The faultString is the new key - let's save it and try again
258261
// The re_key attribute stops us from getting into a loop
259262
if($this->response['faultCode'] == 7025 && empty($mnet_peer->re_key)) {
263+
mnet_debug('recieved an old-key fault, so trying to get the new key and update our records');
260264
// If the new certificate doesn't come thru clean_param() unmolested, error out
261265
if($this->response['faultString'] != clean_param($this->response['faultString'], PARAM_PEM)) {
262266
$this->error[] = $this->response['faultCode'] . " : " . $this->response['faultString'];

mnet/xmlrpc/server.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
$HTTP_RAW_POST_DATA = file_get_contents('php://input');
4040
}
4141

42-
if (!empty($CFG->mnet_rpcdebug)) {
43-
error_log("HTTP_RAW_POST_DATA");
44-
error_log($HTTP_RAW_POST_DATA);
45-
}
42+
mnet_debug("HTTP_RAW_POST_DATA", 2);
43+
mnet_debug($HTTP_RAW_POST_DATA, 2);
4644

4745
if (!isset($_SERVER)) {
4846
exit(mnet_server_fault(712, "phperror"));
@@ -58,22 +56,23 @@
5856
$plaintextmessage = mnet_server_strip_encryption($HTTP_RAW_POST_DATA);
5957
$xmlrpcrequest = mnet_server_strip_signature($plaintextmessage);
6058
} catch (Exception $e) {
59+
mnet_debug('encryption strip exception thrown: ' . $e->getMessage());
6160
exit(mnet_server_fault($e->getCode(), $e->getMessage(), $e->a));
6261
}
6362

64-
if (!empty($CFG->mnet_rpcdebug)) {
65-
error_log("XMLRPC Payload");
66-
error_log(print_r($xmlrpcrequest,1));
67-
}
63+
mnet_debug('XMLRPC Payload', 2);
64+
mnet_debug($xmlrpcrequest, 2);
6865

6966
if($remoteclient->pushkey == true) {
7067
// The peer used one of our older public keys, we will return a
7168
// signed/encrypted error message containing our new public key
7269
// Sign message with our old key, and encrypt to the peer's private key.
70+
mnet_debug('sending back new key');
7371
exit(mnet_server_fault_xml(7025, $mnet->public_key, $remoteclient->useprivatekey));
7472
}
7573
// Have a peek at what the request would be if we were to process it
7674
$params = xmlrpc_decode_request($xmlrpcrequest, $method);
75+
mnet_debug("incoming mnet request $method");
7776

7877
// One of three conditions need to be met before we continue processing this request:
7978
// 1. Request is properly encrypted and signed
@@ -85,24 +84,30 @@
8584
try {
8685
// main dispatch call. will echo the response directly
8786
mnet_server_dispatch($xmlrpcrequest);
87+
mnet_debug('exiting cleanly');
8888
exit;
8989
} catch (Exception $e) {
90+
mnet_debug('dispatch exception thrown: ' . $e->getMessage());
9091
exit(mnet_server_fault($e->getCode(), $e->getMessage(), $e->a));
9192
}
9293
}
9394
// if we get to here, something is wrong
9495
// so detect a few common cases and send appropriate errors
9596
if (($remoteclient->request_was_encrypted == false) && ($remoteclient->plaintext_is_ok() == false)) {
97+
mnet_debug('non encrypted request');
9698
exit(mnet_server_fault(7021, 'forbidden-transport'));
9799
}
98100

99101
if ($remoteclient->request_was_signed == false) {
100102
// Request was not signed
103+
mnet_debug('non signed request');
101104
exit(mnet_server_fault(711, 'verifysignature-error'));
102105
}
103106

104107
if ($remoteclient->signatureok == false) {
105108
// We were unable to verify the signature
109+
mnet_debug('non verified signature');
106110
exit(mnet_server_fault(710, 'verifysignature-invalid'));
107111
}
112+
mnet_debug('unknown error');
108113
exit(mnet_server_fault(7000, 'unknownerror'));

mnet/xmlrpc/serverlib.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ function mnet_server_fault_xml($code, $text, $privatekey = null) {
210210
</fault>
211211
</methodResponse>', $privatekey);
212212

213-
if (!empty($CFG->mnet_rpcdebug)) {
214-
trigger_error("XMLRPC Error Response $code: $text");
215-
trigger_error(print_r($return,1));
213+
if ($code != 7025) { // new key responses
214+
mnet_debug("XMLRPC Error Response $code: $text");
215+
mnet_debug($return);
216216
}
217217

218218
return $return;

0 commit comments

Comments
 (0)