From 2b121779a6c782ce0f50b4ef9b7baca82f740a4a Mon Sep 17 00:00:00 2001 From: Adam Magness Date: Sat, 2 Dec 2017 09:32:45 -0500 Subject: [PATCH] Salmon move to src Salmon class, update function name and function calls. Some standards as well. --- include/crypto.php | 10 +- include/follow.php | 4 +- include/items.php | 1 - include/salmon.php | 185 ------------------------------------ mod/post.php | 39 ++++---- mod/receive.php | 17 ++-- mod/salmon.php | 4 +- src/Object/Contact.php | 4 +- src/Protocol/Salmon.php | 204 ++++++++++++++++++++++++++++++++++++++++ src/Worker/Notifier.php | 6 +- src/Worker/Queue.php | 6 +- 11 files changed, 250 insertions(+), 230 deletions(-) delete mode 100644 include/salmon.php create mode 100644 src/Protocol/Salmon.php diff --git a/include/crypto.php b/include/crypto.php index 703439baa1..dfc44c1792 100644 --- a/include/crypto.php +++ b/include/crypto.php @@ -91,9 +91,9 @@ function metopem($m, $e) { return $key; } -function pubrsatome($key,&$m,&$e) { - require_once('library/asn1.php'); - require_once('include/salmon.php'); +function pubrsatome($key, &$m, &$e) +{ + require_once 'library/asn1.php'; $lines = explode("\n", $key); unset($lines[0]); @@ -117,8 +117,8 @@ function pemtorsa($key) { return metorsa($m, $e); } -function pemtome($key, &$m, &$e) { - require_once('include/salmon.php'); +function pemtome($key, &$m, &$e) +{ $lines = explode("\n", $key); unset($lines[0]); unset($lines[count($lines)]); diff --git a/include/follow.php b/include/follow.php index ac07c4085e..14b9c68ed5 100644 --- a/include/follow.php +++ b/include/follow.php @@ -12,9 +12,9 @@ use Friendica\Object\Contact; use Friendica\Protocol\Diaspora; use Friendica\Protocol\OStatus; use Friendica\Protocol\PortableContact; +use Friendica\Protocol\Salmon; require_once 'include/group.php'; -require_once 'include/salmon.php'; function update_contact($id) { /* @@ -268,7 +268,7 @@ function new_contact($uid, $url, $interactive = false, $network = '') { $item['verb'] = ACTIVITY_FOLLOW; $item['follow'] = $contact["url"]; $slap = OStatus::salmon($item, $r[0]); - slapper($r[0], $contact['notify'], $slap); + Salmon::slapper($r[0], $contact['notify'], $slap); } if ($contact['network'] == NETWORK_DIASPORA) { diff --git a/include/items.php b/include/items.php index 7cbcfaaaf5..36a7ac2a8c 100644 --- a/include/items.php +++ b/include/items.php @@ -17,7 +17,6 @@ use Friendica\Util\Lock; require_once 'include/bbcode.php'; require_once 'include/oembed.php'; -require_once 'include/salmon.php'; require_once 'include/crypto.php'; require_once 'include/tags.php'; require_once 'include/files.php'; diff --git a/include/salmon.php b/include/salmon.php deleted file mode 100644 index 264b92e801..0000000000 --- a/include/salmon.php +++ /dev/null @@ -1,185 +0,0 @@ - 0) { - for ($x = 0; $x < count($ret); $x ++) { - if (substr($ret[$x], 0, 5) === 'data:') { - if (strstr($ret[$x], ',')) { - $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1); - } else { - $ret[$x] = substr($ret[$x], 5); - } - } elseif (normalise_link($ret[$x]) == 'http://') { - $ret[$x] = fetch_url($ret[$x]); - } - } - } - - - logger('Key located: ' . print_r($ret, true)); - - if (count($ret) == 1) { - // We only found one one key so we don't care if the hash matches. - // If it's the wrong key we'll find out soon enough because - // message verification will fail. This also covers some older - // software which don't supply a keyhash. As long as they only - // have one key we'll be right. - - return $ret[0]; - } else { - foreach ($ret as $a) { - $hash = base64url_encode(hash('sha256', $a)); - if ($hash == $keyhash) { - return $a; - } - } - } - - return ''; -} - - - -function slapper($owner, $url, $slap) -{ - // does contact have a salmon endpoint? - - if (! strlen($url)) { - return; - } - - if (! $owner['sprvkey']) { - logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.", - $owner['username'], $owner['uid'])); - return; - } - - logger('slapper called for '.$url.'. Data: ' . $slap); - - // create a magic envelope - - $data = base64url_encode($slap); - $data_type = 'application/atom+xml'; - $encoding = 'base64url'; - $algorithm = 'RSA-SHA256'; - $keyhash = base64url_encode(hash('sha256', salmon_key($owner['spubkey'])), true); - - $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm); - - // GNU Social format - $signature = base64url_encode(rsa_sign($data . $precomputed, $owner['sprvkey'])); - - // Compliant format - $signature2 = base64url_encode(rsa_sign(str_replace('=', '', $data . $precomputed), $owner['sprvkey'])); - - // Old Status.net format - $signature3 = base64url_encode(rsa_sign($data, $owner['sprvkey'])); - - // At first try the non compliant method that works for GNU Social - $xmldata = array("me:env" => array("me:data" => $data, - "@attributes" => array("type" => $data_type), - "me:encoding" => $encoding, - "me:alg" => $algorithm, - "me:sig" => $signature, - "@attributes2" => array("key_id" => $keyhash))); - - $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env"); - - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); - - // slap them - post_url($url, $salmon, array( - 'Content-type: application/magic-envelope+xml', - 'Content-length: ' . strlen($salmon) - )); - - $a = get_app(); - $return_code = $a->get_curl_code(); - - // check for success, e.g. 2xx - - if ($return_code > 299) { - logger('GNU Social salmon failed. Falling back to compliant mode'); - - // Now try the compliant mode that normally isn't used for GNU Social - $xmldata = array("me:env" => array("me:data" => $data, - "@attributes" => array("type" => $data_type), - "me:encoding" => $encoding, - "me:alg" => $algorithm, - "me:sig" => $signature2, - "@attributes2" => array("key_id" => $keyhash))); - - $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env"); - - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); - - // slap them - post_url($url, $salmon, array( - 'Content-type: application/magic-envelope+xml', - 'Content-length: ' . strlen($salmon) - )); - $return_code = $a->get_curl_code(); - } - - if ($return_code > 299) { - logger('compliant salmon failed. Falling back to old status.net'); - - // Last try. This will most likely fail as well. - $xmldata = array("me:env" => array("me:data" => $data, - "@attributes" => array("type" => $data_type), - "me:encoding" => $encoding, - "me:alg" => $algorithm, - "me:sig" => $signature3, - "@attributes2" => array("key_id" => $keyhash))); - - $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env"); - - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); - - // slap them - post_url($url, $salmon, array( - 'Content-type: application/magic-envelope+xml', - 'Content-length: ' . strlen($salmon)) - ); - $return_code = $a->get_curl_code(); - } - - logger('slapper for '.$url.' returned ' . $return_code); - - if (! $return_code) { - return -1; - } - - if (($return_code == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) { - return -1; - } - - return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1); -} diff --git a/mod/post.php b/mod/post.php index 8e279644bd..58d7695d41 100644 --- a/mod/post.php +++ b/mod/post.php @@ -1,54 +1,55 @@ argc == 1) { $bulk_delivery = true; - } - else { + } else { $nickname = $a->argv[2]; - $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' - AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1", - dbesc($nickname) - ); + $r = dba::select('user', array(), array('nickname' => $nickname, 'account_expired' => 0, 'account_removed' => 0), array('limit' => 1)); if (! DBM::is_result($r)) { http_status_exit(500); } - $importer = $r[0]; + $importer = $r; } $xml = file_get_contents('php://input'); logger('mod-post: new zot: ' . $xml, LOGGER_DATA); - if(! $xml) + if (! $xml) { http_status_exit(500); + } - $msg = zot_decode($importer,$xml); + $msg = zot_decode($importer, $xml); - logger('mod-post: decoded msg: ' . print_r($msg,true), LOGGER_DATA); + logger('mod-post: decoded msg: ' . print_r($msg, true), LOGGER_DATA); - if(! is_array($msg)) + if (! is_array($msg)) { http_status_exit(500); + } $ret = 0; - $ret = zot_incoming($bulk_delivery, $importer,$msg); + $ret = zot_incoming($bulk_delivery, $importer, $msg); http_status_exit(($ret) ? $ret : 200); // NOTREACHED } - diff --git a/mod/receive.php b/mod/receive.php index 8241325bf9..467a0d00a5 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -1,7 +1,7 @@ 0) { + for ($x = 0; $x < count($ret); $x ++) { + if (substr($ret[$x], 0, 5) === 'data:') { + if (strstr($ret[$x], ',')) { + $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1); + } else { + $ret[$x] = substr($ret[$x], 5); + } + } elseif (normalise_link($ret[$x]) == 'http://') { + $ret[$x] = fetch_url($ret[$x]); + } + } + } + + + logger('Key located: ' . print_r($ret, true)); + + if (count($ret) == 1) { + // We only found one one key so we don't care if the hash matches. + // If it's the wrong key we'll find out soon enough because + // message verification will fail. This also covers some older + // software which don't supply a keyhash. As long as they only + // have one key we'll be right. + + return $ret[0]; + } else { + foreach ($ret as $a) { + $hash = base64url_encode(hash('sha256', $a)); + if ($hash == $keyhash) { + return $a; + } + } + } + + return ''; + } + + /** + * @param array $owner owner + * @param string $url url + * @param string $slap slap + * @return integer + */ + public static function slapper($owner, $url, $slap) + { + // does contact have a salmon endpoint? + + if (! strlen($url)) { + return; + } + + if (! $owner['sprvkey']) { + logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.", + $owner['username'], $owner['uid'])); + return; + } + + logger('slapper called for '.$url.'. Data: ' . $slap); + + // create a magic envelope + + $data = base64url_encode($slap); + $data_type = 'application/atom+xml'; + $encoding = 'base64url'; + $algorithm = 'RSA-SHA256'; + $keyhash = base64url_encode(hash('sha256', salmon_key($owner['spubkey'])), true); + + $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm); + + // GNU Social format + $signature = base64url_encode(rsa_sign($data . $precomputed, $owner['sprvkey'])); + + // Compliant format + $signature2 = base64url_encode(rsa_sign(str_replace('=', '', $data . $precomputed), $owner['sprvkey'])); + + // Old Status.net format + $signature3 = base64url_encode(rsa_sign($data, $owner['sprvkey'])); + + // At first try the non compliant method that works for GNU Social + $xmldata = array("me:env" => array("me:data" => $data, + "@attributes" => array("type" => $data_type), + "me:encoding" => $encoding, + "me:alg" => $algorithm, + "me:sig" => $signature, + "@attributes2" => array("key_id" => $keyhash))); + + $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env"); + + $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + + // slap them + post_url($url, $salmon, array( + 'Content-type: application/magic-envelope+xml', + 'Content-length: ' . strlen($salmon) + )); + + $a = get_app(); + $return_code = $a->get_curl_code(); + + // check for success, e.g. 2xx + + if ($return_code > 299) { + logger('GNU Social salmon failed. Falling back to compliant mode'); + + // Now try the compliant mode that normally isn't used for GNU Social + $xmldata = array("me:env" => array("me:data" => $data, + "@attributes" => array("type" => $data_type), + "me:encoding" => $encoding, + "me:alg" => $algorithm, + "me:sig" => $signature2, + "@attributes2" => array("key_id" => $keyhash))); + + $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env"); + + $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + + // slap them + post_url($url, $salmon, array( + 'Content-type: application/magic-envelope+xml', + 'Content-length: ' . strlen($salmon) + )); + $return_code = $a->get_curl_code(); + } + + if ($return_code > 299) { + logger('compliant salmon failed. Falling back to old status.net'); + + // Last try. This will most likely fail as well. + $xmldata = array("me:env" => array("me:data" => $data, + "@attributes" => array("type" => $data_type), + "me:encoding" => $encoding, + "me:alg" => $algorithm, + "me:sig" => $signature3, + "@attributes2" => array("key_id" => $keyhash))); + + $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env"); + + $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + + // slap them + post_url($url, $salmon, array( + 'Content-type: application/magic-envelope+xml', + 'Content-length: ' . strlen($salmon)) + ); + $return_code = $a->get_curl_code(); + } + + logger('slapper for '.$url.' returned ' . $return_code); + + if (! $return_code) { + return -1; + } + + if (($return_code == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) { + return -1; + } + + return (($return_code >= 200) && ($return_code < 300)) ? 0 : 1; + } +} diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index e64351f17d..4404b879b7 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -11,11 +11,11 @@ use Friendica\Network\Probe; use Friendica\Object\Contact; use Friendica\Protocol\Diaspora; use Friendica\Protocol\OStatus; +use Friendica\Protocol\Salmon; use dba; require_once 'include/queue_fn.php'; require_once 'include/html2plain.php'; -require_once 'include/salmon.php'; require_once 'include/datetime.php'; require_once 'include/items.php'; require_once 'include/bbcode.php'; @@ -505,11 +505,11 @@ class Notifier { // They are especially used for notifications to OStatus users that don't follow us. if ($slap && count($url_recipients) && ($public_message || $push_notify) && $normal_mode) { - if (!Config::get('system','dfrn_only')) { + if (!Config::get('system', 'dfrn_only')) { foreach ($url_recipients as $url) { if ($url) { logger('notifier: urldelivery: ' . $url); - $deliver_status = slapper($owner,$url,$slap); + $deliver_status = Salmon::slapper($owner, $url, $slap); /// @TODO Redeliver/queue these items on failure, though there is no contact record } } diff --git a/src/Worker/Queue.php b/src/Worker/Queue.php index 47f1c8044b..cb00addc94 100644 --- a/src/Worker/Queue.php +++ b/src/Worker/Queue.php @@ -1,9 +1,7 @@ '); - $deliver_status = slapper($owner, $contact['notify'], $data); + $deliver_status = Salmon::slapper($owner, $contact['notify'], $data); if ($deliver_status == (-1)) { update_queue_time($q_item['id']);