Iptables.php
[Pman.Admin] / Iptables.php
1 <?php
2 /***
3  * how this might work..
4  *
5  * a) login - if it's a new IP not seen that day
6  * --> touch /tmp/run_pman_admin_iptables
7  *
8  * cron every minute... ?? << could do some kind of IPC?!?
9  *
10  * if file exists -> run this code.
11  *
12  * This code finds all the IP's used in the last 24 hours.
13  * and opens the firew all for them.
14  *
15  *
16  *
17  */
18
19 require_once 'Pman.php';
20
21 class Pman_Admin_Iptables extends Pman {
22     
23     static $cli_desc = "Read ip addresses that have been used to log in, and add them to the iptables list..";
24     
25    
26     
27     function getAuth()
28     {
29         if (!$this->bootLoader->cli) {
30             die("cli only");
31         }
32      }
33     function get()
34     {
35         // find IP's that have been used to log in.
36         // dump them to the iptables file.
37         // if it's different - apply it...
38         DB_DataObject::debugLevel(1);
39         // need to get a list of users who have Admin.Iptables rights..
40         /*$gr = DB_DataObject::factory('group_rights');
41         $grps = $gr->groupsWithRights('Admin.Iptables', 'S');
42         
43         $gr = DB_DataObject::factory('groups');
44         $gr->get('name', 'Administrators');
45         $grps[] = $gr->id;
46         
47         $gm = DB_DataObject::factory('group_members');
48         $gm->whereAddIn('group_id', $grps, 'int');
49         $gm->selectAdd();
50         $gm->selectAdd('distinct(user_id) as user_id');
51         $peps = $gm->fetchAll('user_id');
52         
53         
54         */
55         
56         
57         $p = DB_DataObject::Factory('Person');
58         $p->autoJoin();
59         $p->whereAdd("company_id_comptype = 'OWNER'");
60         $peps = $p->fetchAll('id');
61         
62         
63         $e = DB_DataObject::factory('Events');
64         $e->action = 'LOGIN';
65         $e->selectAdd();
66         $e->selectAdd('distinct(ipaddr) as ipaddr');
67         $e->person_table = DB_DataObject::factory('person')->tableName();
68         $e->whereAddIn('person_id', $peps, 'int');
69         switch( $e->getDatabaseConnection()->phptype) {
70             case 'mysql':
71                 $e->whereAdd("event_when > NOW() - INTERVAL 1 DAY");
72                 break;
73             case 'pgsql':
74                 $e->whereAdd("event_when > NOW() - INTERVAL '1 DAY'");
75                 break;   
76         }
77         $ips = $e->fetchAll('ipaddr');
78         print_r($ips);exit;
79         
80          
81
82         
83     }
84     
85 }