4 use vars qw( $FREESIDE_LOG $FREESIDE_LOCK );
5 use vars qw( $Debug %kids $kids $max_kids $ssh_pid %old_ssh_pid $keepalives );
6 use subs qw( lock_write unlock_write myshutdown usage );
8 use POSIX qw(:sys_wait_h);
12 use Storable 2.09 qw(nstore_fd fd_retrieve);
13 use Net::SSH qw(sshopen2);
14 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
15 use FS::UID qw(adminsuidsetup forksuidsetup);
17 use FS::ClientAPI_SessionCache;
18 use FS::Record qw( qsearch qsearchs );
24 $FREESIDE_LOG = "%%%FREESIDE_LOG%%%";
25 $FREESIDE_LOCK = "%%%FREESIDE_LOCK%%%";
27 $Debug = 1; # 2 will turn on more logging
28 # 3 will log packet contents, including passwords
31 $keepalives = 0; #let clientd turn it on, so we don't barf on old ones
34 my $user = shift or die &usage;
35 my $machine = shift or die &usage;
36 my $tag = scalar(@ARGV) ? shift : '';
38 my $lock_file = "$FREESIDE_LOCK/selfservice.$machine.writelock";
40 # to keep pid files unique w/multi machines (and installs!)
41 # $FS::UID::datasrc not posible
42 daemonize1("freeside-selfservice-server","$user.$machine");
44 #false laziness w/Daemon::drop_root
45 my $freeside_gid = scalar(getgrnam('freeside'))
46 or die "can't find freeside group\n";
48 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
49 chown $FS::UID::freeside_uid, $freeside_gid, $lock_file;
53 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
57 #logfile("/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc); #MACHINE
58 logfile("$FREESIDE_LOG/selfservice.$machine.log");
62 my $conf = new FS::Conf;
63 if ( $conf->exists('selfservice-ignore_quantity') ) {
64 $FS::cust_svc::ignore_quantity = 1;
65 $FS::cust_svc::ignore_quantity = 1; #now it is used twice.
68 #clear the signup info cache so an "/etc/init.d/freeside restart" will pick
69 #up new info... (better as a callback in Signup.pm?)
70 my $cache = new FS::ClientAPI_SessionCache( {
71 'namespace' => 'FS::ClientAPI::Signup',
73 $cache->remove('signup_info_cache');
75 my $clientd = "/usr/local/sbin/freeside-selfservice-clientd"; #better name?
79 my($writer,$reader,$error) = (new IO::Handle, new IO::Handle, new IO::Handle);
80 warn "connecting to $machine\n" if $Debug;
82 $ssh_pid = sshopen2($machine,$reader,$writer,$clientd,$tag);
84 # nstore_fd(\*writer, {'hi'=>'there'});
86 warn "entering main loop\n" if $Debug;
88 my $keepalive_count = 0;
89 my $s = IO::Select->new( $reader );
94 warn "waiting for packet from client\n" if $Debug && !$undisp;
96 my @handles = $s->can_read(5);
98 myshutdown() if sigint() || sigterm();
99 if ( $keepalives && $keepalive_count++ > 10 ) {
100 $keepalive_count = 0;
103 nstore_fd( { _token => '_keepalive' }, $writer );
104 foreach my $agent ( qsearch( 'agent', { disabled => '' } ) ) {
105 my $config = qsearchs( 'conf', { name => 'selfservice-bulk_ftp_dir',
106 agentnum => $agent->agentnum,
111 FS::ClientAPI->dispatch( 'Agent/agent_login',
112 { username => $agent->username,
113 password => $agent->_password,
117 nstore_fd( { _token => '_ftp_scan',
118 dir => $config->value,
119 session_id => $session->{session_id},
131 warn "receiving packet from client\n" if $Debug;
133 my $packet = eval { fd_retrieve($reader); };
135 warn "Storable error receiving packet from client".
136 " (assuming lost connection): $@\n"
139 warn "sending TERM signal to ssh process $ssh_pid\n" if $Debug;
140 kill 'TERM', $ssh_pid;
141 $old_ssh_pid{$ssh_pid} = 1;
146 warn "packet received\n".
147 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
150 if ( $packet->{_packet} eq '_enable_keepalive' ) {
151 warn "enabling keep alives\n" if $Debug;
156 #prevent runaway forking
158 while ( $kids >= $max_kids ) {
159 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
164 warn "forking child\n" if $Debug;
165 defined( my $pid = fork ) or die "can't fork: $!";
169 warn "child $pid spawned\n" if $Debug;
173 $FS::UID::dbh->{InactiveDestroy} = 1;
174 forksuidsetup($user);
177 #adminsuidsetup($user);
179 my $type = $packet->{_packet};
180 warn "calling $type handler\n" if $Debug;
181 my $rv = eval { FS::ClientAPI->dispatch($type, $packet); };
183 warn my $error = "WARNING: error dispatching $type: $@";
184 $rv = { _error => $error };
186 $rv->{_token} = $packet->{_token}; #identifier
188 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
190 warn "sending response\n" if $Debug;
191 nstore_fd($rv, $writer) or die "FATAL: can't send response: $!";
192 $writer->flush or die "FATAL: can't flush: $!";
195 warn "child exiting\n" if $Debug;
201 myshutdown if sigint() || sigterm();
202 warn "connection lost, reconnecting\n" if $Debug;
208 # utility subroutines
212 #warn "reaping kids\n";
213 foreach my $pid ( keys %kids ) {
214 my $kid = waitpid($pid, WNOHANG);
221 foreach my $pid ( keys %old_ssh_pid ) {
222 waitpid($pid, WNOHANG) and delete $old_ssh_pid{$pid};
224 #warn "done reaping\n";
229 my $wait = 12; #wait up to 1 minute
230 while ( $kids > 0 && $wait-- ) {
231 warn "waiting for $kids children to terminate";
235 warn "abandoning $kids children" if $kids;
236 kill 'TERM', $ssh_pid if $ssh_pid;
241 warn "locking $lock_file mutex for write to write stream\n" if $Debug > 1;
244 #flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!";
246 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
251 warn "unlocking $lock_file mutex\n" if $Debug > 1;
254 #flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!";
256 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";
261 die "Usage:\n\n freeside-selfservice-server user machine\n";