5 * Copyright (C) 2006-2009 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
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");
39 function Challenge($login, $token, $ip, $tstamp)
41 $this->login = $login;
42 $this->token = $token;
44 $this->tstamp = $tstamp + CHAL_VALID_TIME;
59 $this->item = array();
61 $this->garbage_timeout = 0;
65 function add($login, $token, $ip, $tstamp)
69 log_auth("xxx", sprintf("Challenges::add [%s]\n", $login));
75 // log_auth("xxx", "LOOPI tstamp: ".$this->item[$i]->tstamp." curtime: ".$curtime);
77 if (($chal = new Challenge($login, $token, $ip, $tstamp)) == null) {
81 $this->item[$this->item_n] = $chal;
90 /* remove all istances related to $login */
96 for ($i = 0 ; $i < $this->item_n ; $i++) {
97 if ($this->item[$i]->login == $login) {
99 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
100 $this->item[$e] = $this->item[$e + 1];
105 unset($this->item[$this->item_n]);
113 function garbage_manager()
117 // FIXME remove set to 0
118 $this->garbage_timeout = 0;
119 if ($this->garbage_timeout > $curtime)
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];
133 log_auth("xxx", "LOOPI unset: ".$this->item_n);
134 unset($this->item[$this->item_n]);
140 log_auth("xxx", "LOOPI AFTER: ".count($this->item)." _n:" .$this->item_n );
142 $this->garbage_timeout = $curtime + CHAL_GARBAGE_TIMEOUT;
153 function &init_data()
155 $chal =& new Challenges();
162 function &load_data()
164 GLOBAL $G_false, $sess;
167 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
168 log_main("ftok failed");
173 if (($shm_sz = sharedmem_sz($tok)) == -1) {
174 log_main("shmop_open failed");
178 $shm_sz = CHAL_SHM_DIMS_MIN;
180 if ($shm = shm_attach($tok, $shm_sz)) {
181 $chals = @shm_get_var($shm, $tok);
183 log_only("challenges == ".($chals == FALSE ? "FALSE" : "TRUE")." challenges === ".($chals === FALSE ? "FALSE" : "TRUE")." challenges isset ".(isset($chals) ? "TRUE" : "FALSE"));
185 if ($chals == FALSE) {
186 log_only("INIT CHALLENGES DATA");
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));
194 $chals->shm_sz = $shm_sz;
199 $chals->garbage_manager();
212 function save_data(&$chals)
215 $oldmod = $chals->mod;
217 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1)
220 while ($chals->shm_sz < CHAL_SHM_DIMS_MAX) {
221 if (($shm = shm_attach($tok, $chals->shm_sz)) == FALSE)
225 log_only("challenges count ".count($chals->item)." _n: ".$chals->item_n);
228 if (shm_put_var($shm, $tok, $chals) != FALSE) {
232 $chals->mod = $oldmod;
234 if (shm_remove($shm) === FALSE) {
235 log_only("REMOVE FALLITA");
239 $chals->shm_sz += CHAL_SHM_DIMS_DLT;
250 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
253 // echo "FTOK ".$tok."<br>";
254 if (($res = sem_get($tok)) == FALSE) {
257 if (sem_acquire($res)) {
258 log_lock("LOCK challenges");
265 function unlock_data($res)
269 log_lock("UNLOCK challenges");
271 return (sem_release($res));
273 } // End CLASS Challenges
281 function LoginDBItem($login, $pass, $email)
283 $this->login = $login;
285 $this->email = $email;
296 GLOBAL $DOCUMENT_ROOT;
297 log_main("LoginDB create:start");
299 if (file_exists("$DOCUMENT_ROOT/Etc/".BRISK_AUTH_CONF)) {
300 require("$DOCUMENT_ROOT/Etc/".BRISK_AUTH_CONF);
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") );
308 $this->item_n = count($this->item);
309 log_main("LoginDB create:end");
312 function login_exists($login)
314 log_main("login_exists: ".$login);
316 /* check the existence of the nick in the LoginDB */
317 for ($i = 0 ; $i < $this->item_n ; $i++) {
318 if (strcasecmp($this->item[$i]->login, $login) == 0) {
319 log_main("login[".$i."]: ".$this->item[$i]->login);
326 function getmail($login)
330 /* check the existence of the nick in the LoginDB */
331 for ($i = 0 ; $i < $this->item_n ; $i++) {
332 if (strcasecmp($this->item[$i]->login, $login) == 0) {
333 log_main("login[".$i."]: ".$this->item[$i]->login);
334 return ($this->item[$i]->email);
340 function login_verify($login, $pass)
344 log_main("login_verify: ".$login);
346 /* check the existence of the nick in the LoginDB */
347 for ($i = 0 ; $i < $this->item_n ; $i++) {
348 log_main("login_verify: LOOP");
349 if (strcasecmp($this->item[$i]->login, $login) == 0) {
350 log_main("login[".$i."]: ".$this->item[$i]->login);
352 /* if it exists check for a valid challenge */
353 if (($a_sem = Challenges::lock_data()) != FALSE) {
355 if (($chals = &Challenges::load_data()) != FALSE) {
356 for ($e = 0 ; $e < $chals->item_n ; $e++) {
358 log_main("challenge[".$i."]: ".$chals->item[$e]->login);
359 if (strcmp($login, $chals->item[$e]->login) == 0) {
360 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
362 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
363 log_main("login_verify SUCCESS for ".$login);
370 } // end for ($e = 0 ...
373 if ($chals->ismod()) {
374 Challenges::save_data(&$chals);
377 Challenges::unlock_data($a_sem);
380 } // if (strcasecmp($this->item[$i]->login, ...
385 } // End class LoginDB