4 use vars qw( $Debug %kids $kids $max_kids $ssh_pid %old_ssh_pid $keepalives );
5 use subs qw( lock_write unlock_write myshutdown usage );
7 use POSIX qw(:sys_wait_h);
11 use Storable 2.09 qw(nstore_fd fd_retrieve);
12 use Net::SSH qw(sshopen2);
13 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
14 use FS::UID qw(adminsuidsetup forksuidsetup);
21 $Debug = 1; # 2 will turn on more logging
22 # 3 will log packet contents, including passwords
25 $keepalives = 0; #let clientd turn it on, so we don't barf on old ones
28 my $user = shift or die &usage;
29 my $machine = shift or die &usage;
30 my $tag = scalar(@ARGV) ? shift : '';
32 my $lock_file = "/usr/local/etc/freeside/selfservice.$machine.writelock";
35 # to keep pid files unique w/multi machines (and installs!)
36 # $FS::UID::datasrc not posible
37 daemonize1("freeside-selfservice-server","$user.$machine");
39 #false laziness w/Daemon::drop_root
40 my $freeside_gid = scalar(getgrnam('freeside'))
41 or die "can't find freeside group\n";
43 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
44 chown $FS::UID::freeside_uid, $freeside_gid, $lock_file;
48 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
52 #logfile("/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc); #MACHINE
53 logfile("/usr/local/etc/freeside/selfservice.$machine.log");
58 my $conf = new FS::Conf;
60 my $clientd = "/usr/local/sbin/freeside-selfservice-clientd"; #better name?
64 my($writer,$reader,$error) = (new IO::Handle, new IO::Handle, new IO::Handle);
65 warn "connecting to $machine\n" if $Debug;
67 $ssh_pid = sshopen2($machine,$reader,$writer,$clientd,$tag);
69 # nstore_fd(\*writer, {'hi'=>'there'});
71 warn "entering main loop\n" if $Debug;
73 my $keepalive_count = 0;
74 my $s = IO::Select->new( $reader );
79 warn "waiting for packet from client\n" if $Debug && !$undisp;
81 my @handles = $s->can_read(5);
83 myshutdown() if sigint() || sigterm();
84 if ( $keepalives && $keepalive_count++ > 10 ) {
87 nstore_fd( { _token => '_keepalive' }, $writer );
95 warn "receiving packet from client\n" if $Debug;
97 my $packet = eval { fd_retrieve($reader); };
99 warn "Storable error receiving packet from client".
100 " (assuming lost connection): $@\n"
103 warn "sending TERM signal to ssh process $ssh_pid\n" if $Debug;
104 kill 'TERM', $ssh_pid;
105 $old_ssh_pid{$ssh_pid} = 1;
110 warn "packet received\n".
111 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
114 if ( $packet->{_packet} eq '_enable_keepalive' ) {
115 warn "enabling keep alives\n" if $Debug;
120 #prevent runaway forking
122 while ( $kids >= $max_kids ) {
123 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
128 warn "forking child\n" if $Debug;
129 defined( my $pid = fork ) or die "can't fork: $!";
133 warn "child $pid spawned\n" if $Debug;
137 $FS::UID::dbh->{InactiveDestroy} = 1;
138 forksuidsetup($user);
141 #adminsuidsetup($user);
143 my $type = $packet->{_packet};
144 warn "calling $type handler\n" if $Debug;
145 my $rv = eval { FS::ClientAPI->dispatch($type, $packet); };
147 warn my $error = "WARNING: error dispatching $type: $@";
148 $rv = { _error => $error };
150 $rv->{_token} = $packet->{_token}; #identifier
152 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
154 warn "sending response\n" if $Debug;
155 nstore_fd($rv, $writer) or die "FATAL: can't send response: $!";
156 $writer->flush or die "FATAL: can't flush: $!";
159 warn "child exiting\n" if $Debug;
165 myshutdown if sigint() || sigterm();
166 warn "connection lost, reconnecting\n" if $Debug;
172 # utility subroutines
176 #warn "reaping kids\n";
177 foreach my $pid ( keys %kids ) {
178 my $kid = waitpid($pid, WNOHANG);
185 foreach my $pid ( keys %old_ssh_pid ) {
186 waitpid($pid, WNOHANG) and delete $old_ssh_pid{$pid};
188 #warn "done reaping\n";
193 my $wait = 12; #wait up to 1 minute
194 while ( $kids > 0 && $wait-- ) {
195 warn "waiting for $kids children to terminate";
199 warn "abandoning $kids children" if $kids;
200 kill 'TERM', $ssh_pid if $ssh_pid;
205 warn "locking $lock_file mutex for write to write stream\n" if $Debug > 1;
208 #flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!";
210 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
215 warn "unlocking $lock_file mutex\n" if $Debug > 1;
218 #flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!";
220 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";
225 die "Usage:\n\n freeside-selfservice-server user machine\n";