Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.5KB

  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. * ejabberd extauth script for the integration with friendica
  5. *
  6. * Originally written for joomla by Dalibor Karlovic <dado@krizevci.info>
  7. * modified for Friendica by Michael Vogel <icarus@dabo.de>
  8. * published under GPL
  9. *
  10. * Latest version of the original script for joomla is available at:
  11. * http://87.230.15.86/~dado/ejabberd/joomla-login
  12. *
  13. * Installation:
  14. *
  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
  17. *
  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
  20. *
  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"}.
  24. *
  25. * - Restart your ejabberd service, you should be able to login with your friendica auth info
  26. *
  27. * Other hints:
  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)"
  32. *
  33. */
  34. if (sizeof($_SERVER["argv"]) == 0)
  35. die();
  36. $directory = dirname($_SERVER["argv"][0]);
  37. if (substr($directory, 0, 1) != "/")
  38. $directory = $_SERVER["PWD"]."/".$directory;
  39. $directory = realpath($directory."/..");
  40. chdir($directory);
  41. require_once("boot.php");
  42. global $a, $db;
  43. if(is_null($a)) {
  44. $a = new App;
  45. }
  46. if(is_null($db)) {
  47. @include(".htconfig.php");
  48. require_once("include/dba.php");
  49. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  50. unset($db_host, $db_user, $db_pass, $db_data);
  51. };
  52. // the logfile to which to write, should be writeable by the user which is running the server
  53. $sLogFile = get_config('jabber','logfile');
  54. // set true to debug if needed
  55. $bDebug = get_config('jabber','debug');
  56. $oAuth = new exAuth($sLogFile, $bDebug);
  57. class exAuth
  58. {
  59. private $sLogFile;
  60. private $bDebug;
  61. private $rLogFile;
  62. public function __construct($sLogFile, $bDebug)
  63. {
  64. global $a, $db;
  65. // setter
  66. $this->sLogFile = $sLogFile;
  67. $this->bDebug = $bDebug;
  68. // ovo ne provjeravamo jer ako ne mozes kreirati log file, onda si u kvascu :)
  69. if ($this->sLogFile != '')
  70. $this->rLogFile = fopen($this->sLogFile, "a") or die("Error opening log file: ". $this->sLogFile);
  71. $this->writeLog("[exAuth] start");
  72. // ovdje bi trebali biti spojeni na MySQL, imati otvoren log i zavrtit cekalicu
  73. do {
  74. $iHeader = fgets(STDIN, 3);
  75. $aLength = unpack("n", $iHeader);
  76. $iLength = $aLength["1"];
  77. if($iLength > 0) {
  78. // ovo znaci da smo nesto dobili
  79. $sData = fgets(STDIN, $iLength + 1);
  80. $this->writeDebugLog("[debug] received data: ". $sData);
  81. $aCommand = explode(":", $sData);
  82. if (is_array($aCommand)){
  83. switch ($aCommand[0]){
  84. case "isuser":
  85. // provjeravamo je li korisnik dobar
  86. if (!isset($aCommand[1])){
  87. $this->writeLog("[exAuth] invalid isuser command, no username given");
  88. fwrite(STDOUT, pack("nn", 2, 0));
  89. } else {
  90. // ovdje provjeri je li korisnik OK
  91. $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
  92. $this->writeDebugLog("[debug] checking isuser for ". $sUser);
  93. $sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='". $db->escape($sUser) ."'";
  94. $this->writeDebugLog("[debug] using query ". $sQuery);
  95. if ($oResult = q($sQuery)){
  96. if ($oResult) {
  97. // korisnik OK
  98. $this->writeLog("[exAuth] valid user: ". $sUser);
  99. fwrite(STDOUT, pack("nn", 2, 1));
  100. } else {
  101. // korisnik nije OK
  102. $this->writeLog("[exAuth] invalid user: ". $sUser);
  103. fwrite(STDOUT, pack("nn", 2, 0));
  104. }
  105. //$oResult->close();
  106. } else {
  107. $this->writeLog("[MySQL] invalid query: ". $sQuery);
  108. fwrite(STDOUT, pack("nn", 2, 0));
  109. }
  110. }
  111. break;
  112. case "auth":
  113. // provjeravamo autentifikaciju korisnika
  114. if (sizeof($aCommand) != 4){
  115. $this->writeLog("[exAuth] invalid auth command, data missing");
  116. fwrite(STDOUT, pack("nn", 2, 0));
  117. } else {
  118. // ovdje provjeri prijavu
  119. $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
  120. $this->writeDebugLog("[debug] doing auth for ".$sUser."@".$aCommand[2]);
  121. // If the hostnames doesn't match, we try to authenticate remotely
  122. if ($a->get_hostname() != $aCommand[2])
  123. $Error = !$this->check_credentials($aCommand[2], $aCommand[1], $aCommand[3], true);
  124. else {
  125. //$sQuery = "SELECT `uid`, `password` FROM `user` WHERE `password`='".hash('whirlpool',$aCommand[3])."' AND `nickname`='". $db->escape($sUser) ."'";
  126. $sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='". $db->escape($sUser) ."'";
  127. $this->writeDebugLog("[debug] using query ". $sQuery);
  128. if ($oResult = q($sQuery)){
  129. $uid = $oResult[0]["uid"];
  130. $Error = ($oResult[0]["password"] != hash('whirlpool',$aCommand[3]));
  131. } else {
  132. $this->writeLog("[MySQL] invalid query: ". $sQuery);
  133. $Error = true;
  134. $uid = -1;
  135. }
  136. if ($Error) {
  137. $oConfig = q("SELECT `v` FROM `pconfig` WHERE `uid`=%d AND `cat` = 'xmpp' AND `k`='password' LIMIT 1;", intval($uid));
  138. $this->writeLog("[exAuth] got password ".$oConfig[0]["v"]);
  139. $Error = ($aCommand[3] != $oConfig[0]["v"]);
  140. }
  141. }
  142. if ($Error) {
  143. $this->writeLog("[exAuth] authentification failed for user ". $sUser ."@". $aCommand[2]);
  144. fwrite(STDOUT, pack("nn", 2, 0));
  145. } else {
  146. $this->writeLog("[exAuth] authentificated user ". $sUser ."@". $aCommand[2]);
  147. fwrite(STDOUT, pack("nn", 2, 1));
  148. }
  149. }
  150. break;
  151. case "setpass":
  152. // postavljanje zaporke, onemoguceno
  153. $this->writeLog("[exAuth] setpass command disabled");
  154. fwrite(STDOUT, pack("nn", 2, 0));
  155. break;
  156. default:
  157. // ako je uhvaceno ista drugo
  158. $this->writeLog("[exAuth] unknown command ". $aCommand[0]);
  159. fwrite(STDOUT, pack("nn", 2, 0));
  160. break;
  161. }
  162. } else {
  163. $this->writeDebugLog("[debug] invalid command string");
  164. fwrite(STDOUT, pack("nn", 2, 0));
  165. }
  166. }
  167. unset ($iHeader);
  168. unset ($aLength);
  169. unset ($iLength);
  170. unset($aCommand);
  171. } while (true);
  172. }
  173. public function __destruct()
  174. {
  175. // zatvori log file
  176. $this->writeLog("[exAuth] stop");
  177. if (is_resource($this->rLogFile)){
  178. fclose($this->rLogFile);
  179. }
  180. }
  181. private function check_credentials($host, $user, $password, $ssl) {
  182. $url = ($ssl ? "https":"http")."://".$host."/api/account/verify_credentials.json";
  183. $ch = curl_init();
  184. curl_setopt($ch, CURLOPT_URL, $url);
  185. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  186. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  187. curl_setopt($ch, CURLOPT_HEADER, true);
  188. curl_setopt($ch, CURLOPT_NOBODY, true);
  189. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  190. curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
  191. $header = curl_exec($ch);
  192. $curl_info = @curl_getinfo($ch);
  193. $http_code = $curl_info["http_code"];
  194. curl_close($ch);
  195. return($http_code == 200);
  196. }
  197. private function writeLog($sMessage)
  198. {
  199. if (is_resource($this->rLogFile)) {
  200. fwrite($this->rLogFile, date("r") ." ". $sMessage ."\n");
  201. }
  202. }
  203. private function writeDebugLog($sMessage)
  204. {
  205. if ($this->bDebug){
  206. $this->writeLog($sMessage);
  207. }
  208. }
  209. }
  210. ?>