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
8 require_once('include/items.php');
9 require_once('include/dfrn.php');
10 require_once('include/event.php');
12 require_once('library/defuse/php-encryption-1.2.1/Crypto.php');
14 function dfrn_notify_post(App $a) {
15 logger(__function__, LOGGER_TRACE);
16 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
17 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
18 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
19 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
20 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
21 $rino_remote = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0);
22 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
23 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
24 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
25 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
27 $forum = (($page == 1) ? 1 : 0);
28 $prv = (($page == 2) ? 1 : 0);
31 if($dfrn_version >= 2.21) {
32 $writable = (($perm === 'rw') ? 1 : 0);
36 if(strpos($dfrn_id,':') == 1) {
37 $direction = intval(substr($dfrn_id,0,1));
38 $dfrn_id = substr($dfrn_id,2);
41 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
45 if (! dbm::is_result($r)) {
46 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
50 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
55 // find the local user who owns this relationship.
60 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
63 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
66 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
73 // be careful - $importer will contain both the contact information for the contact
74 // sending us the post, and also the user information for the person receiving it.
75 // since they are mixed together, it is easy to get them confused.
77 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
78 `contact`.`pubkey` AS `cpubkey`,
79 `contact`.`prvkey` AS `cprvkey`,
80 `contact`.`thumb` AS `thumb`,
81 `contact`.`url` as `url`,
82 `contact`.`name` as `senderName`,
85 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
86 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
87 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
91 if (! dbm::is_result($r)) {
92 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
97 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
101 logger("Remote rino version: ".$rino_remote." for ".$importer["url"], LOGGER_DEBUG);
103 if((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
104 q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d",
105 intval(($writable == (-1)) ? $importer['writable'] : $writable),
108 intval($importer['id'])
110 if($writable != (-1))
111 $importer['writable'] = $writable;
112 $importer['forum'] = $page;
116 // if contact's ssl policy changed, update our links
118 fix_contact_ssl_policy($importer,$ssl_policy);
120 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
121 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
126 * Relationship is dissolved permanently
129 require_once('include/Contact.php');
130 contact_remove($importer['id']);
131 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
137 // If we are setup as a soapbox we aren't accepting input from this person
138 // This behaviour is deactivated since it really doesn't make sense to even disallow comments
139 // The check if someone is a friend or simply a follower is done in a later place so it needn't to be done here
140 //if($importer['page-flags'] == PAGE_SOAPBOX)
143 $rino = get_config('system','rino_encrypt');
144 $rino = intval($rino);
145 // use RINO1 if mcrypt isn't installed and RINO2 was selected
146 if ($rino == 2 && !function_exists('mcrypt_create_iv')) {
150 logger("Local rino version: ". $rino, LOGGER_DEBUG);
154 // if local rino is lower than remote rino, abort: should not happen!
155 // but only for $remote_rino > 1, because old code did't send rino version
156 if ($rino_remote_version > 1 && $rino < $rino_remote) {
157 logger("rino version '$rino_remote' is lower than supported '$rino'");
158 xml_status(0,"rino version '$rino_remote' is lower than supported '$rino'");
161 $rawkey = hex2bin(trim($key));
162 logger('rino: md5 raw key: ' . md5($rawkey));
165 if($dfrn_version >= 2.1) {
166 if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
167 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
170 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
174 if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
175 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
178 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
182 #logger('rino: received key : ' . $final_key);
184 switch($rino_remote) {
187 // we got a key. old code send only the key, without RINO version.
188 // we assume RINO 1 if key and no RINO version
189 $data = aes_decrypt(hex2bin($data),$final_key);
193 $data = Crypto::decrypt(hex2bin($data),$final_key);
194 } catch (InvalidCiphertext $ex) { // VERY IMPORTANT
196 // 1. The ciphertext was modified by the attacker,
197 // 2. The key is wrong, or
198 // 3. $ciphertext is not a valid ciphertext or was corrupted.
200 logger('The ciphertext has been tampered with!');
201 xml_status(0,'The ciphertext has been tampered with!');
202 } catch (Ex\CryptoTestFailed $ex) {
203 logger('Cannot safely perform dencryption');
204 xml_status(0,'CryptoTestFailed');
205 } catch (Ex\CannotPerformOperation $ex) {
206 logger('Cannot safely perform decryption');
207 xml_status(0,'Cannot safely perform decryption');
211 logger("rino: invalid sent verision '$rino_remote'");
216 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
219 $ret = dfrn::import($data, $importer);
226 function dfrn_notify_content(App $a) {
228 if(x($_GET,'dfrn_id')) {
230 // initial communication from external contact, $direction is their direction.
231 // If this is a duplex communication, ours will be the opposite.
233 $dfrn_id = notags(trim($_GET['dfrn_id']));
234 $dfrn_version = (float) $_GET['dfrn_version'];
235 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
239 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
242 if(strpos($dfrn_id,':') == 1) {
243 $direction = intval(substr($dfrn_id,0,1));
244 $dfrn_id = substr($dfrn_id,2);
247 $hash = random_string();
251 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
253 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
254 VALUES( '%s', '%s', %d, '%s', '%s' ) ",
257 intval(time() + 90 ),
262 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG);
267 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
271 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
272 $my_id = '1:' . $dfrn_id;
275 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
276 $my_id = '0:' . $dfrn_id;
283 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
284 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
285 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
289 if (! dbm::is_result($r)) {
293 logger("Remote rino version: ".$rino_remote." for ".$r[0]["url"], LOGGER_DEBUG);
297 $id_str = $my_id . '.' . mt_rand(1000,9999);
299 $prv_key = trim($r[0]['prvkey']);
300 $pub_key = trim($r[0]['pubkey']);
301 $dplx = intval($r[0]['duplex']);
303 if ((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
304 openssl_private_encrypt($hash, $challenge, $prv_key);
305 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
306 } elseif (strlen($pub_key)) {
307 openssl_public_encrypt($hash, $challenge, $pub_key);
308 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
310 /// @TODO these kind of else-blocks are making the code harder to understand
314 $challenge = bin2hex($challenge);
315 $encrypted_id = bin2hex($encrypted_id);
318 $rino = get_config('system','rino_encrypt');
319 $rino = intval($rino);
320 // use RINO1 if mcrypt isn't installed and RINO2 was selected
321 /// @TODO Define a code-standard: and/AND/&& are around
322 if ($rino == 2 && !function_exists('mcrypt_create_iv')) {
326 logger("Local rino version: ". $rino, LOGGER_DEBUG);
328 // if requested rino is lower than enabled local rino, lower local rino version
329 // if requested rino is higher than enabled local rino, reply with local rino
330 if ($rino_remote < $rino) {
331 $rino = $rino_remote;
334 if((($r[0]['rel']) && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
340 header("Content-type: text/xml");
342 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
343 . '<dfrn_notify>' . "\r\n"
344 . "\t" . '<status>' . $status . '</status>' . "\r\n"
345 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
346 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
347 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
348 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
349 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
350 . '</dfrn_notify>' . "\r\n" ;