]> git.mxchange.org Git - friendica.git/blob - include/auth_ejabberd.php
New process table for a better detection of running workers
[friendica.git] / include / auth_ejabberd.php
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
35 if (sizeof($_SERVER["argv"]) == 0)
36         die();
37
38 $directory = dirname($_SERVER["argv"][0]);
39
40 if (substr($directory, 0, 1) != "/")
41         $directory = $_SERVER["PWD"]."/".$directory;
42
43 $directory = realpath($directory."/..");
44
45 chdir($directory);
46 require_once("boot.php");
47
48 global $a, $db;
49
50 if(is_null($a)) {
51         $a = new App;
52 }
53
54 if(is_null($db)) {
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);
59 };
60
61 $a->start_process();
62
63 // the logfile to which to write, should be writeable by the user which is running the server
64 $sLogFile = get_config('jabber','logfile');
65
66 // set true to debug if needed
67 $bDebug = get_config('jabber','debug');
68
69 $oAuth = new exAuth($sLogFile, $bDebug);
70
71 class exAuth
72 {
73         private $sLogFile;
74         private $bDebug;
75
76         private $rLogFile;
77
78         public function __construct($sLogFile, $bDebug)
79         {
80                 global $db;
81
82                 // setter
83                 $this->sLogFile         = $sLogFile;
84                 $this->bDebug           = $bDebug;
85
86                 // ovo ne provjeravamo jer ako ne mozes kreirati log file, onda si u kvascu :)
87                 if ($this->sLogFile != '')
88                         $this->rLogFile = fopen($this->sLogFile, "a") or die("Error opening log file: ". $this->sLogFile);
89
90                 $this->writeLog("[exAuth] start");
91
92                 // ovdje bi trebali biti spojeni na MySQL, imati otvoren log i zavrtit cekalicu
93                 do {
94                         $iHeader        = fgets(STDIN, 3);
95                         $aLength        = unpack("n", $iHeader);
96                         $iLength        = $aLength["1"];
97                         if($iLength > 0) {
98                                 // ovo znaci da smo nesto dobili
99                                 $sData = fgets(STDIN, $iLength + 1);
100                                 $this->writeDebugLog("[debug] received data: ". $sData);
101                                 $aCommand = explode(":", $sData);
102                                 if (is_array($aCommand)){
103                                         switch ($aCommand[0]){
104                                                 case "isuser":
105                                                         // provjeravamo je li korisnik dobar
106                                                         if (!isset($aCommand[1])){
107                                                                 $this->writeLog("[exAuth] invalid isuser command, no username given");
108                                                                 fwrite(STDOUT, pack("nn", 2, 0));
109                                                         } else {
110                                                                 // ovdje provjeri je li korisnik OK
111                                                                 $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
112                                                                 $this->writeDebugLog("[debug] checking isuser for ". $sUser);
113                                                                 $sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='". $db->escape($sUser) ."'";
114                                                                 $this->writeDebugLog("[debug] using query ". $sQuery);
115                                                                 if ($oResult = q($sQuery)){
116                                                                         if ($oResult) {
117                                                                                 // korisnik OK
118                                                                                 $this->writeLog("[exAuth] valid user: ". $sUser);
119                                                                                 fwrite(STDOUT, pack("nn", 2, 1));
120                                                                         } else {
121                                                                                 // korisnik nije OK
122                                                                                 $this->writeLog("[exAuth] invalid user: ". $sUser);
123                                                                                 fwrite(STDOUT, pack("nn", 2, 0));
124                                                                         }
125                                                                         //$oResult->close();
126                                                                 } else {
127                                                                         $this->writeLog("[MySQL] invalid query: ". $sQuery);
128                                                                         fwrite(STDOUT, pack("nn", 2, 0));
129                                                                 }
130                                                         }
131                                                 break;
132                                                 case "auth":
133                                                         // provjeravamo autentifikaciju korisnika
134                                                         if (sizeof($aCommand) != 4){
135                                                                 $this->writeLog("[exAuth] invalid auth command, data missing");
136                                                                 fwrite(STDOUT, pack("nn", 2, 0));
137                                                         } else {
138                                                                 // ovdje provjeri prijavu
139                                                                 $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
140                                                                 $this->writeDebugLog("[debug] doing auth for ". $sUser);
141                                                                 //$sQuery = "SELECT `uid`, `password` FROM `user` WHERE `password`='".hash('whirlpool',$aCommand[3])."' AND `nickname`='". $db->escape($sUser) ."'";
142                                                                 $sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='". $db->escape($sUser) ."'";
143                                                                 $this->writeDebugLog("[debug] using query ". $sQuery);
144                                                                 if ($oResult = q($sQuery)){
145                                                                         $uid = $oResult[0]["uid"];
146                                                                         $Error = ($oResult[0]["password"] != hash('whirlpool',$aCommand[3]));
147 /*
148                                                                         if ($oResult[0]["password"] == hash('whirlpool',$aCommand[3])) {
149                                                                                 // korisnik OK
150                                                                                 $this->writeLog("[exAuth] authentificated user ". $sUser ."@". $aCommand[2]);
151                                                                                 fwrite(STDOUT, pack("nn", 2, 1));
152                                                                         } else {
153                                                                                 // korisnik nije OK
154                                                                                 $this->writeLog("[exAuth] authentification failed for user ". $sUser ."@". $aCommand[2]);
155                                                                                 fwrite(STDOUT, pack("nn", 2, 0));
156                                                                         }
157                                                                         $oResult->close();
158 */
159                                                                 } else {
160                                                                         $this->writeLog("[MySQL] invalid query: ". $sQuery);
161                                                                         $Error = true;
162                                                                         $uid = -1;
163                                                                 }
164                                                                 if ($Error) {
165                                                                         $oConfig = q("SELECT `v` FROM `pconfig` WHERE `uid`=%d AND `cat` = 'xmpp' AND `k`='password' LIMIT 1;", intval($uid));
166                                                                         $this->writeLog("[exAuth] got password ".$oConfig[0]["v"]);
167                                                                         $Error = ($aCommand[3] != $oConfig[0]["v"]);
168                                                                 }
169
170                                                                 if ($Error) {
171                                                                         $this->writeLog("[exAuth] authentification failed for user ". $sUser ."@". $aCommand[2]);
172                                                                         fwrite(STDOUT, pack("nn", 2, 0));
173                                                                 } else {
174                                                                         $this->writeLog("[exAuth] authentificated user ". $sUser ."@". $aCommand[2]);
175                                                                         fwrite(STDOUT, pack("nn", 2, 1));
176                                                                 }
177                                                         }
178                                                 break;
179                                                 case "setpass":
180                                                         // postavljanje zaporke, onemoguceno
181                                                         $this->writeLog("[exAuth] setpass command disabled");
182                                                         fwrite(STDOUT, pack("nn", 2, 0));
183                                                 break;
184                                                 default:
185                                                         // ako je uhvaceno ista drugo
186                                                         $this->writeLog("[exAuth] unknown command ". $aCommand[0]);
187                                                         fwrite(STDOUT, pack("nn", 2, 0));
188                                                 break;
189                                         }
190                                 } else {
191                                         $this->writeDebugLog("[debug] invalid command string");
192                                         fwrite(STDOUT, pack("nn", 2, 0));
193                                 }
194                         }
195                         unset ($iHeader);
196                         unset ($aLength);
197                         unset ($iLength);
198                         unset($aCommand);
199                 } while (true);
200         }
201
202         public function __destruct()
203         {
204                 // zatvori log file
205                 $this->writeLog("[exAuth] stop");
206
207                 if (is_resource($this->rLogFile)){
208                         fclose($this->rLogFile);
209                 }
210         }
211
212         private function writeLog($sMessage)
213         {
214                 if (is_resource($this->rLogFile)) {
215                         fwrite($this->rLogFile, date("r") ." ". $sMessage ."\n");
216                 }
217         }
218
219         private function writeDebugLog($sMessage)
220         {
221                 if ($this->bDebug){
222                         $this->writeLog($sMessage);
223                 }
224         }
225
226 }
227 ?>
228
229