]> git.mxchange.org Git - friendica.git/blob - mod/salmon.php
Merge remote-tracking branch 'upstream/develop' into 1701-top-level
[friendica.git] / mod / salmon.php
1 <?php
2
3 require_once('include/salmon.php');
4 require_once('include/ostatus.php');
5 require_once('include/crypto.php');
6 require_once('include/items.php');
7 require_once('include/follow.php');
8
9 function salmon_return($val) {
10
11         if($val >= 400)
12                 $err = 'Error';
13         if($val >= 200 && $val < 300)
14                 $err = 'OK';
15
16         logger('mod-salmon returns ' . $val);
17         header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
18         killme();
19
20 }
21
22 function salmon_post(App $a) {
23
24         $xml = file_get_contents('php://input');
25
26         logger('mod-salmon: new salmon ' . $xml, LOGGER_DATA);
27
28         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
29         $mentions   = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
30
31         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
32                 dbesc($nick)
33         );
34         if (! dbm::is_result($r)) {
35                 http_status_exit(500);
36         }
37
38         $importer = $r[0];
39
40         // parse the xml
41
42         $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
43
44         // figure out where in the DOM tree our data is hiding
45
46         if($dom->provenance->data)
47                 $base = $dom->provenance;
48         elseif($dom->env->data)
49                 $base = $dom->env;
50         elseif($dom->data)
51                 $base = $dom;
52
53         if(! $base) {
54                 logger('mod-salmon: unable to locate salmon data in xml ');
55                 http_status_exit(400);
56         }
57
58         // Stash the signature away for now. We have to find their key or it won't be good for anything.
59
60
61         $signature = base64url_decode($base->sig);
62
63         // unpack the  data
64
65         // strip whitespace so our data element will return to one big base64 blob
66         $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
67
68         // stash away some other stuff for later
69
70         $type = $base->data[0]->attributes()->type[0];
71         $keyhash = $base->sig[0]->attributes()->keyhash[0];
72         $encoding = $base->encoding;
73         $alg = $base->alg;
74
75         // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
76         // flavour we have. We'll try and verify it regardless.
77
78         $stnet_signed_data = $data;
79
80         $signed_data = $data  . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
81
82         $compliant_format = str_replace('=','',$signed_data);
83
84
85         // decode the data
86         $data = base64url_decode($data);
87
88         $author = ostatus::salmon_author($data,$importer);
89         $author_link = $author["author-link"];
90
91         if(! $author_link) {
92                 logger('mod-salmon: Could not retrieve author URI.');
93                 http_status_exit(400);
94         }
95
96         // Once we have the author URI, go to the web and try to find their public key
97
98         logger('mod-salmon: Fetching key for ' . $author_link);
99
100         $key = get_salmon_key($author_link,$keyhash);
101
102         if(! $key) {
103                 logger('mod-salmon: Could not retrieve author key.');
104                 http_status_exit(400);
105         }
106
107         $key_info = explode('.',$key);
108
109         $m = base64url_decode($key_info[1]);
110         $e = base64url_decode($key_info[2]);
111
112         logger('mod-salmon: key details: ' . print_r($key_info,true), LOGGER_DEBUG);
113
114         $pubkey = metopem($m,$e);
115
116         // We should have everything we need now. Let's see if it verifies.
117
118         $verify = rsa_verify($compliant_format,$signature,$pubkey);
119
120         if(! $verify) {
121                 logger('mod-salmon: message did not verify using protocol. Trying padding hack.');
122             $verify = rsa_verify($signed_data,$signature,$pubkey);
123         }
124
125         if(! $verify) {
126                 logger('mod-salmon: message did not verify using padding. Trying old statusnet hack.');
127             $verify = rsa_verify($stnet_signed_data,$signature,$pubkey);
128         }
129
130         if(! $verify) {
131                 logger('mod-salmon: Message did not verify. Discarding.');
132                 http_status_exit(400);
133         }
134
135         logger('mod-salmon: Message verified.');
136
137
138         /*
139         *
140         * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
141         *
142         */
143
144         $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
145                                                 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
146                                                 AND `uid` = %d LIMIT 1",
147                 dbesc(NETWORK_OSTATUS),
148                 dbesc(NETWORK_DFRN),
149                 dbesc(normalise_link($author_link)),
150                 dbesc($author_link),
151                 dbesc(normalise_link($author_link)),
152                 intval($importer['uid'])
153         );
154         if (! dbm::is_result($r)) {
155                 logger('mod-salmon: Author unknown to us.');
156                 if(get_pconfig($importer['uid'],'system','ostatus_autofriend')) {
157                         $result = new_contact($importer['uid'],$author_link);
158                         if($result['success']) {
159                                 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
160                                         AND `uid` = %d LIMIT 1",
161                                         dbesc(NETWORK_OSTATUS),
162                                         dbesc($author_link),
163                                         dbesc($author_link),
164                                         intval($importer['uid'])
165                                 );
166                         }
167                 }
168         }
169
170         // Have we ignored the person?
171         // If so we can not accept this post.
172
173         //if((dbm::is_result($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
174         if (dbm::is_result($r) && $r[0]['blocked']) {
175                 logger('mod-salmon: Ignoring this author.');
176                 http_status_exit(202);
177                 // NOTREACHED
178         }
179
180         // Placeholder for hub discovery.
181         $hub = '';
182
183         $contact_rec = ((dbm::is_result($r)) ? $r[0] : null);
184
185         ostatus::import($data,$importer,$contact_rec, $hub);
186
187         http_status_exit(200);
188 }