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 $FREESIDE_CONF = "%%%FREESIDE_CONF%%%";
23 $Debug = 1; # 2 will turn on more logging
24 # 3 will log packet contents, including passwords
27 $keepalives = 0; #let clientd turn it on, so we don't barf on old ones
30 my $user = shift or die &usage;
31 my $machine = shift or die &usage;
32 my $tag = scalar(@ARGV) ? shift : '';
34 my $lock_file = "/usr/local/etc/freeside/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_SERVER/selfservice.$machine.log");
59 my $conf = new FS::Conf;
61 my $clientd = "/usr/local/sbin/freeside-selfservice-clientd"; #better name?
65 my($writer,$reader,$error) = (new IO::Handle, new IO::Handle, new IO::Handle);
66 warn "connecting to $machine\n" if $Debug;
68 $ssh_pid = sshopen2($machine,$reader,$writer,$clientd,$tag);
70 # nstore_fd(\*writer, {'hi'=>'there'});
72 warn "entering main loop\n" if $Debug;
74 my $keepalive_count = 0;
75 my $s = IO::Select->new( $reader );
80 warn "waiting for packet from client\n" if $Debug && !$undisp;
82 my @handles = $s->can_read(5);
84 myshutdown() if sigint() || sigterm();
85 if ( $keepalives && $keepalive_count++ > 10 ) {
88 nstore_fd( { _token => '_keepalive' }, $writer );
96 warn "receiving packet from client\n" if $Debug;
98 my $packet = eval { fd_retrieve($reader); };
100 warn "Storable error receiving packet from client".
101 " (assuming lost connection): $@\n"
104 warn "sending TERM signal to ssh process $ssh_pid\n" if $Debug;
105 kill 'TERM', $ssh_pid;
106 $old_ssh_pid{$ssh_pid} = 1;
111 warn "packet received\n".
112 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
115 if ( $packet->{_packet} eq '_enable_keepalive' ) {
116 warn "enabling keep alives\n" if $Debug;
121 #prevent runaway forking
123 while ( $kids >= $max_kids ) {
124 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
129 warn "forking child\n" if $Debug;
130 defined( my $pid = fork ) or die "can't fork: $!";
134 warn "child $pid spawned\n" if $Debug;
138 $FS::UID::dbh->{InactiveDestroy} = 1;
139 forksuidsetup($user);
142 #adminsuidsetup($user);
144 my $type = $packet->{_packet};
145 warn "calling $type handler\n" if $Debug;
146 my $rv = eval { FS::ClientAPI->dispatch($type, $packet); };
148 warn my $error = "WARNING: error dispatching $type: $@";
149 $rv = { _error => $error };
151 $rv->{_token} = $packet->{_token}; #identifier
153 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
155 warn "sending response\n" if $Debug;
156 nstore_fd($rv, $writer) or die "FATAL: can't send response: $!";
157 $writer->flush or die "FATAL: can't flush: $!";
160 warn "child exiting\n" if $Debug;
166 myshutdown if sigint() || sigterm();
167 warn "connection lost, reconnecting\n" if $Debug;
173 # utility subroutines
177 #warn "reaping kids\n";
178 foreach my $pid ( keys %kids ) {
179 my $kid = waitpid($pid, WNOHANG);
186 foreach my $pid ( keys %old_ssh_pid ) {
187 waitpid($pid, WNOHANG) and delete $old_ssh_pid{$pid};
189 #warn "done reaping\n";
194 my $wait = 12; #wait up to 1 minute
195 while ( $kids > 0 && $wait-- ) {
196 warn "waiting for $kids children to terminate";
200 warn "abandoning $kids children" if $kids;
201 kill 'TERM', $ssh_pid if $ssh_pid;
206 warn "locking $lock_file mutex for write to write stream\n" if $Debug > 1;
209 #flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!";
211 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
216 warn "unlocking $lock_file mutex\n" if $Debug > 1;
219 #flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!";
221 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";
226 die "Usage:\n\n freeside-selfservice-server user machine\n";