4 * ejabberd extauth script for the integration with friendica
6 * Originally written for joomla by Dalibor Karlovic <dado@krizevci.info>
7 * modified for Friendica by Michael Vogel <icarus@dabo.de>
10 * Latest version of the original script for joomla is available at:
11 * http://87.230.15.86/~dado/ejabberd/joomla-login
15 * - Change it's owner to whichever user is running the server, ie. ejabberd
16 * $ chown ejabberd:ejabberd /path/to/friendica/include/auth_ejabberd.php
18 * - Change the access mode so it is readable only to the user ejabberd and has exec
19 * $ chmod 700 /path/to/friendica/include/auth_ejabberd.php
21 * - Edit your ejabberd.cfg file, comment out your auth_method and add:
22 * {auth_method, external}.
23 * {extauth_program, "/path/to/friendica/include/auth_ejabberd.php"}.
25 * - Restart your ejabberd service, you should be able to login with your friendica auth info
28 * - if your users have a space or a @ in their nickname, they'll run into trouble
29 * registering with any client so they should be instructed to replace these chars
30 * " " (space) is replaced with "%20"
31 * "@" is replaced with "(a)"
35 if (sizeof($_SERVER["argv"]) == 0)
38 $directory = dirname($_SERVER["argv"][0]);
40 if (substr($directory, 0, 1) != "/")
41 $directory = $_SERVER["PWD"]."/".$directory;
43 $directory = realpath($directory."/..");
46 require_once("boot.php");
55 @include(".htconfig.php");
56 require_once("include/dba.php");
57 $db = new dba($db_host, $db_user, $db_pass, $db_data);
58 unset($db_host, $db_user, $db_pass, $db_data);
61 // the logfile to which to write, should be writeable by the user which is running the server
62 $sLogFile = get_config('jabber','logfile');
64 // set true to debug if needed
65 $bDebug = get_config('jabber','debug');
67 $oAuth = new exAuth($sLogFile, $bDebug);
76 public function __construct($sLogFile, $bDebug)
81 $this->sLogFile = $sLogFile;
82 $this->bDebug = $bDebug;
84 // ovo ne provjeravamo jer ako ne mozes kreirati log file, onda si u kvascu :)
85 if ($this->sLogFile != '')
86 $this->rLogFile = fopen($this->sLogFile, "a") or die("Error opening log file: ". $this->sLogFile);
88 $this->writeLog("[exAuth] start");
90 // ovdje bi trebali biti spojeni na MySQL, imati otvoren log i zavrtit cekalicu
92 $iHeader = fgets(STDIN, 3);
93 $aLength = unpack("n", $iHeader);
94 $iLength = $aLength["1"];
96 // ovo znaci da smo nesto dobili
97 $sData = fgets(STDIN, $iLength + 1);
98 $this->writeDebugLog("[debug] received data: ". $sData);
99 $aCommand = explode(":", $sData);
100 if (is_array($aCommand)){
101 switch ($aCommand[0]){
103 // provjeravamo je li korisnik dobar
104 if (!isset($aCommand[1])){
105 $this->writeLog("[exAuth] invalid isuser command, no username given");
106 fwrite(STDOUT, pack("nn", 2, 0));
108 // ovdje provjeri je li korisnik OK
109 $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
110 $this->writeDebugLog("[debug] checking isuser for ". $sUser);
111 $sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='". $db->escape($sUser) ."'";
112 $this->writeDebugLog("[debug] using query ". $sQuery);
113 if ($oResult = q($sQuery)){
116 $this->writeLog("[exAuth] valid user: ". $sUser);
117 fwrite(STDOUT, pack("nn", 2, 1));
120 $this->writeLog("[exAuth] invalid user: ". $sUser);
121 fwrite(STDOUT, pack("nn", 2, 0));
125 $this->writeLog("[MySQL] invalid query: ". $sQuery);
126 fwrite(STDOUT, pack("nn", 2, 0));
131 // provjeravamo autentifikaciju korisnika
132 if (sizeof($aCommand) != 4){
133 $this->writeLog("[exAuth] invalid auth command, data missing");
134 fwrite(STDOUT, pack("nn", 2, 0));
136 // ovdje provjeri prijavu
137 $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
138 $this->writeDebugLog("[debug] doing auth for ".$sUser."@".$aCommand[2]);
140 // If the hostnames doesn't match, we try to authenticate remotely
141 if ($a->get_hostname() != $aCommand[2])
142 $Error = !$this->check_credentials($aCommand[2], $aCommand[1], $aCommand[3], true);
145 //$sQuery = "SELECT `uid`, `password` FROM `user` WHERE `password`='".hash('whirlpool',$aCommand[3])."' AND `nickname`='". $db->escape($sUser) ."'";
146 $sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='". $db->escape($sUser) ."'";
147 $this->writeDebugLog("[debug] using query ". $sQuery);
148 if ($oResult = q($sQuery)){
149 $uid = $oResult[0]["uid"];
150 $Error = ($oResult[0]["password"] != hash('whirlpool',$aCommand[3]));
152 $this->writeLog("[MySQL] invalid query: ". $sQuery);
157 $oConfig = q("SELECT `v` FROM `pconfig` WHERE `uid`=%d AND `cat` = 'xmpp' AND `k`='password' LIMIT 1;", intval($uid));
158 $this->writeLog("[exAuth] got password ".$oConfig[0]["v"]);
159 $Error = ($aCommand[3] != $oConfig[0]["v"]);
163 $this->writeLog("[exAuth] authentification failed for user ". $sUser ."@". $aCommand[2]);
164 fwrite(STDOUT, pack("nn", 2, 0));
166 $this->writeLog("[exAuth] authentificated user ". $sUser ."@". $aCommand[2]);
167 fwrite(STDOUT, pack("nn", 2, 1));
172 // postavljanje zaporke, onemoguceno
173 $this->writeLog("[exAuth] setpass command disabled");
174 fwrite(STDOUT, pack("nn", 2, 0));
177 // ako je uhvaceno ista drugo
178 $this->writeLog("[exAuth] unknown command ". $aCommand[0]);
179 fwrite(STDOUT, pack("nn", 2, 0));
183 $this->writeDebugLog("[debug] invalid command string");
184 fwrite(STDOUT, pack("nn", 2, 0));
194 public function __destruct()
197 $this->writeLog("[exAuth] stop");
199 if (is_resource($this->rLogFile)){
200 fclose($this->rLogFile);
204 private function check_credentials($host, $user, $password, $ssl) {
206 $url = ($ssl ? "https":"http")."://".$host."/api/account/verify_credentials.json";
209 curl_setopt($ch, CURLOPT_URL, $url);
210 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
211 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
212 curl_setopt($ch, CURLOPT_HEADER, true);
213 curl_setopt($ch, CURLOPT_NOBODY, true);
214 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
215 curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
217 $header = curl_exec($ch);
218 $curl_info = @curl_getinfo($ch);
219 $http_code = $curl_info["http_code"];
222 return($http_code == 200);
225 private function writeLog($sMessage)
227 if (is_resource($this->rLogFile)) {
228 fwrite($this->rLogFile, date("r") ." ". $sMessage ."\n");
232 private function writeDebugLog($sMessage)
235 $this->writeLog($sMessage);