summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c62
1 files changed, 59 insertions, 3 deletions
diff --git a/opentracker.c b/opentracker.c
index bb3de02..b13dc11 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -38,6 +38,9 @@ static time_t ot_start_time;
38static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80; 38static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80;
39static const size_t SUCCESS_HTTP_SIZE_OFF = 17; 39static const size_t SUCCESS_HTTP_SIZE_OFF = 17;
40static char g_adminip[4] = {0,0,0,0}; 40static char g_adminip[4] = {0,0,0,0};
41#ifdef WANT_BLACKLISTING
42static char *blacklist_filename = NULL;
43#endif
41 44
42/* To always have space for error messages ;) */ 45/* To always have space for error messages ;) */
43 46
@@ -495,8 +498,11 @@ static void help( char *name ) {
495 HELPLINE("-i ip","specify ip to bind to (default: *, you may specify more than one)"); 498 HELPLINE("-i ip","specify ip to bind to (default: *, you may specify more than one)");
496 HELPLINE("-p port","specify tcp port to bind to (default: 6969, you may specify more than one)"); 499 HELPLINE("-p port","specify tcp port to bind to (default: 6969, you may specify more than one)");
497 HELPLINE("-P port","specify udp port to bind to (default: 6969, you may specify more than one)"); 500 HELPLINE("-P port","specify udp port to bind to (default: 6969, you may specify more than one)");
498 HELPLINE("-d dir","specify directory containing white- or black listed torrent info_hashes (default: \".\")"); 501 HELPLINE("-d dir","specify directory to try to chroot to (default: \".\")");
499 HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)"); 502 HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)");
503#ifdef WANT_BLACKLISTING
504 HELPLINE("-b file","specify blacklist file.");
505#endif
500 506
501 fprintf( stderr, "\nExample: ./opentracker -i 127.0.0.1 -p 6969 -P 6969 -i 10.1.1.23 -p 2710 -p 80\n" ); 507 fprintf( stderr, "\nExample: ./opentracker -i 127.0.0.1 -p 6969 -P 6969 -i 10.1.1.23 -p 2710 -p 80\n" );
502} 508}
@@ -745,6 +751,43 @@ static void ot_try_bind( char ip[4], uint16 port, int is_tcp ) {
745 ++ot_sockets_count; 751 ++ot_sockets_count;
746} 752}
747 753
754#ifdef WANT_BLACKLISTING
755/* Read initial black list */
756void read_blacklist_file( int foo ) {
757 FILE * blacklist_filehandle = fopen( blacklist_filename, "r" );
758 ot_hash infohash;
759 foo = foo;
760
761 /* Free blacklist vector in trackerlogic.c*/
762 blacklist_reset();
763
764 if( blacklist_filehandle == NULL ) {
765 fprintf( stderr, "Warning: Can't open blacklist file: %s (but will try to create it later, if necessary and possible).", blacklist_filename );
766 return;
767 }
768
769 /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */
770 while( fgets( static_inbuf, sizeof(static_inbuf), blacklist_filehandle ) ) {
771 int i;
772 for( i=0; i<20; ++i ) {
773 int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] );
774 if( eger < 0 )
775 goto ignore_line;
776 infohash[i] = eger;
777 }
778 if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 )
779 goto ignore_line;
780
781 /* Append blacklist to blacklist vector */
782 blacklist_addentry( &infohash );
783
784ignore_line:
785 continue;
786 }
787 fclose( blacklist_filehandle );
788}
789#endif
790
748int main( int argc, char **argv ) { 791int main( int argc, char **argv ) {
749 struct passwd *pws = NULL; 792 struct passwd *pws = NULL;
750 char serverip[4] = {0,0,0,0}; 793 char serverip[4] = {0,0,0,0};
@@ -752,10 +795,13 @@ int main( int argc, char **argv ) {
752 int scanon = 1; 795 int scanon = 1;
753 796
754 while( scanon ) { 797 while( scanon ) {
755 switch( getopt( argc, argv, ":i:p:A:P:d:ocbBh" ) ) { 798 switch( getopt( argc, argv, ":i:p:A:P:d:b:h" ) ) {
756 case -1 : scanon = 0; break; 799 case -1 : scanon = 0; break;
757 case 'i': scan_ip4( optarg, serverip ); break; 800 case 'i': scan_ip4( optarg, serverip ); break;
758 case 'A': scan_ip4( optarg, g_adminip ); break; 801 case 'A': scan_ip4( optarg, g_adminip ); break;
802#ifdef WANT_BLACKLISTING
803 case 'b': blacklist_filename = optarg; break;
804#endif
759 case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); break; 805 case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); break;
760 case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); break; 806 case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); break;
761 case 'd': serverdir = optarg; break; 807 case 'd': serverdir = optarg; break;
@@ -771,7 +817,8 @@ int main( int argc, char **argv ) {
771 ot_try_bind( serverip, 6969, 0 ); 817 ot_try_bind( serverip, 6969, 0 );
772 } 818 }
773 819
774 pws = getpwnam( "nobody "); 820 /* Drop permissions */
821 pws = getpwnam( "nobody" );
775 if( !pws ) { 822 if( !pws ) {
776 setegid( (gid_t)-2 ); setuid( (uid_t)-2 ); 823 setegid( (gid_t)-2 ); setuid( (uid_t)-2 );
777 setgid( (gid_t)-2 ); seteuid( (uid_t)-2 ); 824 setgid( (gid_t)-2 ); seteuid( (uid_t)-2 );
@@ -781,8 +828,17 @@ int main( int argc, char **argv ) {
781 } 828 }
782 endpwent(); 829 endpwent();
783 830
831#ifdef WANT_BLACKLISTING
832 /* Passing "0" since read_blacklist_file also is SIGHUP handler */
833 if( blacklist_filename ) {
834 read_blacklist_file( 0 );
835 signal( SIGHUP, read_blacklist_file );
836 }
837#endif
838
784 signal( SIGPIPE, SIG_IGN ); 839 signal( SIGPIPE, SIG_IGN );
785 signal( SIGINT, graceful ); 840 signal( SIGINT, graceful );
841
786 if( init_logic( serverdir ) == -1 ) 842 if( init_logic( serverdir ) == -1 )
787 panic( "Logic not started" ); 843 panic( "Logic not started" );
788 844