From a61ec584d60013f7845d0999deb550a2149f4e76 Mon Sep 17 00:00:00 2001 From: Friendika Date: Sat, 30 Jul 2011 00:31:00 -0700 Subject: basic diaspora decryption --- mod/receive.php | 134 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 103 insertions(+), 31 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 6fb68e154..18f323f42 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -43,10 +43,68 @@ function receive_post(&$a) { if(! $xml) receive_return(500); - // parse the xml - $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME); + $basedom = parse_xml_string($xml); + if($basedom) + logger('parsed dom'); + + $atom = $basedom->children(NAMESPACE_ATOM1); + + logger('atom: ' . count($atom)); + $encrypted_header = json_decode(base64_decode($atom->encrypted_header)); + + print_r($encrypted_header); + + $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key); + $ciphertext = base64_decode($encrypted_header->ciphertext); + + logger('encrypted_aes: ' . print_r($encrypted_aes_key_bundle,true)); + logger('ciphertext: ' . print_r($ciphertext,true)); + + $outer_key_bundle = ''; + openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$localprvkey); + + logger('outer_bundle: ' . print_r($outer_key_bundle,true)); + + $j_outer_key_bundle = json_decode($outer_key_bundle); + + $outer_iv = base64_decode($j_outer_key_bundle->iv); + $outer_key = base64_decode($j_outer_key_bundle->key); + + $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv); + + $decrypted = pkcs5_unpad($decrypted); + + logger('decrypted: ' . print_r($decrypted,true)); + + /** + * $decrypted now contains something like + * + * + * 8e+G2+ET8l5BPuW0sVTnQw== + * UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU= + * + * Ryan Hughes + * acct:galaxor@diaspora.pirateship.org + * + * + */ + + $idom = parse_xml_string($decrypted,false); + + print_r($idom); + $inner_iv = base64_decode($idom->iv); + $inner_aes_key = base64_decode($idom->aes_key); + + logger('inner_iv: ' . $inner_iv); + + $dom = $basedom->children(NAMESPACE_SALMON_ME); + + if($dom) + logger('have dom'); + + logger('dom: ' . count($dom)); // figure out where in the DOM tree our data is hiding if($dom->provenance->data) @@ -58,12 +116,22 @@ function receive_post(&$a) { if(! $base) { logger('mod-diaspora: unable to locate salmon data in xml '); - receive_return(400); + dt_return(400); } + // Stash the signature away for now. We have to find their key or it won't be good for anything. $signature = base64url_decode($base->sig); + logger('signature: ' . bin2hex($signature)); + + openssl_public_encrypt('test',$rrr,$ryanpubkey); + logger('rrr: ' . $rrr); + + $pubdecsig = ''; + openssl_public_decrypt($signature,$pubdecsig,$ryanpubkey); + logger('decsig: ' . bin2hex($pubdecsig)); + // unpack the data // strip whitespace so our data element will return to one big base64 blob @@ -76,40 +144,28 @@ function receive_post(&$a) { $encoding = $base->encoding; $alg = $base->alg; - $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg); + $signed_data = $data . "\n" . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n"; + + logger('signed data: ' . $signed_data); // decode the data $data = base64url_decode($data); - // Remove the xml declaration - $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data); + // Now pull out the inner encrypted blob - // Create a fake feed wrapper so simplepie doesn't choke - $tpl = get_markup_template('fake_feed.tpl'); - - $base = substr($data,strpos($data,''; - logger('mod-diaspora: Processed feed: ' . $feedxml); + $inner_encrypted = base64_decode($data); + + $inner_decrypted = + $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv); + + $inner_decrypted = pkcs5_unpad($inner_decrypted); + + logger('inner_decrypted: ' . $inner_decrypted); + - // Now parse it like a normal atom feed to scrape out the author URI - - $feed = new SimplePie(); - $feed->set_raw_data($feedxml); - $feed->enable_order_by_date(false); - $feed->init(); - - logger('mod-diaspora: Feed parsed.'); - - if($feed->get_item_quantity()) { - foreach($feed->get_items() as $item) { - $author = $item->get_author(); - $author_link = unxmlify($author->get_link()); - break; - } - } if(! $author_link) { logger('mod-diaspora: Could not retrieve author URI.'); @@ -117,17 +173,25 @@ function receive_post(&$a) { } // Once we have the author URI, go to the web and try to find their public key + // *** or look it up locally *** - logger('mod-salmon: Fetching key for ' . $author_link ); + logger('mod-diaspora: Fetching key for ' . $author_link ); +// Get diaspora public key (pkcs#1) and convert to pkcs#8 +// $key = get_diaspora_key($author_link); - $key = get_salmon_key($author_link,$keyhash); +// $key = get_salmon_key($author_link,$keyhash); if(! $key) { logger('mod-salmon: Could not retrieve author key.'); receive_return(400); } +// FIXME +// Use non salmon compliant signature + +/* + // Setup RSA stuff to verify the signature set_include_path(get_include_path() . PATH_SEPARATOR . 'library' . PATH_SEPARATOR . 'phpsec'); @@ -155,6 +219,7 @@ function receive_post(&$a) { logger('mod-diaspora: Message did not verify. Discarding.'); receive_return(400); } +*/ logger('mod-diaspora: Message verified.'); @@ -204,7 +269,14 @@ function receive_post(&$a) { $contact_rec = ((count($r)) ? $r[0] : null); - consume_feed($feedxml,$importer,$contact_rec,$hub); + + + +// figure out what kind of diaspora message we have, and process accordingly. + + + + receive_return(200); } -- cgit v1.2.3 From 01703f3be6fae07d9fbb24f1b57b8c181cb59df9 Mon Sep 17 00:00:00 2001 From: Friendika Date: Sat, 30 Jul 2011 00:51:59 -0700 Subject: get_diaspora_key() --- mod/receive.php | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 18f323f42..86d612dc9 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -6,7 +6,7 @@ require_once('include/salmon.php'); -require_once('library/simplepie/simplepie.inc'); +require_once('include/certfns.php'); function receive_return($val) { @@ -21,6 +21,30 @@ function receive_return($val) { } + +function get_diaspora_key($uri) { + $key = ''; + + logger('Fetching diaspora key for: ' . $uri); + + $arr = lrdd($uri); + + if(is_array($arr)) { + foreach($arr as $a) { + if($a['@attributes']['rel'] === 'diaspora-public-key') { + $key = base64_decode($a['@attributes']['href']); + } + } + } + else { + return ''; + } + + if($key) + return rsatopem($key); + return ''; +} + function receive_post(&$a) { if($a->argc != 3 || $a->argv[1] !== 'users') @@ -125,12 +149,12 @@ function receive_post(&$a) { logger('signature: ' . bin2hex($signature)); - openssl_public_encrypt('test',$rrr,$ryanpubkey); - logger('rrr: ' . $rrr); +// openssl_public_encrypt('test',$rrr,$rpubkey); +// logger('rrr: ' . $rrr); - $pubdecsig = ''; - openssl_public_decrypt($signature,$pubdecsig,$ryanpubkey); - logger('decsig: ' . bin2hex($pubdecsig)); +// $pubdecsig = ''; +// openssl_public_decrypt($signature,$pubdecsig,$rpubkey); +// logger('decsig: ' . bin2hex($pubdecsig)); // unpack the data @@ -178,9 +202,7 @@ function receive_post(&$a) { logger('mod-diaspora: Fetching key for ' . $author_link ); // Get diaspora public key (pkcs#1) and convert to pkcs#8 -// $key = get_diaspora_key($author_link); - -// $key = get_salmon_key($author_link,$keyhash); + $key = get_diaspora_key($author_link); if(! $key) { logger('mod-salmon: Could not retrieve author key.'); -- cgit v1.2.3 From 6eec04b09ca428cd2d125857612f7229e226c606 Mon Sep 17 00:00:00 2001 From: Friendika Date: Fri, 5 Aug 2011 01:34:32 -0700 Subject: updates to Diaspora decrypt/verify from recent testing --- mod/receive.php | 62 +++++++++++++-------------------------------------------- 1 file changed, 14 insertions(+), 48 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 86d612dc9..188f55f21 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -117,7 +117,6 @@ function receive_post(&$a) { $idom = parse_xml_string($decrypted,false); - print_r($idom); $inner_iv = base64_decode($idom->iv); $inner_aes_key = base64_decode($idom->aes_key); @@ -149,17 +148,14 @@ function receive_post(&$a) { logger('signature: ' . bin2hex($signature)); -// openssl_public_encrypt('test',$rrr,$rpubkey); -// logger('rrr: ' . $rrr); - -// $pubdecsig = ''; -// openssl_public_decrypt($signature,$pubdecsig,$rpubkey); -// logger('decsig: ' . bin2hex($pubdecsig)); - // unpack the data // strip whitespace so our data element will return to one big base64 blob $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data); + // Add back the 60 char linefeeds + $lines = str_split($data,60); + $data = implode("\n",$lines); + // stash away some other stuff for later @@ -168,7 +164,7 @@ function receive_post(&$a) { $encoding = $base->encoding; $alg = $base->alg; - $signed_data = $data . "\n" . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n"; + $signed_data = $data . (($data[-1] != "\n") ? "\n" : '') . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n"; logger('signed data: ' . $signed_data); @@ -177,9 +173,6 @@ function receive_post(&$a) { // Now pull out the inner encrypted blob - - - $inner_encrypted = base64_decode($data); $inner_decrypted = @@ -209,52 +202,25 @@ function receive_post(&$a) { receive_return(400); } -// FIXME -// Use non salmon compliant signature - -/* - - // Setup RSA stuff to verify the signature - - set_include_path(get_include_path() . PATH_SEPARATOR . 'library' . PATH_SEPARATOR . 'phpsec'); - require_once('library/phpsec/Crypt/RSA.php'); - - $key_info = explode('.',$key); - - $m = base64url_decode($key_info[1]); - $e = base64url_decode($key_info[2]); - - logger('mod-salmon: key details: ' . print_r($key_info,true)); - - $rsa = new CRYPT_RSA(); - $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1; - $rsa->setHash('sha256'); - - $rsa->modulus = new Math_BigInteger($m, 256); - $rsa->k = strlen($rsa->modulus->toBytes()); - $rsa->exponent = new Math_BigInteger($e, 256); + if (version_compare(PHP_VERSION, '5.3.0', '>=')) { + $verify = openssl_verify($signed_data,$signature,$key,'sha256'); + } + else { + // FIXME + // fallback sha256 verify for PHP < 5.3 - $verify = $rsa->verify($signed_data,$signature); + } if(! $verify) { logger('mod-diaspora: Message did not verify. Discarding.'); receive_return(400); } -*/ logger('mod-diaspora: Message verified.'); - /* decrypt the sucker */ - /* - // TODO - */ - - /* - * - * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff. - * - */ + // If we reached this point, the message is good. + // Now let's figure out if the author is allowed to send us stuff. $r = q("SELECT * FROM `contact` WHERE `network` = 'dspr' AND ( `url` = '%s' OR `alias` = '%s') AND `uid` = %d LIMIT 1", -- cgit v1.2.3 From 5a5a7bfc4cf551f6353358b961399efcaa8269b3 Mon Sep 17 00:00:00 2001 From: Friendika Date: Fri, 5 Aug 2011 05:37:42 -0700 Subject: fallback sha256 openssl_verify code for php releases prior to 5.3 --- mod/receive.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 188f55f21..f5a2eb7b6 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -194,7 +194,7 @@ function receive_post(&$a) { logger('mod-diaspora: Fetching key for ' . $author_link ); -// Get diaspora public key (pkcs#1) and convert to pkcs#8 + // Get diaspora public key (pkcs#1) and convert to pkcs#8 $key = get_diaspora_key($author_link); if(! $key) { @@ -202,14 +202,17 @@ function receive_post(&$a) { receive_return(400); } + $verify = false; if (version_compare(PHP_VERSION, '5.3.0', '>=')) { $verify = openssl_verify($signed_data,$signature,$key,'sha256'); } else { - // FIXME // fallback sha256 verify for PHP < 5.3 - + $rawsig = ''; + $hash = hash('sha256',$signed_data,true); + openssl_public_decrypt($signature,$rawsig,$key); + $verify = (($rawsig && substr($rawsig,-32) === $hash) ? true : false); } if(! $verify) { -- cgit v1.2.3 From 48ffa880f099b19052f18e399bf6af50780a24b0 Mon Sep 17 00:00:00 2001 From: Friendika Date: Sun, 7 Aug 2011 16:15:54 -0700 Subject: cleanup --- mod/receive.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index f5a2eb7b6..e9af087de 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -238,7 +238,7 @@ function receive_post(&$a) { // is this a follower? Or have we ignored the person? // If so we can not accept this post. - if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == REL_VIP) || ($r[0]['blocked']))) { + if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) { logger('mod-diaspora: Ignoring this author.'); receive_return(202); // NOTREACHED -- cgit v1.2.3 From 70017ebb8ce0321976a55d686178715885d1b6a9 Mon Sep 17 00:00:00 2001 From: Friendika Date: Tue, 9 Aug 2011 02:53:51 -0700 Subject: diaspora encrypt+decrypt working !! --- mod/receive.php | 204 ++------------------------------------------------------ 1 file changed, 6 insertions(+), 198 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index e9af087de..851437124 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -7,43 +7,9 @@ require_once('include/salmon.php'); require_once('include/certfns.php'); +require_once('include/diaspora.php'); -function receive_return($val) { - if($val >= 400) - $err = 'Error'; - if($val >= 200 && $val < 300) - $err = 'OK'; - - logger('mod-diaspora returns ' . $val); - header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err); - killme(); - -} - - -function get_diaspora_key($uri) { - $key = ''; - - logger('Fetching diaspora key for: ' . $uri); - - $arr = lrdd($uri); - - if(is_array($arr)) { - foreach($arr as $a) { - if($a['@attributes']['rel'] === 'diaspora-public-key') { - $key = base64_decode($a['@attributes']['href']); - } - } - } - else { - return ''; - } - - if($key) - return rsatopem($key); - return ''; -} function receive_post(&$a) { @@ -56,7 +22,7 @@ function receive_post(&$a) { dbesc($guid) ); if(! count($r)) - salmon_return(500); + receive_return(500); $importer = $r[0]; @@ -67,160 +33,9 @@ function receive_post(&$a) { if(! $xml) receive_return(500); - - $basedom = parse_xml_string($xml); - - if($basedom) - logger('parsed dom'); - - $atom = $basedom->children(NAMESPACE_ATOM1); - - logger('atom: ' . count($atom)); - $encrypted_header = json_decode(base64_decode($atom->encrypted_header)); - - print_r($encrypted_header); - - $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key); - $ciphertext = base64_decode($encrypted_header->ciphertext); - - logger('encrypted_aes: ' . print_r($encrypted_aes_key_bundle,true)); - logger('ciphertext: ' . print_r($ciphertext,true)); - - $outer_key_bundle = ''; - openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$localprvkey); - - logger('outer_bundle: ' . print_r($outer_key_bundle,true)); - - $j_outer_key_bundle = json_decode($outer_key_bundle); - - $outer_iv = base64_decode($j_outer_key_bundle->iv); - $outer_key = base64_decode($j_outer_key_bundle->key); - - $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv); - - $decrypted = pkcs5_unpad($decrypted); - - logger('decrypted: ' . print_r($decrypted,true)); - - /** - * $decrypted now contains something like - * - * - * 8e+G2+ET8l5BPuW0sVTnQw== - * UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU= - * - * Ryan Hughes - * acct:galaxor@diaspora.pirateship.org - * - * - */ - - $idom = parse_xml_string($decrypted,false); - - $inner_iv = base64_decode($idom->iv); - $inner_aes_key = base64_decode($idom->aes_key); - - logger('inner_iv: ' . $inner_iv); - - $dom = $basedom->children(NAMESPACE_SALMON_ME); - - if($dom) - logger('have dom'); - - logger('dom: ' . count($dom)); - // figure out where in the DOM tree our data is hiding - - if($dom->provenance->data) - $base = $dom->provenance; - elseif($dom->env->data) - $base = $dom->env; - elseif($dom->data) - $base = $dom; - - if(! $base) { - logger('mod-diaspora: unable to locate salmon data in xml '); - dt_return(400); - } - - - // Stash the signature away for now. We have to find their key or it won't be good for anything. - $signature = base64url_decode($base->sig); - - logger('signature: ' . bin2hex($signature)); - - // unpack the data - - // strip whitespace so our data element will return to one big base64 blob - $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data); - // Add back the 60 char linefeeds - $lines = str_split($data,60); - $data = implode("\n",$lines); - - - // stash away some other stuff for later - - $type = $base->data[0]->attributes()->type[0]; - $keyhash = $base->sig[0]->attributes()->keyhash[0]; - $encoding = $base->encoding; - $alg = $base->alg; - - $signed_data = $data . (($data[-1] != "\n") ? "\n" : '') . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n"; - - logger('signed data: ' . $signed_data); - - // decode the data - $data = base64url_decode($data); - - // Now pull out the inner encrypted blob - - $inner_encrypted = base64_decode($data); - - $inner_decrypted = - $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv); - - $inner_decrypted = pkcs5_unpad($inner_decrypted); - - logger('inner_decrypted: ' . $inner_decrypted); - - - - if(! $author_link) { - logger('mod-diaspora: Could not retrieve author URI.'); - receive_return(400); - } - - // Once we have the author URI, go to the web and try to find their public key - // *** or look it up locally *** - - logger('mod-diaspora: Fetching key for ' . $author_link ); - - // Get diaspora public key (pkcs#1) and convert to pkcs#8 - $key = get_diaspora_key($author_link); - - if(! $key) { - logger('mod-salmon: Could not retrieve author key.'); - receive_return(400); - } - - $verify = false; - - if (version_compare(PHP_VERSION, '5.3.0', '>=')) { - $verify = openssl_verify($signed_data,$signature,$key,'sha256'); - } - else { - // fallback sha256 verify for PHP < 5.3 - $rawsig = ''; - $hash = hash('sha256',$signed_data,true); - openssl_public_decrypt($signature,$rawsig,$key); - $verify = (($rawsig && substr($rawsig,-32) === $hash) ? true : false); - } - - if(! $verify) { - logger('mod-diaspora: Message did not verify. Discarding.'); - receive_return(400); - } - - logger('mod-diaspora: Message verified.'); + $msg = diaspora_decode($importer,$xml); + if(! $msg) + receive_return(500); // If we reached this point, the message is good. // Now let's figure out if the author is allowed to send us stuff. @@ -261,17 +76,10 @@ function receive_post(&$a) { $contact_rec = ((count($r)) ? $r[0] : null); - - -// figure out what kind of diaspora message we have, and process accordingly. - + receive_return(200); - receive_return(200); } - - - -- cgit v1.2.3 From 1bfe1283aa38454369f29883411a6c012c88df59 Mon Sep 17 00:00:00 2001 From: Friendika Date: Tue, 9 Aug 2011 18:55:46 -0700 Subject: crypto stuff --- mod/receive.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 851437124..e2c110202 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -6,7 +6,7 @@ require_once('include/salmon.php'); -require_once('include/certfns.php'); +require_once('include/crypto.php'); require_once('include/diaspora.php'); -- cgit v1.2.3 From 72873cd827979aeaf64bf6e0402c9b99fea56416 Mon Sep 17 00:00:00 2001 From: Friendika Date: Wed, 10 Aug 2011 05:10:48 -0700 Subject: diaspora function dispatcher --- mod/receive.php | 59 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 23 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index e2c110202..1dfbe59bd 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -14,7 +14,7 @@ require_once('include/diaspora.php'); function receive_post(&$a) { if($a->argc != 3 || $a->argv[1] !== 'users') - receive_return(500); + http_status_exit(500); $guid = $a->argv[2]; @@ -22,7 +22,7 @@ function receive_post(&$a) { dbesc($guid) ); if(! count($r)) - receive_return(500); + http_status_exit(500); $importer = $r[0]; @@ -31,11 +31,16 @@ function receive_post(&$a) { logger('mod-diaspora: new salmon ' . $xml, LOGGER_DATA); if(! $xml) - receive_return(500); + http_status_exit(500); $msg = diaspora_decode($importer,$xml); if(! $msg) - receive_return(500); + http_status_exit(500); + + + $parsed_xml = parse_xml_string($msg); + + $xmlbase = $parsed_xml->post; // If we reached this point, the message is good. // Now let's figure out if the author is allowed to send us stuff. @@ -52,34 +57,42 @@ function receive_post(&$a) { // is this a follower? Or have we ignored the person? // If so we can not accept this post. + // However we will accept a sharing e.g. friend request if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) { - logger('mod-diaspora: Ignoring this author.'); - receive_return(202); - // NOTREACHED + if(! $xmlbase->request) { + logger('mod-diaspora: Ignoring this author.'); + http_status_exit(202); + // NOTREACHED + } } require_once('include/items.php'); - // Placeholder for hub discovery. We shouldn't find any hubs - // since we supplied the fake feed header - and it doesn't have any. - - $hub = ''; - - /** - * - * anti-spam measure: consume_feed will accept a follow activity from - * this person (and nothing else) if there is no existing contact record. - * - */ - - $contact_rec = ((count($r)) ? $r[0] : null); - - - receive_return(200); + $contact = ((count($r)) ? $r[0] : null); + if($xmlbase->request) { + diaspora_request($importer,$contact,$xmlbase->request); + } + elseif($xmlbase->status_message) { + diaspora_post($importer,$contact,$xmlbase->status_message); + } + elseif($xmlbase->comment) { + diaspora_comment($importer,$contact,$xmlbase->comment); + } + elseif($xmlbase->like) { + diaspora_like($importer,$contact,$xmlbase->like); + } + elseif($xmlbase->retraction) { + diaspora_retraction($importer,$contact,$xmlbase->retraction); + } + else { + logger('mod-diaspora: unknown message type: ' . print_r($xmlbase,true)); + } + http_status_exit(200); + // NOTREACHED } -- cgit v1.2.3 From aefc6209a3d07d70835422e690f624c72075c410 Mon Sep 17 00:00:00 2001 From: Friendika Date: Fri, 12 Aug 2011 03:01:11 -0700 Subject: improved diaspora discovery --- mod/receive.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 1dfbe59bd..72d528093 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -71,7 +71,7 @@ function receive_post(&$a) { $contact = ((count($r)) ? $r[0] : null); - + logger('diaspora msg: ' . $msg, LOGGER_DATA); if($xmlbase->request) { diaspora_request($importer,$contact,$xmlbase->request); -- cgit v1.2.3 From 5d6155a9685202b055744549a2cb84e3d8195fd2 Mon Sep 17 00:00:00 2001 From: Friendika Date: Mon, 15 Aug 2011 05:27:24 -0700 Subject: fixes share from diaspora --- mod/receive.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 72d528093..43f8c7bbc 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -26,7 +26,7 @@ function receive_post(&$a) { $importer = $r[0]; - $xml = $_POST['xml']; + $xml = urldecode($_POST['xml']); logger('mod-diaspora: new salmon ' . $xml, LOGGER_DATA); @@ -38,7 +38,7 @@ function receive_post(&$a) { http_status_exit(500); - $parsed_xml = parse_xml_string($msg); + $parsed_xml = parse_xml_string($msg,false); $xmlbase = $parsed_xml->post; -- cgit v1.2.3 From 44918e27367d00d3625daaf751a05b166ecd2fc1 Mon Sep 17 00:00:00 2001 From: Friendika Date: Mon, 15 Aug 2011 17:14:51 -0700 Subject: turn diaspora posts into x-www-form-urlencoded --- mod/receive.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 43f8c7bbc..46b1f2745 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -26,6 +26,9 @@ function receive_post(&$a) { $importer = $r[0]; + // I really don't know why we need urldecode - PHP should be doing this for us. + // It is an application/x-www-form-urlencoded + $xml = urldecode($_POST['xml']); logger('mod-diaspora: new salmon ' . $xml, LOGGER_DATA); @@ -34,6 +37,9 @@ function receive_post(&$a) { http_status_exit(500); $msg = diaspora_decode($importer,$xml); + + logger('mod-diaspora: decoded msg: ' . $msg, LOGGER_DATA); + if(! $msg) http_status_exit(500); @@ -58,21 +64,23 @@ function receive_post(&$a) { // is this a follower? Or have we ignored the person? // If so we can not accept this post. // However we will accept a sharing e.g. friend request + // or a retraction of same. - if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) { - if(! $xmlbase->request) { + + $allow_blocked = (($xmlbase->request || ($xmlbase->retraction && $xmlbase->retraction->type == 'Person')) ? true : false); + + if((count($r)) + && (($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']) || ($r[0]['readonly'])) + && (! $allow_blocked)) { logger('mod-diaspora: Ignoring this author.'); http_status_exit(202); // NOTREACHED - } } require_once('include/items.php'); $contact = ((count($r)) ? $r[0] : null); - logger('diaspora msg: ' . $msg, LOGGER_DATA); - if($xmlbase->request) { diaspora_request($importer,$contact,$xmlbase->request); } -- cgit v1.2.3 From c7197b99f919bee81fa2a8f46a3ef744c76bec6c Mon Sep 17 00:00:00 2001 From: Friendika Date: Mon, 15 Aug 2011 23:19:17 -0700 Subject: refactor the diaspora contact logic --- mod/receive.php | 45 +++++---------------------------------------- 1 file changed, 5 insertions(+), 40 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 46b1f2745..1a99a9aed 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -9,7 +9,6 @@ require_once('include/salmon.php'); require_once('include/crypto.php'); require_once('include/diaspora.php'); - function receive_post(&$a) { @@ -26,7 +25,6 @@ function receive_post(&$a) { $importer = $r[0]; - // I really don't know why we need urldecode - PHP should be doing this for us. // It is an application/x-www-form-urlencoded $xml = urldecode($_POST['xml']); @@ -48,53 +46,20 @@ function receive_post(&$a) { $xmlbase = $parsed_xml->post; - // If we reached this point, the message is good. - // Now let's figure out if the author is allowed to send us stuff. - - $r = q("SELECT * FROM `contact` WHERE `network` = 'dspr' AND ( `url` = '%s' OR `alias` = '%s') - AND `uid` = %d LIMIT 1", - dbesc($author_link), - dbesc($author_link), - intval($importer['uid']) - ); - if(! count($r)) { - logger('mod-diaspora: Author unknown to us.'); - } - - // is this a follower? Or have we ignored the person? - // If so we can not accept this post. - // However we will accept a sharing e.g. friend request - // or a retraction of same. - - - $allow_blocked = (($xmlbase->request || ($xmlbase->retraction && $xmlbase->retraction->type == 'Person')) ? true : false); - - if((count($r)) - && (($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']) || ($r[0]['readonly'])) - && (! $allow_blocked)) { - logger('mod-diaspora: Ignoring this author.'); - http_status_exit(202); - // NOTREACHED - } - - require_once('include/items.php'); - - $contact = ((count($r)) ? $r[0] : null); - if($xmlbase->request) { - diaspora_request($importer,$contact,$xmlbase->request); + diaspora_request($importer,$xmlbase->request); } elseif($xmlbase->status_message) { - diaspora_post($importer,$contact,$xmlbase->status_message); + diaspora_post($importer,$xmlbase->status_message); } elseif($xmlbase->comment) { - diaspora_comment($importer,$contact,$xmlbase->comment); + diaspora_comment($importer,$xmlbase->comment); } elseif($xmlbase->like) { - diaspora_like($importer,$contact,$xmlbase->like); + diaspora_like($importer,$xmlbase->like); } elseif($xmlbase->retraction) { - diaspora_retraction($importer,$contact,$xmlbase->retraction); + diaspora_retraction($importer,$xmlbase->retraction); } else { logger('mod-diaspora: unknown message type: ' . print_r($xmlbase,true)); -- cgit v1.2.3 From 673e114bbd6666ffe9350613284e813a38c5f0d7 Mon Sep 17 00:00:00 2001 From: Friendika Date: Tue, 16 Aug 2011 22:31:14 -0700 Subject: D* like and start of relay code --- mod/receive.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'mod/receive.php') diff --git a/mod/receive.php b/mod/receive.php index 1a99a9aed..19c163967 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -36,13 +36,13 @@ function receive_post(&$a) { $msg = diaspora_decode($importer,$xml); - logger('mod-diaspora: decoded msg: ' . $msg, LOGGER_DATA); + logger('mod-diaspora: decoded msg: ' . print_r($msg,true), LOGGER_DATA); - if(! $msg) + if(! is_array($msg)) http_status_exit(500); - $parsed_xml = parse_xml_string($msg,false); + $parsed_xml = parse_xml_string($msg['message'],false); $xmlbase = $parsed_xml->post; @@ -53,13 +53,13 @@ function receive_post(&$a) { diaspora_post($importer,$xmlbase->status_message); } elseif($xmlbase->comment) { - diaspora_comment($importer,$xmlbase->comment); + diaspora_comment($importer,$xmlbase->comment,$msg); } elseif($xmlbase->like) { - diaspora_like($importer,$xmlbase->like); + diaspora_like($importer,$xmlbase->like,$msg); } elseif($xmlbase->retraction) { - diaspora_retraction($importer,$xmlbase->retraction); + diaspora_retraction($importer,$xmlbase->retraction,$msg); } else { logger('mod-diaspora: unknown message type: ' . print_r($xmlbase,true)); -- cgit v1.2.3