4 * ejabberd extauth script for the integration with friendica
\r
6 * Originally written for joomla by Dalibor Karlovic <dado@krizevci.info>
\r
7 * modified for Friendica by Michael Vogel <icarus@dabo.de>
\r
8 * published under GPL
\r
10 * Latest version of the original script for joomla is available at:
\r
11 * http://87.230.15.86/~dado/ejabberd/joomla-login
\r
15 * - Change it's owner to whichever user is running the server, ie. ejabberd
\r
16 * $ chown ejabberd:ejabberd /path/to/friendica/scripts/auth_ejabberd.php
\r
18 * - Change the access mode so it is readable only to the user ejabberd and has exec
\r
19 * $ chmod 700 /path/to/friendica/scripts/auth_ejabberd.php
\r
21 * - Edit your ejabberd.cfg file, comment out your auth_method and add:
\r
22 * {auth_method, external}.
\r
23 * {extauth_program, "/path/to/friendica/script/auth_ejabberd.php"}.
\r
25 * - Restart your ejabberd service, you should be able to login with your friendica auth info
\r
28 * - if your users have a space or a @ in their nickname, they'll run into trouble
\r
29 * registering with any client so they should be instructed to replace these chars
\r
30 * " " (space) is replaced with "%20"
\r
31 * "@" is replaced with "(a)"
\r
35 namespace Friendica\Util;
\r
37 use Friendica\Core\Config;
\r
38 use Friendica\Core\PConfig;
\r
39 use Friendica\Database\DBM;
\r
42 require_once 'include/dba.php';
\r
49 * @brief Create the class
\r
51 * @param boolean $bDebug Debug mode
\r
53 public function __construct()
\r
55 $this->bDebug = (int) Config::get('jabber', 'debug');
\r
57 openlog('auth_ejabberd', LOG_PID, LOG_USER);
\r
59 $this->writeLog(LOG_NOTICE, 'start');
\r
63 * @brief Standard input reading function, executes the auth with the provided
\r
68 public function readStdin()
\r
70 while (!feof(STDIN)) {
\r
71 // Quit if the database connection went down
\r
72 if (!dba::connected()) {
\r
73 $this->writeLog(LOG_ERR, 'the database connection went down');
\r
77 $iHeader = fgets(STDIN, 3);
\r
78 $aLength = unpack('n', $iHeader);
\r
79 $iLength = $aLength['1'];
\r
81 // No data? Then quit
\r
82 if ($iLength == 0) {
\r
83 $this->writeLog(LOG_ERR, 'we got no data, quitting');
\r
87 // Fetching the data
\r
88 $sData = fgets(STDIN, $iLength + 1);
\r
89 $this->writeLog(LOG_DEBUG, 'received data: ' . $sData);
\r
90 $aCommand = explode(':', $sData);
\r
91 if (is_array($aCommand)) {
\r
92 switch ($aCommand[0]) {
\r
94 // Check the existance of a given username
\r
95 $this->isUser($aCommand);
\r
98 // Check if the givven password is correct
\r
99 $this->auth($aCommand);
\r
102 // We don't accept the setting of passwords here
\r
103 $this->writeLog(LOG_NOTICE, 'setpass command disabled');
\r
104 fwrite(STDOUT, pack('nn', 2, 0));
\r
107 // We don't know the given command
\r
108 $this->writeLog(LOG_NOTICE, 'unknown command ' . $aCommand[0]);
\r
109 fwrite(STDOUT, pack('nn', 2, 0));
\r
113 $this->writeLog(LOG_NOTICE, 'invalid command string ' . $sData);
\r
114 fwrite(STDOUT, pack('nn', 2, 0));
\r
120 * @brief Check if the given username exists
\r
122 * @param array $aCommand The command array
\r
124 private function isUser(array $aCommand)
\r
128 // Check if there is a username
\r
129 if (!isset($aCommand[1])) {
\r
130 $this->writeLog(LOG_NOTICE, 'invalid isuser command, no username given');
\r
131 fwrite(STDOUT, pack('nn', 2, 0));
\r
135 // Now we check if the given user is valid
\r
136 $sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
\r
138 // Does the hostname match? So we try directly
\r
139 if ($a->get_hostname() == $aCommand[2]) {
\r
140 $this->writeLog(LOG_INFO, 'internal user check for ' . $sUser . '@' . $aCommand[2]);
\r
141 $found = dba::exists('user', ['nickname' => $sUser]);
\r
146 // If the hostnames doesn't match or there is some failure, we try to check remotely
\r
148 $found = $this->checkUser($aCommand[2], $aCommand[1], true);
\r
152 // The user is okay
\r
153 $this->writeLog(LOG_NOTICE, 'valid user: ' . $sUser);
\r
154 fwrite(STDOUT, pack('nn', 2, 1));
\r
156 // The user isn't okay
\r
157 $this->writeLog(LOG_WARNING, 'invalid user: ' . $sUser);
\r
158 fwrite(STDOUT, pack('nn', 2, 0));
\r
163 * @brief Check remote user existance via HTTP(S)
\r
165 * @param string $host The hostname
\r
166 * @param string $user Username
\r
167 * @param boolean $ssl Should the check be done via SSL?
\r
169 * @return boolean Was the user found?
\r
171 private function checkUser($host, $user, $ssl)
\r
173 $this->writeLog(LOG_INFO, 'external user check for ' . $user . '@' . $host);
\r
175 $url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;
\r
177 $data = z_fetch_url($url);
\r
179 if (!is_array($data)) {
\r
183 if ($data['return_code'] != '200') {
\r
187 $json = @json_decode($data['body']);
\r
188 if (!is_object($json)) {
\r
192 return $json->nick == $user;
\r
196 * @brief Authenticate the given user and password
\r
198 * @param array $aCommand The command array
\r
200 private function auth(array $aCommand)
\r
204 // check user authentication
\r
205 if (sizeof($aCommand) != 4) {
\r
206 $this->writeLog(LOG_NOTICE, 'invalid auth command, data missing');
\r
207 fwrite(STDOUT, pack('nn', 2, 0));
\r
211 // We now check if the password match
\r
212 $sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
\r
214 // Does the hostname match? So we try directly
\r
215 if ($a->get_hostname() == $aCommand[2]) {
\r
216 $this->writeLog(LOG_INFO, 'internal auth for ' . $sUser . '@' . $aCommand[2]);
\r
218 $aUser = dba::select('user', ['uid', 'password'], ['nickname' => $sUser], ['limit' => 1]);
\r
219 if (DBM::is_result($aUser)) {
\r
220 $uid = $aUser['uid'];
\r
221 $Error = $aUser['password'] != hash('whirlpool', $aCommand[3]);
\r
223 $this->writeLog(LOG_WARNING, 'user not found: ' . $sUser);
\r
228 $this->writeLog(LOG_INFO, 'check against alternate password for ' . $sUser . '@' . $aCommand[2]);
\r
229 $sPassword = PConfig::get($uid, 'xmpp', 'password', null, true);
\r
230 $Error = ($aCommand[3] != $sPassword);
\r
236 // If the hostnames doesn't match or there is some failure, we try to check remotely
\r
238 $Error = !$this->checkCredentials($aCommand[2], $aCommand[1], $aCommand[3], true);
\r
242 $this->writeLog(LOG_WARNING, 'authentification failed for user ' . $sUser . '@' . $aCommand[2]);
\r
243 fwrite(STDOUT, pack('nn', 2, 0));
\r
245 $this->writeLog(LOG_NOTICE, 'authentificated user ' . $sUser . '@' . $aCommand[2]);
\r
246 fwrite(STDOUT, pack('nn', 2, 1));
\r
251 * @brief Check remote credentials via HTTP(S)
\r
253 * @param string $host The hostname
\r
254 * @param string $user Username
\r
255 * @param string $password Password
\r
256 * @param boolean $ssl Should the check be done via SSL?
\r
258 * @return boolean Are the credentials okay?
\r
260 private function checkCredentials($host, $user, $password, $ssl)
\r
262 $url = ($ssl ? 'https' : 'http') . '://' . $host . '/api/account/verify_credentials.json';
\r
265 curl_setopt($ch, CURLOPT_URL, $url);
\r
266 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
\r
267 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
\r
268 curl_setopt($ch, CURLOPT_HEADER, true);
\r
269 curl_setopt($ch, CURLOPT_NOBODY, true);
\r
270 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
\r
271 curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);
\r
274 $curl_info = @curl_getinfo($ch);
\r
275 $http_code = $curl_info['http_code'];
\r
278 $this->writeLog(LOG_INFO, 'external auth for ' . $user . '@' . $host . ' returned ' . $http_code);
\r
280 return $http_code == 200;
\r
284 * @brief write data to the syslog
\r
286 * @param integer $loglevel The syslog loglevel
\r
287 * @param string $sMessage The syslog message
\r
289 private function writeLog($loglevel, $sMessage)
\r
291 if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
\r
294 syslog($loglevel, $sMessage);
\r
298 * @brief destroy the class, close the syslog connection.
\r
300 public function __destruct()
\r
302 $this->writeLog(LOG_NOTICE, 'stop');
\r