#!/usr/bin/php * conversion to PEAR MDB2 by Kae Verens * published under GPL * * Based on: * http://87.230.15.86/~dado/ejabberd/joomla-login * */ $sDBUser = ""; $sDBPassword = ""; $sDBHost = ""; $sDBName = ""; $sDBType = ""; // must be a valid MDB2 DB type // what query should be run to verify a user exists? $sDBQueryUser = "select * from users where userid=?"; // what query should be run to authenticate a user? $sDBAuthUser = "select * from users where pwd=? and userid=?"; // the logfile to which to write, should be writeable by the user which is running the server $sLogFile = "/var/log/ejabberd/exauth.log"; // set true to debug if needed $bDebug = true; $oAuth = new exAuth($sDBType, $sDBUser, $sDBPassword, $sDBHost, $sDBName, $sDBQueryUser, $sDBAuthUser, $sLogFile, $bDebug); class exAuth { private $sDBType; private $sDBUser; private $sDBPassword; private $sDBHost; private $sDBName; private $sDBQueryUser; private $sDBAuthUser; private $sLogFile; private $bDebug; private $oDB; private $rLogFile; public function __construct($sDBType, $sDBUser, $sDBPassword, $sDBHost, $sDBName, $sDBQueryUser, $sDBAuthUser, $sLogFile, $bDebug) { // setter $this->sDBType = $sDBType; $this->sDBUser = $sDBUser; $this->sDBPassword = $sDBPassword; $this->sDBHost = $sDBHost; $this->sDBName = $sDBName; $this->sDBQueryUser = $sDBQueryUser; $this->sDBAuthUser = $sDBAuthUser; $this->sLogFile = $sLogFile; $this->bDebug = $bDebug; // ovo ne provjeravamo jer ako ne mozes kreirati log file, onda si u kvascu :) $this->rLogFile = fopen($this->sLogFile, "a") or die("Error opening log file: ". $this->sLogFile); $this->writeLog("[exAuth] start"); if(!$this->dbconnect())exit; // ovdje bi trebali biti spojeni na DB, imati otvoren log i zavrtit cekalicu do { $iHeader = fgets(STDIN, 3); $aLength = unpack("n", $iHeader); $iLength = $aLength["1"]; if($iLength > 0) { // ovo znaci da smo nesto dobili $sData = fgets(STDIN, $iLength + 1); $this->writeDebugLog("[debug] received data: ". $sData); $aCommand = explode(":", $sData); if (is_array($aCommand)){ switch ($aCommand[0]){ case "isuser": // provjeravamo je li korisnik dobar if (!isset($aCommand[1])){ $this->writeLog("[exAuth] invalid isuser command, no username given"); fwrite(STDOUT, pack("nn", 2, 0)); } else { // ovdje provjeri je li korisnik OK $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]); $this->writeDebugLog("[debug] checking isuser for ". $sUser); $oQuery = $this->oDB->prepare($this->sDBQueryUser); $oExec= $oQuery->execute($sUser); $this->writeDebugLog("[debug] using query ". $oQuery->db->last_query); if ($oResult= $oExec->fetchRow()){ // korisnik OK $this->writeLog("[exAuth] valid user: ". $sUser); fwrite(STDOUT, pack("nn", 2, 1)); } else { // korisnik nije OK $this->writeLog("[exAuth] invalid user: ". $sUser); fwrite(STDOUT, pack("nn", 2, 0)); } } break; case "auth": // provjeravamo autentifikaciju korisnika if (sizeof($aCommand) != 4){ $this->writeLog("[exAuth] invalid auth command, data missing"); fwrite(STDOUT, pack("nn", 2, 0)); } else { // ovdje provjeri prijavu $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]); $this->writeDebugLog("[debug] doing auth for ". $sUser); $convertedpass=$this->oDB->quote(base64_encode(sha1($aCommand[3],true))); $converteduser=$this->oDB->quote($sUser); $oQuery=preg_replace("#(.*)\?(.*)\?(.*)#","$1$convertedpass$2$converteduser$3",$this->sDBAuthUser); # $oQuery=$this->oDB->prepare($this->sDBAuthUser,array('text'),array('text')); $this->writeDebugLog("[debug] using query ". $oQuery); $oQuery=$this->oDB->query($oQuery); $row=$oQuery->fetchRow(); if (count($row)){ // korisnik OK $this->writeLog("[exAuth] authentificated user ". $sUser ."@". $aCommand[2]); fwrite(STDOUT, pack("nn", 2, 1)); } else { // korisnik nije OK $this->writeLog("[exAuth] authentification failed for user ". $sUser ."@". $aCommand[2]); fwrite(STDOUT, pack("nn", 2, 0)); } } 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); } // zatvori DBi vezu if (is_object($this->oDB) && !PEAR::isError($this->oDB)){ $this->oDB->close(); } } private function writeLog($sMessage) { if (is_resource($this->rLogFile)) { fwrite($this->rLogFile, date("r") ." ". $sMessage ."\n"); } } private function writeDebugLog($sMessage) { if ($this->bDebug){ $this->writeLog($sMessage); } } private function dbconnect(){ if (!is_object($this->oDB)){ $dsn=array('phptype'=>$this->sDBType,'hostspec'=>$this->sDBHost,'username'=>$this->sDBUser,'password'=>$this->sDBPassword,'database'=>$this->sDBName); $this->oDB=MDB2::factory($dsn); if (PEAR::isError($this->oDB)) { $this->writeLog(sprintf("[DB] connection failed: %s\n", $this->oDB->getMessage())); $this->writeLog("[exAuth] killing"); return false; } else { $this->writeLog("[DB] connected"); return true; } } } }