diff --git a/opentracker.c b/opentracker.c index f39dbdb..4ffde37 100644 --- a/opentracker.c +++ b/opentracker.c @@ -38,8 +38,13 @@ static time_t ot_start_time; static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80; static const size_t SUCCESS_HTTP_SIZE_OFF = 17; static char g_adminip[4] = {0,0,0,0}; -#ifdef WANT_BLACKLISTING -static char *blacklist_filename = NULL; + +#if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER ) + #error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive. +#endif +#if defined ( WANT_BLACKLISTING ) || defined (WANT_CLOSED_TRACKER ) +static char *accesslist_filename = NULL; +#define WANT_ACCESS_CONTROL #endif /* To always have space for error messages ;) */ @@ -493,7 +498,13 @@ static void graceful( int s ) { } static void usage( char *name ) { - fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-d dir] [-A ip]\n", name ); + fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-d dir] [-A ip]" +#ifdef WANT_BLACKLISTING + " [-b blacklistfile]" +#elif defined ( WANT_CLOSED_TRACKER ) + " [-w whitelistfile]" +#endif + "\n", name ); } #define HELPLINE(opt,desc) fprintf(stderr, "\t%-10s%s\n",opt,desc) @@ -507,6 +518,8 @@ static void help( char *name ) { HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)"); #ifdef WANT_BLACKLISTING HELPLINE("-b file","specify blacklist file."); +#elif defined( WANT_CLOSED_TRACKER ) + HELPLINE("-w file","specify whitelist file."); #endif fprintf( stderr, "\nExample: ./opentracker -i 127.0.0.1 -p 6969 -P 6969 -i 10.1.1.23 -p 2710 -p 80\n" ); @@ -756,27 +769,25 @@ static void ot_try_bind( char ip[4], uint16 port, int is_tcp ) { ++ot_sockets_count; } -#ifdef WANT_BLACKLISTING -/* Read initial black list */ -void read_blacklist_file( int foo ) { - FILE * blacklist_filehandle; +#ifdef WANT_ACCESS_CONTROL +/* Read initial access list */ +void read_accesslist_file( int foo ) { + FILE * accesslist_filehandle; ot_hash infohash; foo = foo; - signal( SIGHUP, SIG_IGN ); - blacklist_filehandle = fopen( blacklist_filename, "r" ); + accesslist_filehandle = fopen( accesslist_filename, "r" ); - /* Free blacklist vector in trackerlogic.c*/ - blacklist_reset(); + /* Free accesslist vector in trackerlogic.c*/ + accesslist_reset(); - if( blacklist_filehandle == NULL ) { - fprintf( stderr, "Warning: Can't open blacklist file: %s (but will try to create it later, if necessary and possible).", blacklist_filename ); - signal( SIGHUP, read_blacklist_file ); + if( accesslist_filehandle == NULL ) { + fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", accesslist_filename ); return; } /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ - while( fgets( static_inbuf, sizeof(static_inbuf), blacklist_filehandle ) ) { + while( fgets( static_inbuf, sizeof(static_inbuf), accesslist_filehandle ) ) { int i; for( i=0; i<20; ++i ) { int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] ); @@ -787,15 +798,14 @@ void read_blacklist_file( int foo ) { if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 ) goto ignore_line; - /* Append blacklist to blacklist vector */ - blacklist_addentry( &infohash ); + /* Append accesslist to accesslist vector */ + accesslist_addentry( &infohash ); ignore_line: continue; } - fclose( blacklist_filehandle ); - signal( SIGHUP, read_blacklist_file ); + fclose( accesslist_filehandle ); } #endif @@ -806,12 +816,20 @@ int main( int argc, char **argv ) { int scanon = 1; while( scanon ) { - switch( getopt( argc, argv, ":i:p:A:P:d:b:h" ) ) { + switch( getopt( argc, argv, ":i:p:A:P:d:" +#ifdef WANT_BLACKLISTING +"b:" +#elif defined( WANT_CLOSED_TRACKER ) +"w:" +#endif + "h" ) ) { case -1 : scanon = 0; break; case 'i': scan_ip4( optarg, serverip ); break; case 'A': scan_ip4( optarg, g_adminip ); break; #ifdef WANT_BLACKLISTING - case 'b': blacklist_filename = optarg; break; + case 'b': accesslist_filename = optarg; break; +#elif defined( WANT_CLOSED_TRACKER ) + case 'w': accesslist_filename = optarg; break; #endif case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); break; case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); break; @@ -839,11 +857,11 @@ int main( int argc, char **argv ) { } endpwent(); -#ifdef WANT_BLACKLISTING +#ifdef WANT_ACCESS_CONTROL /* Passing "0" since read_blacklist_file also is SIGHUP handler */ - if( blacklist_filename ) { - read_blacklist_file( 0 ); - signal( SIGHUP, read_blacklist_file ); + if( accesslist_filename ) { + read_accesslist_file( 0 ); + signal( SIGHUP, read_accesslist_file ); } #endif diff --git a/trackerlogic.c b/trackerlogic.c index 8308205..184edae 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -22,8 +22,9 @@ /* GLOBAL VARIABLES */ static ot_vector all_torrents[256]; static ot_vector changeset; -#ifdef WANT_BLACKLISTING -static ot_vector blacklist; +#if defined ( WANT_BLACKLISTING ) || defined( WANT_CLOSED_TRACKER ) +static ot_vector accesslist; +#define WANT_ACCESS_CONTROL #endif size_t changeset_size = 0; @@ -159,9 +160,14 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer, int from_changese ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool; int base_pool = 0; -#ifdef WANT_BLACKLISTING - binary_search( hash, blacklist.data, blacklist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch ); - if( exactmatch ) +#ifdef WANT_ACCESS_CONTROL + binary_search( hash, accesslist.data, accesslist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch ); + +#ifdef WANT_CLOSED_TRACKER + exactmatch = !exactmatch; +#endif + + if( !exactmatch ) return NULL; #endif @@ -736,15 +742,15 @@ void deinit_logic( void ) { changeset_size = 0; } -#ifdef WANT_BLACKLISTING -void blacklist_reset( void ) { - free( blacklist.data ); - byte_zero( &blacklist, sizeof( blacklist ) ); +#ifdef WANT_ACCESS_CONTROL +void accesslist_reset( void ) { + free( accesslist.data ); + byte_zero( &accesslist, sizeof( accesslist ) ); } -int blacklist_addentry( ot_hash *infohash ) { +int accesslist_addentry( ot_hash *infohash ) { int em; - void *insert = vector_find_or_insert( &blacklist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em ); + void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em ); if( !insert ) return -1; diff --git a/trackerlogic.h b/trackerlogic.h index 3b2d883..8fcf66e 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -98,9 +98,9 @@ size_t return_memstat_for_tracker( char **reply ); size_t return_changeset_for_tracker( char **reply ); void clean_all_torrents( void ); void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ); -#ifdef WANT_BLACKLISTING -int blacklist_addentry( ot_hash *hash ); -void blacklist_reset( void ); +#if defined ( WANT_BLACKLISTING ) || defined ( WANT_CLOSED_TRACKER ) +int accesslist_addentry( ot_hash *hash ); +void accesslist_reset( void ); #endif #endif