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;
22 $FREESIDE_LOG = "%%%FREESIDE_LOG%%%";
23 $FREESIDE_LOCK = "%%%FREESIDE_LOCK%%%";
25 $Debug = 1; # 2 will turn on more logging
26 # 3 will log packet contents, including passwords
29 $keepalives = 0; #let clientd turn it on, so we don't barf on old ones
32 my $user = shift or die &usage;
33 my $machine = shift or die &usage;
34 my $tag = scalar(@ARGV) ? shift : '';
36 my $lock_file = "$FREESIDE_LOCK/selfservice.$machine.writelock";
38 # to keep pid files unique w/multi machines (and installs!)
39 # $FS::UID::datasrc not posible
40 daemonize1("freeside-selfservice-server","$user.$machine");
42 #false laziness w/Daemon::drop_root
43 my $freeside_gid = scalar(getgrnam('freeside'))
44 or die "can't find freeside group\n";
46 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
47 chown $FS::UID::freeside_uid, $freeside_gid, $lock_file;
51 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
55 #logfile("/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc); #MACHINE
56 logfile("$FREESIDE_LOG/selfservice.$machine.log");
60 my $conf = new FS::Conf;
61 if ( $conf->exists('selfservice-ignore_quantity') ) {
62 $FS::cust_svc::ignore_quantity = 1;
63 $FS::cust_svc::ignore_quantity = 1; #now it is used twice.
66 #clear the signup info cache so an "/etc/init.d/freeside restart" will pick
67 #up new info... (better as a callback in Signup.pm?)
68 my $cache = new FS::ClientAPI_SessionCache( {
69 'namespace' => 'FS::ClientAPI::Signup',
71 $cache->remove('signup_info_cache');
73 my $clientd = "/usr/local/sbin/freeside-selfservice-clientd"; #better name?
77 my($writer,$reader,$error) = (new IO::Handle, new IO::Handle, new IO::Handle);
78 warn "connecting to $machine\n" if $Debug;
80 $ssh_pid = sshopen2($machine,$reader,$writer,$clientd,$tag);
82 # nstore_fd(\*writer, {'hi'=>'there'});
84 warn "entering main loop\n" if $Debug;
86 my $keepalive_count = 0;
87 my $s = IO::Select->new( $reader );
92 warn "waiting for packet from client\n" if $Debug && !$undisp;
94 my @handles = $s->can_read(5);
96 myshutdown() if sigint() || sigterm();
97 if ( $keepalives && $keepalive_count++ > 10 ) {
100 nstore_fd( { _token => '_keepalive' }, $writer );
108 warn "receiving packet from client\n" if $Debug;
110 my $packet = eval { fd_retrieve($reader); };
112 warn "Storable error receiving packet from client".
113 " (assuming lost connection): $@\n"
116 warn "sending TERM signal to ssh process $ssh_pid\n" if $Debug;
117 kill 'TERM', $ssh_pid;
118 $old_ssh_pid{$ssh_pid} = 1;
123 warn "packet received\n".
124 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
127 if ( $packet->{_packet} eq '_enable_keepalive' ) {
128 warn "enabling keep alives\n" if $Debug;
133 #prevent runaway forking
135 while ( $kids >= $max_kids ) {
136 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
141 warn "forking child\n" if $Debug;
142 defined( my $pid = fork ) or die "can't fork: $!";
146 warn "child $pid spawned\n" if $Debug;
150 $FS::UID::dbh->{InactiveDestroy} = 1;
151 forksuidsetup($user);
154 #adminsuidsetup($user);
156 my $type = $packet->{_packet};
157 warn "calling $type handler\n" if $Debug;
158 my $rv = eval { FS::ClientAPI->dispatch($type, $packet); };
160 warn my $error = "WARNING: error dispatching $type: $@";
161 $rv = { _error => $error };
163 $rv->{_token} = $packet->{_token}; #identifier
165 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
167 warn "sending response\n" if $Debug;
168 nstore_fd($rv, $writer) or die "FATAL: can't send response: $!";
169 $writer->flush or die "FATAL: can't flush: $!";
172 warn "child exiting\n" if $Debug;
178 myshutdown if sigint() || sigterm();
179 warn "connection lost, reconnecting\n" if $Debug;
185 # utility subroutines
189 #warn "reaping kids\n";
190 foreach my $pid ( keys %kids ) {
191 my $kid = waitpid($pid, WNOHANG);
198 foreach my $pid ( keys %old_ssh_pid ) {
199 waitpid($pid, WNOHANG) and delete $old_ssh_pid{$pid};
201 #warn "done reaping\n";
206 my $wait = 12; #wait up to 1 minute
207 while ( $kids > 0 && $wait-- ) {
208 warn "waiting for $kids children to terminate";
212 warn "abandoning $kids children" if $kids;
213 kill 'TERM', $ssh_pid if $ssh_pid;
218 warn "locking $lock_file mutex for write to write stream\n" if $Debug > 1;
221 #flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!";
223 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
228 warn "unlocking $lock_file mutex\n" if $Debug > 1;
231 #flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!";
233 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";
238 die "Usage:\n\n freeside-selfservice-server user machine\n";