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);
21 $FREESIDE_LOG = "%%%FREESIDE_LOG%%%";
22 $FREESIDE_LOCK = "%%%FREESIDE_LOCK%%%";
24 $Debug = 1; # 2 will turn on more logging
25 # 3 will log packet contents, including passwords
28 $keepalives = 0; #let clientd turn it on, so we don't barf on old ones
31 my $user = shift or die &usage;
32 my $machine = shift or die &usage;
33 my $tag = scalar(@ARGV) ? shift : '';
35 my $lock_file = "$FREESIDE_LOCK/selfservice.$machine.writelock";
37 # to keep pid files unique w/multi machines (and installs!)
38 # $FS::UID::datasrc not posible
39 daemonize1("freeside-selfservice-server","$user.$machine");
41 #false laziness w/Daemon::drop_root
42 my $freeside_gid = scalar(getgrnam('freeside'))
43 or die "can't find freeside group\n";
45 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
46 chown $FS::UID::freeside_uid, $freeside_gid, $lock_file;
50 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
54 #logfile("/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc); #MACHINE
55 logfile("$FREESIDE_LOG/selfservice.$machine.log");
59 my $conf = new FS::Conf;
60 if ( $conf->exists('selfservice-ignore_quantity') ) {
61 $FS::cust_svc::ignore_quantity = 1;
62 $FS::cust_svc::ignore_quantity = 1; #now it is used twice.
65 my $clientd = "/usr/local/sbin/freeside-selfservice-clientd"; #better name?
69 my($writer,$reader,$error) = (new IO::Handle, new IO::Handle, new IO::Handle);
70 warn "connecting to $machine\n" if $Debug;
72 $ssh_pid = sshopen2($machine,$reader,$writer,$clientd,$tag);
74 # nstore_fd(\*writer, {'hi'=>'there'});
76 warn "entering main loop\n" if $Debug;
78 my $keepalive_count = 0;
79 my $s = IO::Select->new( $reader );
84 warn "waiting for packet from client\n" if $Debug && !$undisp;
86 my @handles = $s->can_read(5);
88 myshutdown() if sigint() || sigterm();
89 if ( $keepalives && $keepalive_count++ > 10 ) {
92 nstore_fd( { _token => '_keepalive' }, $writer );
100 warn "receiving packet from client\n" if $Debug;
102 my $packet = eval { fd_retrieve($reader); };
104 warn "Storable error receiving packet from client".
105 " (assuming lost connection): $@\n"
108 warn "sending TERM signal to ssh process $ssh_pid\n" if $Debug;
109 kill 'TERM', $ssh_pid;
110 $old_ssh_pid{$ssh_pid} = 1;
115 warn "packet received\n".
116 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
119 if ( $packet->{_packet} eq '_enable_keepalive' ) {
120 warn "enabling keep alives\n" if $Debug;
125 #prevent runaway forking
127 while ( $kids >= $max_kids ) {
128 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
133 warn "forking child\n" if $Debug;
134 defined( my $pid = fork ) or die "can't fork: $!";
138 warn "child $pid spawned\n" if $Debug;
142 $FS::UID::dbh->{InactiveDestroy} = 1;
143 forksuidsetup($user);
146 #adminsuidsetup($user);
148 my $type = $packet->{_packet};
149 warn "calling $type handler\n" if $Debug;
150 my $rv = eval { FS::ClientAPI->dispatch($type, $packet); };
152 warn my $error = "WARNING: error dispatching $type: $@";
153 $rv = { _error => $error };
155 $rv->{_token} = $packet->{_token}; #identifier
157 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
159 warn "sending response\n" if $Debug;
160 nstore_fd($rv, $writer) or die "FATAL: can't send response: $!";
161 $writer->flush or die "FATAL: can't flush: $!";
164 warn "child exiting\n" if $Debug;
170 myshutdown if sigint() || sigterm();
171 warn "connection lost, reconnecting\n" if $Debug;
177 # utility subroutines
181 #warn "reaping kids\n";
182 foreach my $pid ( keys %kids ) {
183 my $kid = waitpid($pid, WNOHANG);
190 foreach my $pid ( keys %old_ssh_pid ) {
191 waitpid($pid, WNOHANG) and delete $old_ssh_pid{$pid};
193 #warn "done reaping\n";
198 my $wait = 12; #wait up to 1 minute
199 while ( $kids > 0 && $wait-- ) {
200 warn "waiting for $kids children to terminate";
204 warn "abandoning $kids children" if $kids;
205 kill 'TERM', $ssh_pid if $ssh_pid;
210 warn "locking $lock_file mutex for write to write stream\n" if $Debug > 1;
213 #flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!";
215 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
220 warn "unlocking $lock_file mutex\n" if $Debug > 1;
223 #flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!";
225 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";
230 die "Usage:\n\n freeside-selfservice-server user machine\n";