forked from grizz/IXP-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsec-processor.php
More file actions
executable file
·297 lines (242 loc) · 8.79 KB
/
sec-processor.php
File metadata and controls
executable file
·297 lines (242 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#! /usr/bin/env php
<?php
/*
* Copyright (C) 2009-2011 Internet Neutral Exchange Association Limited.
* All Rights Reserved.
*
* This file is part of IXP Manager.
*
* IXP Manager is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version v2.0 of the License.
*
* IXP Manager is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License v2.0
* along with IXP Manager. If not, see:
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* INEX Log Processor Script
*
* Takes events from the Simple Event Correlator (see /usr/local/etc/sec/*.conf files)
*
* Barry O'Donovan <[email protected]>
*
* http://www.inex.ie/
* (c) Copyright 2009 Internet Neutral Exchange Association Ltd (INEX)
*
*/
/**
* Instructions:
* =============
*
* In sec.conf files, call this script with an action such as:
*
* action=pipe 'type=%s:switch=$1:mac=$2:port=$3' /home/barryo/ixp-manager-v2/bin/sec-test.php
*
* Note how the parameters are piped to this script. This script reads a line from stdin and
* parses for param1=val1:param2=val2:...
*
* If you pass the switch= param (where the value is, for example, sw01), the $switch variable
* will be populated from the INEX database with the row of the relevent switch. Passing a
* switch port as port= (where the value might be GigabitEthernet1/6) will additionally
* populate $switchPort, $physicalInterface, $virtualInterface and $cust with the
* appropriate details.
*
* Lastly, if you parse the log file date as follows:
*
* Log file line: Feb 25 06:42:51 ...
* Reg exp: ^(\w+)\s(\d+)\s(\d\d):(\d\d):(\d\d)
*
* and pass in those parameters as:
*
* 'month=$1:day=$2:hour=$3:minute=$4:second=$5'
*
* then $date will contain (in this example) "Feb 25 06:42:51"
*
* NB: only parameters explicitly ALLOWED in the switch() below will be permitted. Please add new
* parameters there.
*
*/
error_reporting( E_ALL|E_STRICT );
ini_set( 'display_errors', true );
define('APPLICATION_ENV', 'production');
defined( 'APPLICATION_PATH' ) || define( 'APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application' ) );
set_include_path( implode( PATH_SEPARATOR, array(
realpath( APPLICATION_PATH . '/../library' ),
get_include_path(),
)));
date_default_timezone_set( 'Europe/Dublin' );
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try
{
$application->bootstrap();
$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap( 'frontController' );
$bootstrap->bootstrap();
if( $bootstrap->hasResource( 'zfdebug' ) )
$bootstrap->unregisterPluginResource( 'zfdebug' );
$namespace = $bootstrap->getResource( 'namespace' );
}
catch( Exception $e )
{
die( print_r( $e, true ) );
}
define( 'MAILTO', '[email protected]' );
// Read the parameters from STDIN
if( !defined( 'STDIN' ) )
{
define( 'STDIN', fopen( 'php://stdin', 'r' ) );
}
while( !feof( STDIN ) )
{
$in = fgets( STDIN, 4096 );
$bootstrap->getResource( 'logger' )->debug( "STDIN: $in" );
break;
}
// Now process the parameters only allowing those explicitly listed
$args = split( '&', $in );
foreach( $args as $arg )
{
$temp = split( '=', $arg );
switch( $temp[0] )
{
case 'day':
case 'hour':
case 'minute':
case 'month':
case 'second':
case 'switch':
$$temp[0] = trim( $temp[1] );
break;
case 'ip':
case 'mac':
case 'port':
case 'router':
case 'state':
case 'type':
$namespace->$temp[0] = trim( $temp[1] );
break;
default:
die( 'Illegal parameter: ' . $temp[0] . "\n" );
break;
}
}
// die( print_r( $namespace->getIterator(), true ) );
if( isset( $hour ) )
{
$namespace->date = ( isset( $month ) ? $month : '' )
. ' ' . ( isset( $day ) ? $day : '' )
. " $hour:$minute:$second";
}
else
$namespace->date = '';
//
// Populate $switch, $switchPort, $physicalInterface, $virtualInterface, and $cust
// variables if we have that information
//
if( isset( $switch ) )
{
$namespace->switch = Doctrine::getTable('SwitchTable')->findOneByName( $switch );
if( !$namespace->switch )
die( "No such switch: $switch\n" );
// Weed out core ports (and also Port-channel interfaces which are all currently core)
// FIXME Port-channel could also be a customer but we don't have any yet
$namespace->isCorePort = false;
if( preg_match( "/^Port-channel/", $namespace->port ) )
{
$namespace->isCorePort = true;
$namespace->switchPort = null;
}
else
{
$namespace->switchPort = Doctrine_Query::create()
->from( 'Switchport sp' )
->where( 'sp.switchid = ? AND sp.name = ?', array( $namespace->switch['id'], $namespace->port ) )
->fetchOne();
}
if( $namespace->isCorePort || $namespace->switchPort['type'] == Switchport::TYPE_CORE )
{
$namespace->isCorePort = true;
$namespace->physicalInterface = null;
$namespace->virtualInterface = null;
$namespace->cust = Doctrine::getTable( 'Cust' )->findOneByShortname( 'inex' );
}
else
{
$namespace->physicalInterface = Doctrine::getTable( 'Physicalinterface' )->findOneBySwitchportid( $namespace->switchPort['id'] );
$namespace->virtualInterface = Doctrine::getTable( 'Virtualinterface' )->find( $namespace->physicalInterface['virtualinterfaceid'] );
$namespace->cust = Doctrine::getTable( 'Cust' )->find( $namespace->virtualInterface['custid'] );
$namespace->user = Doctrine::getTable( 'User' )->findOneByCustidAndPrivs( $namespace->cust['id'], User::AUTH_CUSTADMIN );
}
}
if( isset( $namespace->ip ) )
{
$ip = $namespace->ip;
// is it an IPv4 or a v6 address?
if( ip2long( $ip ) === false )
{
$namespace->ipv = 6;
// FIXME: Yeah, this isn't good. We need a better way to reliably find an IPv6 address in the DB
if( $namespace->ip = Doctrine::getTable('Ipv6address')->findOneByAddress( strtolower( $ip ) ) )
$namespace->vlaninterface = Doctrine::getTable( 'Vlaninterface' )->findOneByIpv6addressid( $namespace->ip['id'] );
}
else
{
$namespace->ipv = 4;
if( $namespace->ip = Doctrine::getTable('Ipv4address')->findOneByAddress( $ip ) )
$namespace->vlaninterface = Doctrine::getTable( 'Vlaninterface' )->findOneByIpv4addressid( $namespace->ip['id'] );
}
$namespace->virtualinterface = Doctrine::getTable( 'Virtualinterface' )->find( $namespace->vlaninterface['virtualinterfaceid'] );
$namespace->cust = Doctrine::getTable( 'Cust' )->find( $namespace->virtualinterface['custid'] );
$namespace->vlan = Doctrine::getTable( 'Vlan' )->find( $namespace->vlaninterface->vlanid );
$namespace->user = Doctrine::getTable( 'User' )->findOneByCustidAndPrivs( $namespace->cust['id'], User::AUTH_CUSTADMIN );
}
//
// Now act on individual log messages
//
$front = $bootstrap->getResource( 'frontController' );
$front->throwExceptions( true );
$front->setRouter( new IXP_Controller_Router_Cli() );
$front->setResponse( new Zend_Controller_Response_Cli() );
switch( $namespace->type )
{
case 'BGP_AUTH':
$front->setRequest( new IXP_Controller_Request_Simple( 'bgp-auth', 'sec', null ) );
break;
case 'PORT_UPDOWN':
case 'LINEPROTO_UPDOWN':
$front->setRequest( new IXP_Controller_Request_Simple( 'port-updown', 'sec', null ) );
break;
case 'SECURITY_VIOLATION':
$front->setRequest( new IXP_Controller_Request_Simple( 'security-violation', 'sec', null ) );
break;
default:
// FIXME!!
break;
}
$front->setParam( 'noViewRenderer', true )
->setParam( 'disableOutputBuffering', true );
$application->run();
function switch_port_updown( $switch, $switchPort, $cust, $state, $date )
{
$mail = new Zend_Mail();
$mail->setBodyText( "\n=== TEST MODE :: NO MAIL GONE TO CUSTOMERS ===\n\n"
. "Port $state alert:\n\n Switch {$switch['name']}\n Interface {$switchPort['name']}\n Date: $date\n\nPort is owned by {$cust['name']}.\n\n"
);
$mail->setFrom( '[email protected]', 'INEX Operations' );
$mail->addTo( MAILTO, 'INEX Operations' );
$mail->setSubject( "Port $state alert :: {$cust['name']} :: {$switch['name']}/{$switchPort['name']}" );
$mail->send();
}
?>