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(HBAN_SHM_DIMS_MIN, 16384);
26 define(HBAN_SHM_DIMS_MAX, 65536);
27 define(HBAN_SHM_DIMS_DLT, 16384);
28 define(HBAN_VALID_TIME, 15);
29 define(HBAN_GARBAGE_TIMEOUT, 5);
37 function Hardban($login, $ip, $session, $timeout)
39 $this->login = $login;
41 $this->session = $session;
42 $this->timeout = $timeout;
57 $this->item = array();
59 $this->garbage_timeout = 0;
63 function add_item($login, $ip, $session, $timeout)
67 log_auth("xxx", sprintf("Hardbans::add [%s]\n", $login));
69 if (($chal = new Hardban($login, $ip, $session, $timeout)) == null) {
73 $this->item[$this->item_n] = $chal;
82 /* remove all istances related to $login */
89 for ($i = 0 ; $i < $this->item_n ; $i++) {
90 if ($this->item[$i]->timeout < $curtime || strcasecmp($this->item[$i]->login, $login) == 0) {
92 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
93 $this->item[$e] = $this->item[$e + 1];
98 unset($this->item[$this->item_n]);
106 function garbage_manager($force)
110 // FIXME remove set to 0
111 $this->garbage_timeout = 0;
112 if ($this->garbage_timeout > $curtime && $force == FALSE)
117 for ($i = 0 ; $i < $this->item_n ; $i++) {
118 log_auth("xxx", "LOOPI item: ".$i." timeout: ".$this->item[$i]->timeout." curtime: ".$curtime);
119 if ($this->item[$i]->timeout < $curtime) {
120 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
121 $this->item[$e] = $this->item[$e + 1];
126 log_auth("xxx", "LOOPI unset: ".$this->item_n);
127 unset($this->item[$this->item_n]);
133 log_auth("xxx", "LOOPI AFTER: ".count($this->item)." _n:" .$this->item_n );
135 $this->garbage_timeout = $curtime + HBAN_GARBAGE_TIMEOUT;
146 function &init_data()
148 $chal =& new Hardbans();
155 function &load_data()
157 GLOBAL $G_false, $sess;
160 if (($tok = @ftok(FTOK_PATH."/hardbans", "B")) == -1) {
161 log_main("ftok failed");
166 if (($shm_sz = sharedmem_sz($tok)) == -1) {
167 log_main("shmop_open failed");
171 $shm_sz = HBAN_SHM_DIMS_MIN;
173 if ($shm = shm_attach($tok, $shm_sz)) {
174 $hban = @shm_get_var($shm, $tok);
176 log_only("hardban == ".($hban == FALSE ? "FALSE" : "TRUE")." hardban === ".($hban === FALSE ? "FALSE" : "TRUE")." hardban isset ".(isset($hban) ? "TRUE" : "FALSE"));
178 if ($hban == FALSE) {
179 log_only("INIT HARDBAN DATA");
181 $hban =& Hardbans::init_data();
182 if (@shm_put_var($shm, $tok, $hban) == FALSE) {
183 log_only("PUT_VAR FALLITA ".strlen(serialize($hban)));
184 log_only(serialize($hban));
187 $hban->shm_sz = $shm_sz;
192 $hban->garbage_manager(TRUE);
205 function save_data(&$hban)
208 $oldmod = $hban->mod;
210 if (($tok = @ftok(FTOK_PATH."/hardbans", "B")) == -1)
213 while ($hban->shm_sz < HBAN_SHM_DIMS_MAX) {
214 if (($shm = shm_attach($tok, $hban->shm_sz)) == FALSE)
218 log_only("hardban count ".count($hban->item)." _n: ".$hban->item_n);
221 if (shm_put_var($shm, $tok, $hban) != FALSE) {
225 $hban->mod = $oldmod;
227 if (shm_remove($shm) === FALSE) {
228 log_only("REMOVE FALLITA");
232 $hban->shm_sz += HBAN_SHM_DIMS_DLT;
243 if (($tok = @ftok(FTOK_PATH."/hardbans", "B")) == -1) {
246 // echo "FTOK ".$tok."<br>";
247 if (($res = sem_get($tok)) == FALSE) {
250 if (sem_acquire($res)) {
251 log_lock("LOCK hardbans");
258 function unlock_data($res)
262 log_lock("UNLOCK hardbans");
264 return (sem_release($res));
268 function check($login, $ip, $session)
271 /* if it exists check for a valid challenge */
272 if (($a_sem = Hardbans::lock_data()) != FALSE) {
274 if (($hban = &Hardbans::load_data()) != FALSE) {
275 for ($e = 0 ; $e < $hban->item_n ; $e++) {
276 if ($login != FALSE) {
277 if (strcasecmp($login, $hban->item[$e]->login) == 0 || $hban->item[$e]->session == $session) {
278 $bantime = $hban->item[$e]->timeout;
283 /* check on ip and sess */
284 if ($hban->item[$e]->ip == $ip || $hban->item[$e]->session == $session) {
285 $bantime = $hban->item[$e]->timeout;
290 if ($hban->ismod()) {
291 Hardbans::save_data(&$hban);
294 Hardbans::unlock_data($a_sem);
302 function add($login, $ip, $session, $timeout)
305 /* if it exists check for a valid challenge */
306 if (($a_sem = Hardbans::lock_data()) != FALSE) {
308 if (($hban = &Hardbans::load_data()) != FALSE) {
310 $hban->add_item($login, $ip, $session, $timeout);
312 if ($hban->ismod()) {
313 Hardbans::save_data(&$hban);
316 Hardbans::unlock_data($a_sem);
322 } // End CLASS Hardbans