--- telnetbbs/telnetbbs.pl 2010/01/06 05:19:47 1.1 +++ telnetbbs/telnetbbs.pl 2010/12/16 14:24:25 1.7 @@ -6,14 +6,13 @@ ## ## Nicholas DeClario ## October 2009 -## $Id: telnetbbs.pl,v 1.1 2010/01/06 05:19:47 nick Exp $ +## $Id: telnetbbs.pl,v 1.7 2010/12/16 14:24:25 nick Exp $ ## ################################################################################ BEGIN { delete @ENV{qw(IFS CDPATH ENV BASH_ENV PATH)}; $ENV{PATH} = "/bin:/usr/bin"; $|++; -# Flip this back on for more detailed error reporting # $SIG{__DIE__} = sub { require Carp; Carp::confess(@_); } } @@ -30,17 +29,34 @@ use threads; use threads::shared; ## -## Fetch our command line options +## Fetch our command line and configuration options ## my %opts = &fetchOptions( ); -my $pidFile = "/var/run/telnetbbs.pid"; +my %cfg = &fetchConfig( ); my $EOL = "\015\012"; +## +## These are read in from the config file +## +my $BBS_NODE = 0; +my $pidFile = $cfg{'pidfile'} || "/tmp/telnetbbs.pid"; +my $port = $opts{'port'} || $cfg{'port'} || 23; +my $DISPLAY = $cfg{'display'} || ":0.0"; +my $BBS_NAME = $cfg{'bbs_name'} || "Hell's Dominion BBS"; +my $DBCONF = $cfg{'dosbox_cfg'} || "/tmp/dosbox-__NODE__.conf"; +my $BBS_CMD = $cfg{'bbs_cmd'} || "DISPLAY=$DISPLAY /usr/bin/dosbox -conf "; +my $LOGGING = $cfg{'logging'} || 0; +my $LOG = $cfg{'log_path'} || "/tmp/bbs.log"; +my $MAX_NODE = $cfg{'nodes'} || 1; +my $DOSBOXT = $cfg{'dosboxt'} || "dosbox.conf.template"; +my $BASE_PORT = $cfg{'base_port'} || 7000; +my $LOCK_PATH = $cfg{'lock_path'} || "/tmp"; + ## ## Check that we are 'root' ## die( "Must be root user to run this.\n" ) - if ( getpwuid( $> ) ne "root" ); + if ( getpwuid( $> ) ne "root" && $port < 1023 ); ## ## Check for a PID @@ -59,9 +75,22 @@ $SIG{CHLD} = 'IGNORE'; local $SIG{HUP} = $SIG{INT} = $SIG{TERM} = \&shutdown; ## +## Open the Log +## +open LOG, ">>$LOG" if ( $LOGGING ); +&logmsg( "Starting telnetbbs server" ); + +## +## Display running information +## +&display_config_and_options( \%opts, "Options" ); +&display_config_and_options( \%cfg, "Configuration" ); + +## ## Start the network server ## -my $netThread = threads->create( \&startNetServer ); +my $netThread = threads->create( \&startNetServer( ) ); + while( 1 ) { sleep 1; } @@ -78,7 +107,27 @@ while( 1 ) { sleep 1; } ############################################################################### ############################################################################### -sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } +sub logmsg +{ + my $message = "$0 $$ " . scalar( localtime( ) ) . ":@_\n"; + print STDOUT $message; + print LOG $message if ( $LOGGING ); +} + + +############################################################################### +############################################################################### +sub display_config_and_options +{ + my $hr = shift || 0; + my $name = shift || "Unknown"; + my $title = "Displaying $name\n"; + + return $hr if ( ! $hr ); + + print LOG $title . Dumper( $hr ) if ( $LOGGING ); + print STDOUT $title . Dumper( $hr ) if ( $opts{'verbose'}); +} ############################################################################### ## startNetServer( ); @@ -91,8 +140,7 @@ sub startNetServer { my $hostConnection; my $childPID; - my $port = $opts{'port'}; - my $node = 1; + my @nodes = ( ); my $server = IO::Socket::INET->new( LocalPort => $port, @@ -110,6 +158,44 @@ sub startNetServer ## REQUEST: while( $hostConnection = $server->accept( ) ) { + ## + ## Find the next available node + ## + my $node = 0; + my $lock_file = ""; + foreach (1 .. $MAX_NODE) + { + next if ( -f $LOCK_PATH."/".$BBS_NAME."_node".$_.".lock" ); + + ## + ## Create node lock file + ## + $lock_file = $LOCK_PATH."/".$BBS_NAME."_node".$_.".lock"; + open LOCK, ">$lock_file"; + close( LOCK ); + $node = $BBS_NODE = $_; + } + + ## + ## Create our dosbox config + ## + open( DBT, "<$DOSBOXT" ); + my @dbt = ; + close( DBT ); + + my $bpn = $BASE_PORT + $BBS_NODE; + $DBCONF =~ s/__NODE__/$BBS_NODE/g; + open( DBC, ">$DBCONF" ); + foreach( @dbt ) + { + $_ =~ s/__NODE__/$BBS_NODE/g; + $_ =~ s/__LISTEN_PORT__/$bpn/g; + print DBC $_; + } + close( DBC ); + + &logmsg( "Connecting on node $BBS_NODE\n" ); + my $kidpid; my $line; @@ -119,13 +205,23 @@ sub startNetServer } defined( $childPID ) || die( "Cannot fork: $!\n" ); + select $hostConnection; + ## ## Default file descriptor to the client and turn on autoflush ## $hostConnection->autoflush( 1 ); - print $hostConnection "Welcome to Hell's Dominion BBS!" . $EOL; - print $hostConnection "Starting BBS on node $node...$EOL"; + print "Welcome to $BBS_NAME!" . $EOL; + + ## + if ( ! $lock_file ) + { + print "No available nodes. Try again later.".$EOL; + exit; + } + + print "Starting BBS on node $BBS_NODE...$EOL"; ## ## Launch BBS via dosbox @@ -133,18 +229,31 @@ sub startNetServer my $bbsPID = fork( ); if ( $bbsPID ) { - exec( "dosbox" ); + select STDOUT; + my $cmd = $BBS_CMD . $DBCONF; + system( $cmd ); + print "Shutting down node $BBS_NODE\n"; + ## + ## Remove Lock + ## + unlink( $lock_file ); + unlink( $DBCONF ); exit; } - sleep 10; + + ## + ## We wait for dosbox to start and the BBS to start + ## There really should be a better way to determine this + ## + sleep 5; ## ## Create connection to BBS ## my $bbs = IO::Socket::INET->new ( PeerAddr => 'localhost', - Type => SOCK_STREAM, - PeerPort => 5000, + Type => SOCK_STREAM, + PeerPort => $bpn, Proto => 'tcp', ) || die "Could not open BBS socket: $!\n"; $bbs->autoflush( 1 ); @@ -158,7 +267,8 @@ sub startNetServer { print $hostConnection $byte; } - kill( "TERM" => $childPID ); + $nodes[$BBS_NODE] = 0; + kill( "TERM" => $kidpid ); } else { @@ -168,11 +278,13 @@ sub startNetServer print $bbs $byte; } } + + unlink( $DBCONF ); } close( $hostConnection ); exit; - close( $server ); +# close( $server ); } ############################################################################### @@ -188,15 +300,31 @@ sub shutdown { my $signame = shift || 0; - print "$0: Shutdown (SIG$signame) received.\n" + select STDOUT; + + &logmsg( "$0: Shutdown (SIG$signame) received.\n" ) if( $signame ); + ## + ## Close Log + ## + close( LOG ) if ( $LOGGING ); + ## ## Remove the PID ## unlink( $pidFile ); ## + ## Remove node lock files + ## + foreach (1 .. $MAX_NODE) + { + my $node_lock = $LOCK_PATH."/".$BBS_NAME."_node".$_.".lock"; + unlink( $node_lock ) if ( -f $node_lock ); + } + + ## ## Wait for the thread to shutdown ## # $netThread->detach( ); @@ -305,6 +433,50 @@ sub processExists return 0; } +############################################################################### +############################################################################### +sub fetchConfig +{ + my %conf = ( ); + my $cf = &findConfig; + + if ( $cf ) + { + open( CONF, "<$cf" ) or die ( "Error opening $cf: $!\n" ); + while( ) + { + next if ( $_ =~ m/^#/ ); + if ( $_ =~ m/(.*?)=(.*)/ ) + { + my $k = $1; my $v = $2; + $k =~ s/\s+//; + $v =~ s/\s+//; + $conf{$k} = $v; + } + } + close( CONF ); + } + + return %conf; +} + +############################################################################### +############################################################################### +sub findConfig +{ + my $cf = 0; + my @paths = qw| ./ ./.telnetbbs /etc /usr/local/etc |; + + return $opts{'config'} if defined $opts{'config'}; + + foreach ( @paths ) + { + my $fn = $_ . "/telnetbbs.conf"; + return $fn if ( -f $fn ); + } + + return $cf; +} ############################################################################### ## @@ -317,9 +489,11 @@ sub fetchOptions { my %opts; &GetOptions( + "config:s" => \$opts{'config'}, "help|?" => \$opts{'help'}, "man" => \$opts{'man'}, - "port:i" => \$opts{'port'}, + "port:i" => \$opts{'port'}, + "verbose" => \$opts{'verbose'}, ) || &pod2usage( ); &pod2usage( ) if defined $opts{'help'}; &pod2usage( { -verbose => 2, -input => \*DATA } ) if defined $opts{'man'}; @@ -331,15 +505,18 @@ __END__ =head1 NAME -masterbuild.pl - blurb +telnetbbs.pl - A telnet server designed to launch a multi-node BBS. =head1 SYNOPSIS -masterbuild.pl [options] +telnetbbs.pl [options] Options: + --config,c Specify the configuration file to use --help,? Display the basic help menu --man,m Display the detailed man page + --port,p Port to listen on, default 23. + --verbose,v Enable verbose output =head1 DESCRIPTION