In the abuse list, IP addresses can be whitelisted.
[gnucomo.git] / src / web / abuse.php
1 <?php
2
3 /**************************************************************************
4 **  (c) Copyright 2003, Andromeda Technology & Automation
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***************************************************************************
8 ** MODULE INFORMATION *
9 ***********************
10 **      FILE NAME      : abuse.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.3 $
13 **
14 **  DESCRIPTION      : Abuse list page
15 **
16 **  EXPORTED OBJECTS : 
17 **  LOCAL    OBJECTS : 
18 **  MODULES  USED    :
19 ***************************************************************************
20 **  ADMINISTRATIVE INFORMATION *
21 ********************************
22 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
23 **      CREATION DATE   : Apr 13, 2004
24 **      LAST UPDATE     : Apr 13, 2004
25 **      MODIFICATIONS   : 
26 **************************************************************************/
27
28 /*****************************
29    $Log: abuse.php,v $
30    Revision 1.3  2007-10-19 07:15:52  arjen
31    In the abuse list, IP addresses can be whitelisted.
32
33    Revision 1.2  2007/01/11 13:44:29  arjen
34    Manually edit parameters.
35    View logs from abusing IP addresses.
36
37    Revision 1.1  2005/06/04 07:24:38  arjen
38    New page: Abuse list
39
40 ******************************/
41
42 // RCSID = "$Id: abuse.php,v 1.3 2007-10-19 07:15:52 arjen Exp $";
43
44 ini_set('include_path', '.:./classes:../phpclasses');
45
46 require_once('page.class.php');
47
48
49 class abuse_list extends page
50 {
51
52    function Body()
53    {
54       if (!empty($_GET['oid']))
55       {
56          $res = pg_exec($this->database, "SELECT objectname FROM object
57                                           WHERE objectid=CAST('" . $_GET['oid']."' AS BIGINT)");
58          $obj = pg_fetch_object($res, 0);
59          echo "<h1>Abuse List for " . $obj->objectname . "</h1><hr>";
60
61          if (!empty($_POST['ACTION']) && ($_POST['ACTION'] == 'Abuse'))
62          {
63             $abuse_points = 2;
64             $Source_IP = $_POST['source'];
65             echo "Reporting " . $_POST['ACTION'] . " for " . $_POST['source'] . "<br>\n";
66             $res = pg_exec($this->database, "SELECT * FROM object_abuse
67                                   WHERE objectid='". $_GET['oid'] ."' AND source='$Source_IP'");
68             if (pg_numrows($res) == 0)
69             {
70                echo "$Source_IP is new.<br>";
71                pg_exec($this->database, "INSERT INTO object_abuse VALUES ('" . $_GET['oid'] .
72                                   "', '$Source_IP', '$abuse_points', '', NOW())");
73             }
74             else
75             {
76                $abuse = pg_fetch_object($res, 0);
77                $abuse_points += $abuse->nr_abuses;
78                echo $Source_IP . " will get " . $abuse_points . " abuse points.<br>";
79                echo "Status was " . $abuse->status . "<br>";
80                pg_exec($this->database, "UPDATE object_abuse SET nr_abuses='$abuse_points'" .
81                            ", last_change=NOW() WHERE objectid='" . $_GET['oid'] . "' AND source='$Source_IP'");
82
83                if ($abuse_points >= 6)
84                {
85                   echo "<h2 class='error'>Block IP adrress $Source_IP on the firewall.</h2>";
86                   pg_exec($this->database, "UPDATE object_abuse SET status='dropped'" .
87                            " WHERE objectid='" . $_GET['oid'] . "' AND source='$Source_IP'");
88                }
89             }
90 //select rawdata from log where logid in (select logid from log_abuse where source='');
91
92          }
93          else if (!empty($_POST['ACTION']) && $_POST['ACTION'] == 'Whitelist')
94          {
95             $Source_IP = $_POST['source'];
96             echo "Whitelisting " . $_POST['source'] . "<br>\n";
97             $res = pg_exec($this->database, "SELECT * FROM object_abuse
98                                   WHERE objectid='". $_GET['oid'] ."' AND source='$Source_IP'");
99             $Status = $_POST['status'];
100             if (pg_numrows($res) == 0)
101             {
102                echo "$Source_IP is new.<br>";
103                pg_exec($this->database, "INSERT INTO object_abuse VALUES ('" . $_GET['oid'] .
104                                   "', '$Source_IP', 0, '$Status', NOW())");
105             }
106             else
107             {
108                $abuse = pg_fetch_object($res, 0);
109                echo $Source_IP . " will be whitelisted.<br>";
110                echo "Status was " . $abuse->status . "<br>";
111                pg_exec($this->database, "UPDATE object_abuse SET status='$Status'" .
112                            ", last_change=NOW() WHERE objectid='" . $_GET['oid'] . "' AND source='$Source_IP'");
113             }
114          }
115          else if (!empty($_POST['ACTION']) && $_POST['ACTION'] == 'Investigate')
116          {
117             //  Present a list of abuse addresses in one subnet
118
119             $Subnet = $_POST['subnet'];
120             $res = pg_exec($this->database, "SELECT * FROM object_abuse
121                                   WHERE objectid='". $_GET['oid'] ."' AND source << '$Subnet'");
122
123             echo pg_numrows($res) . " records found.<br>";
124
125             echo "<table>";
126             echo "<tr><th>IP address</th><th>Abuses</th><th>Status</th></tr>\n";
127
128             for ($ip = 0; $ip < pg_numrows($res); $ip++)
129             {
130                $abuse = pg_fetch_object($res, $ip);
131                echo "<tr><td>";
132                echo $abuse->source;
133                echo "</td><td>";
134                echo $abuse->nr_abuses;
135                echo "</td><td>";
136                echo $abuse->status;
137                echo "</td></tr>";
138             }
139             echo "</table>";
140
141          }
142          else if (!empty($_POST['ACTION']) && $_POST['ACTION'] == 'Block')
143          {
144             //  Block an entire subnet and remove the addresses from the list
145
146             $Subnet = $_POST['subnet'];
147             pg_exec($this->database, "DELETE FROM object_abuse WHERE objectid = '" . $_GET['oid']
148                                          . "' AND source << '$Subnet'");
149             $abuse_points = 8;
150             pg_exec($this->database, "INSERT INTO object_abuse VALUES ('" . $_GET['oid'] .
151                                   "', '$Subnet', '$abuse_points', 'dropped', NOW())");
152          }
153
154          if (!empty($_GET['src']))
155          {
156             $oid = $_GET['oid'];
157             $src = $_GET['src'];
158             $res = pg_exec($this->database, "SELECT * FROM log WHERE logid IN
159                                      (SELECT logid FROM log_abuse WHERE objectid=$oid AND source <<= '$src')
160                                           ORDER BY object_timestamp");
161             echo "<table>";
162             echo "<tr><th>Time</th><th>Service</th><th>Abused log entry</th></tr>";
163             for ($row = 0; $row < pg_numrows($res); $row++)
164             {
165                $log = pg_fetch_object($res, $row);
166                //$log = pg_fetch_object(pg_exec($this->database, "SELECT rawdata FROM log WHERE logid=" . $logid->logid), 0);
167                echo "<tr><td>";
168                echo $log->object_timestamp;
169                echo "</td><td>";
170                echo $log->servicecode;
171                echo "</td><td>";
172                echo $log->rawdata;
173                echo "</td></tr>";
174             }
175             echo "</table>";
176          }
177          else
178          {
179             echo "<form method='post'>";
180             echo "Report <input type='submit' name='ACTION' value='Abuse'>";
181             echo " for IP address <input type='text' name='source'>";
182             echo " or <input type='submit' name='ACTION' value='Whitelist'>";
183             echo " with status <input type='text' name='status'>";
184             echo "</form>"; 
185    
186             echo "<form method='post'>";
187             echo "<input type='submit' name='ACTION' value='Investigate'>";
188             echo " or <input type='submit' name='ACTION' value='Block'>";
189             echo " this subnet: <input type='text' name='subnet'>";
190             echo "</form>"; 
191    
192             $res = pg_exec($this->database, "SELECT * FROM object_abuse
193                                      WHERE objectid='". $_GET['oid'] ."' ORDER BY source");
194    
195             echo pg_numrows($res) . " records found.<br>";
196    
197             echo "<table>";
198             echo "<tr><th>IP address</th><th>Abuses</th><th>Status</th><th>Last Changed</th></tr>\n";
199    
200             for ($ip = 0; $ip < pg_numrows($res); $ip++)
201             {
202                $abuse = pg_fetch_object($res, $ip);
203                echo "<tr><td>";
204                echo $abuse->source;
205                echo "</td><td>";
206                echo "<a href='abuse.php?oid=" . $_GET['oid'] . "&src=" . $abuse->source . "'>" .  $abuse->nr_abuses . "</a>";
207                echo "</td><td>";
208                echo $abuse->status;
209                echo "</td><td>";
210                echo $abuse->last_change;
211                echo "</td></tr>";
212             }
213             echo "</table>";
214          }
215       }
216    }
217 }
218
219 $page = new abuse_list("Gnucomo Abuse List");
220
221 $page->Showpage();
222
223 ?>