4 * @file mod/dfrn_notify.php
5 * @brief The dfrn notify endpoint
6 * @see PDF with dfrn specs: https://github.com/friendica/friendica/blob/master/spec/dfrn2.pdf
10 use Friendica\Core\Config;
11 use Friendica\Core\Logger;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Contact;
15 use Friendica\Model\User;
16 use Friendica\Protocol\DFRN;
17 use Friendica\Protocol\Diaspora;
18 use Friendica\Util\Strings;
20 function dfrn_notify_post(App $a) {
21 Logger::log(__function__, Logger::TRACE);
23 $postdata = file_get_contents('php://input');
25 if (empty($_POST) || !empty($postdata)) {
26 $data = json_decode($postdata);
27 if (is_object($data)) {
28 $nick = defaults($a->argv, 1, '');
30 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
31 if (!DBA::isResult($user)) {
32 System::httpExit(500);
34 dfrn_dispatch_private($user, $postdata);
35 } elseif (!dfrn_dispatch_public($postdata)) {
36 require_once 'mod/salmon.php';
37 salmon_post($a, $postdata);
41 $dfrn_id = (!empty($_POST['dfrn_id']) ? Strings::escapeTags(trim($_POST['dfrn_id'])) : '');
42 $dfrn_version = (!empty($_POST['dfrn_version']) ? (float) $_POST['dfrn_version'] : 2.0);
43 $challenge = (!empty($_POST['challenge']) ? Strings::escapeTags(trim($_POST['challenge'])) : '');
44 $data = defaults($_POST, 'data', '');
45 $key = defaults($_POST, 'key', '');
46 $rino_remote = (!empty($_POST['rino']) ? intval($_POST['rino']) : 0);
47 $dissolve = (!empty($_POST['dissolve']) ? intval($_POST['dissolve']) : 0);
48 $perm = (!empty($_POST['perm']) ? Strings::escapeTags(trim($_POST['perm'])) : 'r');
49 $ssl_policy = (!empty($_POST['ssl_policy']) ? Strings::escapeTags(trim($_POST['ssl_policy'])): 'none');
50 $page = (!empty($_POST['page']) ? intval($_POST['page']) : 0);
52 $forum = (($page == 1) ? 1 : 0);
53 $prv = (($page == 2) ? 1 : 0);
56 if ($dfrn_version >= 2.21) {
57 $writable = (($perm === 'rw') ? 1 : 0);
61 if (strpos($dfrn_id, ':') == 1) {
62 $direction = intval(substr($dfrn_id, 0, 1));
63 $dfrn_id = substr($dfrn_id, 2);
66 if (!DBA::exists('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge])) {
67 Logger::log('could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
68 System::xmlExit(3, 'Could not match challenge');
71 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
73 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $a->argv[1]]);
74 if (!DBA::isResult($user)) {
75 Logger::log('User not found for nickname ' . $a->argv[1]);
76 System::xmlExit(3, 'User not found');
79 // find the local user who owns this relationship.
83 $condition = ["(`issued-id` = ? OR `dfrn-id` = ?) AND `uid` = ?", $dfrn_id, $dfrn_id, $user['uid']];
86 $condition = ['issued-id' => $dfrn_id, 'duplex' => true, 'uid' => $user['uid']];
89 $condition = ['dfrn-id' => $dfrn_id, 'duplex' => true, 'uid' => $user['uid']];
92 System::xmlExit(3, 'Invalid direction');
96 $contact = DBA::selectFirst('contact', ['id'], $condition);
97 if (!DBA::isResult($contact)) {
98 Logger::log('contact not found for dfrn_id ' . $dfrn_id);
99 System::xmlExit(3, 'Contact not found');
102 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
103 $importer = DFRN::getImporter($contact['id'], $user['uid']);
105 if ((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
106 $fields = ['writable' => ($writable == (-1)) ? $importer['writable'] : $writable,
107 'forum' => $forum, 'prv' => $prv];
108 DBA::update('contact', $fields, ['id' => $importer['id']]);
110 if ($writable != (-1)) {
111 $importer['writable'] = $writable;
113 $importer['forum'] = $page;
117 // if contact's ssl policy changed, update our links
119 $importer = Contact::updateSslPolicy($importer, $ssl_policy);
121 Logger::log('data: ' . $data, Logger::DATA);
123 if ($dissolve == 1) {
124 // Relationship is dissolved permanently
125 Contact::remove($importer['id']);
126 Logger::log('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
127 System::xmlExit(0, 'relationship dissolved');
130 $rino = Config::get('system', 'rino_encrypt');
131 $rino = intval($rino);
135 // if local rino is lower than remote rino, abort: should not happen!
136 // but only for $remote_rino > 1, because old code did't send rino version
137 if ($rino_remote > 1 && $rino < $rino_remote) {
138 Logger::log("rino version '$rino_remote' is lower than supported '$rino'");
139 System::xmlExit(0, "rino version '$rino_remote' is lower than supported '$rino'");
142 $rawkey = hex2bin(trim($key));
143 Logger::log('rino: md5 raw key: ' . md5($rawkey), Logger::DATA);
147 if ($dfrn_version >= 2.1) {
148 if (($importer['duplex'] && strlen($importer['cprvkey'])) || !strlen($importer['cpubkey'])) {
149 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
151 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
154 if (($importer['duplex'] && strlen($importer['cpubkey'])) || !strlen($importer['cprvkey'])) {
155 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
157 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
161 switch ($rino_remote) {
164 // we got a key. old code send only the key, without RINO version.
165 // we assume RINO 1 if key and no RINO version
166 $data = DFRN::aesDecrypt(hex2bin($data), $final_key);
169 Logger::log("rino: invalid sent version '$rino_remote'");
170 System::xmlExit(0, "Invalid sent version '$rino_remote'");
173 Logger::log('rino: decrypted data: ' . $data, Logger::DATA);
176 Logger::log('Importing post from ' . $importer['addr'] . ' to ' . $importer['nickname'] . ' with the RINO ' . $rino_remote . ' encryption.', Logger::DEBUG);
178 $ret = DFRN::import($data, $importer);
179 System::xmlExit($ret, 'Processed');
184 function dfrn_dispatch_public($postdata)
186 $msg = Diaspora::decodeRaw([], $postdata, true);
188 // We have to fail silently to be able to hand it over to the salmon parser
192 // Fetch the corresponding public contact
193 $contact = Contact::getDetailsByAddr($msg['author'], 0);
195 Logger::log('Contact not found for address ' . $msg['author']);
196 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
199 $importer = DFRN::getImporter($contact['id']);
201 // This should never fail
202 if (empty($importer)) {
203 Logger::log('Contact not found for address ' . $msg['author']);
204 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
207 Logger::log('Importing post from ' . $msg['author'] . ' with the public envelope.', Logger::DEBUG);
209 // Now we should be able to import it
210 $ret = DFRN::import($msg['message'], $importer);
211 System::xmlExit($ret, 'Done');
214 function dfrn_dispatch_private($user, $postdata)
216 $msg = Diaspora::decodeRaw($user, $postdata);
218 System::xmlExit(4, 'Unable to parse message');
221 // Check if the user has got this contact
222 $cid = Contact::getIdForURL($msg['author'], $user['uid']);
224 // Otherwise there should be a public contact
225 $cid = Contact::getIdForURL($msg['author']);
227 Logger::log('Contact not found for address ' . $msg['author']);
228 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
232 $importer = DFRN::getImporter($cid, $user['uid']);
234 // This should never fail
235 if (empty($importer)) {
236 Logger::log('Contact not found for address ' . $msg['author']);
237 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
240 Logger::log('Importing post from ' . $msg['author'] . ' to ' . $user['nickname'] . ' with the private envelope.', Logger::DEBUG);
242 // Now we should be able to import it
243 $ret = DFRN::import($msg['message'], $importer);
244 System::xmlExit($ret, 'Done');
247 function dfrn_notify_content(App $a) {
249 if (!empty($_GET['dfrn_id'])) {
252 * initial communication from external contact, $direction is their direction.
253 * If this is a duplex communication, ours will be the opposite.
256 $dfrn_id = Strings::escapeTags(trim($_GET['dfrn_id']));
257 $rino_remote = (!empty($_GET['rino']) ? intval($_GET['rino']) : 0);
261 Logger::log('new notification dfrn_id=' . $dfrn_id);
264 if (strpos($dfrn_id,':') == 1) {
265 $direction = intval(substr($dfrn_id,0,1));
266 $dfrn_id = substr($dfrn_id,2);
269 $hash = Strings::getRandomHex();
273 DBA::delete('challenge', ["`expire` < ?", time()]);
275 $fields = ['challenge' => $hash, 'dfrn-id' => $dfrn_id, 'expire' => time() + 90,
276 'type' => $type, 'last_update' => $last_update];
277 DBA::insert('challenge', $fields);
279 Logger::log('challenge=' . $hash, Logger::DATA);
281 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $a->argv[1]]);
282 if (!DBA::isResult($user)) {
283 Logger::log('User not found for nickname ' . $a->argv[1]);
288 switch ($direction) {
290 $condition = ["(`issued-id` = ? OR `dfrn-id` = ?) AND `uid` = ?", $dfrn_id, $dfrn_id, $user['uid']];
294 $condition = ['issued-id' => $dfrn_id, 'duplex' => true, 'uid' => $user['uid']];
295 $my_id = '1:' . $dfrn_id;
298 $condition = ['dfrn-id' => $dfrn_id, 'duplex' => true, 'uid' => $user['uid']];
299 $my_id = '0:' . $dfrn_id;
307 $contact = DBA::selectFirst('contact', ['id'], $condition);
308 if (!DBA::isResult($contact)) {
309 Logger::log('contact not found for dfrn_id ' . $dfrn_id);
310 System::xmlExit(3, 'Contact not found');
313 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
314 $importer = DFRN::getImporter($contact['id'], $user['uid']);
315 if (empty($importer)) {
316 Logger::log('No importer data found for user ' . $a->argv[1] . ' and contact ' . $dfrn_id);
320 Logger::log("Remote rino version: ".$rino_remote." for ".$importer["url"], Logger::DATA);
324 $id_str = $my_id . '.' . mt_rand(1000,9999);
326 $prv_key = trim($importer['cprvkey']);
327 $pub_key = trim($importer['cpubkey']);
328 $dplx = intval($importer['duplex']);
330 if (($dplx && strlen($prv_key)) || (strlen($prv_key) && !strlen($pub_key))) {
331 openssl_private_encrypt($hash, $challenge, $prv_key);
332 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
333 } elseif (strlen($pub_key)) {
334 openssl_public_encrypt($hash, $challenge, $pub_key);
335 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
337 /// @TODO these kind of else-blocks are making the code harder to understand
341 $challenge = bin2hex($challenge);
342 $encrypted_id = bin2hex($encrypted_id);
345 $rino = Config::get('system', 'rino_encrypt');
346 $rino = intval($rino);
348 Logger::log("Local rino version: ". $rino, Logger::DATA);
350 // if requested rino is lower than enabled local rino, lower local rino version
351 // if requested rino is higher than enabled local rino, reply with local rino
352 if ($rino_remote < $rino) {
353 $rino = $rino_remote;
356 if (($importer['rel'] && ($importer['rel'] != Contact::SHARING)) || ($importer['page-flags'] == User::PAGE_FLAGS_COMMUNITY)) {
362 header("Content-type: text/xml");
364 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
365 . '<dfrn_notify>' . "\r\n"
366 . "\t" . '<status>' . $status . '</status>' . "\r\n"
367 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
368 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
369 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
370 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
371 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
372 . '</dfrn_notify>' . "\r\n";