2004-09-10 15:38:14 +00:00
/* NeoStats - IRC Statistical Services
2005-01-24 22:22:57 +00:00
* * Copyright ( c ) 1999 - 2005 Adam Rutter , Justin Hammond , Mark Hetherington
2002-09-04 08:52:34 +00:00
* * http : //www.neostats.net/
* *
* * This program 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 ; either version 2 of the License , or
* * ( at your option ) any later version .
* *
* * This program 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
* * along with this program ; if not , write to the Free Software
* * Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307
* * USA
* *
* * NeoStats CVS Identification
2003-09-22 15:14:02 +00:00
* * $ Id $
2002-08-31 09:28:34 +00:00
*/
2005-05-23 22:58:06 +00:00
/* TODO:
* - Akill support .
* - remove akill must check whether an akill was added by opsb before
* removing it otherwise blsb becomes a way for opers to remove any
* akill on the network including those they may not normally have
* access to .
*/
2005-02-11 21:16:05 +00:00
# include "neostats.h"
# ifdef HAVE_ARPA_NAMESER_H
2002-08-31 09:28:34 +00:00
# include <arpa/nameser.h>
2004-08-01 23:05:50 +00:00
# endif
2002-08-31 09:28:34 +00:00
# include "opsb.h"
2005-10-19 22:34:35 +00:00
typedef struct cache_entry {
unsigned long ip ;
time_t when ;
} cache_entry ;
static void dns_callback ( void * scandata , adns_answer * a ) ;
2005-05-23 22:58:06 +00:00
static int startscan ( scaninfo * scandata ) ;
2005-08-09 12:12:53 +00:00
static int unconf ( void * ) ;
2005-10-14 19:15:17 +00:00
static int event_nickip ( const CmdParams * cmdparams ) ;
static int opsb_cmd_list ( const CmdParams * cmdparams ) ;
static int opsb_cmd_add ( const CmdParams * cmdparams ) ;
static int opsb_cmd_del ( const CmdParams * cmdparams ) ;
static int opsb_cmd_check ( const CmdParams * cmdparams ) ;
static int opsb_cmd_remove ( const CmdParams * cmdparams ) ;
static int opsb_set_cb ( const CmdParams * cmdparams , SET_REASON reason ) ;
2005-10-19 22:04:37 +00:00
static int opsb_set_target_cb ( const CmdParams * cmdparams , SET_REASON reason ) ;
2005-10-14 19:15:17 +00:00
static int opsb_set_exclusions_cb ( const CmdParams * cmdparams , SET_REASON reason ) ;
2003-11-05 12:31:21 +00:00
2004-08-01 23:05:50 +00:00
Bot * opsb_bot ;
2005-10-19 22:20:27 +00:00
opsbcfg opsb ;
list_t * opsbq ;
list_t * opsbl ;
list_t * cache ;
2003-10-29 22:43:06 +00:00
2004-08-01 23:05:50 +00:00
/** Copyright info */
2005-10-19 22:29:37 +00:00
static const char * opsb_copyright [ ] = {
2005-01-24 22:22:57 +00:00
" Copyright (c) 1999-2005, NeoStats " ,
2004-08-01 23:05:50 +00:00
" http://www.neostats.net/ " ,
NULL
} ;
2002-10-24 09:27:58 +00:00
2004-08-01 23:05:50 +00:00
/** Module Info definition
* version information about our module
* This structure is required for your module to load and run on NeoStats
*/
ModuleInfo module_info = {
2002-08-31 09:28:34 +00:00
" OPSB " ,
2005-05-23 22:58:06 +00:00
" Open Proxy Scanning Bot " ,
2004-08-01 23:05:50 +00:00
opsb_copyright ,
opsb_about ,
NEOSTATS_VERSION ,
2004-03-13 23:35:08 +00:00
MODULE_VERSION ,
2003-10-08 13:57:16 +00:00
__DATE__ ,
2004-08-01 23:05:50 +00:00
__TIME__ ,
2004-09-30 19:35:16 +00:00
MODULE_FLAG_LOCAL_EXCLUDES ,
2004-08-01 23:05:50 +00:00
0 ,
2005-10-19 22:04:37 +00:00
0 ,
2003-10-08 13:57:16 +00:00
} ;
2002-08-31 09:28:34 +00:00
2005-05-23 22:58:06 +00:00
static bot_cmd opsb_commands [ ] =
{
2005-10-19 22:04:37 +00:00
{ " STATUS " , opsb_cmd_status , 0 , NS_ULEVEL_OPER , opsb_help_status , 0 , NULL , NULL } ,
{ " REMOVE " , opsb_cmd_remove , 1 , NS_ULEVEL_OPER , opsb_help_remove , 0 , NULL , NULL } ,
{ " CHECK " , opsb_cmd_check , 1 , NS_ULEVEL_OPER , opsb_help_check , 0 , NULL , NULL } ,
{ " ADD " , opsb_cmd_add , 2 , NS_ULEVEL_ADMIN , opsb_help_add , 0 , NULL , NULL } ,
{ " DEL " , opsb_cmd_del , 1 , NS_ULEVEL_ADMIN , opsb_help_del , 0 , NULL , NULL } ,
{ " LIST " , opsb_cmd_list , 0 , NS_ULEVEL_ADMIN , opsb_help_list , 0 , NULL , NULL } ,
2005-08-17 20:59:24 +00:00
NS_CMD_END ( )
2005-05-23 22:58:06 +00:00
} ;
static bot_setting opsb_settings [ ] =
{
2005-10-19 22:04:37 +00:00
{ " TARGETIP " , opsb . targetip , SET_TYPE_IPV4 , 0 , MAXHOST , NS_ULEVEL_ADMIN , NULL , opsb_help_set_targetip , opsb_set_target_cb , ( void * ) 0 } ,
{ " TARGETPORT " , & opsb . targetport , SET_TYPE_INT , 0 , 65535 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_targetport , opsb_set_target_cb , ( void * ) 6667 } ,
2005-05-23 22:58:06 +00:00
{ " AKILL " , & opsb . doakill , SET_TYPE_BOOLEAN , 0 , 0 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_akill , opsb_set_cb , ( void * ) 1 } ,
2005-05-25 19:04:32 +00:00
{ " AKILLTIME " , & opsb . akilltime , SET_TYPE_INT , 0 , 20736000 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_akilltime , opsb_set_cb , ( void * ) TS_ONE_DAY } ,
2005-05-23 22:58:06 +00:00
{ " MAXBYTES " , & opsb . maxbytes , SET_TYPE_INT , 0 , 100000 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_maxbytes , opsb_set_cb , ( void * ) 500 } ,
{ " TIMEOUT " , & opsb . timeout , SET_TYPE_INT , 0 , 120 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_timeout , opsb_set_cb , ( void * ) 30 } ,
{ " OPENSTRING " , opsb . openstring , SET_TYPE_MSG , 0 , BUFSIZE , NS_ULEVEL_ADMIN , NULL , opsb_help_set_openstring , opsb_set_cb , ( void * ) " *** Looking up your hostname... " } ,
{ " SCANMSG " , opsb . scanmsg , SET_TYPE_MSG , 0 , BUFSIZE , NS_ULEVEL_ADMIN , NULL , opsb_help_set_scanmsg , opsb_set_cb , ( void * ) " Your Host is being Scanned for Open Proxies " } ,
2005-05-25 19:04:32 +00:00
{ " CACHETIME " , & opsb . cachetime , SET_TYPE_INT , 0 , TS_ONE_DAY , NS_ULEVEL_ADMIN , NULL , opsb_help_set_cachetime , opsb_set_cb , ( void * ) TS_ONE_HOUR } ,
2005-05-23 22:58:06 +00:00
{ " CACHESIZE " , & opsb . cachesize , SET_TYPE_INT , 0 , 10000 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_cachesize , opsb_set_cb , ( void * ) 1000 } ,
{ " VERBOSE " , & opsb . verbose , SET_TYPE_BOOLEAN , 0 , 0 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_verbose , opsb_set_cb , ( void * ) 1 } ,
{ " EXCLUSIONS " , & opsb . exclusions , SET_TYPE_BOOLEAN , 0 , 0 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_exclusions , opsb_set_exclusions_cb , ( void * ) 0 } ,
2005-06-11 06:11:06 +00:00
{ " DOREPORT " , & opsb . doreport , SET_TYPE_BOOLEAN , 0 , 0 , NS_ULEVEL_ADMIN , NULL , opsb_help_set_doreport , opsb_set_cb , ( void * ) 1 } ,
2005-08-17 20:59:24 +00:00
NS_SETTING_END ( )
2005-05-23 22:58:06 +00:00
} ;
/** BotInfo */
static BotInfo opsb_botinfo =
{
" opsb " ,
" opsb1 " ,
" opsb " ,
BOT_COMMON_HOST ,
" Proxy Scanning Bot " ,
BOT_FLAG_SERVICEBOT | BOT_FLAG_RESTRICT_OPERS | BOT_FLAG_DEAF ,
opsb_commands ,
opsb_settings ,
} ;
ModuleEvent module_events [ ] =
{
{ EVENT_NICKIP , event_nickip , EVENT_FLAG_EXCLUDE_ME } ,
2005-09-19 20:38:09 +00:00
NS_EVENT_END ( )
2005-05-23 22:58:06 +00:00
} ;
/** @brief ports_sort
*
* list handler to find scan
*
* @ param
*
* @ return
*/
int findscan ( const void * key1 , const void * key2 )
{
const scaninfo * chan1 = key1 ;
return ircstrcasecmp ( chan1 - > who , key2 ) ;
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief ports_sort
*
* Sort ports list handler
*
* @ param
*
* @ return
*/
2005-10-19 22:29:37 +00:00
static int ports_sort ( const void * key1 , const void * key2 )
2005-05-23 22:58:06 +00:00
{
2003-10-29 11:07:23 +00:00
port_list * pl1 = ( port_list * ) key1 ;
port_list * pl2 = ( port_list * ) key2 ;
2005-05-23 22:58:06 +00:00
if ( pl1 - > type = = pl2 - > type )
{
if ( pl1 - > port = = pl2 - > port )
2003-10-29 11:07:23 +00:00
return 0 ;
2005-05-23 22:58:06 +00:00
if ( pl1 - > port > pl2 - > port )
2003-10-29 11:07:23 +00:00
return 1 ;
2005-05-23 22:58:06 +00:00
return - 1 ;
2003-10-29 11:07:23 +00:00
}
2005-05-23 22:58:06 +00:00
if ( pl1 - > type > pl2 - > type )
return 1 ;
2004-02-26 19:49:27 +00:00
return - 1 ;
2003-10-29 11:07:23 +00:00
}
2002-08-31 09:28:34 +00:00
2004-02-26 19:49:27 +00:00
2005-05-23 22:58:06 +00:00
/** @brief opsb_cmd_remove
*
* REMOVE command handler
*
* @ param cmdparam struct
*
* @ return NS_SUCCESS if suceeds else result of command
*/
2005-10-14 19:15:17 +00:00
int opsb_cmd_remove ( const CmdParams * cmdparams )
2004-02-26 19:49:27 +00:00
{
2004-08-01 23:05:50 +00:00
irc_rakill ( opsb_bot , cmdparams - > av [ 0 ] , " * " ) ;
irc_chanalert ( opsb_bot , " %s attempted to remove an akill for *@%s " , cmdparams - > source - > name , cmdparams - > av [ 0 ] ) ;
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
2004-02-26 19:49:27 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief opsb_cmd_check
*
* CHECK command handler
*
* @ param cmdparam struct
*
* @ return NS_SUCCESS if suceeds else result of command
*/
2005-10-14 19:15:17 +00:00
int opsb_cmd_check ( const CmdParams * cmdparams )
2004-02-26 19:49:27 +00:00
{
2005-03-03 22:42:00 +00:00
Client * scanuser ;
2004-02-26 19:49:27 +00:00
scaninfo * scandata ;
2004-08-01 23:05:50 +00:00
if ( ( list_find ( opsbl , cmdparams - > av [ 0 ] , findscan ) ) | | ( list_find ( opsbq , cmdparams - > av [ 0 ] , findscan ) ) ) {
irc_prefmsg ( opsb_bot , cmdparams - > source , " Already Scanning (or in queue) %s. Not Scanning again " , cmdparams - > av [ 0 ] ) ;
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
2004-02-26 19:49:27 +00:00
}
2005-05-12 20:15:50 +00:00
scandata = ns_malloc ( sizeof ( scaninfo ) ) ;
2004-02-26 19:49:27 +00:00
scandata - > doneban = 0 ;
2004-08-09 20:40:55 +00:00
scandata - > reqclient = cmdparams - > source ;
2005-03-03 22:42:00 +00:00
scanuser = FindUser ( cmdparams - > av [ 0 ] ) ;
if ( scanuser ) {
2004-02-26 19:49:27 +00:00
/* don't scan users from my server */
2005-03-03 22:42:00 +00:00
if ( IsMe ( scanuser ) ) {
2004-08-01 23:05:50 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Error, Can not scan NeoStats Bots " ) ;
2004-09-25 21:41:07 +00:00
ns_free ( scandata ) ;
2005-03-03 22:42:00 +00:00
return NS_SUCCESS ;
2002-08-31 09:28:34 +00:00
}
2005-10-19 22:11:24 +00:00
if ( scanuser - > ip . s_addr = = 0 ) {
2004-07-06 12:10:07 +00:00
/* if its here, we don't have the IP address yet */
2005-04-03 13:46:49 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Error: We don't have a IP address for %s. " , scanuser - > name ) ;
2004-09-25 21:41:07 +00:00
ns_free ( scandata ) ;
2005-03-03 22:42:00 +00:00
return NS_SUCCESS ;
2002-08-31 09:28:34 +00:00
}
2005-04-03 13:46:49 +00:00
strlcpy ( scandata - > who , scanuser - > name , MAXHOST ) ;
strlcpy ( scandata - > lookup , scanuser - > user - > hostname , MAXHOST ) ;
strlcpy ( scandata - > server , scanuser - > uplink - > name , MAXHOST ) ;
scandata - > ip . s_addr = scanuser - > ip . s_addr ;
2004-02-26 19:49:27 +00:00
} else {
2004-08-01 23:05:50 +00:00
strlcpy ( scandata - > who , cmdparams - > av [ 0 ] , MAXHOST ) ;
strlcpy ( scandata - > lookup , cmdparams - > av [ 0 ] , MAXHOST ) ;
2005-05-12 20:23:16 +00:00
os_memset ( scandata - > server , 0 , MAXHOST ) ;
2004-07-06 12:10:07 +00:00
/* is it a ip address or host */
2005-04-03 13:46:49 +00:00
if ( inet_aton ( cmdparams - > av [ 0 ] , & scandata - > ip ) < = 0 ) {
2004-08-01 23:05:50 +00:00
scandata - > ip . s_addr = 0 ;
2005-05-23 22:58:06 +00:00
if ( dns_lookup ( scandata - > lookup , adns_r_a , dns_callback , ( void * ) scandata ) ! = 1 ) {
2005-05-08 09:15:23 +00:00
nlog ( LOG_WARNING , " DNS: startscan() DO_DNS_HOST_LOOKUP dns_lookup() failed " ) ;
ns_free ( scandata ) ;
2005-05-13 00:19:07 +00:00
return NS_SUCCESS ;
2005-05-08 09:15:23 +00:00
}
irc_prefmsg ( opsb_bot , cmdparams - > source , " Checking %s for open Proxies " , cmdparams - > av [ 0 ] ) ;
2005-05-13 00:19:07 +00:00
return NS_SUCCESS ;
2005-05-08 09:15:23 +00:00
}
2004-02-26 19:49:27 +00:00
}
2004-08-01 23:05:50 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Checking %s for open Proxies " , cmdparams - > av [ 0 ] ) ;
2004-02-26 19:49:27 +00:00
if ( ! startscan ( scandata ) )
2004-08-01 23:05:50 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Check Failed " ) ;
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
2004-02-26 19:49:27 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief opsb_cmd_list
*
* LIST command handler
*
* @ param cmdparam struct
*
* @ return NS_SUCCESS if suceeds else result of command
*/
2005-10-14 19:15:17 +00:00
int opsb_cmd_list ( const CmdParams * cmdparams )
2004-02-26 19:49:27 +00:00
{
port_list * pl ;
int i ;
lnode_t * lnode ;
2002-08-31 09:28:34 +00:00
2004-08-10 20:08:01 +00:00
lnode = list_first ( opsb . ports ) ;
i = 1 ;
irc_prefmsg ( opsb_bot , cmdparams - > source , " Port List: " ) ;
while ( lnode ) {
pl = lnode_get ( lnode ) ;
irc_prefmsg ( opsb_bot , cmdparams - > source , " %d) %s Port: %d " , i , type_of_proxy ( pl - > type ) , pl - > port ) ;
+ + i ;
lnode = list_next ( opsb . ports , lnode ) ;
}
irc_prefmsg ( opsb_bot , cmdparams - > source , " End of list. " ) ;
2004-10-27 20:23:09 +00:00
CommandReport ( opsb_bot , " %s requested Port List " , cmdparams - > source - > name ) ;
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
}
2005-05-23 22:58:06 +00:00
/** @brief opsb_cmd_add
*
* ADD command handler
*
* @ param cmdparam struct
*
* @ return NS_SUCCESS if suceeds else result of command
*/
2005-10-14 19:15:17 +00:00
int opsb_cmd_add ( const CmdParams * cmdparams )
2004-08-10 20:08:01 +00:00
{
port_list * pl ;
lnode_t * lnode ;
if ( list_isfull ( opsb . ports ) ) {
irc_prefmsg ( opsb_bot , cmdparams - > source , " Error, Ports list is full " ) ;
return NS_SUCCESS ;
}
2005-08-20 11:33:02 +00:00
if ( ! atoi ( cmdparams - > av [ 1 ] ) ) {
2004-09-25 21:41:07 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Port field does not contain a valid port " ) ;
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
}
2005-08-20 11:33:02 +00:00
if ( get_proxy_by_name ( cmdparams - > av [ 0 ] ) < 1 ) {
2004-08-10 20:08:01 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Unknown Proxy type %s " , cmdparams - > av [ 1 ] ) ;
return NS_SUCCESS ;
}
/* check for duplicates */
lnode = list_first ( opsb . ports ) ;
while ( lnode ) {
pl = lnode_get ( lnode ) ;
2005-08-20 11:33:02 +00:00
if ( ( pl - > type = = get_proxy_by_name ( cmdparams - > av [ 0 ] ) ) & & ( pl - > port = = atoi ( cmdparams - > av [ 1 ] ) ) ) {
irc_prefmsg ( opsb_bot , cmdparams - > source , " Duplicate Entry for Protocol %s " , cmdparams - > av [ 0 ] ) ;
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
2003-05-14 14:21:44 +00:00
}
2004-08-10 20:08:01 +00:00
lnode = list_next ( opsb . ports , lnode ) ;
}
2005-05-12 20:15:50 +00:00
pl = ns_malloc ( sizeof ( port_list ) ) ;
2004-08-10 20:08:01 +00:00
pl - > type = get_proxy_by_name ( cmdparams - > av [ 1 ] ) ;
pl - > port = atoi ( cmdparams - > av [ 2 ] ) ;
2004-09-25 21:41:07 +00:00
lnode_create_append ( opsb . ports , pl ) ;
2004-08-10 20:08:01 +00:00
list_sort ( opsb . ports , ports_sort ) ;
save_ports ( ) ;
2005-04-12 11:05:17 +00:00
/* add_port(pl->type, pl->port); */
2005-08-20 11:33:02 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Added Port %d for Protocol %s to Ports list " , pl - > port , cmdparams - > av [ 0 ] ) ;
CommandReport ( opsb_bot , " %s added port %d for protocol %s to Ports list " , cmdparams - > source - > name , pl - > port , cmdparams - > av [ 0 ] ) ;
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
}
2005-05-23 22:58:06 +00:00
/** @brief opsb_cmd_del
*
* DEL command handler
*
* @ param cmdparam struct
*
* @ return NS_SUCCESS if suceeds else result of command
*/
2005-10-14 19:15:17 +00:00
int opsb_cmd_del ( const CmdParams * cmdparams )
2004-08-10 20:08:01 +00:00
{
port_list * pl ;
int i ;
lnode_t * lnode ;
2005-08-20 11:33:02 +00:00
if ( atoi ( cmdparams - > av [ 0 ] ) ! = 0 ) {
2004-02-26 19:49:27 +00:00
lnode = list_first ( opsb . ports ) ;
2004-08-10 20:08:01 +00:00
i = 1 ;
2004-02-26 19:49:27 +00:00
while ( lnode ) {
2005-08-20 11:33:02 +00:00
if ( i = = atoi ( cmdparams - > av [ 0 ] ) ) {
2004-08-10 20:08:01 +00:00
/* delete the entry */
pl = lnode_get ( lnode ) ;
list_delete ( opsb . ports , lnode ) ;
2004-09-25 21:41:07 +00:00
lnode_destroy ( lnode ) ;
2004-08-10 20:08:01 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Deleted Port %d of Protocol %s out of Ports list " , pl - > port , type_of_proxy ( pl - > type ) ) ;
irc_prefmsg ( opsb_bot , cmdparams - > source , " You need to Restart OPSB for the changes to take effect " ) ;
2004-10-27 20:23:09 +00:00
CommandReport ( opsb_bot , " %s deleted port %d of Protocol %s out of Ports list " , cmdparams - > source - > name , pl - > port , type_of_proxy ( pl - > type ) ) ;
2004-09-25 21:41:07 +00:00
ns_free ( pl ) ;
2004-08-10 20:08:01 +00:00
/* just to be sure, lets sort the list */
list_sort ( opsb . ports , ports_sort ) ;
save_ports ( ) ;
return 1 ;
2004-02-26 19:49:27 +00:00
}
2004-08-10 20:08:01 +00:00
+ + i ;
2004-02-26 19:49:27 +00:00
lnode = list_next ( opsb . ports , lnode ) ;
2004-08-10 20:08:01 +00:00
}
/* if we get here, then we can't find the entry */
2005-08-20 11:33:02 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Error, Can't find entry %d. /msg %s ports list " , atoi ( cmdparams - > av [ 0 ] ) , opsb_bot - > name ) ;
2003-05-14 14:21:44 +00:00
} else {
2004-08-10 20:08:01 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " Error, Out of Range " ) ;
}
return NS_SUCCESS ;
}
2005-05-23 22:58:06 +00:00
/** @brief opsb_set_cb
*
* Set callback
* Remove unconfigured warning if needed
*
* @ cmdparams pointer to commands param struct
* @ cmdparams reason for SET
*
* @ return NS_SUCCESS if suceeds else NS_FAILURE
*/
2005-10-14 19:15:17 +00:00
int opsb_set_cb ( const CmdParams * cmdparams , SET_REASON reason )
2004-02-26 19:49:27 +00:00
{
2005-02-15 23:20:49 +00:00
if ( reason = = SET_CHANGE )
{
opsb . confed = 1 ;
DBAStoreConfigInt ( " Confed " , & opsb . confed ) ;
DelTimer ( " unconf " ) ;
2005-10-19 22:04:37 +00:00
}
return NS_SUCCESS ;
}
/** @brief opsb_set_cb
*
* Set callback
* Remove unconfigured warning if needed
*
* @ cmdparams pointer to commands param struct
* @ cmdparams reason for SET
*
* @ return NS_SUCCESS if suceeds else NS_FAILURE
*/
int opsb_set_target_cb ( const CmdParams * cmdparams , SET_REASON reason )
{
if ( reason = = SET_CHANGE )
{
2005-10-20 21:22:15 +00:00
( void ) opsb_set_cb ( cmdparams , reason ) ;
( void ) init_scanengine ( ) ;
2004-09-28 22:05:51 +00:00
}
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief opsb_set_exclusions_cb
*
* Set callback for exclusions
* Enable or disable exclude event flag
*
* @ cmdparams pointer to commands param struct
* @ cmdparams reason for SET
*
* @ return NS_SUCCESS if suceeds else NS_FAILURE
*/
2005-10-14 19:15:17 +00:00
static int opsb_set_exclusions_cb ( const CmdParams * cmdparams , SET_REASON reason )
2005-03-07 23:27:30 +00:00
{
if ( reason = = SET_LOAD | | reason = = SET_CHANGE )
{
SetAllEventFlags ( EVENT_FLAG_USE_EXCLUDE , opsb . exclusions ) ;
}
return NS_SUCCESS ;
}
2005-05-23 22:58:06 +00:00
/** @brief unconf
2004-08-01 23:05:50 +00:00
*
2005-05-23 22:58:06 +00:00
* unconfigured warn timer callback
2004-08-01 23:05:50 +00:00
*
* @ param none
*
* @ return NS_SUCCESS if suceeds else NS_FAILURE
*/
2004-02-26 19:49:27 +00:00
2005-08-09 12:12:53 +00:00
static int unconf ( void * userptr )
2004-08-01 23:05:50 +00:00
{
if ( opsb . confed ! = 1 )
{
irc_chanalert ( opsb_bot , " Warning, OPSB is configured with default Settings. Please Update this ASAP " ) ;
irc_globops ( opsb_bot , " Warning, OPSB is configured with default Settings, Please Update this ASAP " ) ;
}
2004-08-10 20:08:01 +00:00
return NS_SUCCESS ;
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief checkqueue
*
* check queue
*
* @ param none
*
* @ return none
*/
2005-10-20 21:22:15 +00:00
void checkqueue ( void )
2004-08-01 23:05:50 +00:00
{
2002-08-31 09:28:34 +00:00
lnode_t * scannode ;
scaninfo * scandata ;
2002-09-04 08:52:34 +00:00
2003-09-29 12:59:07 +00:00
SET_SEGV_LOCATION ( ) ;
2002-08-31 09:28:34 +00:00
/* exit, if the list is full */
if ( list_isfull ( opsbl ) | | list_isempty ( opsbq ) )
return ;
scannode = list_first ( opsbq ) ;
scandata = lnode_get ( scannode ) ;
list_delete ( opsbq , scannode ) ;
lnode_destroy ( scannode ) ;
2005-10-20 21:22:15 +00:00
( void ) startscan ( scandata ) ;
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief addtocache
*
* add to cache
*
* @ param ip
*
* @ return
*/
2004-08-01 23:05:50 +00:00
void addtocache ( unsigned long ip )
{
2002-08-31 09:28:34 +00:00
lnode_t * cachenode ;
2004-08-01 23:05:50 +00:00
cache_entry * ce ;
2002-09-04 08:52:34 +00:00
2003-09-29 12:59:07 +00:00
SET_SEGV_LOCATION ( ) ;
2002-09-04 08:52:34 +00:00
2002-08-31 09:28:34 +00:00
/* pop off the oldest entry */
if ( list_isfull ( cache ) ) {
2004-08-01 23:05:50 +00:00
dlog ( DEBUG2 , " OPSB: Deleting Tail of Cache: %d " , ( int ) list_count ( cache ) ) ;
2002-08-31 09:28:34 +00:00
cachenode = list_del_last ( cache ) ;
ce = lnode_get ( cachenode ) ;
lnode_destroy ( cachenode ) ;
2004-09-25 21:41:07 +00:00
ns_free ( ce ) ;
2002-08-31 09:28:34 +00:00
}
cachenode = list_first ( cache ) ;
while ( cachenode ) {
ce = lnode_get ( cachenode ) ;
2004-08-01 23:05:50 +00:00
if ( ce - > ip = = ip ) {
dlog ( DEBUG2 , " OPSB: Not adding %ld to cache as it already exists " , ip ) ;
2002-09-04 08:52:34 +00:00
return ;
2002-08-31 09:28:34 +00:00
}
cachenode = list_next ( cache , cachenode ) ;
}
2005-05-12 20:15:50 +00:00
ce = ns_malloc ( sizeof ( cache_entry ) ) ;
2004-08-01 23:05:50 +00:00
ce - > ip = ip ;
2002-08-31 09:28:34 +00:00
ce - > when = time ( NULL ) ;
2004-09-25 21:41:07 +00:00
lnode_create_append ( cache , ce ) ;
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief checkcache
*
* check cache
*
* @ param scandata
*
* @ return
*/
2005-10-19 22:29:37 +00:00
static int checkcache ( scaninfo * scandata )
2004-08-01 23:05:50 +00:00
{
2005-03-03 22:42:00 +00:00
Client * scanclient ;
2002-08-31 09:28:34 +00:00
lnode_t * node , * node2 ;
2004-08-01 23:05:50 +00:00
cache_entry * ce ;
2002-09-04 08:52:34 +00:00
2003-09-29 12:59:07 +00:00
SET_SEGV_LOCATION ( ) ;
2005-03-03 22:42:00 +00:00
if ( scandata - > server )
{
scanclient = FindServer ( scandata - > server ) ;
if ( scanclient & & ModIsServerExcluded ( scanclient ) )
{
return 1 ;
}
}
if ( scandata - > who )
{
scanclient = FindUser ( scandata - > who ) ;
if ( scanclient & & ModIsUserExcluded ( scanclient ) )
{
return 2 ;
}
}
2002-08-31 09:28:34 +00:00
node = list_first ( cache ) ;
while ( node ) {
ce = lnode_get ( node ) ;
/* delete any old cache entries */
if ( ( time ( NULL ) - ce - > when ) > opsb . cachetime ) {
2004-08-01 23:05:50 +00:00
dlog ( DEBUG1 , " OPSB: Deleting old cache entry %ld " , ce - > ip ) ;
2002-08-31 09:28:34 +00:00
node2 = list_next ( cache , node ) ;
list_delete ( cache , node ) ;
lnode_destroy ( node ) ;
2004-09-25 21:41:07 +00:00
ns_free ( ce ) ;
2002-08-31 09:28:34 +00:00
node = node2 ;
break ;
}
2004-08-01 23:05:50 +00:00
if ( ce - > ip = = scandata - > ip . s_addr ) {
dlog ( DEBUG1 , " OPSB: user %s is already in Cache " , scandata - > who ) ;
2003-01-30 11:29:25 +00:00
opsb . cachehits + + ;
2005-03-03 22:42:00 +00:00
if ( scandata - > reqclient )
irc_prefmsg ( opsb_bot , scandata - > reqclient , " User %s is already in Cache " , scandata - > who ) ;
2002-08-31 09:28:34 +00:00
return 3 ;
}
2004-02-26 19:49:27 +00:00
node = list_next ( cache , node ) ;
2002-08-31 09:28:34 +00:00
}
return 0 ;
}
2005-05-23 22:58:06 +00:00
/** @brief event_nickip
*
* NICKIP event handler
* scan user that just signed on the network
*
* @ cmdparams pointer to commands param struct
*
* @ return NS_SUCCESS if suceeds else NS_FAILURE
*/
2002-08-31 09:28:34 +00:00
2005-10-14 19:15:17 +00:00
static int event_nickip ( const CmdParams * cmdparams )
2004-08-01 23:05:50 +00:00
{
2002-08-31 09:28:34 +00:00
scaninfo * scandata ;
lnode_t * scannode ;
2002-09-04 08:52:34 +00:00
2003-09-29 12:59:07 +00:00
SET_SEGV_LOCATION ( ) ;
2002-10-24 09:27:58 +00:00
2003-01-08 14:53:28 +00:00
/* don't scan users from a server that is excluded */
2005-05-23 22:58:06 +00:00
if ( ModIsServerExcluded ( cmdparams - > source - > uplink ) )
2005-05-13 00:19:07 +00:00
return NS_SUCCESS ;
2005-05-23 22:58:06 +00:00
if ( IsNetSplit ( cmdparams - > source ) )
{
dlog ( DEBUG1 , " Ignoring netsplit nick %s " , cmdparams - > source - > name ) ;
2005-05-13 00:19:07 +00:00
return NS_SUCCESS ;
2002-08-31 09:28:34 +00:00
}
2004-08-01 23:05:50 +00:00
scannode = list_find ( opsbl , cmdparams - > source - > name , findscan ) ;
2005-05-23 22:58:06 +00:00
if ( ! scannode )
scannode = list_find ( opsbq , cmdparams - > source - > name , findscan ) ;
if ( scannode )
{
dlog ( DEBUG1 , " event_nickip: Not scanning %s as we are already scanning them " , cmdparams - > source - > name ) ;
2005-05-13 00:19:07 +00:00
return NS_SUCCESS ;
2002-08-31 09:28:34 +00:00
}
2004-08-01 23:05:50 +00:00
irc_prefmsg ( opsb_bot , cmdparams - > source , " %s " , opsb . scanmsg ) ;
2005-05-12 20:15:50 +00:00
scandata = ns_malloc ( sizeof ( scaninfo ) ) ;
2004-08-09 20:40:55 +00:00
scandata - > reqclient = NULL ;
2002-09-04 08:52:34 +00:00
scandata - > doneban = 0 ;
2004-08-01 23:05:50 +00:00
strlcpy ( scandata - > who , cmdparams - > source - > name , MAXHOST ) ;
strlcpy ( scandata - > lookup , cmdparams - > source - > user - > hostname , MAXHOST ) ;
2004-08-09 20:40:55 +00:00
strlcpy ( scandata - > server , cmdparams - > source - > uplink - > name , MAXHOST ) ;
2004-08-01 23:05:50 +00:00
scandata - > ip . s_addr = cmdparams - > source - > ip . s_addr ;
2002-09-06 04:33:28 +00:00
if ( ! startscan ( scandata ) ) {
2004-08-01 23:05:50 +00:00
irc_chanalert ( opsb_bot , " Warning Can't scan %s " , cmdparams - > source - > name ) ;
2005-05-23 22:58:06 +00:00
nlog ( LOG_WARNING , " OBSB event_nickip: Can't scan %s. Check logs for possible errors " , cmdparams - > source - > name ) ;
2002-09-06 04:33:28 +00:00
}
2005-05-13 00:19:07 +00:00
return NS_SUCCESS ;
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief startscan
*
* entry point for all scans including moving scans
* from the queue to the active list
*
* @ param scaninfo * scandata
*
* @ return
*/
2002-08-31 09:28:34 +00:00
2005-10-20 21:22:15 +00:00
static int startscan ( scaninfo * scandata )
2004-08-01 23:05:50 +00:00
{
2002-08-31 09:28:34 +00:00
int i ;
2002-09-04 08:52:34 +00:00
2003-09-29 12:59:07 +00:00
SET_SEGV_LOCATION ( ) ;
2002-09-04 08:52:34 +00:00
2002-09-06 04:33:28 +00:00
/* only check the cache when we have IP addy */
2005-04-03 13:46:49 +00:00
if ( scandata - > ip . s_addr > 0 ) {
2002-09-06 04:33:28 +00:00
i = checkcache ( scandata ) ;
2004-08-09 20:40:55 +00:00
if ( ( i > 0 ) & & ( scandata - > reqclient = = NULL ) ) {
2004-09-25 21:41:07 +00:00
ns_free ( scandata ) ;
2002-09-06 04:33:28 +00:00
return 1 ;
}
2002-08-31 09:28:34 +00:00
}
2005-04-03 13:46:49 +00:00
if ( list_isfull ( opsbl ) ) {
if ( list_isfull ( opsbq ) ) {
irc_chanalert ( opsb_bot , " Warning, Both Current and queue lists are full. Not Adding additional scans " ) ;
2005-05-13 00:19:07 +00:00
dlog ( DEBUG1 , " OPSB: dropped scanning of %s, as queue is full " , scandata - > who ) ;
2005-04-03 13:46:49 +00:00
if ( scandata - > reqclient ) irc_prefmsg ( opsb_bot , scandata - > reqclient , " To Busy. Try again later " ) ;
ns_free ( scandata ) ;
return 0 ;
}
lnode_create_append ( opsbq , scandata ) ;
dlog ( DEBUG1 , " OPSB: Added %s to dns queue " , scandata - > who ) ;
if ( scandata - > reqclient ) irc_prefmsg ( opsb_bot , scandata - > reqclient , " Your Request has been added to the Queue " ) ;
return 1 ;
}
start_proxy_scan ( scandata ) ;
#if 0
2005-05-23 22:58:06 +00:00
if ( dns_lookup ( scandata - > lookup , adns_r_a , dns_callback , scandata ) ! = 1 ) {
2005-04-03 13:46:49 +00:00
nlog ( LOG_WARNING , " OPSB: startscan() DO_DNS_HOST_LOOKUP dns_lookup() failed " ) ;
ns_free ( scandata ) ;
checkqueue ( ) ;
return 0 ;
2002-08-31 09:28:34 +00:00
}
2005-04-03 13:46:49 +00:00
# endif
lnode_create_append ( opsbl , scandata ) ;
dlog ( DEBUG1 , " OPSB: Added %s to Scan active list " , scandata - > who ) ;
return 1 ;
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief dns_callback
*
* DNS callback
*
* @ param data
* @ param a
*
* @ return NS_SUCCESS if suceeds else result of command
*/
2002-08-31 09:28:34 +00:00
/* this function is called when either checking the opm list, or when we are trying to resolve the hostname */
2005-10-19 22:29:37 +00:00
static void dns_callback ( void * data , adns_answer * a )
2004-08-01 23:05:50 +00:00
{
2005-04-03 13:46:49 +00:00
scaninfo * scandata = ( scaninfo * ) data ;
2002-08-31 09:28:34 +00:00
char * show ;
int len , ri ;
2003-09-29 12:59:07 +00:00
SET_SEGV_LOCATION ( ) ;
2002-09-04 08:52:34 +00:00
2002-08-31 09:28:34 +00:00
if ( a ) {
2005-04-03 13:46:49 +00:00
if ( a - > nrrs < 1 ) {
irc_chanalert ( opsb_bot , " No Record for %s. Aborting Scan " , scandata - > lookup ) ;
if ( scandata - > reqclient ) irc_prefmsg ( opsb_bot , scandata - > reqclient , " No A record for %s. Aborting Scan " , scandata - > lookup ) ;
ns_free ( scandata ) ;
checkqueue ( ) ;
return ;
2002-08-31 09:28:34 +00:00
}
adns_rr_info ( a - > type , 0 , 0 , & len , 0 , 0 ) ;
2005-04-03 13:46:49 +00:00
ri = adns_rr_info ( a - > type , 0 , 0 , 0 , a - > rrs . bytes , & show ) ;
if ( ! ri ) {
dlog ( DEBUG1 , " OPSB: Got IP for %s -> %s " , scandata - > who , show ) ;
if ( a - > nrrs > 1 ) {
irc_chanalert ( opsb_bot , " Warning, More than one IP address for %s. Using %s only " , scandata - > lookup , show ) ;
if ( scandata - > reqclient ) irc_prefmsg ( opsb_bot , scandata - > reqclient , " Warning, More than one IP address for %s. Using %s only " , scandata - > lookup , show ) ;
}
if ( inet_aton ( show , & scandata - > ip ) > 0 ) {
2005-10-20 21:22:15 +00:00
( void ) startscan ( scandata ) ;
2002-08-31 09:28:34 +00:00
} else {
2005-05-23 22:58:06 +00:00
nlog ( LOG_CRITICAL , " OPSB: dns_callback() GETNICKIP failed-> %s " , show ) ;
2005-04-03 13:46:49 +00:00
irc_chanalert ( opsb_bot , " Warning, Couldn't get the address for %s " , scandata - > who ) ;
ns_free ( scandata ) ;
checkqueue ( ) ;
2002-08-31 09:28:34 +00:00
}
2005-04-03 13:46:49 +00:00
} else {
2005-05-23 22:58:06 +00:00
nlog ( LOG_CRITICAL , " OPSB: dns_callback GETNICKIP rr_info failed " ) ;
2005-04-03 13:46:49 +00:00
irc_chanalert ( opsb_bot , " Warning, Couldnt get the address for %s. rr_info failed " , scandata - > who ) ;
ns_free ( scandata ) ;
checkqueue ( ) ;
2002-08-31 09:28:34 +00:00
}
2005-04-03 13:46:49 +00:00
ns_free ( show ) ;
return ;
2002-08-31 09:28:34 +00:00
} else {
2005-04-03 13:46:49 +00:00
nlog ( LOG_CRITICAL , " OPSP() Answer is Empty! " ) ;
ns_free ( scandata ) ;
}
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief ModInit
*
* Init handler
*
* @ param none
*
* @ return NS_SUCCESS if suceeds else NS_FAILURE
*/
2005-02-18 22:19:38 +00:00
int ModInit ( void )
2003-09-29 12:59:07 +00:00
{
2004-09-22 21:38:11 +00:00
DBAFetchConfigInt ( " Confed " , & opsb . confed ) ;
2004-08-01 23:05:50 +00:00
ModuleConfig ( opsb_settings ) ;
2005-10-20 21:34:41 +00:00
/* we have to be careful here. Currently, we have SCAN_SOCKET_COUNT sockets that get opened per connection. Soooo.
* we check that MAX_SCANS is not greater than the maxsockets available / SCAN_SOCKET_COUNT
2003-11-08 12:29:09 +00:00
* this way , we * shouldn ' t * get problems with running out of sockets
*/
2005-10-20 21:34:41 +00:00
if ( MAX_SCANS > me . maxsocks / SCAN_SOCKET_COUNT ) {
opsbl = list_create ( me . maxsocks / SCAN_SOCKET_COUNT ) ;
opsb . socks = me . maxsocks / SCAN_SOCKET_COUNT ;
2003-11-08 12:29:09 +00:00
} else {
opsbl = list_create ( MAX_SCANS ) ;
opsb . socks = MAX_SCANS ;
}
/* queue can be anything we want */
opsbq = list_create ( MAX_QUEUE ) ;
2005-05-11 09:14:33 +00:00
cache = list_create ( opsb . cachesize ) ;
2003-11-08 12:29:09 +00:00
opsb . ports = list_create ( MAX_PORTS ) ;
2002-08-31 09:28:34 +00:00
opsb . open = 0 ;
opsb . scanned = 0 ;
2005-05-11 09:14:33 +00:00
opsb . cachehits = 0 ;
2005-10-20 21:34:41 +00:00
if ( load_ports ( ) ! = NS_SUCCESS )
{
2004-08-01 23:05:50 +00:00
nlog ( LOG_WARNING , " Can't Load opsb. No Ports Defined for Scanner. Did you install Correctly? " ) ;
return NS_FAILURE ;
2003-10-29 11:07:23 +00:00
}
2004-08-01 23:05:50 +00:00
return NS_SUCCESS ;
2002-08-31 09:28:34 +00:00
}
2005-05-23 22:58:06 +00:00
/** @brief ModSynch
*
* Startup handler
*
* @ param none
*
* @ return NS_SUCCESS if suceeds else NS_FAILURE
*/
int ModSynch ( void )
{
SET_SEGV_LOCATION ( ) ;
2005-11-14 22:37:21 +00:00
if ( opsb . targetip [ 0 ] = = 0 )
2005-10-19 22:16:31 +00:00
{
2005-06-11 06:11:06 +00:00
strlcpy ( opsb . targetip , inet_ntoa ( me . srvip . sin_addr ) , MAXHOST ) ;
}
if ( init_scanengine ( ) ! = NS_SUCCESS ) {
return NS_FAILURE ;
}
2005-05-23 22:58:06 +00:00
opsb_bot = AddBot ( & opsb_botinfo ) ;
if ( opsb . confed = = 0 ) {
2005-08-09 12:12:53 +00:00
AddTimer ( TIMER_TYPE_INTERVAL , unconf , " unconf " , TS_ONE_MINUTE , NULL ) ;
2005-10-20 21:22:15 +00:00
( void ) unconf ( NULL ) ;
2005-05-23 22:58:06 +00:00
}
if ( opsb . verbose ) {
2005-10-20 21:34:41 +00:00
irc_chanalert ( opsb_bot , " Open Proxy Scanning bot has started (Concurrent Scans: %d Sockets %d) " , opsb . socks , opsb . socks * SCAN_SOCKET_COUNT ) ;
2005-05-23 22:58:06 +00:00
}
return NS_SUCCESS ;
}
/** @brief ModFini
*
* Fini handler
*
* @ param none
*
* @ return NS_SUCCESS if suceeds else NS_FAILURE
*/
2005-02-18 22:19:38 +00:00
int ModFini ( void )
2003-09-29 12:59:07 +00:00
{
2005-06-11 06:11:06 +00:00
return NS_SUCCESS ;
2004-08-09 20:40:55 +00:00
}