2013-06-22 17:34:52 +02:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* ejabberd extauth script for the integration with friendica
|
|
|
|
*
|
|
|
|
* Originally written for joomla by Dalibor Karlovic <dado@krizevci.info>
|
|
|
|
* modified for Friendica by Michael Vogel <icarus@dabo.de>
|
|
|
|
* published under GPL
|
|
|
|
*
|
|
|
|
* Latest version of the original script for joomla is available at:
|
|
|
|
* http://87.230.15.86/~dado/ejabberd/joomla-login
|
|
|
|
*
|
|
|
|
* Installation:
|
|
|
|
*
|
|
|
|
* - Change it's owner to whichever user is running the server, ie. ejabberd
|
|
|
|
* $ chown ejabberd:ejabberd /path/to/friendica/include/auth_ejabberd.php
|
|
|
|
*
|
|
|
|
* - Change the access mode so it is readable only to the user ejabberd and has exec
|
|
|
|
* $ chmod 700 /path/to/friendica/include/auth_ejabberd.php
|
|
|
|
*
|
|
|
|
* - Edit your ejabberd.cfg file, comment out your auth_method and add:
|
|
|
|
* {auth_method, external}.
|
|
|
|
* {extauth_program, "/path/to/friendica/include/auth_ejabberd.php"}.
|
|
|
|
*
|
|
|
|
* - 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)"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-30 06:01:26 +02:00
|
|
|
use Friendica\App;
|
2017-11-04 09:36:42 +01:00
|
|
|
use Friendica\Core\Config;
|
2017-04-30 06:01:26 +02:00
|
|
|
|
2013-06-22 17:34:52 +02:00
|
|
|
if (sizeof($_SERVER["argv"]) == 0)
|
|
|
|
die();
|
|
|
|
|
|
|
|
$directory = dirname($_SERVER["argv"][0]);
|
|
|
|
|
|
|
|
if (substr($directory, 0, 1) != "/")
|
|
|
|
$directory = $_SERVER["PWD"]."/".$directory;
|
|
|
|
|
|
|
|
$directory = realpath($directory."/..");
|
|
|
|
|
|
|
|
chdir($directory);
|
|
|
|
require_once("boot.php");
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
global $a;
|
2013-06-22 17:34:52 +02:00
|
|
|
|
2017-10-11 16:52:08 +02:00
|
|
|
if (empty($a)) {
|
2017-05-03 04:42:42 +02:00
|
|
|
$a = new App(dirname(__DIR__));
|
|
|
|
}
|
2013-06-22 17:34:52 +02:00
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
@include(".htconfig.php");
|
|
|
|
require_once("include/dba.php");
|
|
|
|
dba::connect($db_host, $db_user, $db_pass, $db_data);
|
|
|
|
unset($db_host, $db_user, $db_pass, $db_data);
|
2013-06-22 17:34:52 +02:00
|
|
|
|
2017-11-04 09:36:42 +01:00
|
|
|
$oAuth = new exAuth();
|
2013-06-22 17:34:52 +02:00
|
|
|
|
2016-09-22 04:57:40 +02:00
|
|
|
class exAuth {
|
2013-06-22 17:34:52 +02:00
|
|
|
private $bDebug;
|
|
|
|
|
2016-09-22 04:57:40 +02:00
|
|
|
/**
|
|
|
|
* @brief Create the class and do the authentification studd
|
|
|
|
*
|
|
|
|
* @param boolean $bDebug Debug mode
|
|
|
|
*/
|
2017-11-04 09:36:42 +01:00
|
|
|
public function __construct() {
|
2013-06-22 17:34:52 +02:00
|
|
|
// setter
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->bDebug = (int)Config::get('jabber', 'debug');
|
|
|
|
|
2013-06-22 17:34:52 +02:00
|
|
|
|
2017-11-04 09:36:42 +01:00
|
|
|
openlog('auth_ejabberd', LOG_PID, LOG_USER);
|
2013-06-22 17:34:52 +02:00
|
|
|
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "start");
|
2013-06-22 17:34:52 +02:00
|
|
|
|
2017-11-04 09:36:42 +01:00
|
|
|
// We are connected to the SQL server.
|
2017-11-07 22:53:11 +01:00
|
|
|
while (!feof(STDIN)) {
|
2016-09-22 04:57:40 +02:00
|
|
|
// Quit if the database connection went down
|
2017-10-11 14:56:36 +02:00
|
|
|
if (!dba::connected()) {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_ERR, "the database connection went down");
|
2016-09-22 04:57:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$iHeader = fgets(STDIN, 3);
|
|
|
|
$aLength = unpack("n", $iHeader);
|
|
|
|
$iLength = $aLength["1"];
|
|
|
|
|
|
|
|
// No data? Then quit
|
|
|
|
if ($iLength == 0) {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_ERR, "we got no data, quitting");
|
2016-09-22 04:57:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetching the data
|
|
|
|
$sData = fgets(STDIN, $iLength + 1);
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_DEBUG, "received data: ". $sData);
|
2016-09-22 04:57:40 +02:00
|
|
|
$aCommand = explode(":", $sData);
|
|
|
|
if (is_array($aCommand)) {
|
|
|
|
switch ($aCommand[0]) {
|
|
|
|
case "isuser":
|
|
|
|
// Check the existance of a given username
|
|
|
|
$this->isuser($aCommand);
|
2013-06-22 17:34:52 +02:00
|
|
|
break;
|
2016-09-22 04:57:40 +02:00
|
|
|
case "auth":
|
|
|
|
// Check if the givven password is correct
|
|
|
|
$this->auth($aCommand);
|
2013-06-22 17:34:52 +02:00
|
|
|
break;
|
2016-09-22 04:57:40 +02:00
|
|
|
case "setpass":
|
|
|
|
// We don't accept the setting of passwords here
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "setpass command disabled");
|
2016-09-22 04:57:40 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 0));
|
2013-06-22 17:34:52 +02:00
|
|
|
break;
|
2016-09-22 04:57:40 +02:00
|
|
|
default:
|
|
|
|
// We don't know the given command
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "unknown command ". $aCommand[0]);
|
2016-09-22 04:57:40 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 0));
|
2013-06-22 17:34:52 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-09-22 04:57:40 +02:00
|
|
|
} else {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "invalid command string ".$sData);
|
2016-09-22 04:57:40 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 0));
|
2013-06-22 17:34:52 +02:00
|
|
|
}
|
2017-11-07 22:53:11 +01:00
|
|
|
}
|
2013-06-22 17:34:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 04:57:40 +02:00
|
|
|
/**
|
|
|
|
* @brief Check if the given username exists
|
|
|
|
*
|
2016-09-22 20:07:32 +02:00
|
|
|
* @param array $aCommand The command array
|
2016-09-22 04:57:40 +02:00
|
|
|
*/
|
|
|
|
private function isuser($aCommand) {
|
2016-12-13 10:16:36 +01:00
|
|
|
$a = get_app();
|
2016-09-22 04:57:40 +02:00
|
|
|
|
|
|
|
// Check if there is a username
|
|
|
|
if (!isset($aCommand[1])) {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "invalid isuser command, no username given");
|
2016-09-22 04:57:40 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 0));
|
2016-09-22 20:18:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we check if the given user is valid
|
|
|
|
$sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
|
|
|
|
|
2016-12-20 08:08:03 +01:00
|
|
|
// Does the hostname match? So we try directly
|
|
|
|
if ($a->get_hostname() == $aCommand[2]) {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_INFO, "internal user check for ". $sUser."@".$aCommand[2]);
|
2016-09-22 20:18:26 +02:00
|
|
|
$sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_DEBUG, "using query ". $sQuery);
|
2016-09-22 20:18:26 +02:00
|
|
|
$r = q($sQuery);
|
|
|
|
$found = dbm::is_result($r);
|
2016-12-20 08:08:03 +01:00
|
|
|
} 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);
|
2016-09-22 20:18:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($found) {
|
|
|
|
// The user is okay
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "valid user: ". $sUser);
|
2016-09-22 20:18:26 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 1));
|
2016-09-22 04:57:40 +02:00
|
|
|
} else {
|
2016-09-22 20:18:26 +02:00
|
|
|
// The user isn't okay
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_WARNING, "invalid user: ". $sUser);
|
2016-09-22 20:18:26 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 0));
|
2016-09-22 04:57:40 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-22 17:34:52 +02:00
|
|
|
|
2016-09-22 04:57:40 +02:00
|
|
|
/**
|
|
|
|
* @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) {
|
|
|
|
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_INFO, "external user check for ".$user."@".$host);
|
|
|
|
|
2016-09-22 04:57:40 +02:00
|
|
|
$url = ($ssl ? "https":"http")."://".$host."/noscrape/".$user;
|
|
|
|
|
|
|
|
$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
|
|
|
|
*
|
2016-09-22 20:07:32 +02:00
|
|
|
* @param array $aCommand The command array
|
2016-09-22 04:57:40 +02:00
|
|
|
*/
|
|
|
|
private function auth($aCommand) {
|
2016-12-13 10:16:36 +01:00
|
|
|
$a = get_app();
|
2016-09-22 04:57:40 +02:00
|
|
|
|
|
|
|
// check user authentication
|
|
|
|
if (sizeof($aCommand) != 4) {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "invalid auth command, data missing");
|
2016-09-22 04:57:40 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 0));
|
2016-09-22 20:18:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now check if the password match
|
|
|
|
$sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
|
|
|
|
|
2016-12-20 08:08:03 +01:00
|
|
|
// Does the hostname match? So we try directly
|
|
|
|
if ($a->get_hostname() == $aCommand[2]) {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_INFO, "internal auth for ".$sUser."@".$aCommand[2]);
|
|
|
|
|
2016-09-22 20:18:26 +02:00
|
|
|
$sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_DEBUG, "using query ". $sQuery);
|
2016-09-22 20:18:26 +02:00
|
|
|
if ($oResult = q($sQuery)) {
|
|
|
|
$uid = $oResult[0]["uid"];
|
|
|
|
$Error = ($oResult[0]["password"] != hash('whirlpool',$aCommand[3]));
|
|
|
|
} else {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_WARNING, "invalid query: ". $sQuery);
|
2016-09-22 20:18:26 +02:00
|
|
|
$Error = true;
|
|
|
|
$uid = -1;
|
2016-09-22 04:57:40 +02:00
|
|
|
}
|
|
|
|
if ($Error) {
|
2016-09-22 20:18:26 +02:00
|
|
|
$oConfig = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = 'xmpp' AND `k`='password' LIMIT 1;", intval($uid));
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_INFO, "check against alternate password for ".$sUser."@".$aCommand[2]);
|
2016-09-22 20:18:26 +02:00
|
|
|
$Error = ($aCommand[3] != $oConfig[0]["v"]);
|
2016-09-22 04:57:40 +02:00
|
|
|
}
|
2016-12-20 08:08:03 +01:00
|
|
|
} 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);
|
2013-06-22 17:34:52 +02:00
|
|
|
}
|
2016-09-22 20:18:26 +02:00
|
|
|
|
|
|
|
if ($Error) {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_WARNING, "authentification failed for user ".$sUser."@". $aCommand[2]);
|
2016-09-22 20:18:26 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 0));
|
|
|
|
} else {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "authentificated user ".$sUser."@".$aCommand[2]);
|
2016-09-22 20:18:26 +02:00
|
|
|
fwrite(STDOUT, pack("nn", 2, 1));
|
|
|
|
}
|
2013-06-22 17:34:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 04:57:40 +02:00
|
|
|
/**
|
|
|
|
* @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?
|
|
|
|
*/
|
2016-09-13 22:29:47 +02:00
|
|
|
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);
|
|
|
|
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_INFO, "external auth for ".$user."@".$host." returned ".$http_code);
|
2016-09-22 04:57:40 +02:00
|
|
|
|
|
|
|
return ($http_code == 200);
|
2016-09-13 22:29:47 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 04:57:40 +02:00
|
|
|
/**
|
2017-11-04 09:36:42 +01:00
|
|
|
* @brief write data to the syslog
|
2016-09-22 04:57:40 +02:00
|
|
|
*
|
2017-11-04 09:36:42 +01:00
|
|
|
* @param integer $loglevel The syslog loglevel
|
|
|
|
* @param string $sMessage The syslog message
|
2016-09-22 04:57:40 +02:00
|
|
|
*/
|
2017-11-04 09:36:42 +01:00
|
|
|
private function writeLog($loglevel, $sMessage) {
|
|
|
|
if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
syslog($loglevel, $sMessage);
|
2013-06-22 17:34:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 04:57:40 +02:00
|
|
|
/**
|
2017-11-04 09:36:42 +01:00
|
|
|
* @brief destroy the class, close the syslog connection.
|
2016-09-22 04:57:40 +02:00
|
|
|
*/
|
|
|
|
public function __destruct() {
|
2017-11-04 09:36:42 +01:00
|
|
|
$this->writeLog(LOG_NOTICE, "stop");
|
|
|
|
closelog();
|
2016-09-22 04:57:40 +02:00
|
|
|
}
|
2013-06-22 17:34:52 +02:00
|
|
|
}
|