Merge pull request #2787 from annando/1609-xmpp-auth

Reworked ejabberd authentification
This commit is contained in:
Tobias Diekershoff 2016-09-22 23:28:46 +02:00 committed by GitHub
commit 4948460232
2 changed files with 218 additions and 126 deletions

View file

@ -47,9 +47,8 @@ require_once("boot.php");
global $a, $db; global $a, $db;
if(is_null($a)) { if (is_null($a))
$a = new App; $a = new App;
}
if (is_null($db)) { if (is_null($db)) {
@include(".htconfig.php"); @include(".htconfig.php");
@ -66,74 +65,165 @@ $bDebug = get_config('jabber','debug');
$oAuth = new exAuth($sLogFile, $bDebug); $oAuth = new exAuth($sLogFile, $bDebug);
class exAuth class exAuth {
{
private $sLogFile; private $sLogFile;
private $bDebug; private $bDebug;
private $rLogFile; private $rLogFile;
public function __construct($sLogFile, $bDebug) /**
{ * @brief Create the class and do the authentification studd
global $a, $db; *
* @param string $sLogFile The logfile name
* @param boolean $bDebug Debug mode
*/
public function __construct($sLogFile, $bDebug) {
global $db;
// setter // setter
$this->sLogFile = $sLogFile; $this->sLogFile = $sLogFile;
$this->bDebug = $bDebug; $this->bDebug = $bDebug;
// ovo ne provjeravamo jer ako ne mozes kreirati log file, onda si u kvascu :) // Open the logfile if the logfile name is defined
if ($this->sLogFile != '') if ($this->sLogFile != '')
$this->rLogFile = fopen($this->sLogFile, "a") or die("Error opening log file: ". $this->sLogFile); $this->rLogFile = fopen($this->sLogFile, "a") or die("Error opening log file: ". $this->sLogFile);
$this->writeLog("[exAuth] start"); $this->writeLog("[exAuth] start");
// ovdje bi trebali biti spojeni na MySQL, imati otvoren log i zavrtit cekalicu // We are connected to the SQL server and are having a log file.
do { do {
// Quit if the database connection went down
if (!$db->connected()) {
$this->writeDebugLog("[debug] the database connection went down");
return;
}
$iHeader = fgets(STDIN, 3); $iHeader = fgets(STDIN, 3);
$aLength = unpack("n", $iHeader); $aLength = unpack("n", $iHeader);
$iLength = $aLength["1"]; $iLength = $aLength["1"];
if($iLength > 0) {
// ovo znaci da smo nesto dobili // No data? Then quit
if ($iLength == 0) {
$this->writeDebugLog("[debug] we got no data");
return;
}
// Fetching the data
$sData = fgets(STDIN, $iLength + 1); $sData = fgets(STDIN, $iLength + 1);
$this->writeDebugLog("[debug] received data: ". $sData); $this->writeDebugLog("[debug] received data: ". $sData);
$aCommand = explode(":", $sData); $aCommand = explode(":", $sData);
if (is_array($aCommand)) { if (is_array($aCommand)) {
switch ($aCommand[0]) { switch ($aCommand[0]) {
case "isuser": case "isuser":
// provjeravamo je li korisnik dobar // 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("[exAuth] setpass command disabled");
fwrite(STDOUT, pack("nn", 2, 0));
break;
default:
// We don't know the given command
$this->writeLog("[exAuth] unknown command ". $aCommand[0]);
fwrite(STDOUT, pack("nn", 2, 0));
break;
}
} else {
$this->writeDebugLog("[debug] invalid command string");
fwrite(STDOUT, pack("nn", 2, 0));
}
} while (true);
}
/**
* @brief Check if the given username exists
*
* @param array $aCommand The command array
*/
private function isuser($aCommand) {
global $a;
// Check if there is a username
if (!isset($aCommand[1])) { if (!isset($aCommand[1])) {
$this->writeLog("[exAuth] invalid isuser command, no username given"); $this->writeLog("[exAuth] invalid isuser command, no username given");
fwrite(STDOUT, pack("nn", 2, 0)); fwrite(STDOUT, pack("nn", 2, 0));
} else { return;
// ovdje provjeri je li korisnik OK }
// Now we check if the given user is valid
$sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]); $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
$this->writeDebugLog("[debug] checking isuser for ". $sUser); $this->writeDebugLog("[debug] checking isuser for ". $sUser."@".$aCommand[2]);
$sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='". $db->escape($sUser) ."'";
// If the hostnames doesn't match, we try to check remotely
if ($a->get_hostname() != $aCommand[2])
$found = $this->check_user($aCommand[2], $aCommand[1], true);
else {
$sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
$this->writeDebugLog("[debug] using query ". $sQuery); $this->writeDebugLog("[debug] using query ". $sQuery);
if ($oResult = q($sQuery)){ $r = q($sQuery);
if ($oResult) { $found = dbm::is_result($r);
// korisnik OK }
if ($found) {
// The user is okay
$this->writeLog("[exAuth] valid user: ". $sUser); $this->writeLog("[exAuth] valid user: ". $sUser);
fwrite(STDOUT, pack("nn", 2, 1)); fwrite(STDOUT, pack("nn", 2, 1));
} else { } else {
// korisnik nije OK // The user isn't okay
$this->writeLog("[exAuth] invalid user: ". $sUser); $this->writeLog("[exAuth] invalid user: ". $sUser);
fwrite(STDOUT, pack("nn", 2, 0)); fwrite(STDOUT, pack("nn", 2, 0));
} }
//$oResult->close();
} else {
$this->writeLog("[MySQL] invalid query: ". $sQuery);
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) {
$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);
} }
break;
case "auth": /**
// provjeravamo autentifikaciju korisnika * @brief Authenticate the givven user and password
*
* @param array $aCommand The command array
*/
private function auth($aCommand) {
global $a;
// check user authentication
if (sizeof($aCommand) != 4) { if (sizeof($aCommand) != 4) {
$this->writeLog("[exAuth] invalid auth command, data missing"); $this->writeLog("[exAuth] invalid auth command, data missing");
fwrite(STDOUT, pack("nn", 2, 0)); fwrite(STDOUT, pack("nn", 2, 0));
} else { return;
// ovdje provjeri prijavu }
// We now check if the password match
$sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]); $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
$this->writeDebugLog("[debug] doing auth for ".$sUser."@".$aCommand[2]); $this->writeDebugLog("[debug] doing auth for ".$sUser."@".$aCommand[2]);
@ -141,9 +231,7 @@ class exAuth
if ($a->get_hostname() != $aCommand[2]) if ($a->get_hostname() != $aCommand[2])
$Error = !$this->check_credentials($aCommand[2], $aCommand[1], $aCommand[3], true); $Error = !$this->check_credentials($aCommand[2], $aCommand[1], $aCommand[3], true);
else { else {
$sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
//$sQuery = "SELECT `uid`, `password` FROM `user` WHERE `password`='".hash('whirlpool',$aCommand[3])."' AND `nickname`='". $db->escape($sUser) ."'";
$sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='". $db->escape($sUser) ."'";
$this->writeDebugLog("[debug] using query ". $sQuery); $this->writeDebugLog("[debug] using query ". $sQuery);
if ($oResult = q($sQuery)) { if ($oResult = q($sQuery)) {
$uid = $oResult[0]["uid"]; $uid = $oResult[0]["uid"];
@ -159,6 +247,7 @@ class exAuth
$Error = ($aCommand[3] != $oConfig[0]["v"]); $Error = ($aCommand[3] != $oConfig[0]["v"]);
} }
} }
if ($Error) { if ($Error) {
$this->writeLog("[exAuth] authentification failed for user ".$sUser."@". $aCommand[2]); $this->writeLog("[exAuth] authentification failed for user ".$sUser."@". $aCommand[2]);
fwrite(STDOUT, pack("nn", 2, 0)); fwrite(STDOUT, pack("nn", 2, 0));
@ -167,41 +256,19 @@ class exAuth
fwrite(STDOUT, pack("nn", 2, 1)); fwrite(STDOUT, pack("nn", 2, 1));
} }
} }
break;
case "setpass":
// postavljanje zaporke, onemoguceno
$this->writeLog("[exAuth] setpass command disabled");
fwrite(STDOUT, pack("nn", 2, 0));
break;
default:
// ako je uhvaceno ista drugo
$this->writeLog("[exAuth] unknown command ". $aCommand[0]);
fwrite(STDOUT, pack("nn", 2, 0));
break;
}
} else {
$this->writeDebugLog("[debug] invalid command string");
fwrite(STDOUT, pack("nn", 2, 0));
}
}
unset ($iHeader);
unset ($aLength);
unset ($iLength);
unset($aCommand);
} while (true);
}
public function __destruct()
{
// zatvori log file
$this->writeLog("[exAuth] stop");
if (is_resource($this->rLogFile)){
fclose($this->rLogFile);
}
}
/**
* @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) { private function check_credentials($host, $user, $password, $ssl) {
$this->writeDebugLog("[debug] check credentials for user ".$user." on ".$host);
$url = ($ssl ? "https":"http")."://".$host."/api/account/verify_credentials.json"; $url = ($ssl ? "https":"http")."://".$host."/api/account/verify_credentials.json";
@ -219,24 +286,40 @@ class exAuth
$http_code = $curl_info["http_code"]; $http_code = $curl_info["http_code"];
curl_close($ch); curl_close($ch);
$this->writeDebugLog("[debug] got HTTP code ".$http_code);
return ($http_code == 200); return ($http_code == 200);
} }
private function writeLog($sMessage) /**
{ * @brief write data to the logfile
if (is_resource($this->rLogFile)) { *
* @param string $sMessage The logfile message
*/
private function writeLog($sMessage) {
if (is_resource($this->rLogFile))
fwrite($this->rLogFile, date("r")." ".$sMessage."\n"); fwrite($this->rLogFile, date("r")." ".$sMessage."\n");
} }
}
private function writeDebugLog($sMessage) /**
{ * @brief write debug data to the logfile
if ($this->bDebug){ *
* @param string $sMessage The logfile message
*/
private function writeDebugLog($sMessage) {
if ($this->bDebug)
$this->writeLog($sMessage); $this->writeLog($sMessage);
} }
}
/**
* @brief destroy the class
*/
public function __destruct() {
// close the log file
$this->writeLog("[exAuth] stop");
if (is_resource($this->rLogFile))
fclose($this->rLogFile);
}
} }
?> ?>

View file

@ -249,6 +249,15 @@ class dba {
} }
} }
function connected() {
if ($this->mysqli)
$connected = $this->db->ping();
else
$connected = mysql_ping($this->db);
return $connected;
}
function __destruct() { function __destruct() {
if ($this->db) if ($this->db)
if($this->mysqli) if($this->mysqli)