X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2Fbin%2Ffreeside-selfservice-server;h=fc04ee91ec9f1e9e243a975eeffa4078a6413022;hp=264cbc56df56094f2f4678d0cfe4424d38205ac2;hb=7422aa77086326ba1e6032150ce5704d1003ea6f;hpb=0ebeec96313dd7edfca340f01f8fbbbac1f4aa1d diff --git a/FS/bin/freeside-selfservice-server b/FS/bin/freeside-selfservice-server index 264cbc56d..fc04ee91e 100644 --- a/FS/bin/freeside-selfservice-server +++ b/FS/bin/freeside-selfservice-server @@ -1,21 +1,16 @@ #!/usr/bin/perl -w -# -# freeside-selfservice-server - -# alas, much false laziness with freeside-queued and fs_signup_server. at -# least it is slated to replace fs_{signup,passwd,mailadmin}_server -# should probably generalize the version in here, or better yet use -# Proc::Daemon or somesuch use strict; -use vars qw( $Debug %kids $kids $max_kids $shutdown $log_file $ssh_pid ); +use vars qw( $Debug %kids $kids $max_kids $ssh_pid %old_ssh_pid $keepalives ); +use subs qw( lock_write unlock_write myshutdown usage ); use Fcntl qw(:flock); -use POSIX qw(:sys_wait_h setsid); +use POSIX qw(:sys_wait_h); use IO::Handle; use IO::Select; use IO::File; -use Storable qw(nstore_fd fd_retrieve); +use Storable 2.09 qw(nstore_fd fd_retrieve); use Net::SSH qw(sshopen2); +use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm); use FS::UID qw(adminsuidsetup forksuidsetup); use FS::ClientAPI; @@ -23,26 +18,45 @@ use FS::Conf; use FS::cust_bill; use FS::cust_pkg; -$Debug = 2; # >= 2 will log packet contents, including potentially compromising - # information +$FREESIDE_CONF = "%%%FREESIDE_CONF%%%"; + +$Debug = 1; # 2 will turn on more logging + # 3 will log packet contents, including passwords -$shutdown = 0; $max_kids = '10'; #? +$keepalives = 0; #let clientd turn it on, so we don't barf on old ones $kids = 0; my $user = shift or die &usage; my $machine = shift or die &usage; -my $pid_file = "/var/run/freeside-selfservice-server.$user.pid"; -#my $pid_file = "/var/run/freeside-selfservice-server.$user.pid"; $FS::UID::datasrc not posible, but should include machine name at least, hmm +my $tag = scalar(@ARGV) ? shift : ''; -&init($user); +my $lock_file = "/usr/local/etc/freeside/selfservice.$machine.writelock"; -my $conf = new FS::Conf; -if ($conf->exists('selfservice_server-quiet')) { - $FS::cust_bill::quiet = 1; - $FS::cust_pkg::quiet = 1; -} +# to keep pid files unique w/multi machines (and installs!) +# $FS::UID::datasrc not posible +daemonize1("freeside-selfservice-server","$user.$machine"); + +#false laziness w/Daemon::drop_root +my $freeside_gid = scalar(getgrnam('freeside')) + or die "can't find freeside group\n"; + +open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!"; +chown $FS::UID::freeside_uid, $freeside_gid, $lock_file; + +drop_root(); + +$ENV{HOME} = (getpwuid($>))[7]; #for ssh + +adminsuidsetup $user; + +#logfile("/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc); #MACHINE +logfile("$FREESIDE_SERVER/selfservice.$machine.log"); + +daemonize2(); + +my $conf = new FS::Conf; my $clientd = "/usr/local/sbin/freeside-selfservice-clientd"; #better name? @@ -51,12 +65,13 @@ while (1) { my($writer,$reader,$error) = (new IO::Handle, new IO::Handle, new IO::Handle); warn "connecting to $machine\n" if $Debug; - $ssh_pid = sshopen2($machine,$reader,$writer,$clientd); + $ssh_pid = sshopen2($machine,$reader,$writer,$clientd,$tag); # nstore_fd(\*writer, {'hi'=>'there'}); warn "entering main loop\n" if $Debug; my $undisp = 0; + my $keepalive_count = 0; my $s = IO::Select->new( $reader ); while (1) { @@ -66,7 +81,13 @@ while (1) { $undisp = 1; my @handles = $s->can_read(5); unless ( @handles ) { - &shutdown if $shutdown; + myshutdown() if sigint() || sigterm(); + if ( $keepalives && $keepalive_count++ > 10 ) { + $keepalive_count = 0; + lock_write; + nstore_fd( { _token => '_keepalive' }, $writer ); + unlock_write; + } next; } @@ -74,10 +95,28 @@ while (1) { warn "receiving packet from client\n" if $Debug; - my $packet = fd_retrieve($reader); + my $packet = eval { fd_retrieve($reader); }; + if ( $@ ) { + warn "Storable error receiving packet from client". + " (assuming lost connection): $@\n" + if $Debug; + if ( $ssh_pid ) { + warn "sending TERM signal to ssh process $ssh_pid\n" if $Debug; + kill 'TERM', $ssh_pid; + $old_ssh_pid{$ssh_pid} = 1; + $ssh_pid = 0; + } + last; + } warn "packet received\n". join('', map { " $_=>$packet->{$_}\n" } keys %$packet ) - if $Debug > 1; + if $Debug > 2; + + if ( $packet->{_packet} eq '_enable_keepalive' ) { + warn "enabling keep alives\n" if $Debug; + $keepalives=1; + next; + } #prevent runaway forking my $warnkids = 0; @@ -95,10 +134,13 @@ while (1) { warn "child $pid spawned\n" if $Debug; } else { #kid time - #get new db handle + ##get new db handle $FS::UID::dbh->{InactiveDestroy} = 1; forksuidsetup($user); + #get db handle + #adminsuidsetup($user); + my $type = $packet->{_packet}; warn "calling $type handler\n" if $Debug; my $rv = eval { FS::ClientAPI->dispatch($type, $packet); }; @@ -108,11 +150,12 @@ while (1) { } $rv->{_token} = $packet->{_token}; #identifier + open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!"; + lock_write; warn "sending response\n" if $Debug; - flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!"; nstore_fd($rv, $writer) or die "FATAL: can't send response: $!"; $writer->flush or die "FATAL: can't flush: $!"; - flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!"; + unlock_write; warn "child exiting\n" if $Debug; exit; #end-of-kid @@ -120,6 +163,10 @@ while (1) { } + myshutdown if sigint() || sigterm(); + warn "connection lost, reconnecting\n" if $Debug; + sleep 3; + } ### @@ -135,101 +182,47 @@ sub reap_kids { delete $kids{$kid}; } } - #warn "done reaping\n"; -} -sub init { - my $user = shift; - - chdir "/" or die "Can't chdir to /: $!"; - open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; - defined(my $pid = fork) or die "Can't fork: $!"; - if ( $pid ) { - print "freeside-selfservice-server to $machine started with pid $pid\n"; #logging to $log_file - exit unless $pid_file; - my $pidfh = new IO::File ">$pid_file" or exit; - print $pidfh "$pid\n"; - exit; + foreach my $pid ( keys %old_ssh_pid ) { + waitpid($pid, WNOHANG) and delete $old_ssh_pid{$pid}; } - -# sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; $kids--; } -# #sub REAPER { my $pid = wait; $kids--; $SIG{CHLD} = \&REAPER; } -# $SIG{CHLD} = \&REAPER; - - $shutdown = 0; - $SIG{HUP} = sub { warn "SIGHUP received; shutting down\n"; $shutdown++; }; - $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $shutdown++; }; - $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $shutdown++; }; - $SIG{QUIT} = sub { warn "SIGQUIT received; shutting down\n"; $shutdown++; }; - $SIG{PIPE} = sub { warn "SIGPIPE received; shutting down\n"; $shutdown++; }; - - #false laziness w/freeside-queued - my $freeside_gid = scalar(getgrnam('freeside')) - or die "can't setgid to freeside group\n"; - $) = $freeside_gid; - $( = $freeside_gid; - #if freebsd can't setuid(), presumably it can't setgid() either. grr fleabsd - ($(,$)) = ($),$(); - $) = $freeside_gid; - - $> = $FS::UID::freeside_uid; - $< = $FS::UID::freeside_uid; - #freebsd is sofa king broken, won't setuid() - ($<,$>) = ($>,$<); - $> = $FS::UID::freeside_uid; - #eslaf - - $ENV{HOME} = (getpwuid($>))[7]; #for ssh - adminsuidsetup $user; - - #$log_file = "/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc; #MACHINE NAME - $log_file = "/usr/local/etc/freeside/selfservice.$machine.log"; - - open STDOUT, '>/dev/null' - or die "Can't write to /dev/null: $!"; - setsid or die "Can't start a new session: $!"; - open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; - - $SIG{__DIE__} = \&_die; - $SIG{__WARN__} = \&_logmsg; - - warn "freeside-selfservice-server starting\n"; - + #warn "done reaping\n"; } -sub shutdown { +sub myshutdown { + &reap_kids; my $wait = 12; #wait up to 1 minute while ( $kids > 0 && $wait-- ) { warn "waiting for $kids children to terminate"; sleep 5; + &reap_kids; } warn "abandoning $kids children" if $kids; kill 'TERM', $ssh_pid if $ssh_pid; die "exiting"; } -sub _die { - my $msg = shift; - unlink $pid_file if -e $pid_file; - _logmsg($msg); -} +sub lock_write { + warn "locking $lock_file mutex for write to write stream\n" if $Debug > 1; + + #broken on freebsd? + #flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!"; + + flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!"; -sub _logmsg { - chomp( my $msg = shift ); - _do_logmsg( "[server] [". scalar(localtime). "] [$$] $msg\n" ); } -sub _do_logmsg { - chomp( my $msg = shift ); - my $log = new IO::File ">>$log_file"; - flock($log, LOCK_EX); - seek($log, 0, 2); - print $log "$msg\n"; - flock($log, LOCK_UN); - close $log; +sub unlock_write { + warn "unlocking $lock_file mutex\n" if $Debug > 1; + + #broken on freebsd? + #flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!"; + + flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!"; + } sub usage { - die "Usage:\n\n fs_signup_server user machine\n"; + die "Usage:\n\n freeside-selfservice-server user machine\n"; }