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
40 use Friendica\Model\User;
\r
43 require_once 'include/dba.php';
\r
50 * @brief Create the class
\r
52 * @param boolean $bDebug Debug mode
\r
54 public function __construct()
\r
56 $this->bDebug = (int) Config::get('jabber', 'debug');
\r
58 openlog('auth_ejabberd', LOG_PID, LOG_USER);
\r
60 $this->writeLog(LOG_NOTICE, 'start');
\r
64 * @brief Standard input reading function, executes the auth with the provided
\r
69 public function readStdin()
\r
71 while (!feof(STDIN)) {
\r
72 // Quit if the database connection went down
\r
73 if (!dba::connected()) {
\r
74 $this->writeLog(LOG_ERR, 'the database connection went down');
\r
78 $iHeader = fgets(STDIN, 3);
\r
79 $aLength = unpack('n', $iHeader);
\r
80 $iLength = $aLength['1'];
\r
82 // No data? Then quit
\r
83 if ($iLength == 0) {
\r
84 $this->writeLog(LOG_ERR, 'we got no data, quitting');
\r
88 // Fetching the data
\r
89 $sData = fgets(STDIN, $iLength + 1);
\r
90 $this->writeLog(LOG_DEBUG, 'received data: ' . $sData);
\r
91 $aCommand = explode(':', $sData);
\r
92 if (is_array($aCommand)) {
\r
93 switch ($aCommand[0]) {
\r
95 // Check the existance of a given username
\r
96 $this->isUser($aCommand);
\r
99 // Check if the givven password is correct
\r
100 $this->auth($aCommand);
\r
103 // We don't accept the setting of passwords here
\r
104 $this->writeLog(LOG_NOTICE, 'setpass command disabled');
\r
105 fwrite(STDOUT, pack('nn', 2, 0));
\r
108 // We don't know the given command
\r
109 $this->writeLog(LOG_NOTICE, 'unknown command ' . $aCommand[0]);
\r
110 fwrite(STDOUT, pack('nn', 2, 0));
\r
114 $this->writeLog(LOG_NOTICE, 'invalid command string ' . $sData);
\r
115 fwrite(STDOUT, pack('nn', 2, 0));
\r
121 * @brief Check if the given username exists
\r
123 * @param array $aCommand The command array
\r
125 private function isUser(array $aCommand)
\r
129 // Check if there is a username
\r
130 if (!isset($aCommand[1])) {
\r
131 $this->writeLog(LOG_NOTICE, 'invalid isuser command, no username given');
\r
132 fwrite(STDOUT, pack('nn', 2, 0));
\r
136 // Now we check if the given user is valid
\r
137 $sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
\r
139 // Does the hostname match? So we try directly
\r
140 if ($a->get_hostname() == $aCommand[2]) {
\r
141 $this->writeLog(LOG_INFO, 'internal user check for ' . $sUser . '@' . $aCommand[2]);
\r
142 $found = dba::exists('user', ['nickname' => $sUser]);
\r
147 // If the hostnames doesn't match or there is some failure, we try to check remotely
\r
149 $found = $this->checkUser($aCommand[2], $aCommand[1], true);
\r
153 // The user is okay
\r
154 $this->writeLog(LOG_NOTICE, 'valid user: ' . $sUser);
\r
155 fwrite(STDOUT, pack('nn', 2, 1));
\r
157 // The user isn't okay
\r
158 $this->writeLog(LOG_WARNING, 'invalid user: ' . $sUser);
\r
159 fwrite(STDOUT, pack('nn', 2, 0));
\r
164 * @brief Check remote user existance via HTTP(S)
\r
166 * @param string $host The hostname
\r
167 * @param string $user Username
\r
168 * @param boolean $ssl Should the check be done via SSL?
\r
170 * @return boolean Was the user found?
\r
172 private function checkUser($host, $user, $ssl)
\r
174 $this->writeLog(LOG_INFO, 'external user check for ' . $user . '@' . $host);
\r
176 $url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;
\r
178 $data = z_fetch_url($url);
\r
180 if (!is_array($data)) {
\r
184 if ($data['return_code'] != '200') {
\r
188 $json = @json_decode($data['body']);
\r
189 if (!is_object($json)) {
\r
193 return $json->nick == $user;
\r
197 * @brief Authenticate the given user and password
\r
199 * @param array $aCommand The command array
\r
201 private function auth(array $aCommand)
\r
205 // check user authentication
\r
206 if (sizeof($aCommand) != 4) {
\r
207 $this->writeLog(LOG_NOTICE, 'invalid auth command, data missing');
\r
208 fwrite(STDOUT, pack('nn', 2, 0));
\r
212 // We now check if the password match
\r
213 $sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
\r
215 // Does the hostname match? So we try directly
\r
216 if ($a->get_hostname() == $aCommand[2]) {
\r
217 $this->writeLog(LOG_INFO, 'internal auth for ' . $sUser . '@' . $aCommand[2]);
\r
219 $aUser = dba::select('user', ['uid', 'password'], ['nickname' => $sUser], ['limit' => 1]);
\r
220 if (DBM::is_result($aUser)) {
\r
221 $uid = User::authenticate($aUser, $aCommand[3]);
\r
222 $Error = $uid === false;
\r
224 $this->writeLog(LOG_WARNING, 'user not found: ' . $sUser);
\r
229 $this->writeLog(LOG_INFO, 'check against alternate password for ' . $sUser . '@' . $aCommand[2]);
\r
230 $sPassword = PConfig::get($uid, 'xmpp', 'password', null, true);
\r
231 $Error = ($aCommand[3] != $sPassword);
\r
237 // If the hostnames doesn't match or there is some failure, we try to check remotely
\r
239 $Error = !$this->checkCredentials($aCommand[2], $aCommand[1], $aCommand[3], true);
\r
243 $this->writeLog(LOG_WARNING, 'authentification failed for user ' . $sUser . '@' . $aCommand[2]);
\r
244 fwrite(STDOUT, pack('nn', 2, 0));
\r
246 $this->writeLog(LOG_NOTICE, 'authentificated user ' . $sUser . '@' . $aCommand[2]);
\r
247 fwrite(STDOUT, pack('nn', 2, 1));
\r
252 * @brief Check remote credentials via HTTP(S)
\r
254 * @param string $host The hostname
\r
255 * @param string $user Username
\r
256 * @param string $password Password
\r
257 * @param boolean $ssl Should the check be done via SSL?
\r
259 * @return boolean Are the credentials okay?
\r
261 private function checkCredentials($host, $user, $password, $ssl)
\r
263 $url = ($ssl ? 'https' : 'http') . '://' . $host . '/api/account/verify_credentials.json';
\r
266 curl_setopt($ch, CURLOPT_URL, $url);
\r
267 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
\r
268 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
\r
269 curl_setopt($ch, CURLOPT_HEADER, true);
\r
270 curl_setopt($ch, CURLOPT_NOBODY, true);
\r
271 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
\r
272 curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);
\r
275 $curl_info = @curl_getinfo($ch);
\r
276 $http_code = $curl_info['http_code'];
\r
279 $this->writeLog(LOG_INFO, 'external auth for ' . $user . '@' . $host . ' returned ' . $http_code);
\r
281 return $http_code == 200;
\r
285 * @brief write data to the syslog
\r
287 * @param integer $loglevel The syslog loglevel
\r
288 * @param string $sMessage The syslog message
\r
290 private function writeLog($loglevel, $sMessage)
\r
292 if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
\r
295 syslog($loglevel, $sMessage);
\r
299 * @brief destroy the class, close the syslog connection.
\r
301 public function __destruct()
\r
303 $this->writeLog(LOG_NOTICE, 'stop');
\r