5f4426d9ef205a9e85760d8ed3fc159864a658f9
[brisk.git] / web / Obj / auth.phh
1 <?php
2 /*
3  *  brisk - auth.phh
4  *
5  *  Copyright (C) 2006-2009 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 define(CHAL_SHM_DIMS_MIN, 16384);
26 define(CHAL_SHM_DIMS_MAX, 65536);
27 define(CHAL_SHM_DIMS_DLT, 16384);
28 define(CHAL_VALID_TIME,      15);
29 define(CHAL_GARBAGE_TIMEOUT,  5);
30 define(BRISK_AUTH_CONF,   "brisk_auth.conf.pho");
31
32
33 class Challenge {
34   var $login;
35   var $token;
36   var $ip;
37   var $tstamp;
38
39   function Challenge($login, $token, $ip, $tstamp)
40   {
41     $this->login  = $login;
42     $this->token  = $token;
43     $this->ip     = $ip;
44     $this->tstamp = $tstamp + CHAL_VALID_TIME;
45   }
46 }
47
48 class Challenges {
49   var $item;
50   var $item_n;
51   var $mod;
52   var $shm_sz;
53
54   var $garbage_timeout;
55
56
57   function Challenges()
58   {
59     $this->item = array();
60     $this->item_n = 0;
61     $this->garbage_timeout = 0;
62     $this->mod = FALSE;
63   }
64
65   function add($login, $token, $ip, $tstamp) 
66   {
67     $chal = null;
68
69     log_auth("xxx", sprintf("Challenges::add [%s]\n", $login));
70     // FIXME Checks here
71     if ($login == '') {
72       return ($G_false);
73     }
74
75     // log_auth("xxx", "LOOPI tstamp: ".$this->item[$i]->tstamp."  curtime: ".$curtime);
76
77     if (($chal = new Challenge($login, $token, $ip, $tstamp)) == null) {
78       return ($G_false);
79     }
80
81     $this->item[$this->item_n] = $chal;
82     $this->item_n++;
83
84     $this->mod = TRUE;
85
86     return ($chal);
87   }
88
89
90   /* remove all istances related to $login */
91
92   function rem($login)
93   {
94     $ismod  = FALSE;
95
96     for ($i = 0 ; $i < $this->item_n ; $i++) {
97       if ($this->item[$i]->login == $login) {
98         $ismod = TRUE;
99         for ($e = $i ; $e  < ($this->item_n - 1) ; $e++) {
100           $this->item[$e] = $this->item[$e + 1];
101         }
102         
103         $i--;
104         $this->item_n--;
105         unset($this->item[$this->item_n]);
106         $this->mod = TRUE;
107       }
108     }
109
110     return ($ismod);
111   }
112
113   function garbage_manager()
114   {
115     $curtime = time();
116
117     // FIXME remove set to 0
118     $this->garbage_timeout = 0;
119     if ($this->garbage_timeout > $curtime)
120       return (FALSE);
121
122     $ismod = FALSE;
123     
124     for ($i = 0 ; $i < $this->item_n ; $i++) {
125       log_auth("xxx", "LOOPI item: ".$i." tstamp: ".$this->item[$i]->tstamp."  curtime: ".$curtime);
126       if ($this->item[$i]->tstamp < $curtime) {
127         for ($e = $i ; $e  < ($this->item_n - 1) ; $e++) {
128           $this->item[$e] = $this->item[$e + 1];
129         }
130         
131         $i--;
132         $this->item_n--;
133         log_auth("xxx", "LOOPI unset: ".$this->item_n);
134         unset($this->item[$this->item_n]);
135         $ismod = TRUE;
136         $this->mod = TRUE;
137       }
138     }
139     
140     log_auth("xxx", "LOOPI AFTER: ".count($this->item)." _n:" .$this->item_n );
141     
142     $this->garbage_timeout = $curtime + CHAL_GARBAGE_TIMEOUT;
143     
144     return ($ismod);
145   }
146   
147   function ismod()
148   {
149     return ($this->mod);
150   }
151
152   // Static functions
153   function &init_data()
154   {
155     $chal =& new Challenges();
156     
157     $chal->mod = TRUE;
158
159     return $chal;
160   }
161
162   function &load_data() 
163   {
164     GLOBAL $G_false, $sess;
165     $doexit = FALSE;
166     do {
167       if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
168         log_main("ftok failed");
169         $doexit = TRUE;
170         break;
171       }
172     
173       if (($shm_sz = sharedmem_sz($tok)) == -1) {
174         log_main("shmop_open failed");
175       }
176         
177       if ($shm_sz == -1)
178         $shm_sz = CHAL_SHM_DIMS_MIN;
179
180       if ($shm = shm_attach($tok, $shm_sz)) {
181         $chals = @shm_get_var($shm, $tok);
182         
183         log_only("challenges ==  ".($chals == FALSE ?   "FALSE" : "TRUE")."  challenges ===  ".($chals === FALSE ? "FALSE" : "TRUE")."  challenges isset ".(isset($chals) ?   "TRUE" : "FALSE"));
184         
185         if ($chals == FALSE) {
186           log_only("INIT CHALLENGES DATA");
187           
188           $chals =& Challenges::init_data();
189           if (@shm_put_var($shm, $tok, $chals) == FALSE) {
190             log_only("PUT_VAR FALLITA ".strlen(serialize($chals)));
191             log_only(serialize($chals));
192           }
193         }
194         $chals->shm_sz = $shm_sz;
195         
196         shm_detach($shm);
197       }
198
199       $chals->garbage_manager();
200
201       $ret = &$chals;
202       return ($ret);
203     } while (0);
204     
205     if ($doexit)
206       exit();
207     
208     return ($G_false);
209   }
210   
211
212   function save_data(&$chals) 
213   {
214     $shm =   FALSE;
215     $oldmod = $chals->mod;
216
217     if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) 
218       return (FALSE);
219     
220     while ($chals->shm_sz < CHAL_SHM_DIMS_MAX) {
221       if (($shm = shm_attach($tok, $chals->shm_sz)) == FALSE)
222         break;
223       
224       if (isset($chals)) 
225         log_only("challenges count ".count($chals->item)."  _n: ".$chals->item_n);
226
227       $chals->mod = FALSE;
228       if (shm_put_var($shm, $tok, $chals) != FALSE) {
229         shm_detach($shm);
230         return (TRUE);
231       }
232       $chals->mod = $oldmod;
233
234       if (shm_remove($shm) === FALSE) {
235         log_only("REMOVE FALLITA");
236         break;
237       }
238       shm_detach($shm);
239       $chals->shm_sz += CHAL_SHM_DIMS_DLT;
240     } 
241
242     if ($shm)
243       shm_detach($shm);
244     
245     return (FALSE);
246   }
247
248   function lock_data()
249   {
250     if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
251       return (FALSE);
252     }
253     // echo "FTOK ".$tok."<br>";
254     if (($res = sem_get($tok)) == FALSE) {
255       return (FALSE);
256     }
257     if (sem_acquire($res)) {   
258       log_lock("LOCK challenges");
259       return ($res);
260     }
261     else
262       return (FALSE);
263   }
264   
265   function unlock_data($res)
266   {
267     GLOBAL $sess; 
268     
269     log_lock("UNLOCK challenges");
270
271     return (sem_release($res));
272   }
273 } // End CLASS Challenges
274
275
276 class LoginDBItem {
277   var $login;
278   var $pass;
279   var $email;
280
281   function LoginDBItem($login, $pass, $email)
282   {
283     $this->login = $login;
284     $this->pass  = $pass;
285     $this->email = $email;
286   }
287 }
288
289 class LoginDB {
290   var $item;
291   var $item_n;
292
293   
294   function LoginDB()
295   {
296     GLOBAL $DOCUMENT_ROOT;
297     log_main("LoginDB create:start");
298
299     if (file_exists("$DOCUMENT_ROOT/Etc/".BRISK_AUTH_CONF)) {
300       require("$DOCUMENT_ROOT/Etc/".BRISK_AUTH_CONF);
301     }
302     else {
303       $this->item = array( new LoginDBItem("uno", md5("one"), "pippo@pluto.com"),
304                            new LoginDBItem("due", md5("two"), "pippo@pluto.com"),
305                            new LoginDBItem("a_b", md5("abb"), "pippo@pluto.com"),
306                            new LoginDBItem("tre", md5("three"), "pippo@pluto.com") );
307     }
308     $this->item_n = count($this->item);
309     log_main("LoginDB create:end");
310   }
311
312   function count()
313   {
314     return ($this->item_n);
315   }
316
317   function login_exists($login)
318   {
319     log_main("login_exists: ".$login);
320     
321     /* check the existence of the nick in the LoginDB */
322     for ($i = 0 ; $i < $this->item_n ; $i++) {
323       if (strcasecmp($this->item[$i]->login, $login) == 0) {
324         log_main("login[".$i."]: ".$this->item[$i]->login);
325         return (TRUE);
326       }
327     }
328     return (FALSE);
329   }
330
331   function getlogin_byidx($idx)
332   {
333     if ($idx >= $this->item_n)
334       return FALSE;
335     return ($this->item[$idx]->login);
336   }
337
338   function &getitem_bylogin($login, &$id)
339   {
340     GLOBAL $G_false;
341
342     log_main("login_exists: ".$login);
343     
344     /* check the existence of the nick in the LoginDB */
345     for ($i = 0 ; $i < $this->item_n ; $i++) {
346       if (strcasecmp($this->item[$i]->login, $login) == 0) {
347         log_main("login[".$i."]: ".$this->item[$i]->login);
348         $ret = &$this->item[$i];
349         $id = $i;
350         return ($ret);
351       }
352     }
353     $id = -1;
354     return ($G_false);
355   }
356
357   function getmail($login)
358   {
359     log_main("getmail");
360     
361     /* check the existence of the nick in the LoginDB */
362     for ($i = 0 ; $i < $this->item_n ; $i++) {
363       if (strcasecmp($this->item[$i]->login, $login) == 0) {
364         log_main("login[".$i."]: ".$this->item[$i]->login);
365         return ($this->item[$i]->email);
366       }
367     }
368     return (FALSE);
369   }
370
371   function login_verify($login, $pass)
372   {
373     $ret = FALSE;
374
375     log_main("login_verify: ".$login);
376         
377     /* check the existence of the nick in the LoginDB */
378     for ($i = 0 ; $i < $this->item_n ; $i++) {
379       log_main("login_verify: LOOP");
380       if (strcasecmp($this->item[$i]->login, $login) == 0) {
381         log_main("login[".$i."]: ".$this->item[$i]->login);
382
383         /* if it exists check for a valid challenge */
384         if (($a_sem = Challenges::lock_data()) != FALSE) { 
385           
386           if (($chals = &Challenges::load_data()) != FALSE) {
387             for ($e = 0 ; $e < $chals->item_n ; $e++) {
388               
389               log_main("challenge[".$i."]: ".$chals->item[$e]->login);
390               if (strcmp($login, $chals->item[$e]->login) == 0) {
391                 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
392                   
393                 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
394                   log_main("login_verify SUCCESS for ".$login);
395    
396                   $chals->rem($login);
397                   $ret = TRUE;
398                   break;
399                 }
400               }
401             } // end for ($e = 0 ...
402           }
403
404           if ($chals->ismod()) {
405             Challenges::save_data(&$chals);
406           }
407           
408           Challenges::unlock_data($a_sem);
409         }
410         break;
411       } //  if (strcasecmp($this->item[$i]->login, ...
412     }
413
414     return ($ret);
415   }
416 } // End class LoginDB
417
418
419 ?>