5 * Copyright (C) 2006-2008 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);
37 function Challenge($login, $token, $ip, $tstamp)
39 $this->login = $login;
40 $this->token = $token;
42 $this->tstamp = $tstamp + CHAL_VALID_TIME;
57 $this->item = array();
59 $this->garbage_timeout = 0;
63 function add($login, $token, $ip, $tstamp)
67 log_auth("xxx", sprintf("Challenges::add [%s]\n", $login));
73 log_auth("xxx", "LOOPI item: ".$i." tstamp: ".$this->item[$i]->tstamp." curtime: ".$curtime);
75 if (($chal = new Challenge($login, $token, $ip, $tstamp)) == null) {
79 $this->item[$this->item_n] = $chal;
88 /* remove all istances related to $login */
94 for ($i = 0 ; $i < $this->item_n ; $i++) {
95 if ($this->item[$i]->login == $login) {
97 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
98 $this->item[$e] = $this->item[$e + 1];
103 unset($this->item[$this->item_n]);
111 function garbage_manager()
115 // FIXME remove set to 0
116 $this->garbage_timeout = 0;
117 if ($this->garbage_timeout > $curtime)
122 for ($i = 0 ; $i < $this->item_n ; $i++) {
123 log_auth("xxx", "LOOPI item: ".$i." tstamp: ".$this->item[$i]->tstamp." curtime: ".$curtime);
124 if ($this->item[$i]->tstamp < $curtime) {
125 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
126 $this->item[$e] = $this->item[$e + 1];
131 log_auth("xxx", "LOOPI unset: ".$this->item_n);
132 unset($this->item[$this->item_n]);
138 log_auth("xxx", "LOOPI AFTER: ".count($this->item)." _n:" .$this->item_n );
140 $this->garbage_timeout = $curtime + CHAL_GARBAGE_TIMEOUT;
151 function &init_data()
153 $chal =& new Challenges();
160 function &load_data()
162 GLOBAL $G_false, $sess;
165 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
166 log_main("ftok failed");
171 if (($shm_sz = sharedmem_sz($tok)) == -1) {
172 log_main("shmop_open failed");
176 $shm_sz = CHAL_SHM_DIMS_MIN;
178 if ($shm = shm_attach($tok, $shm_sz)) {
179 $chals = @shm_get_var($shm, $tok);
181 log_only("challenges == ".($chals == FALSE ? "FALSE" : "TRUE")." challenges === ".($chals === FALSE ? "FALSE" : "TRUE")." challenges isset ".(isset($chals) ? "TRUE" : "FALSE"));
183 if ($chals == FALSE) {
184 log_only("INIT CHALLENGES DATA");
186 $chals =& Challenges::init_data();
187 if (@shm_put_var($shm, $tok, $chals) == FALSE) {
188 log_only("PUT_VAR FALLITA ".strlen(serialize($chals)));
189 log_only(serialize($chals));
192 $chals->shm_sz = $shm_sz;
197 $chals->garbage_manager();
210 function save_data(&$chals)
213 $oldmod = $chals->mod;
215 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1)
218 while ($chals->shm_sz < CHAL_SHM_DIMS_MAX) {
219 if (($shm = shm_attach($tok, $chals->shm_sz)) == FALSE)
223 log_only("challenges count ".count($chals->item)." _n: ".$chals->item_n);
226 if (shm_put_var($shm, $tok, $chals) != FALSE) {
230 $chals->mod = $oldmod;
232 if (shm_remove($shm) === FALSE) {
233 log_only("REMOVE FALLITA");
237 $chals->shm_sz += CHAL_SHM_DIMS_DLT;
248 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
251 // echo "FTOK ".$tok."<br>";
252 if (($res = sem_get($tok)) == FALSE) {
255 if (sem_acquire($res)) {
256 log_lock("LOCK challenges");
263 function unlock_data($res)
267 log_lock("UNLOCK challenges");
269 return (sem_release($res));
271 } // End CLASS Challenges
278 function LoginDBItem($login, $pass)
280 $this->login = $login;
292 GLOBAL $DOCUMENT_ROOT;
293 log_main("LoginDB create:start");
295 if (file_exists("$DOCUMENT_ROOT/Etc/brisk_auth.conf.pho")) {
296 require("$DOCUMENT_ROOT/Etc/brisk_auth.conf.pho");
299 $this->item = array( new LoginDBItem("uno", md5("one")),
300 new LoginDBItem("due", md5("two")),
301 new LoginDBItem("a_b", md5("abb")),
302 new LoginDBItem("tre", md5("three")) );
304 $this->item_n = count($this->item);
305 log_main("LoginDB create:end");
308 function login_exists($login)
310 log_main("login_verify");
312 /* check the existence of the nick in the LoginDB */
313 for ($i = 0 ; $i < $this->item_n ; $i++) {
314 if (strcasecmp($this->item[$i]->login, $login) == 0) {
315 log_main("login[".$i."]: ".$this->item[$i]->login);
322 function login_verify($login, $pass)
326 log_main("login_verify");
328 /* check the existence of the nick in the LoginDB */
329 for ($i = 0 ; $i < $this->item_n ; $i++) {
330 if (strcasecmp($this->item[$i]->login, $login) == 0) {
331 log_main("login[".$i."]: ".$this->item[$i]->login);
333 /* if it exists check for a valid challenge */
334 if (($a_sem = Challenges::lock_data()) != FALSE) {
336 if (($chals = &Challenges::load_data()) != FALSE) {
337 for ($e = 0 ; $e < $chals->item_n ; $e++) {
339 log_main("challenge[".$i."]: ".$chals->item[$e]->login);
340 if (strcmp($login, $chals->item[$e]->login) == 0) {
341 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
343 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
344 log_main("login_verify SUCCESS for ".$login);
351 } // end for ($e = 0 ...
354 if ($chals->ismod()) {
355 Challenges::save_data(&$chals);
358 Challenges::unlock_data($a_sem);
361 } // if (strcasecmp($this->item[$i]->login, ...
366 } // End class LoginDB