]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
Rino Bugfix: A variable was used that never was fetched
[friendica.git] / mod / dfrn_notify.php
1 <?php
2
3 require_once('library/simplepie/simplepie.inc');
4 require_once('include/items.php');
5 require_once('include/event.php');
6
7 require_once('library/defuse/php-encryption-1.2.1/Crypto.php');
8
9 function dfrn_notify_post(&$a) {
10     logger(__function__, LOGGER_TRACE);
11         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? notags(trim($_POST['dfrn_id']))   : '');
12         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version']    : 2.0);
13         $challenge    = ((x($_POST,'challenge'))    ? notags(trim($_POST['challenge'])) : '');
14         $data         = ((x($_POST,'data'))         ? $_POST['data']                    : '');
15         $key          = ((x($_POST,'key'))          ? $_POST['key']                     : '');
16         $rino_remote  = ((x($_POST,'rino'))         ? intval($_POST['rino'])            :  0);
17         $dissolve     = ((x($_POST,'dissolve'))     ? intval($_POST['dissolve'])        :  0);
18         $perm         = ((x($_POST,'perm'))         ? notags(trim($_POST['perm']))      : 'r');
19         $ssl_policy   = ((x($_POST,'ssl_policy'))   ? notags(trim($_POST['ssl_policy'])): 'none');
20         $page         = ((x($_POST,'page'))         ? intval($_POST['page'])            :  0);
21
22         $forum = (($page == 1) ? 1 : 0);
23         $prv   = (($page == 2) ? 1 : 0);
24
25         $writable = (-1);
26         if($dfrn_version >= 2.21) {
27                 $writable = (($perm === 'rw') ? 1 : 0);
28         }
29
30         $direction = (-1);
31         if(strpos($dfrn_id,':') == 1) {
32                 $direction = intval(substr($dfrn_id,0,1));
33                 $dfrn_id = substr($dfrn_id,2);
34         }
35
36         logger("Remote rino version: ".$rino_remote, LOGGER_DEBUG);
37
38         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
39                 dbesc($dfrn_id),
40                 dbesc($challenge)
41         );
42         if(! count($r)) {
43                 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
44                 xml_status(3);
45         }
46
47         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
48                 dbesc($dfrn_id),
49                 dbesc($challenge)
50         );
51
52         // find the local user who owns this relationship.
53
54         $sql_extra = '';
55         switch($direction) {
56                 case (-1):
57                         $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
58                         break;
59                 case 0:
60                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
61                         break;
62                 case 1:
63                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
64                         break;
65                 default:
66                         xml_status(3);
67                         break; // NOTREACHED
68         }
69
70         // be careful - $importer will contain both the contact information for the contact
71         // sending us the post, and also the user information for the person receiving it.
72         // since they are mixed together, it is easy to get them confused.
73
74         $r = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`,
75                                         `contact`.`pubkey` AS `cpubkey`,
76                                         `contact`.`prvkey` AS `cprvkey`,
77                                         `contact`.`thumb` AS `thumb`,
78                                         `contact`.`url` as `url`,
79                                         `contact`.`name` as `senderName`,
80                                         `user`.*
81                         FROM `contact`
82                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
83                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
84                                 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
85                 dbesc($a->argv[1])
86         );
87
88         if(! count($r)) {
89                 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
90                 xml_status(3);
91                 //NOTREACHED
92         }
93
94         // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
95
96         $importer = $r[0];
97
98         if((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
99                 q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d",
100                         intval(($writable == (-1)) ? $importer['writable'] : $writable),
101                         intval($forum),
102                         intval($prv),
103                         intval($importer['id'])
104                 );
105                 if($writable != (-1))
106                         $importer['writable'] = $writable;
107                 $importer['forum'] = $page;
108         }
109
110
111         // if contact's ssl policy changed, update our links
112
113         fix_contact_ssl_policy($importer,$ssl_policy);
114
115         logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
116         logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
117
118         if($dissolve == 1) {
119
120                 /**
121                  * Relationship is dissolved permanently
122                  */
123
124                 require_once('include/Contact.php');
125                 contact_remove($importer['id']);
126                 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
127                 xml_status(0);
128
129         }
130
131
132         // If we are setup as a soapbox we aren't accepting input from this person
133
134         if($importer['page-flags'] == PAGE_SOAPBOX)
135                 xml_status(0);
136
137         $rino = get_config('system','rino_encrypt');
138         $rino = intval($rino);
139
140         logger("Local rino version: ". $rino, LOGGER_DEBUG);
141
142         if(strlen($key)) {
143
144                 // if local rino is lower than remote rino, abort: should not happen!
145                 // but only for $remote_rino > 1, because old code did't send rino version
146                 if ($rino_remote_version > 1 && $rino < $rino_remote) {
147                         logger("rino version '$rino_remote' is lower than supported '$rino'");
148                         xml_status(0,"rino version '$rino_remote' is lower than supported '$rino'");
149                 }
150
151                 $rawkey = hex2bin(trim($key));
152                 logger('rino: md5 raw key: ' . md5($rawkey));
153                 $final_key = '';
154
155                 if($dfrn_version >= 2.1) {
156                         if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
157                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
158                         }
159                         else {
160                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
161                         }
162                 }
163                 else {
164                         if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
165                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
166                         }
167                         else {
168                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
169                         }
170                 }
171
172                 #logger('rino: received key : ' . $final_key);
173
174                 switch($rino_remote) {
175                         case 0:
176                         case 1:
177                                 // we got a key. old code send only the key, without RINO version.
178                                 // we assume RINO 1 if key and no RINO version
179                                 $data = aes_decrypt(hex2bin($data),$final_key);
180                                 break;
181                         case 2:
182                                 try {
183                                         $data = Crypto::decrypt(hex2bin($data),$final_key);
184                                 } catch (InvalidCiphertext $ex) { // VERY IMPORTANT
185                                         // Either:
186                                         //   1. The ciphertext was modified by the attacker,
187                                         //   2. The key is wrong, or
188                                         //   3. $ciphertext is not a valid ciphertext or was corrupted.
189                                         // Assume the worst.
190                                         logger('The ciphertext has been tampered with!');
191                                         xml_status(0,'The ciphertext has been tampered with!');
192                                 } catch (Ex\CryptoTestFailed $ex) {
193                                         logger('Cannot safely perform dencryption');
194                                         xml_status(0,'CryptoTestFailed');
195                                 } catch (Ex\CannotPerformOperation $ex) {
196                                         logger('Cannot safely perform decryption');
197                                         xml_status(0,'Cannot safely perform decryption');
198                                 }
199                                 break;
200                         default:
201                                 logger("rino: invalid sent verision '$rino_remote'");
202                                 xml_status(0);
203                 }
204
205
206                 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
207         }
208
209         $ret = local_delivery($importer,$data);
210         xml_status($ret);
211
212         // NOTREACHED
213 }
214
215
216 function dfrn_notify_content(&$a) {
217
218         if(x($_GET,'dfrn_id')) {
219
220                 // initial communication from external contact, $direction is their direction.
221                 // If this is a duplex communication, ours will be the opposite.
222
223                 $dfrn_id = notags(trim($_GET['dfrn_id']));
224                 $dfrn_version = (float) $_GET['dfrn_version'];
225                 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
226                 $type = "";
227                 $last_update = "";
228
229                 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
230
231                 logger("Remote rino version: ".$rino_remote, LOGGER_DEBUG);
232
233                 $direction = (-1);
234                 if(strpos($dfrn_id,':') == 1) {
235                         $direction = intval(substr($dfrn_id,0,1));
236                         $dfrn_id = substr($dfrn_id,2);
237                 }
238
239                 $hash = random_string();
240
241                 $status = 0;
242
243                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
244
245                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
246                         VALUES( '%s', '%s', %d, '%s', '%s' ) ",
247                         dbesc($hash),
248                         dbesc($dfrn_id),
249                         intval(time() + 90 ),
250                         dbesc($type),
251                         dbesc($last_update)
252                 );
253
254                 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG);
255
256                 $sql_extra = '';
257                 switch($direction) {
258                         case (-1):
259                                 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
260                                 $my_id = $dfrn_id;
261                                 break;
262                         case 0:
263                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
264                                 $my_id = '1:' . $dfrn_id;
265                                 break;
266                         case 1:
267                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
268                                 $my_id = '0:' . $dfrn_id;
269                                 break;
270                         default:
271                                 $status = 1;
272                                 break; // NOTREACHED
273                 }
274
275                 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
276                                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
277                                 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
278                                 dbesc($a->argv[1])
279                 );
280
281                 if(! count($r))
282                         $status = 1;
283
284                 $challenge = '';
285                 $encrypted_id = '';
286                 $id_str = $my_id . '.' . mt_rand(1000,9999);
287
288                 $prv_key = trim($r[0]['prvkey']);
289                 $pub_key = trim($r[0]['pubkey']);
290                 $dplx = intval($r[0]['duplex']);
291
292                 if((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
293                         openssl_private_encrypt($hash,$challenge,$prv_key);
294                         openssl_private_encrypt($id_str,$encrypted_id,$prv_key);
295                 }
296                 elseif(strlen($pub_key)) {
297                         openssl_public_encrypt($hash,$challenge,$pub_key);
298                         openssl_public_encrypt($id_str,$encrypted_id,$pub_key);
299                 }
300                 else
301                         $status = 1;
302
303                 $challenge    = bin2hex($challenge);
304                 $encrypted_id = bin2hex($encrypted_id);
305
306
307                 $rino = get_config('system','rino_encrypt');
308                 $rino = intval($rino);
309
310                 logger("Local rino version: ". $rino, LOGGER_DEBUG);
311
312                 // if requested rino is lower than enabled local rino, lower local rino version
313                 // if requested rino is higher than enabled local rino, reply with local rino
314                 if ($rino_remote < $rino) $rino = $rino_remote;
315
316                 if((($r[0]['rel']) && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
317                         $perm = 'rw';
318                 }
319                 else {
320                         $perm = 'r';
321                 }
322
323                 header("Content-type: text/xml");
324
325                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
326                         . '<dfrn_notify>' . "\r\n"
327                         . "\t" . '<status>' . $status . '</status>' . "\r\n"
328                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
329                         . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
330                         . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
331                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
332                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
333                         . '</dfrn_notify>' . "\r\n" ;
334
335                 killme();
336         }
337
338 }