]> git.mxchange.org Git - friendica.git/blob - mod/salmon.php
Merge pull request #6487 from MrPetovan/bug/critical-fixes-after-develop-rebase
[friendica.git] / mod / salmon.php
1 <?php
2 /**
3  * @file mod/salmon.php
4  */
5 use Friendica\App;
6 use Friendica\Core\Logger;
7 use Friendica\Core\PConfig;
8 use Friendica\Core\Protocol;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Contact;
12 use Friendica\Protocol\OStatus;
13 use Friendica\Protocol\Salmon;
14 use Friendica\Util\Crypto;
15 use Friendica\Util\Strings;
16
17 function salmon_post(App $a, $xml = '') {
18
19         if (empty($xml)) {
20                 $xml = file_get_contents('php://input');
21         }
22
23         Logger::log('new salmon ' . $xml, Logger::DATA);
24
25         $nick       = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
26
27         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
28                 DBA::escape($nick)
29         );
30         if (! DBA::isResult($r)) {
31                 System::httpExit(500);
32         }
33
34         $importer = $r[0];
35
36         // parse the xml
37
38         $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
39
40         $base = null;
41
42         // figure out where in the DOM tree our data is hiding
43         if (!empty($dom->provenance->data))
44                 $base = $dom->provenance;
45         elseif (!empty($dom->env->data))
46                 $base = $dom->env;
47         elseif (!empty($dom->data))
48                 $base = $dom;
49
50         if (empty($base)) {
51                 Logger::log('unable to locate salmon data in xml ');
52                 System::httpExit(400);
53         }
54
55         // Stash the signature away for now. We have to find their key or it won't be good for anything.
56
57
58         $signature = Strings::base64UrlDecode($base->sig);
59
60         // unpack the  data
61
62         // strip whitespace so our data element will return to one big base64 blob
63         $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
64
65         // stash away some other stuff for later
66
67         $type = $base->data[0]->attributes()->type[0];
68         $keyhash = $base->sig[0]->attributes()->keyhash[0];
69         $encoding = $base->encoding;
70         $alg = $base->alg;
71
72         // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
73         // flavour we have. We'll try and verify it regardless.
74
75         $stnet_signed_data = $data;
76
77         $signed_data = $data  . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
78
79         $compliant_format = str_replace('=', '', $signed_data);
80
81
82         // decode the data
83         $data = Strings::base64UrlDecode($data);
84
85         $author = OStatus::salmonAuthor($data, $importer);
86         $author_link = $author["author-link"];
87
88         if(! $author_link) {
89                 Logger::log('Could not retrieve author URI.');
90                 System::httpExit(400);
91         }
92
93         // Once we have the author URI, go to the web and try to find their public key
94
95         Logger::log('Fetching key for ' . $author_link);
96
97         $key = Salmon::getKey($author_link, $keyhash);
98
99         if(! $key) {
100                 Logger::log('Could not retrieve author key.');
101                 System::httpExit(400);
102         }
103
104         $key_info = explode('.',$key);
105
106         $m = Strings::base64UrlDecode($key_info[1]);
107         $e = Strings::base64UrlDecode($key_info[2]);
108
109         Logger::log('key details: ' . print_r($key_info,true), Logger::DEBUG);
110
111         $pubkey = Crypto::meToPem($m, $e);
112
113         // We should have everything we need now. Let's see if it verifies.
114
115         // Try GNU Social format
116         $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
117         $mode = 1;
118
119         if (! $verify) {
120                 Logger::log('message did not verify using protocol. Trying compliant format.');
121                 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
122                 $mode = 2;
123         }
124
125         if (! $verify) {
126                 Logger::log('message did not verify using padding. Trying old statusnet format.');
127                 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
128                 $mode = 3;
129         }
130
131         if (! $verify) {
132                 Logger::log('Message did not verify. Discarding.');
133                 System::httpExit(400);
134         }
135
136         Logger::log('Message verified with mode '.$mode);
137
138
139         /*
140         *
141         * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
142         *
143         */
144
145         $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
146                                                 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
147                                                 AND `uid` = %d LIMIT 1",
148                 DBA::escape(Protocol::OSTATUS),
149                 DBA::escape(Protocol::DFRN),
150                 DBA::escape(Strings::normaliseLink($author_link)),
151                 DBA::escape($author_link),
152                 DBA::escape(Strings::normaliseLink($author_link)),
153                 intval($importer['uid'])
154         );
155
156         if (!DBA::isResult($r)) {
157                 Logger::log('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.');
158
159                 if (PConfig::get($importer['uid'], 'system', 'ostatus_autofriend')) {
160                         $result = Contact::createFromProbe($importer['uid'], $author_link);
161
162                         if ($result['success']) {
163                                 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
164                                         AND `uid` = %d LIMIT 1",
165                                         DBA::escape(Protocol::OSTATUS),
166                                         DBA::escape($author_link),
167                                         DBA::escape($author_link),
168                                         intval($importer['uid'])
169                                 );
170                         }
171                 }
172         }
173
174         // Have we ignored the person?
175         // If so we can not accept this post.
176
177         //if((DBA::isResult($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == Contact::FOLLOWER) || ($r[0]['blocked']))) {
178         if (DBA::isResult($r) && $r[0]['blocked']) {
179                 Logger::log('Ignoring this author.');
180                 System::httpExit(202);
181                 // NOTREACHED
182         }
183
184         // Placeholder for hub discovery.
185         $hub = '';
186
187         $contact_rec = ((DBA::isResult($r)) ? $r[0] : []);
188
189         OStatus::import($data, $importer, $contact_rec, $hub);
190
191         System::httpExit(200);
192 }