4 * @file mod/dfrn_notify.php
5 * 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;
15 use Friendica\Model\Contact;
16 use Friendica\Model\User;
17 use Friendica\Protocol\DFRN;
18 use Friendica\Protocol\Diaspora;
19 use Friendica\Util\Network;
20 use Friendica\Util\Strings;
22 function dfrn_notify_post(App $a) {
23 Logger::log(__function__, Logger::TRACE);
25 $postdata = Network::postdata();
27 if (empty($_POST) || !empty($postdata)) {
28 $data = json_decode($postdata);
29 if (is_object($data)) {
30 $nick = $a->argv[1] ?? '';
32 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
33 if (!DBA::isResult($user)) {
34 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
36 dfrn_dispatch_private($user, $postdata);
37 } elseif (!dfrn_dispatch_public($postdata)) {
38 require_once 'mod/salmon.php';
39 salmon_post($a, $postdata);
43 $dfrn_id = (!empty($_POST['dfrn_id']) ? Strings::escapeTags(trim($_POST['dfrn_id'])) : '');
44 $dfrn_version = (!empty($_POST['dfrn_version']) ? (float) $_POST['dfrn_version'] : 2.0);
45 $challenge = (!empty($_POST['challenge']) ? Strings::escapeTags(trim($_POST['challenge'])) : '');
46 $data = $_POST['data'] ?? '';
47 $key = $_POST['key'] ?? '';
48 $rino_remote = (!empty($_POST['rino']) ? intval($_POST['rino']) : 0);
49 $dissolve = (!empty($_POST['dissolve']) ? intval($_POST['dissolve']) : 0);
50 $perm = (!empty($_POST['perm']) ? Strings::escapeTags(trim($_POST['perm'])) : 'r');
51 $ssl_policy = (!empty($_POST['ssl_policy']) ? Strings::escapeTags(trim($_POST['ssl_policy'])): 'none');
52 $page = (!empty($_POST['page']) ? intval($_POST['page']) : 0);
54 $forum = (($page == 1) ? 1 : 0);
55 $prv = (($page == 2) ? 1 : 0);
58 if ($dfrn_version >= 2.21) {
59 $writable = (($perm === 'rw') ? 1 : 0);
63 if (strpos($dfrn_id, ':') == 1) {
64 $direction = intval(substr($dfrn_id, 0, 1));
65 $dfrn_id = substr($dfrn_id, 2);
68 if (!DBA::exists('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge])) {
69 Logger::log('could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
70 System::xmlExit(3, 'Could not match challenge');
73 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
75 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $a->argv[1]]);
76 if (!DBA::isResult($user)) {
77 Logger::log('User not found for nickname ' . $a->argv[1]);
78 System::xmlExit(3, 'User not found');
81 // find the local user who owns this relationship.
85 $condition = ["(`issued-id` = ? OR `dfrn-id` = ?) AND `uid` = ?", $dfrn_id, $dfrn_id, $user['uid']];
88 $condition = ['issued-id' => $dfrn_id, 'duplex' => true, 'uid' => $user['uid']];
91 $condition = ['dfrn-id' => $dfrn_id, 'duplex' => true, 'uid' => $user['uid']];
94 System::xmlExit(3, 'Invalid direction');
98 $contact = DBA::selectFirst('contact', ['id'], $condition);
99 if (!DBA::isResult($contact)) {
100 Logger::log('contact not found for dfrn_id ' . $dfrn_id);
101 System::xmlExit(3, 'Contact not found');
104 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
105 $importer = DFRN::getImporter($contact['id'], $user['uid']);
107 if ((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
108 $fields = ['writable' => ($writable == (-1)) ? $importer['writable'] : $writable,
109 'forum' => $forum, 'prv' => $prv];
110 DBA::update('contact', $fields, ['id' => $importer['id']]);
112 if ($writable != (-1)) {
113 $importer['writable'] = $writable;
115 $importer['forum'] = $page;
119 // if contact's ssl policy changed, update our links
121 $importer = Contact::updateSslPolicy($importer, $ssl_policy);
123 Logger::log('data: ' . $data, Logger::DATA);
125 if ($dissolve == 1) {
126 // Relationship is dissolved permanently
127 Contact::remove($importer['id']);
128 Logger::log('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
129 System::xmlExit(0, 'relationship dissolved');
132 $rino = DI::config()->get('system', 'rino_encrypt');
133 $rino = intval($rino);
137 // if local rino is lower than remote rino, abort: should not happen!
138 // but only for $remote_rino > 1, because old code did't send rino version
139 if ($rino_remote > 1 && $rino < $rino_remote) {
140 Logger::log("rino version '$rino_remote' is lower than supported '$rino'");
141 System::xmlExit(0, "rino version '$rino_remote' is lower than supported '$rino'");
144 $rawkey = hex2bin(trim($key));
145 Logger::log('rino: md5 raw key: ' . md5($rawkey), Logger::DATA);
149 if ($dfrn_version >= 2.1) {
150 if (($importer['duplex'] && strlen($importer['cprvkey'])) || !strlen($importer['cpubkey'])) {
151 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
153 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
156 if (($importer['duplex'] && strlen($importer['cpubkey'])) || !strlen($importer['cprvkey'])) {
157 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
159 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
163 switch ($rino_remote) {
166 // we got a key. old code send only the key, without RINO version.
167 // we assume RINO 1 if key and no RINO version
168 $data = DFRN::aesDecrypt(hex2bin($data), $final_key);
171 Logger::log("rino: invalid sent version '$rino_remote'");
172 System::xmlExit(0, "Invalid sent version '$rino_remote'");
175 Logger::log('rino: decrypted data: ' . $data, Logger::DATA);
178 Logger::log('Importing post from ' . $importer['addr'] . ' to ' . $importer['nickname'] . ' with the RINO ' . $rino_remote . ' encryption.', Logger::DEBUG);
180 $ret = DFRN::import($data, $importer);
181 System::xmlExit($ret, 'Processed');
186 function dfrn_dispatch_public($postdata)
188 $msg = Diaspora::decodeRaw($postdata, '', true);
190 // We have to fail silently to be able to hand it over to the salmon parser
194 // Fetch the corresponding public contact
195 $contact_id = Contact::getIdForURL($msg['author']);
196 if (empty($contact_id)) {
197 Logger::log('Contact not found for address ' . $msg['author']);
198 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
201 $importer = DFRN::getImporter($contact_id);
203 // This should never fail
204 if (empty($importer)) {
205 Logger::log('Contact not found for address ' . $msg['author']);
206 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
209 Logger::log('Importing post from ' . $msg['author'] . ' with the public envelope.', Logger::DEBUG);
211 // Now we should be able to import it
212 $ret = DFRN::import($msg['message'], $importer);
213 System::xmlExit($ret, 'Done');
216 function dfrn_dispatch_private($user, $postdata)
218 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
220 System::xmlExit(4, 'Unable to parse message');
223 // Check if the user has got this contact
224 $cid = Contact::getIdForURL($msg['author'], $user['uid']);
226 // Otherwise there should be a public contact
227 $cid = Contact::getIdForURL($msg['author']);
229 Logger::log('Contact not found for address ' . $msg['author']);
230 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
234 $importer = DFRN::getImporter($cid, $user['uid']);
236 // This should never fail
237 if (empty($importer)) {
238 Logger::log('Contact not found for address ' . $msg['author']);
239 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
242 Logger::log('Importing post from ' . $msg['author'] . ' to ' . $user['nickname'] . ' with the private envelope.', Logger::DEBUG);
244 // Now we should be able to import it
245 $ret = DFRN::import($msg['message'], $importer);
246 System::xmlExit($ret, 'Done');
249 function dfrn_notify_content(App $a) {
251 if (!empty($_GET['dfrn_id'])) {
254 * initial communication from external contact, $direction is their direction.
255 * If this is a duplex communication, ours will be the opposite.
258 $dfrn_id = Strings::escapeTags(trim($_GET['dfrn_id']));
259 $rino_remote = (!empty($_GET['rino']) ? intval($_GET['rino']) : 0);
263 Logger::log('new notification dfrn_id=' . $dfrn_id);
266 if (strpos($dfrn_id,':') == 1) {
267 $direction = intval(substr($dfrn_id,0,1));
268 $dfrn_id = substr($dfrn_id,2);
271 $hash = Strings::getRandomHex();
275 DBA::delete('challenge', ["`expire` < ?", time()]);
277 $fields = ['challenge' => $hash, 'dfrn-id' => $dfrn_id, 'expire' => time() + 90,
278 'type' => $type, 'last_update' => $last_update];
279 DBA::insert('challenge', $fields);
281 Logger::log('challenge=' . $hash, Logger::DATA);
283 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $a->argv[1]]);
284 if (!DBA::isResult($user)) {
285 Logger::log('User not found for nickname ' . $a->argv[1]);
290 switch ($direction) {
292 $condition = ["(`issued-id` = ? OR `dfrn-id` = ?) AND `uid` = ?", $dfrn_id, $dfrn_id, $user['uid']];
296 $condition = ['issued-id' => $dfrn_id, 'duplex' => true, 'uid' => $user['uid']];
297 $my_id = '1:' . $dfrn_id;
300 $condition = ['dfrn-id' => $dfrn_id, 'duplex' => true, 'uid' => $user['uid']];
301 $my_id = '0:' . $dfrn_id;
309 $contact = DBA::selectFirst('contact', ['id'], $condition);
310 if (!DBA::isResult($contact)) {
311 Logger::log('contact not found for dfrn_id ' . $dfrn_id);
312 System::xmlExit(3, 'Contact not found');
315 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
316 $importer = DFRN::getImporter($contact['id'], $user['uid']);
317 if (empty($importer)) {
318 Logger::log('No importer data found for user ' . $a->argv[1] . ' and contact ' . $dfrn_id);
322 Logger::log("Remote rino version: ".$rino_remote." for ".$importer["url"], Logger::DATA);
326 $id_str = $my_id . '.' . mt_rand(1000,9999);
328 $prv_key = trim($importer['cprvkey']);
329 $pub_key = trim($importer['cpubkey']);
330 $dplx = intval($importer['duplex']);
332 if (($dplx && strlen($prv_key)) || (strlen($prv_key) && !strlen($pub_key))) {
333 openssl_private_encrypt($hash, $challenge, $prv_key);
334 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
335 } elseif (strlen($pub_key)) {
336 openssl_public_encrypt($hash, $challenge, $pub_key);
337 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
339 /// @TODO these kind of else-blocks are making the code harder to understand
343 $challenge = bin2hex($challenge);
344 $encrypted_id = bin2hex($encrypted_id);
347 $rino = DI::config()->get('system', 'rino_encrypt');
348 $rino = intval($rino);
350 Logger::log("Local rino version: ". $rino, Logger::DATA);
352 // if requested rino is lower than enabled local rino, lower local rino version
353 // if requested rino is higher than enabled local rino, reply with local rino
354 if ($rino_remote < $rino) {
355 $rino = $rino_remote;
358 if (($importer['rel'] && ($importer['rel'] != Contact::SHARING)) || ($importer['page-flags'] == User::PAGE_FLAGS_COMMUNITY)) {
364 header("Content-type: text/xml");
366 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
367 . '<dfrn_notify>' . "\r\n"
368 . "\t" . '<status>' . $status . '</status>' . "\r\n"
369 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
370 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
371 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
372 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
373 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
374 . '</dfrn_notify>' . "\r\n";