-#!/usr/bin/php
+#!/usr/bin/env php
<?php
/*
* ejabberd extauth script for the integration with friendica
*
* Installation:
*
- * - Change it's owner to whichever user is running the server, ie. ejabberd
- * $ chown ejabberd:ejabberd /path/to/friendica/scripts/auth_ejabberd.php
+ * - Change it's owner to whichever user is running the server, ie. ejabberd
+ * $ chown ejabberd:ejabberd /path/to/friendica/scripts/auth_ejabberd.php
*
* - Change the access mode so it is readable only to the user ejabberd and has exec
- * $ chmod 700 /path/to/friendica/scripts/auth_ejabberd.php
+ * $ chmod 700 /path/to/friendica/scripts/auth_ejabberd.php
*
- * - Edit your ejabberd.cfg file, comment out your auth_method and add:
- * {auth_method, external}.
- * {extauth_program, "/path/to/friendica/script/auth_ejabberd.php"}.
+ * - Edit your ejabberd.cfg file, comment out your auth_method and add:
+ * {auth_method, external}.
+ * {extauth_program, "/path/to/friendica/script/auth_ejabberd.php"}.
*
- * - Restart your ejabberd service, you should be able to login with your friendica auth info
+ * - Restart your ejabberd service, you should be able to login with your friendica auth info
*
* Other hints:
- * - if your users have a space or a @ in their nickname, they'll run into trouble
- * registering with any client so they should be instructed to replace these chars
- * " " (space) is replaced with "%20"
- * "@" is replaced with "(a)"
+ * - if your users have a space or a @ in their nickname, they'll run into trouble
+ * registering with any client so they should be instructed to replace these chars
+ * " " (space) is replaced with "%20"
+ * "@" is replaced with "(a)"
*
*/
use Friendica\App;
-use Friendica\Core\Config;
-use Friendica\Database\DBM;
+use Friendica\Util\ExAuth;
-if (sizeof($_SERVER["argv"]) == 0)
+if (sizeof($_SERVER["argv"]) == 0) {
die();
+}
$directory = dirname($_SERVER["argv"][0]);
-if (substr($directory, 0, 1) != "/")
- $directory = $_SERVER["PWD"]."/".$directory;
+if (substr($directory, 0, 1) != DIRECTORY_SEPARATOR) {
+ $directory = $_SERVER["PWD"] . DIRECTORY_SEPARATOR . $directory;
+}
-$directory = realpath($directory."/..");
+$directory = realpath($directory . DIRECTORY_SEPARATOR . "..");
chdir($directory);
$a = new App(dirname(__DIR__));
-@include(".htconfig.php");
+@include ".htconfig.php";
dba::connect($db_host, $db_user, $db_pass, $db_data);
unset($db_host, $db_user, $db_pass, $db_data);
-$oAuth = new exAuth();
-
-class exAuth {
- private $bDebug;
-
- /**
- * @brief Create the class and do the authentification studd
- *
- * @param boolean $bDebug Debug mode
- */
- public function __construct() {
- // setter
- $this->bDebug = (int)Config::get('jabber', 'debug');
-
-
- openlog('auth_ejabberd', LOG_PID, LOG_USER);
-
- $this->writeLog(LOG_NOTICE, "start");
-
- // We are connected to the SQL server.
- while (!feof(STDIN)) {
- // Quit if the database connection went down
- if (!dba::connected()) {
- $this->writeLog(LOG_ERR, "the database connection went down");
- return;
- }
-
- $iHeader = fgets(STDIN, 3);
- $aLength = unpack("n", $iHeader);
- $iLength = $aLength["1"];
-
- // No data? Then quit
- if ($iLength == 0) {
- $this->writeLog(LOG_ERR, "we got no data, quitting");
- return;
- }
-
- // Fetching the data
- $sData = fgets(STDIN, $iLength + 1);
- $this->writeLog(LOG_DEBUG, "received data: ". $sData);
- $aCommand = explode(":", $sData);
- if (is_array($aCommand)) {
- switch ($aCommand[0]) {
- case "isuser":
- // Check the existance of a given username
- $this->isuser($aCommand);
- break;
- case "auth":
- // Check if the givven password is correct
- $this->auth($aCommand);
- break;
- case "setpass":
- // We don't accept the setting of passwords here
- $this->writeLog(LOG_NOTICE, "setpass command disabled");
- fwrite(STDOUT, pack("nn", 2, 0));
- break;
- default:
- // We don't know the given command
- $this->writeLog(LOG_NOTICE, "unknown command ". $aCommand[0]);
- fwrite(STDOUT, pack("nn", 2, 0));
- break;
- }
- } else {
- $this->writeLog(LOG_NOTICE, "invalid command string ".$sData);
- fwrite(STDOUT, pack("nn", 2, 0));
- }
- }
- }
-
- /**
- * @brief Check if the given username exists
- *
- * @param array $aCommand The command array
- */
- private function isuser($aCommand) {
- $a = get_app();
-
- // Check if there is a username
- if (!isset($aCommand[1])) {
- $this->writeLog(LOG_NOTICE, "invalid isuser command, no username given");
- fwrite(STDOUT, pack("nn", 2, 0));
- return;
- }
-
- // Now we check if the given user is valid
- $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
-
- // Does the hostname match? So we try directly
- if ($a->get_hostname() == $aCommand[2]) {
- $this->writeLog(LOG_INFO, "internal user check for ". $sUser."@".$aCommand[2]);
- $sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
- $this->writeLog(LOG_DEBUG, "using query ". $sQuery);
- $r = q($sQuery);
- $found = DBM::is_result($r);
- } else {
- $found = false;
- }
-
- // If the hostnames doesn't match or there is some failure, we try to check remotely
- if (!$found) {
- $found = $this->check_user($aCommand[2], $aCommand[1], true);
- }
-
- if ($found) {
- // The user is okay
- $this->writeLog(LOG_NOTICE, "valid user: ". $sUser);
- fwrite(STDOUT, pack("nn", 2, 1));
- } else {
- // The user isn't okay
- $this->writeLog(LOG_WARNING, "invalid user: ". $sUser);
- fwrite(STDOUT, pack("nn", 2, 0));
- }
- }
-
- /**
- * @brief Check remote user existance via HTTP(S)
- *
- * @param string $host The hostname
- * @param string $user Username
- * @param boolean $ssl Should the check be done via SSL?
- *
- * @return boolean Was the user found?
- */
- private function check_user($host, $user, $ssl) {
-
- $this->writeLog(LOG_INFO, "external user check for ".$user."@".$host);
-
- $url = ($ssl ? "https":"http")."://".$host."/noscrape/".$user;
+$oAuth = new ExAuth();
- $data = z_fetch_url($url);
-
- if (!is_array($data))
- return(false);
-
- if ($data["return_code"] != "200")
- return(false);
-
- $json = @json_decode($data["body"]);
- if (!is_object($json))
- return(false);
-
- return($json->nick == $user);
- }
-
- /**
- * @brief Authenticate the givven user and password
- *
- * @param array $aCommand The command array
- */
- private function auth($aCommand) {
- $a = get_app();
-
- // check user authentication
- if (sizeof($aCommand) != 4) {
- $this->writeLog(LOG_NOTICE, "invalid auth command, data missing");
- fwrite(STDOUT, pack("nn", 2, 0));
- return;
- }
-
- // We now check if the password match
- $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
-
- // Does the hostname match? So we try directly
- if ($a->get_hostname() == $aCommand[2]) {
- $this->writeLog(LOG_INFO, "internal auth for ".$sUser."@".$aCommand[2]);
-
- $sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
- $this->writeLog(LOG_DEBUG, "using query ". $sQuery);
- if ($oResult = q($sQuery)) {
- $uid = $oResult[0]["uid"];
- $Error = ($oResult[0]["password"] != hash('whirlpool',$aCommand[3]));
- } else {
- $this->writeLog(LOG_WARNING, "invalid query: ". $sQuery);
- $Error = true;
- $uid = -1;
- }
- if ($Error) {
- $oConfig = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = 'xmpp' AND `k`='password' LIMIT 1;", intval($uid));
- $this->writeLog(LOG_INFO, "check against alternate password for ".$sUser."@".$aCommand[2]);
- $Error = ($aCommand[3] != $oConfig[0]["v"]);
- }
- } else {
- $Error = true;
- }
-
- // If the hostnames doesn't match or there is some failure, we try to check remotely
- if ($Error) {
- $Error = !$this->check_credentials($aCommand[2], $aCommand[1], $aCommand[3], true);
- }
-
- if ($Error) {
- $this->writeLog(LOG_WARNING, "authentification failed for user ".$sUser."@". $aCommand[2]);
- fwrite(STDOUT, pack("nn", 2, 0));
- } else {
- $this->writeLog(LOG_NOTICE, "authentificated user ".$sUser."@".$aCommand[2]);
- fwrite(STDOUT, pack("nn", 2, 1));
- }
- }
-
- /**
- * @brief Check remote credentials via HTTP(S)
- *
- * @param string $host The hostname
- * @param string $user Username
- * @param string $password Password
- * @param boolean $ssl Should the check be done via SSL?
- *
- * @return boolean Are the credentials okay?
- */
- private function check_credentials($host, $user, $password, $ssl) {
- $url = ($ssl ? "https":"http")."://".$host."/api/account/verify_credentials.json";
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLOPT_NOBODY, true);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
-
- $header = curl_exec($ch);
- $curl_info = @curl_getinfo($ch);
- $http_code = $curl_info["http_code"];
- curl_close($ch);
-
- $this->writeLog(LOG_INFO, "external auth for ".$user."@".$host." returned ".$http_code);
-
- return ($http_code == 200);
- }
-
- /**
- * @brief write data to the syslog
- *
- * @param integer $loglevel The syslog loglevel
- * @param string $sMessage The syslog message
- */
- private function writeLog($loglevel, $sMessage) {
- if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
- return;
- }
- syslog($loglevel, $sMessage);
- }
-
- /**
- * @brief destroy the class, close the syslog connection.
- */
- public function __destruct() {
- $this->writeLog(LOG_NOTICE, "stop");
- closelog();
- }
-}
+$oAuth->readStdin();
--- /dev/null
+<?php\r
+\r
+/*\r
+ * ejabberd extauth script for the integration with friendica\r
+ *\r
+ * Originally written for joomla by Dalibor Karlovic <dado@krizevci.info>\r
+ * modified for Friendica by Michael Vogel <icarus@dabo.de>\r
+ * published under GPL\r
+ *\r
+ * Latest version of the original script for joomla is available at:\r
+ * http://87.230.15.86/~dado/ejabberd/joomla-login\r
+ *\r
+ * Installation:\r
+ *\r
+ * - Change it's owner to whichever user is running the server, ie. ejabberd\r
+ * $ chown ejabberd:ejabberd /path/to/friendica/scripts/auth_ejabberd.php\r
+ *\r
+ * - Change the access mode so it is readable only to the user ejabberd and has exec\r
+ * $ chmod 700 /path/to/friendica/scripts/auth_ejabberd.php\r
+ *\r
+ * - Edit your ejabberd.cfg file, comment out your auth_method and add:\r
+ * {auth_method, external}.\r
+ * {extauth_program, "/path/to/friendica/script/auth_ejabberd.php"}.\r
+ *\r
+ * - Restart your ejabberd service, you should be able to login with your friendica auth info\r
+ *\r
+ * Other hints:\r
+ * - if your users have a space or a @ in their nickname, they'll run into trouble\r
+ * registering with any client so they should be instructed to replace these chars\r
+ * " " (space) is replaced with "%20"\r
+ * "@" is replaced with "(a)"\r
+ *\r
+ */\r
+\r
+namespace Friendica\Util;\r
+\r
+use Friendica\Core\Config;\r
+use Friendica\Core\PConfig;\r
+use Friendica\Database\DBM;\r
+\r
+require_once 'include/dba.php';\r
+\r
+class ExAuth\r
+{\r
+ private $bDebug;\r
+\r
+ /**\r
+ * @brief Create the class\r
+ *\r
+ * @param boolean $bDebug Debug mode\r
+ */\r
+ public function __construct()\r
+ {\r
+ $this->bDebug = (int) Config::get('jabber', 'debug');\r
+\r
+ openlog('auth_ejabberd', LOG_PID, LOG_USER);\r
+\r
+ $this->writeLog(LOG_NOTICE, 'start');\r
+ }\r
+\r
+ /**\r
+ * @brief Standard input reading function, executes the auth with the provided\r
+ * parameters\r
+ *\r
+ * @return null\r
+ */\r
+ public function readStdin()\r
+ {\r
+ while (!feof(STDIN)) {\r
+ // Quit if the database connection went down\r
+ if (!dba::connected()) {\r
+ $this->writeLog(LOG_ERR, 'the database connection went down');\r
+ return;\r
+ }\r
+\r
+ $iHeader = fgets(STDIN, 3);\r
+ $aLength = unpack('n', $iHeader);\r
+ $iLength = $aLength['1'];\r
+\r
+ // No data? Then quit\r
+ if ($iLength == 0) {\r
+ $this->writeLog(LOG_ERR, 'we got no data, quitting');\r
+ return;\r
+ }\r
+\r
+ // Fetching the data\r
+ $sData = fgets(STDIN, $iLength + 1);\r
+ $this->writeLog(LOG_DEBUG, 'received data: ' . $sData);\r
+ $aCommand = explode(':', $sData);\r
+ if (is_array($aCommand)) {\r
+ switch ($aCommand[0]) {\r
+ case 'isuser':\r
+ // Check the existance of a given username\r
+ $this->isUser($aCommand);\r
+ break;\r
+ case 'auth':\r
+ // Check if the givven password is correct\r
+ $this->auth($aCommand);\r
+ break;\r
+ case 'setpass':\r
+ // We don't accept the setting of passwords here\r
+ $this->writeLog(LOG_NOTICE, 'setpass command disabled');\r
+ fwrite(STDOUT, pack('nn', 2, 0));\r
+ break;\r
+ default:\r
+ // We don't know the given command\r
+ $this->writeLog(LOG_NOTICE, 'unknown command ' . $aCommand[0]);\r
+ fwrite(STDOUT, pack('nn', 2, 0));\r
+ break;\r
+ }\r
+ } else {\r
+ $this->writeLog(LOG_NOTICE, 'invalid command string ' . $sData);\r
+ fwrite(STDOUT, pack('nn', 2, 0));\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @brief Check if the given username exists\r
+ *\r
+ * @param array $aCommand The command array\r
+ */\r
+ private function isUser(array $aCommand)\r
+ {\r
+ $a = get_app();\r
+\r
+ // Check if there is a username\r
+ if (!isset($aCommand[1])) {\r
+ $this->writeLog(LOG_NOTICE, 'invalid isuser command, no username given');\r
+ fwrite(STDOUT, pack('nn', 2, 0));\r
+ return;\r
+ }\r
+\r
+ // Now we check if the given user is valid\r
+ $sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);\r
+\r
+ // Does the hostname match? So we try directly\r
+ if ($a->get_hostname() == $aCommand[2]) {\r
+ $this->writeLog(LOG_INFO, 'internal user check for ' . $sUser . '@' . $aCommand[2]);\r
+ $found = dba::exists('user', ['nickname' => $sUser]);\r
+ } else {\r
+ $found = false;\r
+ }\r
+\r
+ // If the hostnames doesn't match or there is some failure, we try to check remotely\r
+ if (!$found) {\r
+ $found = $this->checkUser($aCommand[2], $aCommand[1], true);\r
+ }\r
+\r
+ if ($found) {\r
+ // The user is okay\r
+ $this->writeLog(LOG_NOTICE, 'valid user: ' . $sUser);\r
+ fwrite(STDOUT, pack('nn', 2, 1));\r
+ } else {\r
+ // The user isn't okay\r
+ $this->writeLog(LOG_WARNING, 'invalid user: ' . $sUser);\r
+ fwrite(STDOUT, pack('nn', 2, 0));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @brief Check remote user existance via HTTP(S)\r
+ *\r
+ * @param string $host The hostname\r
+ * @param string $user Username\r
+ * @param boolean $ssl Should the check be done via SSL?\r
+ *\r
+ * @return boolean Was the user found?\r
+ */\r
+ private function checkUser($host, $user, $ssl)\r
+ {\r
+ $this->writeLog(LOG_INFO, 'external user check for ' . $user . '@' . $host);\r
+\r
+ $url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;\r
+\r
+ $data = z_fetch_url($url);\r
+\r
+ if (!is_array($data)) {\r
+ return false;\r
+ }\r
+\r
+ if ($data['return_code'] != '200') {\r
+ return false;\r
+ }\r
+\r
+ $json = @json_decode($data['body']);\r
+ if (!is_object($json)) {\r
+ return false;\r
+ }\r
+\r
+ return $json->nick == $user;\r
+ }\r
+\r
+ /**\r
+ * @brief Authenticate the given user and password\r
+ *\r
+ * @param array $aCommand The command array\r
+ */\r
+ private function auth(array $aCommand)\r
+ {\r
+ $a = get_app();\r
+\r
+ // check user authentication\r
+ if (sizeof($aCommand) != 4) {\r
+ $this->writeLog(LOG_NOTICE, 'invalid auth command, data missing');\r
+ fwrite(STDOUT, pack('nn', 2, 0));\r
+ return;\r
+ }\r
+\r
+ // We now check if the password match\r
+ $sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);\r
+\r
+ // Does the hostname match? So we try directly\r
+ if ($a->get_hostname() == $aCommand[2]) {\r
+ $this->writeLog(LOG_INFO, 'internal auth for ' . $sUser . '@' . $aCommand[2]);\r
+\r
+ $aUser = dba::select('user', ['uid', 'password'], ['nickname' => $sUser], ['limit' => 1]);\r
+ if (DBM::is_result($aUser)) {\r
+ $uid = $aUser['uid'];\r
+ $Error = $aUser['password'] != hash('whirlpool', $aCommand[3]);\r
+ } else {\r
+ $this->writeLog(LOG_WARNING, 'user not found: ' . $sUser);\r
+ $Error = true;\r
+ $uid = -1;\r
+ }\r
+ if ($Error) {\r
+ $this->writeLog(LOG_INFO, 'check against alternate password for ' . $sUser . '@' . $aCommand[2]);\r
+ $sPassword = PConfig::get($uid, 'xmpp', 'password');\r
+ $Error = ($aCommand[3] != $sPassword);\r
+ }\r
+ } else {\r
+ $Error = true;\r
+ }\r
+\r
+ // If the hostnames doesn't match or there is some failure, we try to check remotely\r
+ if ($Error) {\r
+ $Error = !$this->checkCredentials($aCommand[2], $aCommand[1], $aCommand[3], true);\r
+ }\r
+\r
+ if ($Error) {\r
+ $this->writeLog(LOG_WARNING, 'authentification failed for user ' . $sUser . '@' . $aCommand[2]);\r
+ fwrite(STDOUT, pack('nn', 2, 0));\r
+ } else {\r
+ $this->writeLog(LOG_NOTICE, 'authentificated user ' . $sUser . '@' . $aCommand[2]);\r
+ fwrite(STDOUT, pack('nn', 2, 1));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @brief Check remote credentials via HTTP(S)\r
+ *\r
+ * @param string $host The hostname\r
+ * @param string $user Username\r
+ * @param string $password Password\r
+ * @param boolean $ssl Should the check be done via SSL?\r
+ *\r
+ * @return boolean Are the credentials okay?\r
+ */\r
+ private function checkCredentials($host, $user, $password, $ssl)\r
+ {\r
+ $url = ($ssl ? 'https' : 'http') . '://' . $host . '/api/account/verify_credentials.json';\r
+\r
+ $ch = curl_init();\r
+ curl_setopt($ch, CURLOPT_URL, $url);\r
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);\r
+ curl_setopt($ch, CURLOPT_HEADER, true);\r
+ curl_setopt($ch, CURLOPT_NOBODY, true);\r
+ curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\r
+ curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);\r
+\r
+ curl_exec($ch);\r
+ $curl_info = @curl_getinfo($ch);\r
+ $http_code = $curl_info['http_code'];\r
+ curl_close($ch);\r
+\r
+ $this->writeLog(LOG_INFO, 'external auth for ' . $user . '@' . $host . ' returned ' . $http_code);\r
+\r
+ return $http_code == 200;\r
+ }\r
+\r
+ /**\r
+ * @brief write data to the syslog\r
+ *\r
+ * @param integer $loglevel The syslog loglevel\r
+ * @param string $sMessage The syslog message\r
+ */\r
+ private function writeLog($loglevel, $sMessage)\r
+ {\r
+ if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {\r
+ return;\r
+ }\r
+ syslog($loglevel, $sMessage);\r
+ }\r
+\r
+ /**\r
+ * @brief destroy the class, close the syslog connection.\r
+ */\r
+ public function __destruct()\r
+ {\r
+ $this->writeLog(LOG_NOTICE, 'stop');\r
+ closelog();\r
+ }\r
+}\r