]> git.mxchange.org Git - friendica.git/blob - mod/salmon.php
Some more "exit" replaced
[friendica.git] / mod / salmon.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Core\Logger;
24 use Friendica\Core\Protocol;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\GServer;
28 use Friendica\Model\Post;
29 use Friendica\Protocol\ActivityNamespace;
30 use Friendica\Protocol\OStatus;
31 use Friendica\Protocol\Salmon;
32 use Friendica\Util\Crypto;
33 use Friendica\Util\Network;
34 use Friendica\Util\Strings;
35
36 function salmon_post(App $a, $xml = '') {
37
38         if (empty($xml)) {
39                 $xml = Network::postdata();
40         }
41
42         Logger::debug('new salmon ' . $xml);
43
44         $nick = trim(DI::args()->getArgv()[1] ?? '');
45
46         $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
47         if (! DBA::isResult($importer)) {
48                 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
49         }
50
51         // parse the xml
52
53         $dom = simplexml_load_string($xml,'SimpleXMLElement',0, ActivityNamespace::SALMON_ME);
54
55         $base = null;
56
57         // figure out where in the DOM tree our data is hiding
58         if (!empty($dom->provenance->data))
59                 $base = $dom->provenance;
60         elseif (!empty($dom->env->data))
61                 $base = $dom->env;
62         elseif (!empty($dom->data))
63                 $base = $dom;
64
65         if (empty($base)) {
66                 Logger::notice('unable to locate salmon data in xml');
67                 throw new \Friendica\Network\HTTPException\BadRequestException();
68         }
69
70         // Stash the signature away for now. We have to find their key or it won't be good for anything.
71
72
73         $signature = Strings::base64UrlDecode($base->sig);
74
75         // unpack the  data
76
77         // strip whitespace so our data element will return to one big base64 blob
78         $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
79
80         // stash away some other stuff for later
81
82         $type = $base->data[0]->attributes()->type[0];
83         $keyhash = $base->sig[0]->attributes()->keyhash[0] ?? '';
84         $encoding = $base->encoding;
85         $alg = $base->alg;
86
87         // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
88         // flavour we have. We'll try and verify it regardless.
89
90         $stnet_signed_data = $data;
91
92         $signed_data = $data  . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
93
94         $compliant_format = str_replace('=', '', $signed_data);
95
96
97         // decode the data
98         $data = Strings::base64UrlDecode($data);
99
100         $author = OStatus::salmonAuthor($data, $importer);
101         $author_link = $author["author-link"];
102
103         if(! $author_link) {
104                 Logger::notice('Could not retrieve author URI.');
105                 throw new \Friendica\Network\HTTPException\BadRequestException();
106         }
107
108         // Once we have the author URI, go to the web and try to find their public key
109
110         Logger::notice('Fetching key for ' . $author_link);
111
112         $key = Salmon::getKey($author_link, $keyhash);
113
114         if(! $key) {
115                 Logger::notice('Could not retrieve author key.');
116                 throw new \Friendica\Network\HTTPException\BadRequestException();
117         }
118
119         $key_info = explode('.',$key);
120
121         $m = Strings::base64UrlDecode($key_info[1]);
122         $e = Strings::base64UrlDecode($key_info[2]);
123
124         Logger::info('key details', ['info' => $key_info]);
125
126         $pubkey = Crypto::meToPem($m, $e);
127
128         // We should have everything we need now. Let's see if it verifies.
129
130         // Try GNU Social format
131         $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
132         $mode = 1;
133
134         if (! $verify) {
135                 Logger::notice('message did not verify using protocol. Trying compliant format.');
136                 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
137                 $mode = 2;
138         }
139
140         if (! $verify) {
141                 Logger::notice('message did not verify using padding. Trying old statusnet format.');
142                 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
143                 $mode = 3;
144         }
145
146         if (! $verify) {
147                 Logger::notice('Message did not verify. Discarding.');
148                 throw new \Friendica\Network\HTTPException\BadRequestException();
149         }
150
151         Logger::notice('Message verified with mode '.$mode);
152
153
154         /*
155         *
156         * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
157         *
158         */
159
160         $contact = DBA::selectFirst('contact', [], ["`network` IN (?, ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `uid` = ?",
161                 Protocol::OSTATUS, Protocol::DFRN, Strings::normaliseLink($author_link), $author_link, Strings::normaliseLink($author_link), $importer['uid']]);
162
163         if (!empty($contact['gsid'])) {
164                 GServer::setProtocol($contact['gsid'], Post\DeliveryData::OSTATUS);
165         }
166
167         // Have we ignored the person?
168         // If so we can not accept this post.
169
170         if (!empty($contact['blocked'])) {
171                 Logger::notice('Ignoring this author.');
172                 throw new \Friendica\Network\HTTPException\AcceptedException();
173         }
174
175         // Placeholder for hub discovery.
176         $hub = '';
177
178         $contact = $contact ?: [];
179
180         OStatus::import($data, $importer, $contact, $hub);
181
182         throw new \Friendica\Network\HTTPException\OKException();
183 }