#!/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
+=head1 NAME
+
+freeside-selfservice-server
+
+=cut
use strict;
-use vars qw( $Debug %kids $kids $max_kids $shutdown $log_file $ssh_pid
- $keepalives );
-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 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::TicketSystem;
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 = 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 $machine = shift or die &usage;
my $tag = scalar(@ARGV) ? shift : '';
+my $lock_file = "$FREESIDE_LOCK/selfservice.$machine.writelock";
+
+# to keep pid files unique w/multi machines (and installs!)
# $FS::UID::datasrc not posible
-my $pid_file = "/var/run/freeside-selfservice-server.$user.$machine.pid";
+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;
-my $lock_file = "/usr/local/etc/freeside/selfservice.$machine.writelock";
+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;
+my $ticket_system = $conf->config('ticket_system');
+if ( $conf->exists('selfservice-ignore_quantity') ) {
+ $FS::cust_svc::ignore_quantity = 1;
+ $FS::cust_svc::ignore_quantity = 1; #now it is used twice.
+}
+if ( $conf->exists('selfservice-db_profile') ) {
+ eval "use DBIx::Profile";
+ warn $@ if $@;
+}
+
+#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?
$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;
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 "child $pid spawned\n" if $Debug;
} else { #kid time
- ##get new db handle
- #$FS::UID::dbh->{InactiveDestroy} = 1;
- #forksuidsetup($user);
+ $FS::UID::dbh->{InactiveDestroy} = 1;
+ forksuidsetup($user);
- #get db handle
- adminsuidsetup($user);
+ FS::TicketSystem->init() if $ticket_system;
my $type = $packet->{_packet};
warn "calling $type handler\n" if $Debug;
}
- &shutdown if $shutdown;
+ myshutdown if sigint() || sigterm();
warn "connection lost, reconnecting\n" if $Debug;
sleep 3;
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";
-
- open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
- chown $FS::UID::freeside_uid, $freeside_gid, $lock_file;
-
- $) = $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-- ) {
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;