X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2Fbin%2Ffreeside-selfservice-server;h=c10623c9688edb58e1683af50e226686cbe5eba1;hp=05fa72b43cc1e232bb24799a66626543da8233a8;hb=aed8ec35ccb9cdeb7ea0cb6ff2946f9d83d582f6;hpb=bacea004a735c7a9c30a35e9184a63897a200e6e diff --git a/FS/bin/freeside-selfservice-server b/FS/bin/freeside-selfservice-server index 05fa72b43..c10623c96 100644 --- a/FS/bin/freeside-selfservice-server +++ b/FS/bin/freeside-selfservice-server @@ -1,48 +1,85 @@ #!/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 subs qw( lock_write unlock_write ); +use vars qw( $FREESIDE_LOG $FREESIDE_LOCK ); +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; +use FS::ClientAPI qw( load_clientapi_modules ); +use FS::ClientAPI_SessionCache; +use FS::Record qw( qsearch qsearchs ); use FS::Conf; -use FS::cust_bill; -use FS::cust_pkg; +use FS::cust_svc; +use FS::agent; + +$FREESIDE_LOG = "%%%FREESIDE_LOG%%%"; +$FREESIDE_LOCK = "%%%FREESIDE_LOCK%%%"; -$Debug = 2; # >= 2 will log packet contents, including potentially compromising - # information +$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 $tag = scalar(@ARGV) ? shift : ''; -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 $lock_file = "/usr/local/etc/freeside/selfservice.$machine.writelock"; +my $lock_file = "$FREESIDE_LOCK/selfservice.$machine.writelock"; + +# 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 + +load_clientapi_modules; + +adminsuidsetup $user; -&init($user); +#logfile("/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc); #MACHINE +logfile("$FREESIDE_LOG/selfservice.$machine.log"); + +daemonize2(); my $conf = new FS::Conf; +if ( $conf->exists('selfservice-ignore_quantity') ) { + $FS::cust_svc::ignore_quantity = 1; + $FS::cust_svc::ignore_quantity = 1; #now it is used twice. +} + +#clear the signup info cache so an "/etc/init.d/freeside restart" will pick +#up new info... (better as a callback in Signup.pm?) +my $cache = new FS::ClientAPI_SessionCache( { + 'namespace' => 'FS::ClientAPI::Signup', +} ); +$cache->remove('signup_info_cache'); + +#and also clear the selfservice skin info cache, for the same reason +my $ss_cache = new FS::ClientAPI_SessionCache( { + 'namespace' => 'FS::ClientAPI::MyAccount', +} ); +$ss_cache->remove($_) + foreach grep /^skin_info_cache_agent/, $ss_cache->get_keys(); my $clientd = "/usr/local/sbin/freeside-selfservice-clientd"; #better name? @@ -57,6 +94,7 @@ while (1) { warn "entering main loop\n" if $Debug; my $undisp = 0; + my $keepalive_count = 0; my $s = IO::Select->new( $reader ); while (1) { @@ -66,7 +104,37 @@ 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 ); + +#commenting izoom stuff out until we can move it to a branch (or just remove) +# foreach my $agent ( qsearch( 'agent', { disabled => '' } ) ) { +# my $config = qsearchs( 'conf', { name => 'selfservice-bulk_ftp_dir', +# agentnum => $agent->agentnum, +# } ) +# or next; +# +# my $session = +# FS::ClientAPI->dispatch( 'Agent/agent_login', +# { username => $agent->username, +# password => $agent->_password, +# } +# ); +# +# nstore_fd( { _token => '_ftp_scan', +# dir => $config->value, +# session_id => $session->{session_id}, +# }, +# $writer +# ); +# } + + unlock_write; + } next; } @@ -74,10 +142,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 +181,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,8 +197,9 @@ while (1) { } $rv->{_token} = $packet->{_token}; #identifier - warn "sending response\n" if $Debug; + open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!"; lock_write; + warn "sending response\n" if $Debug; nstore_fd($rv, $writer) or die "FATAL: can't send response: $!"; $writer->flush or die "FATAL: can't flush: $!"; unlock_write; @@ -120,6 +210,10 @@ while (1) { } + myshutdown if sigint() || sigterm(); + warn "connection lost, reconnecting\n" if $Debug; + sleep 3; + } ### @@ -135,101 +229,29 @@ 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 _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 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: $!"; @@ -238,6 +260,8 @@ sub lock_write { } 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: $!";