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.

305 lines
8.4KB

  1. <?php
  2. /*
  3. * ejabberd extauth script for the integration with friendica
  4. *
  5. * Originally written for joomla by Dalibor Karlovic <dado@krizevci.info>
  6. * modified for Friendica by Michael Vogel <icarus@dabo.de>
  7. * published under GPL
  8. *
  9. * Latest version of the original script for joomla is available at:
  10. * http://87.230.15.86/~dado/ejabberd/joomla-login
  11. *
  12. * Installation:
  13. *
  14. * - Change it's owner to whichever user is running the server, ie. ejabberd
  15. * $ chown ejabberd:ejabberd /path/to/friendica/scripts/auth_ejabberd.php
  16. *
  17. * - Change the access mode so it is readable only to the user ejabberd and has exec
  18. * $ chmod 700 /path/to/friendica/scripts/auth_ejabberd.php
  19. *
  20. * - Edit your ejabberd.cfg file, comment out your auth_method and add:
  21. * {auth_method, external}.
  22. * {extauth_program, "/path/to/friendica/script/auth_ejabberd.php"}.
  23. *
  24. * - Restart your ejabberd service, you should be able to login with your friendica auth info
  25. *
  26. * Other hints:
  27. * - if your users have a space or a @ in their nickname, they'll run into trouble
  28. * registering with any client so they should be instructed to replace these chars
  29. * " " (space) is replaced with "%20"
  30. * "@" is replaced with "(a)"
  31. *
  32. */
  33. namespace Friendica\Util;
  34. use Friendica\Core\Config;
  35. use Friendica\Core\PConfig;
  36. use Friendica\Database\DBM;
  37. require_once 'include/dba.php';
  38. class ExAuth
  39. {
  40. private $bDebug;
  41. /**
  42. * @brief Create the class
  43. *
  44. * @param boolean $bDebug Debug mode
  45. */
  46. public function __construct()
  47. {
  48. $this->bDebug = (int) Config::get('jabber', 'debug');
  49. openlog('auth_ejabberd', LOG_PID, LOG_USER);
  50. $this->writeLog(LOG_NOTICE, 'start');
  51. }
  52. /**
  53. * @brief Standard input reading function, executes the auth with the provided
  54. * parameters
  55. *
  56. * @return null
  57. */
  58. public function readStdin()
  59. {
  60. while (!feof(STDIN)) {
  61. // Quit if the database connection went down
  62. if (!dba::connected()) {
  63. $this->writeLog(LOG_ERR, 'the database connection went down');
  64. return;
  65. }
  66. $iHeader = fgets(STDIN, 3);
  67. $aLength = unpack('n', $iHeader);
  68. $iLength = $aLength['1'];
  69. // No data? Then quit
  70. if ($iLength == 0) {
  71. $this->writeLog(LOG_ERR, 'we got no data, quitting');
  72. return;
  73. }
  74. // Fetching the data
  75. $sData = fgets(STDIN, $iLength + 1);
  76. $this->writeLog(LOG_DEBUG, 'received data: ' . $sData);
  77. $aCommand = explode(':', $sData);
  78. if (is_array($aCommand)) {
  79. switch ($aCommand[0]) {
  80. case 'isuser':
  81. // Check the existance of a given username
  82. $this->isUser($aCommand);
  83. break;
  84. case 'auth':
  85. // Check if the givven password is correct
  86. $this->auth($aCommand);
  87. break;
  88. case 'setpass':
  89. // We don't accept the setting of passwords here
  90. $this->writeLog(LOG_NOTICE, 'setpass command disabled');
  91. fwrite(STDOUT, pack('nn', 2, 0));
  92. break;
  93. default:
  94. // We don't know the given command
  95. $this->writeLog(LOG_NOTICE, 'unknown command ' . $aCommand[0]);
  96. fwrite(STDOUT, pack('nn', 2, 0));
  97. break;
  98. }
  99. } else {
  100. $this->writeLog(LOG_NOTICE, 'invalid command string ' . $sData);
  101. fwrite(STDOUT, pack('nn', 2, 0));
  102. }
  103. }
  104. }
  105. /**
  106. * @brief Check if the given username exists
  107. *
  108. * @param array $aCommand The command array
  109. */
  110. private function isUser(array $aCommand)
  111. {
  112. $a = get_app();
  113. // Check if there is a username
  114. if (!isset($aCommand[1])) {
  115. $this->writeLog(LOG_NOTICE, 'invalid isuser command, no username given');
  116. fwrite(STDOUT, pack('nn', 2, 0));
  117. return;
  118. }
  119. // Now we check if the given user is valid
  120. $sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
  121. // Does the hostname match? So we try directly
  122. if ($a->get_hostname() == $aCommand[2]) {
  123. $this->writeLog(LOG_INFO, 'internal user check for ' . $sUser . '@' . $aCommand[2]);
  124. $found = dba::exists('user', ['nickname' => $sUser]);
  125. } else {
  126. $found = false;
  127. }
  128. // If the hostnames doesn't match or there is some failure, we try to check remotely
  129. if (!$found) {
  130. $found = $this->checkUser($aCommand[2], $aCommand[1], true);
  131. }
  132. if ($found) {
  133. // The user is okay
  134. $this->writeLog(LOG_NOTICE, 'valid user: ' . $sUser);
  135. fwrite(STDOUT, pack('nn', 2, 1));
  136. } else {
  137. // The user isn't okay
  138. $this->writeLog(LOG_WARNING, 'invalid user: ' . $sUser);
  139. fwrite(STDOUT, pack('nn', 2, 0));
  140. }
  141. }
  142. /**
  143. * @brief Check remote user existance via HTTP(S)
  144. *
  145. * @param string $host The hostname
  146. * @param string $user Username
  147. * @param boolean $ssl Should the check be done via SSL?
  148. *
  149. * @return boolean Was the user found?
  150. */
  151. private function checkUser($host, $user, $ssl)
  152. {
  153. $this->writeLog(LOG_INFO, 'external user check for ' . $user . '@' . $host);
  154. $url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;
  155. $data = z_fetch_url($url);
  156. if (!is_array($data)) {
  157. return false;
  158. }
  159. if ($data['return_code'] != '200') {
  160. return false;
  161. }
  162. $json = @json_decode($data['body']);
  163. if (!is_object($json)) {
  164. return false;
  165. }
  166. return $json->nick == $user;
  167. }
  168. /**
  169. * @brief Authenticate the given user and password
  170. *
  171. * @param array $aCommand The command array
  172. */
  173. private function auth(array $aCommand)
  174. {
  175. $a = get_app();
  176. // check user authentication
  177. if (sizeof($aCommand) != 4) {
  178. $this->writeLog(LOG_NOTICE, 'invalid auth command, data missing');
  179. fwrite(STDOUT, pack('nn', 2, 0));
  180. return;
  181. }
  182. // We now check if the password match
  183. $sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
  184. // Does the hostname match? So we try directly
  185. if ($a->get_hostname() == $aCommand[2]) {
  186. $this->writeLog(LOG_INFO, 'internal auth for ' . $sUser . '@' . $aCommand[2]);
  187. $aUser = dba::select('user', ['uid', 'password'], ['nickname' => $sUser], ['limit' => 1]);
  188. if (DBM::is_result($aUser)) {
  189. $uid = $aUser['uid'];
  190. $Error = $aUser['password'] != hash('whirlpool', $aCommand[3]);
  191. } else {
  192. $this->writeLog(LOG_WARNING, 'user not found: ' . $sUser);
  193. $Error = true;
  194. $uid = -1;
  195. }
  196. if ($Error) {
  197. $this->writeLog(LOG_INFO, 'check against alternate password for ' . $sUser . '@' . $aCommand[2]);
  198. $sPassword = PConfig::get($uid, 'xmpp', 'password');
  199. $Error = ($aCommand[3] != $sPassword);
  200. }
  201. } else {
  202. $Error = true;
  203. }
  204. // If the hostnames doesn't match or there is some failure, we try to check remotely
  205. if ($Error) {
  206. $Error = !$this->checkCredentials($aCommand[2], $aCommand[1], $aCommand[3], true);
  207. }
  208. if ($Error) {
  209. $this->writeLog(LOG_WARNING, 'authentification failed for user ' . $sUser . '@' . $aCommand[2]);
  210. fwrite(STDOUT, pack('nn', 2, 0));
  211. } else {
  212. $this->writeLog(LOG_NOTICE, 'authentificated user ' . $sUser . '@' . $aCommand[2]);
  213. fwrite(STDOUT, pack('nn', 2, 1));
  214. }
  215. }
  216. /**
  217. * @brief Check remote credentials via HTTP(S)
  218. *
  219. * @param string $host The hostname
  220. * @param string $user Username
  221. * @param string $password Password
  222. * @param boolean $ssl Should the check be done via SSL?
  223. *
  224. * @return boolean Are the credentials okay?
  225. */
  226. private function checkCredentials($host, $user, $password, $ssl)
  227. {
  228. $url = ($ssl ? 'https' : 'http') . '://' . $host . '/api/account/verify_credentials.json';
  229. $ch = curl_init();
  230. curl_setopt($ch, CURLOPT_URL, $url);
  231. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  232. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  233. curl_setopt($ch, CURLOPT_HEADER, true);
  234. curl_setopt($ch, CURLOPT_NOBODY, true);
  235. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  236. curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);
  237. curl_exec($ch);
  238. $curl_info = @curl_getinfo($ch);
  239. $http_code = $curl_info['http_code'];
  240. curl_close($ch);
  241. $this->writeLog(LOG_INFO, 'external auth for ' . $user . '@' . $host . ' returned ' . $http_code);
  242. return $http_code == 200;
  243. }
  244. /**
  245. * @brief write data to the syslog
  246. *
  247. * @param integer $loglevel The syslog loglevel
  248. * @param string $sMessage The syslog message
  249. */
  250. private function writeLog($loglevel, $sMessage)
  251. {
  252. if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
  253. return;
  254. }
  255. syslog($loglevel, $sMessage);
  256. }
  257. /**
  258. * @brief destroy the class, close the syslog connection.
  259. */
  260. public function __destruct()
  261. {
  262. $this->writeLog(LOG_NOTICE, 'stop');
  263. closelog();
  264. }
  265. }