4 use vars qw( $Debug %kids $kids $max_kids $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;
109 warn "packet received\n".
110 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
113 if ( $packet->{_packet} eq '_enable_keepalive' ) {
114 warn "enabling keep alives\n" if $Debug;
119 #prevent runaway forking
121 while ( $kids >= $max_kids ) {
122 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
127 warn "forking child\n" if $Debug;
128 defined( my $pid = fork ) or die "can't fork: $!";
132 warn "child $pid spawned\n" if $Debug;
136 #$FS::UID::dbh->{InactiveDestroy} = 1;
137 #forksuidsetup($user);
140 adminsuidsetup($user);
142 my $type = $packet->{_packet};
143 warn "calling $type handler\n" if $Debug;
144 my $rv = eval { FS::ClientAPI->dispatch($type, $packet); };
146 warn my $error = "WARNING: error dispatching $type: $@";
147 $rv = { _error => $error };
149 $rv->{_token} = $packet->{_token}; #identifier
151 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
153 warn "sending response\n" if $Debug;
154 nstore_fd($rv, $writer) or die "FATAL: can't send response: $!";
155 $writer->flush or die "FATAL: can't flush: $!";
158 warn "child exiting\n" if $Debug;
164 myshutdown if sigint() || sigterm();
165 warn "connection lost, reconnecting\n" if $Debug;
171 # utility subroutines
175 #warn "reaping kids\n";
176 foreach my $pid ( keys %kids ) {
177 my $kid = waitpid($pid, WNOHANG);
183 #warn "done reaping\n";
188 my $wait = 12; #wait up to 1 minute
189 while ( $kids > 0 && $wait-- ) {
190 warn "waiting for $kids children to terminate";
194 warn "abandoning $kids children" if $kids;
195 kill 'TERM', $ssh_pid if $ssh_pid;
200 warn "locking $lock_file mutex for write to write stream\n" if $Debug > 1;
203 #flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!";
205 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
210 warn "unlocking $lock_file mutex\n" if $Debug > 1;
213 #flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!";
215 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";
220 die "Usage:\n\n freeside-selfservice-server user machine\n";