3 # freeside-selfservice-server
5 # alas, much false laziness with freeside-queued and fs_signup_server. at
6 # least it is slated to replace fs_{signup,passwd,mailadmin}_server
7 # should probably generalize the version in here, or better yet use
8 # Proc::Daemon or somesuch
11 use vars qw( $Debug %kids $kids $max_kids $shutdown $log_file $ssh_pid
13 use subs qw( lock_write unlock_write );
15 use POSIX qw(:sys_wait_h setsid);
19 use Storable 2.09 qw(nstore_fd fd_retrieve);
20 use Net::SSH qw(sshopen2);
21 use FS::UID qw(adminsuidsetup forksuidsetup);
28 $Debug = 1; # >= 2 will log packet contents, including potentially compromising
33 $keepalives = 0; #let clientd turn it on, so we don't barf on old ones
36 my $user = shift or die &usage;
37 my $machine = shift or die &usage;
38 my $tag = scalar(@ARGV) ? shift : '';
40 # $FS::UID::datasrc not posible
41 my $pid_file = "/var/run/freeside-selfservice-server.$user.$machine.pid";
43 my $lock_file = "/usr/local/etc/freeside/selfservice.$machine.writelock";
44 open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
48 my $conf = new FS::Conf;
50 my $clientd = "/usr/local/sbin/freeside-selfservice-clientd"; #better name?
54 my($writer,$reader,$error) = (new IO::Handle, new IO::Handle, new IO::Handle);
55 warn "connecting to $machine\n" if $Debug;
57 $ssh_pid = sshopen2($machine,$reader,$writer,$clientd,$tag);
59 # nstore_fd(\*writer, {'hi'=>'there'});
61 warn "entering main loop\n" if $Debug;
63 my $keepalive_count = 0;
64 my $s = IO::Select->new( $reader );
69 warn "waiting for packet from client\n" if $Debug && !$undisp;
71 my @handles = $s->can_read(5);
73 &shutdown if $shutdown;
74 if ( $keepalives && $keepalive_count++ > 10 ) {
76 nstore_fd( { _token => '_keepalive' }, $writer );
83 warn "receiving packet from client\n" if $Debug;
85 my $packet = eval { fd_retrieve($reader); };
87 warn "Storable error receiving packet from client".
88 " (assuming lost connection): $@\n"
91 warn "sending TERM signal to ssh process $ssh_pid\n" if $Debug;
92 kill 'TERM', $ssh_pid;
97 warn "packet received\n".
98 join('', map { " $_=>$packet->{$_}\n" } keys %$packet )
101 if ( $packet->{_packet} eq '_enable_keepalive' ) {
102 warn "enabling keep alives\n" if $Debug;
107 #prevent runaway forking
109 while ( $kids >= $max_kids ) {
110 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
115 warn "forking child\n" if $Debug;
116 defined( my $pid = fork ) or die "can't fork: $!";
120 warn "child $pid spawned\n" if $Debug;
124 #$FS::UID::dbh->{InactiveDestroy} = 1;
125 #forksuidsetup($user);
128 adminsuidsetup($user);
130 my $type = $packet->{_packet};
131 warn "calling $type handler\n" if $Debug;
132 my $rv = eval { FS::ClientAPI->dispatch($type, $packet); };
134 warn my $error = "WARNING: error dispatching $type: $@";
135 $rv = { _error => $error };
137 $rv->{_token} = $packet->{_token}; #identifier
139 warn "sending response\n" if $Debug;
141 nstore_fd($rv, $writer) or die "FATAL: can't send response: $!";
142 $writer->flush or die "FATAL: can't flush: $!";
145 warn "child exiting\n" if $Debug;
151 warn "connection lost, reconnecting\n" if $Debug;
157 # utility subroutines
161 #warn "reaping kids\n";
162 foreach my $pid ( keys %kids ) {
163 my $kid = waitpid($pid, WNOHANG);
169 #warn "done reaping\n";
175 chdir "/" or die "Can't chdir to /: $!";
176 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
177 defined(my $pid = fork) or die "Can't fork: $!";
179 print "freeside-selfservice-server to $machine started with pid $pid\n"; #logging to $log_file
180 exit unless $pid_file;
181 my $pidfh = new IO::File ">$pid_file" or exit;
182 print $pidfh "$pid\n";
186 # sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; $kids--; }
187 # #sub REAPER { my $pid = wait; $kids--; $SIG{CHLD} = \&REAPER; }
188 # $SIG{CHLD} = \&REAPER;
191 $SIG{HUP} = sub { warn "SIGHUP received; shutting down\n"; $shutdown++; };
192 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $shutdown++; };
193 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $shutdown++; };
194 $SIG{QUIT} = sub { warn "SIGQUIT received; shutting down\n"; $shutdown++; };
195 $SIG{PIPE} = sub { warn "SIGPIPE received; shutting down\n"; $shutdown++; };
197 #false laziness w/freeside-queued
198 my $freeside_gid = scalar(getgrnam('freeside'))
199 or die "can't setgid to freeside group\n";
202 #if freebsd can't setuid(), presumably it can't setgid() either. grr fleabsd
206 $> = $FS::UID::freeside_uid;
207 $< = $FS::UID::freeside_uid;
208 #freebsd is sofa king broken, won't setuid()
210 $> = $FS::UID::freeside_uid;
213 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
214 #adminsuidsetup $user;
216 #$log_file = "/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc; #MACHINE NAME
217 $log_file = "/usr/local/etc/freeside/selfservice.$machine.log";
219 open STDOUT, '>/dev/null'
220 or die "Can't write to /dev/null: $!";
221 setsid or die "Can't start a new session: $!";
222 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
224 $SIG{__DIE__} = \&_die;
225 $SIG{__WARN__} = \&_logmsg;
227 warn "freeside-selfservice-server starting\n";
232 my $wait = 12; #wait up to 1 minute
233 while ( $kids > 0 && $wait-- ) {
234 warn "waiting for $kids children to terminate";
237 warn "abandoning $kids children" if $kids;
238 kill 'TERM', $ssh_pid if $ssh_pid;
244 unlink $pid_file if -e $pid_file;
249 chomp( my $msg = shift );
250 _do_logmsg( "[server] [". scalar(localtime). "] [$$] $msg\n" );
254 chomp( my $msg = shift );
255 my $log = new IO::File ">>$log_file";
256 flock($log, LOCK_EX);
259 flock($log, LOCK_UN);
265 #flock($writer, LOCK_EX) or die "FATAL: can't lock write stream: $!";
267 flock(LOCKFILE, LOCK_EX) or die "FATAL: can't lock $lock_file: $!";
273 #flock($writer, LOCK_UN) or die "WARNING: can't release write lock: $!";
275 flock(LOCKFILE, LOCK_UN) or die "FATAL: can't unlock $lock_file: $!";
280 die "Usage:\n\n freeside-selfservice-server user machine\n";