5 * Copyright (C) 2006-2011 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 require_once("Obj/dbase_pgsql.phh");
27 define(CHAL_SHM_DIMS_MIN, 16384);
28 define(CHAL_SHM_DIMS_MAX, 65536);
29 define(CHAL_SHM_DIMS_DLT, 16384);
30 define(CHAL_VALID_TIME, 15);
31 define(CHAL_GARBAGE_TIMEOUT, 5);
40 function Challenge($login, $token, $ip, $tstamp)
42 $this->login = $login;
43 $this->token = $token;
45 $this->tstamp = $tstamp + CHAL_VALID_TIME;
60 $this->item = array();
62 $this->garbage_timeout = 0;
66 function add($login, $token, $ip, $tstamp)
70 log_auth("xxx", sprintf("Challenges::add [%s]\n", $login));
76 // log_auth("xxx", "LOOPI tstamp: ".$this->item[$i]->tstamp." curtime: ".$curtime);
78 if (($chal = new Challenge($login, $token, $ip, $tstamp)) == null) {
82 $this->item[$this->item_n] = $chal;
91 /* remove all istances related to $login */
97 for ($i = 0 ; $i < $this->item_n ; $i++) {
98 if ($this->item[$i]->login == $login) {
100 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
101 $this->item[$e] = $this->item[$e + 1];
106 unset($this->item[$this->item_n]);
114 function garbage_manager()
118 // FIXME remove set to 0
119 $this->garbage_timeout = 0;
120 if ($this->garbage_timeout > $curtime)
125 for ($i = 0 ; $i < $this->item_n ; $i++) {
126 log_auth("xxx", "LOOPI item: ".$i." tstamp: ".$this->item[$i]->tstamp." curtime: ".$curtime);
127 if ($this->item[$i]->tstamp < $curtime) {
128 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
129 $this->item[$e] = $this->item[$e + 1];
134 log_auth("xxx", "LOOPI unset: ".$this->item_n);
135 unset($this->item[$this->item_n]);
141 log_auth("xxx", "LOOPI AFTER: ".count($this->item)." _n:" .$this->item_n );
143 $this->garbage_timeout = $curtime + CHAL_GARBAGE_TIMEOUT;
154 function &init_data()
156 $chal =& new Challenges();
163 function &load_data()
165 GLOBAL $G_false, $sess;
168 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
169 log_main("ftok failed");
174 if (($shm_sz = sharedmem_sz($tok)) == -1) {
175 log_main("shmop_open failed");
179 $shm_sz = CHAL_SHM_DIMS_MIN;
181 if ($shm = shm_attach($tok, $shm_sz)) {
182 $chals = @shm_get_var($shm, $tok);
184 log_only("challenges == ".($chals == FALSE ? "FALSE" : "TRUE")." challenges === ".($chals === FALSE ? "FALSE" : "TRUE")." challenges isset ".(isset($chals) ? "TRUE" : "FALSE"));
186 if ($chals == FALSE) {
187 log_only("INIT CHALLENGES DATA");
189 $chals =& Challenges::init_data();
190 if (@shm_put_var($shm, $tok, $chals) == FALSE) {
191 log_only("PUT_VAR FALLITA ".strlen(serialize($chals)));
192 log_only(serialize($chals));
195 $chals->shm_sz = $shm_sz;
200 $chals->garbage_manager();
213 function save_data(&$chals)
216 $oldmod = $chals->mod;
218 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1)
221 while ($chals->shm_sz < CHAL_SHM_DIMS_MAX) {
222 if (($shm = shm_attach($tok, $chals->shm_sz)) == FALSE)
226 log_only("challenges count ".count($chals->item)." _n: ".$chals->item_n);
229 if (shm_put_var($shm, $tok, $chals) != FALSE) {
233 $chals->mod = $oldmod;
235 if (shm_remove($shm) === FALSE) {
236 log_only("REMOVE FALLITA");
240 $chals->shm_sz += CHAL_SHM_DIMS_DLT;
251 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
254 // echo "FTOK ".$tok."<br>";
255 if (($res = sem_get($tok)) == FALSE) {
258 if (sem_acquire($res)) {
259 log_lock("LOCK challenges");
266 function unlock_data($res)
270 log_lock("UNLOCK challenges");
272 return (sem_release($res));
274 } // End CLASS Challenges