5 * Copyright (C) 2006-2012 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;
59 $this->item = array();
61 $this->garbage_timeout = 0;
65 function add_item($login, $ip, $session, $timeout)
69 log_auth("xxx", sprintf("Hardbans::add [%s]\n", $login));
71 if (($chal = new Hardban($login, $ip, $session, $timeout)) == FALSE) {
75 $this->item[$this->item_n] = $chal;
84 /* remove all istances related to $login */
91 for ($i = 0 ; $i < $this->item_n ; $i++) {
92 if ($this->item[$i]->timeout < $curtime || strcasecmp($this->item[$i]->login, $login) == 0) {
94 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
95 $this->item[$e] = $this->item[$e + 1];
100 unset($this->item[$this->item_n]);
108 function garbage_manager($force)
112 // FIXME remove set to 0
113 $this->garbage_timeout = 0;
114 if ($this->garbage_timeout > $curtime && $force == FALSE)
119 for ($i = 0 ; $i < $this->item_n ; $i++) {
120 log_auth("xxx", "LOOPI item: ".$i." timeout: ".$this->item[$i]->timeout." curtime: ".$curtime);
121 if ($this->item[$i]->timeout < $curtime) {
122 for ($e = $i ; $e < ($this->item_n - 1) ; $e++) {
123 $this->item[$e] = $this->item[$e + 1];
128 log_auth("xxx", "LOOPI unset: ".$this->item_n);
129 unset($this->item[$this->item_n]);
135 log_auth("xxx", "LOOPI AFTER: ".count($this->item)." _n:" .$this->item_n );
137 $this->garbage_timeout = $curtime + HBAN_GARBAGE_TIMEOUT;
148 static function create()
150 $chal =& new Hardbans();
162 if (($tok = @ftok(FTOK_PATH."/hardbans", "C")) == -1) {
163 log_main("ftok failed");
166 fprintf(STDERR, "hardpans: %x\n", $tok);
167 if (($shm_sz = sharedmem_sz($tok)) == -1) {
168 log_main("shmop_open failed");
172 $shm_sz = HBAN_SHM_DIMS_MIN;
174 if ($shm = shm_attach($tok, $shm_sz)) {
175 $hban = @shm_get_var($shm, $tok); // CHECKED BELOW
177 log_only("hardban == ".($hban == FALSE ? "FALSE" : "TRUE")." hardban === ".($hban === FALSE ? "FALSE" : "TRUE")." hardban isset ".(isset($hban) ? "TRUE" : "FALSE"));
179 if ($hban == FALSE) {
180 log_only("INIT HARDBAN DATA");
182 $hban =& Hardbans::create();
183 if (@shm_put_var($shm, $tok, $hban) == FALSE) {
184 log_only("PUT_VAR FALLITA ".strlen(serialize($hban)));
185 log_only(serialize($hban));
187 log_shme("Hardban::save_data2");
190 $hban->shm_sz = $shm_sz;
195 $hban->garbage_manager(TRUE);
204 function save_data($hban)
207 $oldmod = $hban->mod;
209 if (($tok = @ftok(FTOK_PATH."/hardbans", "C")) == -1)
212 while ($hban->shm_sz < HBAN_SHM_DIMS_MAX) {
213 if (($shm = shm_attach($tok, $hban->shm_sz)) == FALSE)
217 log_only("hardban count ".count($hban->item)." _n: ".$hban->item_n);
220 if (@shm_put_var($shm, $tok, $hban) != FALSE) {
222 log_shme("Hardban::save_data");
225 $hban->mod = $oldmod;
227 if (shm_remove($shm) === FALSE) {
228 log_only("REMOVE FALLITA");
232 $hban->shm_sz += HBAN_SHM_DIMS_DLT;
241 static function lock_data($is_exclusive)
243 if (($res = file_lock(FTOK_PATH."/hardbans", $is_exclusive)) != FALSE) {
244 self::$delta_t = microtime(TRUE);
245 log_lock("LOCK hardbans [".self::$delta_t."]");
253 static function unlock_data($res)
257 log_lock("UNLOCK hardbans [".(microtime(TRUE) - (self::$delta_t))."]");
263 function check($login, $ip, $session)
266 /* if it exists check for a valid challenge */
267 if (($a_sem = Hardbans::lock_data(TRUE)) != FALSE) {
269 if (($hban = &Hardbans::load_data()) != FALSE) {
270 for ($e = 0 ; $e < $hban->item_n ; $e++) {
271 if ($login != FALSE) {
272 if (strcasecmp($login, $hban->item[$e]->login) == 0 || $hban->item[$e]->session == $session) {
273 $bantime = $hban->item[$e]->timeout;
278 /* check on ip and sess */
279 if ($hban->item[$e]->ip == $ip || $hban->item[$e]->session == $session) {
280 $bantime = $hban->item[$e]->timeout;
285 if ($hban->ismod()) {
286 Hardbans::save_data(&$hban);
289 Hardbans::unlock_data($a_sem);
297 function add($login, $ip, $session, $timeout)
300 /* if it exists check for a valid challenge */
301 if (($a_sem = Hardbans::lock_data(TRUE)) != FALSE) {
303 if (($hban = &Hardbans::load_data()) != FALSE) {
305 $hban->add_item($login, $ip, $session, $timeout);
307 if ($hban->ismod()) {
308 Hardbans::save_data(&$hban);
311 Hardbans::unlock_data($a_sem);
317 } // End CLASS Hardbans