diff --git a/jappixmini/README b/jappixmini/README index cf9eb70b..bd1e2aa1 100644 --- a/jappixmini/README +++ b/jappixmini/README @@ -10,6 +10,9 @@ address of a BOSH proxy that works with your account. The addon has an experimental autosubscribe and autosuggest functionality which tries to add your Friendica contacts to your roster automatically. +Limitations: + - can only handle Jabber passwords that are at most 39 characters long + Installation ------------ diff --git a/jappixmini/jappixmini.php b/jappixmini/jappixmini.php index 88959448..08ba248d 100644 --- a/jappixmini/jappixmini.php +++ b/jappixmini/jappixmini.php @@ -23,7 +23,8 @@ and not to the server (at least as soon as the user is logged in). It can be sto This encryption key could be the friendica password, but then this password would be stored in the browser in cleartext. It is better to use a hash of the password. The server should not be able to reconstruct the password, so we can't take the same hash the server stores. But we can - use hash("some_prefix"+password). This will however not work with OpenID logins. + use hash("some_prefix"+password). This will however not work with OpenID logins, for this type of login the password must +be queried manually. Problem: How to discover the jabber addresses of the friendica contacts? @@ -37,17 +38,23 @@ We do not want to make the jabber address public. Solution: When two friendica users connect using DFRN, the relation gets a DFRN ID and a keypair is generated. -Using this keypair, we can provide the jabber address only to contacs: +Using this keypair, we can provide the jabber address only to contacts: -Case 1: Alice has prvkey, Bob has pubkey. - Alice encrypts request - Bob decrypts the request, send jabber address unencrypted - Alice reads address +Alice: + signed_address = openssl_*_encrypt(alice_jabber_address) +send signed_address to Bob, who does + trusted_address = openssl_*_decrypt(signed_address) + save trusted_address + encrypted_address = openssl_*_encrypt(bob_jabber_address) +reply with encrypted_address to Alice, who does + decrypted_address = openssl_*_decrypt(encrypted_address) + save decrypted_address -Case 2: Alice has prvkey, Bob has pubkey - Alice send request - Bob encrypts jabber address - Alice decrypts jabber address +Interface for this: +GET /jappixmini/?role=%s&signed_address=%s&dfrn_id=%s + +Response: +json({"status":"ok", "encrypted_address":"%s"}) */ @@ -78,6 +85,8 @@ unregister_hook('about_hook', 'addon/jappixmini/jappixmini.php', 'jappixmini_dow } function jappixmini_plugin_admin(&$a, &$o) { + // display instructions and warnings on addon settings page for admin + if (!file_exists("addon/jappixmini/jappix")) { $o .= '
You need to install the Jappix application, adapted for Friendica (see README).
'; } @@ -91,81 +100,80 @@ function jappixmini_plugin_admin_post(&$a) { function jappixmini_module() {} function jappixmini_init(&$a) { - // Here, other friendica sites can fetch the jabber address of local users. - // Because we do not want to publish the addresses publicly, they are encrypted so - // that only contacts can read it. - $encrypt_for = $_REQUEST["encrypt_for"]; - if ($encrypt_for) { - $r = q("SELECT * FROM `contact` WHERE LENGTH(`pubkey`) AND `dfrn-id` = '%s' LIMIT 1", - dbesc($encrypt_for) + // module page where other Friendica sites can submit Jabber addresses to and also can query Jabber addresses + // of local users + + $dfrn_id = $_REQUEST["dfrn_id"]; + if (!$dfrn_id) killme(); + + $role = $_REQUEST["role"]; + if ($role=="pub") { + $r = q("SELECT * FROM `contact` WHERE LENGTH(`pubkey`) AND `dfrn-id`='%s' LIMIT 1", + dbesc($dfrn_id) ); if (!count($r)) killme(); - // get public key to encrypt address - $pubkey = $r[0]['pubkey']; + $encrypt_func = openssl_public_encrypt; + $decrypt_func = openssl_public_decrypt; + $key = $r[0]["pubkey"]; + } else if ($role=="prv") { + $r = q("SELECT * FROM `contact` WHERE LENGTH(`prvkey`) AND `issued-id`='%s' LIMIT 1", + dbesc($dfrn_id) + ); + if (!count($r)) killme(); - // get jabber address - $uid = $r[0]['uid']; - $username = get_pconfig($uid, 'jappixmini', 'username'); - if (!$username) killme(); - $server = get_pconfig($uid, 'jappixmini', 'server'); - if (!$server) killme(); - - $address = $username."@".$server; - - // encrypt address - $encrypted = ""; - openssl_public_encrypt($address,$encrypted,$pubkey); - - // calculate hex representation of encrypted address - $hex = bin2hex($encrypted); - - // construct answer - $answer = Array("status"=>"ok", "encrypted_address"=>$hex); - - // return answer as json - echo json_encode($answer); + $encrypt_func = openssl_private_encrypt; + $decrypt_func = openssl_private_decrypt; + $key = $r[0]["prvkey"]; + } else { killme(); } - // If we have only a private key, other site sends encrypted request, we answer unencrypted. - $encrypted_for = $_REQUEST["encrypted_for"]; - if (!$encrypted_for) killme(); + $uid = $r[0]["uid"]; - $encrypted_request_hex = $_REQUEST["encrypted_request"]; - if (!$encrypted_request_hex) killme(); - $encrypted_request = hex2bin($encrypted_request_hex); + // save the Jabber address we received + try { + $signed_address_hex = $_REQUEST["signed_address"]; + $signed_address = hex2bin($signed_address_hex); - $r = q("SELECT * FROM `contact` WHERE LENGTH(`prvkey`) AND `issued-id` = '%s' LIMIT 1", - dbesc($encrypted_for) - ); - if (!count($r)) killme(); + $trusted_address = ""; + $decrypt_func($signed_address, $trusted_address, $key); - // decrypt request, validate it - $prvkey = $r[0]['prvkey']; - $decrypted_request = ""; - openssl_private_decrypt($encrypted_request, $decrypted_request, $prvkey); + $now = intval(time()); + set_pconfig($uid, "jappixmini", "id:$dfrn_id", "$now:$trusted_address"); + } catch (Exception $e) { + } - if ($decrypted_request!=$encrypted_for) killme(); + // return the requested Jabber address + try { + $username = get_pconfig($uid, 'jappixmini', 'username'); + $server = get_pconfig($uid, 'jappixmini', 'server'); + $address = "$username@$server"; - // get jabber address - $uid = $r[0]['uid']; - $username = get_pconfig($uid, 'jappixmini', 'username'); - if (!$username) killme(); - $server = get_pconfig($uid, 'jappixmini', 'server'); - if (!$server) killme(); + $encrypted_address = ""; + $encrypt_func($address, $encrypted_address, $key); - $address = $username."@".$server; + $encrypted_address_hex = bin2hex($encrypted_address); - // construct answer - $answer = Array("status"=>"ok", "address"=>$address); + $answer = Array( + "status"=>"ok", + "encrypted_address"=>$encrypted_address_hex + ); - // return answer as json - echo json_encode($answer); - killme(); + $answer_json = json_encode($answer); + echo $answer_json; + killme(); + } catch (Exception $e) { + killme(); + } } function jappixmini_settings(&$a, &$s) { + // addon settings for a user + + $activate = get_pconfig(local_user(),'jappixmini','activate'); + $activate = intval($activate) ? ' checked="checked"' : ''; + $username = get_pconfig(local_user(),'jappixmini','username'); $username = htmlentities($username); $server = get_pconfig(local_user(),'jappixmini','server'); @@ -177,8 +185,6 @@ function jappixmini_settings(&$a, &$s) { $autosubscribe = intval($autosubscribe) ? ' checked="checked"' : ''; $autoapprove = get_pconfig(local_user(),'jappixmini','autoapprove'); $autoapprove = intval($autoapprove) ? ' checked="checked"' : ''; - $activate = get_pconfig(local_user(),'jappixmini','activate'); - $activate = intval($activate) ? ' checked="checked"' : ''; $s .= '