/** * On-receive event handler * * This handler gets called when server (B) receives a trust_sendkey2 message from server (A), * and it generally happens to determine whether server (A) did send a corresponding trust_sendkey1 * before. It is also used to send server (B)'s key. * * @param array $data * @param object $msg * @return boolean */ function trust_sendkey2_receive(&$data, &$msg) { $serverUrl = getDefault($data['author']['server']); if ($serverUrl == '') { $msg->fail('invalid server field in "from" array'); } else { // does it really originate from server (A)? if (!$msg->validateSignature()) { return true; } // accept this server (A)'s key for future data $msg->fromServer->ds['s_key_out'] = $msg->data['mykey']; DB_UpdateField('servers', $msg->fromServer->ds['s_key'], 's_key_out', $msg->data['mykey']); $msg->ok(); } return true; }
/** * On-receive event handler * * Gets called when this server (A) receives a trust_sendkey1 message from another server (B). * Usually this means, server (B) wants to establish contact for the first time or it wants * to revoke its existing key. In both cases, the key can only be accepted when this server (A) * contacts server (B) to confirm the origin of the trust_sendkey1 message. * * @param array $data * @param object $msg * @return boolean */ function trust_sendkey1_receive(&$data, &$msg) { $data['mykey'] = trim($data['mykey']); $serverUrl = getDefault($data['author']['server']); if ($serverUrl == '') { $msg->fail('invalid server field in "author" array'); } if ($data['mykey'] == '') { $msg->fail('"mykey" field missing'); } else { // accept the new key (it's not confirmed yet) $server = new HubbubServer($serverUrl, true); $server->ds['s_newkey_out'] = $data['mykey']; $server->ds['s_key_in'] = getDefault($server->ds['s_key_in'], randomHashId()); DB_UpdateField('servers', $server->ds['s_key'], 's_key_in', $server->ds['s_key_in']); logError('notrace', 'received temp outbound key: ' . $data['mykey'] . ' /// ' . dumpArray($server->ds)); // now, get origin confirmation $confirmMsg = new HubbubMessage('trust_sendkey2'); $confirmMsg->author($server->localEntity()); $confirmMsg->owner($server->entity()); $confirmMsg->data['mykey'] = $server->ds['s_key_in']; $responseData = $confirmMsg->sendtourl($server->ds['s_url'], $server->ds['s_newkey_out']); if ($responseData['result'] == 'OK') { /* we need to reload, because the server record might have changed in the meantime */ $server = new HubbubServer($serverUrl, true); // okay, the remote server really sent the original message $server->ds['s_key_out'] = $server->ds['s_newkey_out']; $server->ds['s_status'] = 'OK'; DB_UpdateField('servers', $server->ds['s_key'], 's_key_out', $data['mykey']); $msg->ok(); } else { // this didn't work $server->ds['s_newkey_out'] = ''; $msg->fail('unsuccessful trust_sendkey2: ' . $responseData['reason']); } } return true; }
function msg_trust_sendkey1() { // make a trust_sendkey1 message $msg = new HubbubMessage('trust_sendkey1'); $msg->to($this->entity()); $msg->data['author'] = $this->localEntity(); $this->save(); // make new key if there is none $this->ds['s_key_in'] = getDefault($this->ds['s_key_in'], randomHashId()); if ($this->ds['s_url'] != '') { DB_UpdateField('servers', $this->ds['s_key'], 's_key_in', $this->ds['s_key_in']); } $msg->data['mykey'] = $this->ds['s_key_in']; // we need to save at this point because the other server will try to make a trust_sendkey2-request in the meantime // send message to other server $responseData = $msg->sendToUrl($this->ds['s_url']); if ($responseData['result'] == 'OK') { $this->ds['s_status'] = 'OK'; $ret = result_ok(); } else { $this->ds['s_status'] = 'fail'; $this->ds['s_key_in'] = ''; logError('notrace', '[OUT] trust_sendkey1 failed, server ' . $this->ds['s_url'] . ' says: ' . getDefault($responseData['reason'], $responseData['result'])); $ret = result_fail('trust_sendkey1 failed: ' . getDefault($responseData['reason'], $responseData['result'])); } return $ret; }