Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

194 lines
5.4 KiB

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