freeside/FS/bin freeside-queued,1.30,1.31 freeside-sqlradius-radacctd,1.2,1.3 freeside-selfservice-server,1.15,1.16
ivan
ivan at pouncequick.420.am
Mon Apr 25 02:33:39 PDT 2005
Update of /home/cvs/cvsroot/freeside/FS/bin
In directory pouncequick:/tmp/cvs-serv6040/FS/bin
Modified Files:
freeside-queued freeside-sqlradius-radacctd
freeside-selfservice-server
Log Message:
pick up freeside-sqlradius-radacctd again after all these years, now it just needs to update the "seconds" field(s), finally closes: Bug#1125
Index: freeside-selfservice-server
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/bin/freeside-selfservice-server,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- freeside-selfservice-server 5 Oct 2004 12:17:35 -0000 1.15
+++ freeside-selfservice-server 25 Apr 2005 09:33:35 -0000 1.16
@@ -1,23 +1,16 @@
#!/usr/bin/perl -w
-#
-# freeside-selfservice-server
-
-# alas, much false laziness with freeside-queued and fs_signup_server. at
-# least it is slated to replace fs_{signup,passwd,mailadmin}_server
-# should probably generalize the version in here, or better yet use
-# Proc::Daemon or somesuch
use strict;
-use vars qw( $Debug %kids $kids $max_kids $shutdown $log_file $ssh_pid
- $keepalives );
-use subs qw( lock_write unlock_write );
+use vars qw( $Debug %kids $kids $max_kids $ssh_pid $keepalives );
+use subs qw( lock_write unlock_write myshutdown usage );
use Fcntl qw(:flock);
-use POSIX qw(:sys_wait_h setsid);
+use POSIX qw(:sys_wait_h);
use IO::Handle;
use IO::Select;
use IO::File;
use Storable 2.09 qw(nstore_fd fd_retrieve);
use Net::SSH qw(sshopen2);
+use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
use FS::UID qw(adminsuidsetup forksuidsetup);
use FS::ClientAPI;
@@ -28,7 +21,6 @@
$Debug = 1; # 2 will turn on more logging
# 3 will log packet contents, including passwords
-$shutdown = 0;
$max_kids = '10'; #?
$keepalives = 0; #let clientd turn it on, so we don't barf on old ones
$kids = 0;
@@ -37,12 +29,31 @@
my $machine = shift or die &usage;
my $tag = scalar(@ARGV) ? shift : '';
+my $lock_file = "/usr/local/etc/freeside/selfservice.$machine.writelock";
+
+
+# to keep pid files unique w/multi machines (and installs!)
# $FS::UID::datasrc not posible
-my $pid_file = "/var/run/freeside-selfservice-server.$user.$machine.pid";
+daemonize1("freeside-selfservice-server","$user.$machine");
-my $lock_file = "/usr/local/etc/freeside/selfservice.$machine.writelock";
+#false laziness w/Daemon::drop_root
+my $freeside_gid = scalar(getgrnam('freeside'))
+ or die "can't find freeside group\n";
+
+open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
+chown $FS::UID::freeside_uid, $freeside_gid, $lock_file;
+
+drop_root();
+
+$ENV{HOME} = (getpwuid($>))[7]; #for ssh
+
+adminsuidsetup $user;
+
+#logfile("/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc); #MACHINE
+logfile("/usr/local/etc/freeside/selfservice.$machine.log");
+
+daemonize2();
-&init($user);
my $conf = new FS::Conf;
@@ -69,7 +80,7 @@
$undisp = 1;
my @handles = $s->can_read(5);
unless ( @handles ) {
- &shutdown if $shutdown;
+ myshutdown() if sigint() || sigterm();
if ( $keepalives && $keepalive_count++ > 10 ) {
$keepalive_count = 0;
lock_write;
@@ -150,7 +161,7 @@
}
- &shutdown if $shutdown;
+ myshutdown if sigint() || sigterm();
warn "connection lost, reconnecting\n" if $Debug;
sleep 3;
@@ -172,70 +183,7 @@
#warn "done reaping\n";
}
-sub init {
- my $user = shift;
-
- chdir "/" or die "Can't chdir to /: $!";
- open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
- defined(my $pid = fork) or die "Can't fork: $!";
- if ( $pid ) {
- print "freeside-selfservice-server to $machine started with pid $pid\n"; #logging to $log_file
- exit unless $pid_file;
- my $pidfh = new IO::File ">$pid_file" or exit;
- print $pidfh "$pid\n";
- exit;
- }
-
-# sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; $kids--; }
-# #sub REAPER { my $pid = wait; $kids--; $SIG{CHLD} = \&REAPER; }
-# $SIG{CHLD} = \&REAPER;
-
- $shutdown = 0;
- $SIG{HUP} = sub { warn "SIGHUP received; shutting down\n"; $shutdown++; };
- $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $shutdown++; };
- $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $shutdown++; };
- $SIG{QUIT} = sub { warn "SIGQUIT received; shutting down\n"; $shutdown++; };
- $SIG{PIPE} = sub { warn "SIGPIPE received; shutting down\n"; $shutdown++; };
-
- #false laziness w/freeside-queued
- my $freeside_gid = scalar(getgrnam('freeside'))
- or die "can't setgid to freeside group\n";
-
- open(LOCKFILE,">$lock_file") or die "can't open $lock_file: $!";
- chown $FS::UID::freeside_uid, $freeside_gid, $lock_file;
-
- $) = $freeside_gid;
- $( = $freeside_gid;
- #if freebsd can't setuid(), presumably it can't setgid() either. grr fleabsd
- ($(,$)) = ($),$();
- $) = $freeside_gid;
-
- $> = $FS::UID::freeside_uid;
- $< = $FS::UID::freeside_uid;
- #freebsd is sofa king broken, won't setuid()
- ($<,$>) = ($>,$<);
- $> = $FS::UID::freeside_uid;
- #eslaf
-
- $ENV{HOME} = (getpwuid($>))[7]; #for ssh
- adminsuidsetup $user;
-
- #$log_file = "/usr/local/etc/freeside/selfservice.". $FS::UID::datasrc; #MACHINE NAME
- $log_file = "/usr/local/etc/freeside/selfservice.$machine.log";
-
- open STDOUT, '>/dev/null'
- or die "Can't write to /dev/null: $!";
- setsid or die "Can't start a new session: $!";
- open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
-
- $SIG{__DIE__} = \&_die;
- $SIG{__WARN__} = \&_logmsg;
-
- warn "freeside-selfservice-server starting\n";
-
-}
-
-sub shutdown {
+sub myshutdown {
&reap_kids;
my $wait = 12; #wait up to 1 minute
while ( $kids > 0 && $wait-- ) {
@@ -246,27 +194,6 @@
warn "abandoning $kids children" if $kids;
kill 'TERM', $ssh_pid if $ssh_pid;
die "exiting";
-}
-
-sub _die {
- my $msg = shift;
- unlink $pid_file if -e $pid_file;
- _logmsg($msg);
-}
-
-sub _logmsg {
- chomp( my $msg = shift );
- _do_logmsg( "[server] [". scalar(localtime). "] [$$] $msg\n" );
-}
-
-sub _do_logmsg {
- chomp( my $msg = shift );
- my $log = new IO::File ">>$log_file";
- flock($log, LOCK_EX);
- seek($log, 0, 2);
- print $log "$msg\n";
- flock($log, LOCK_UN);
- close $log;
}
sub lock_write {
Index: freeside-queued
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/bin/freeside-queued,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- freeside-queued 27 Jan 2005 10:21:18 -0000 1.30
+++ freeside-queued 25 Apr 2005 09:33:35 -0000 1.31
@@ -1,13 +1,11 @@
#!/usr/bin/perl -w
use strict;
-use vars qw( $log_file $sigterm $sigint $kids $max_kids %kids );
-use subs qw( _die _logmsg );
-use Fcntl qw(:flock);
-use POSIX qw(:sys_wait_h setsid);
-use Date::Format;
+use vars qw( $DEBUG $kids $max_kids %kids );
+use POSIX qw(:sys_wait_h);
use IO::File;
use FS::UID qw(adminsuidsetup forksuidsetup driver_name dbh myconnect);
+use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
use FS::Record qw(qsearch qsearchs);
use FS::queue;
use FS::queue_depend;
@@ -18,40 +16,24 @@
use Net::SSH 0.07;
use FS::part_export;
+$DEBUG = 0;
+
$max_kids = '10'; #guess it should be a config file...
$kids = 0;
my $user = shift or die &usage;
-#my $pid_file = "/var/run/freeside-queued.$user.pid";
-my $pid_file = "/var/run/freeside-queued.pid";
-
-&daemonize1;
-
-#sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; $kids--; }
-#$SIG{CHLD} = \&REAPER;
-
-$sigterm = 0;
-$sigint = 0;
-$SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
-$SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
+warn "starting daemonization (forking)\n" if $DEBUG;
+#daemonize1('freeside-queued',$user); #to keep pid files unique w/multi installs
+daemonize1('freeside-queued');
-my $freeside_gid = scalar(getgrnam('freeside'))
- or die "can't setgid to freeside group\n";
-$) = $freeside_gid;
-$( = $freeside_gid;
-#if freebsd can't setuid(), presumably it can't setgid() either. grr fleabsd
-($(,$)) = ($),$();
-$) = $freeside_gid;
+warn "dropping privledges\n" if $DEBUG;
+drop_root();
-$> = $FS::UID::freeside_uid;
-$< = $FS::UID::freeside_uid;
-#freebsd is sofa king broken, won't setuid()
-($<,$>) = ($>,$<);
-$> = $FS::UID::freeside_uid;
$ENV{HOME} = (getpwuid($>))[7]; #for ssh
+warn "connecting to database\n" if $DEBUG;
$@ = 'not connected';
while ( $@ ) {
eval { adminsuidsetup $user; };
@@ -62,14 +44,12 @@
}
}
-$log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
-
-&daemonize2;
+logfile( "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc );
-$SIG{__DIE__} = \&_die;
-$SIG{__WARN__} = \&_logmsg;
+warn "completing daemonization (detaching))\n" if $DEBUG;
+daemonize2();
-warn "freeside-queued starting\n";
+#--
my $warnkids=0;
while (1) {
@@ -225,11 +205,11 @@
}
} continue {
- if ( $sigterm ) {
+ if ( sigterm() ) {
warn "received TERM signal; exiting\n";
exit;
}
- if ( $sigint ) {
+ if ( sigint() ) {
warn "received INT signal; exiting\n";
exit;
}
@@ -237,48 +217,6 @@
sub usage {
die "Usage:\n\n freeside-queued user\n";
-}
-
-sub _die {
- my $msg = shift;
- unlink $pid_file if -e $pid_file;
- _logmsg($msg);
-}
-
-sub _logmsg {
- chomp( my $msg = shift );
- my $log = new IO::File ">>$log_file";
- flock($log, LOCK_EX);
- seek($log, 0, 2);
- print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
- flock($log, LOCK_UN);
- close $log;
-}
-
-sub daemonize1 {
-
- chdir "/" or die "Can't chdir to /: $!";
- open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
- defined(my $pid = fork) or die "Can't fork: $!";
- if ( $pid ) {
- print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
- exit unless $pid_file;
- my $pidfh = new IO::File ">$pid_file" or exit;
- print $pidfh "$pid\n";
- exit;
- }
- #open STDOUT, '>/dev/null'
- # or die "Can't write to /dev/null: $!";
- #setsid or die "Can't start a new session: $!";
- #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
-
-}
-
-sub daemonize2 {
- open STDOUT, '>/dev/null'
- or die "Can't write to /dev/null: $!";
- setsid or die "Can't start a new session: $!";
- open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
}
sub reap_kids {
Index: freeside-sqlradius-radacctd
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/bin/freeside-sqlradius-radacctd,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- freeside-sqlradius-radacctd 5 Nov 2002 20:29:57 -0000 1.2
+++ freeside-sqlradius-radacctd 25 Apr 2005 09:33:35 -0000 1.3
@@ -1,158 +1,115 @@
#!/usr/bin/perl -Tw
use strict;
-use vars qw( $log_file $sigterm $sigint );
-use subs qw( _die _logmsg );
-use Fcntl qw(:flock);
-use POSIX qw(setsid);
-use Date::Format;
+use vars qw( @part_export );
+use subs qw(myshutdown);
+use POSIX qw(:sys_wait_h);
use IO::File;
-use FS::UID qw(adminsuidsetup);
-#use FS::Record qw(qsearch qsearchs);
-#use FS::part_export;
+use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
+use FS::UID qw(adminsuidsetup); #forksuidsetup driver_name dbh myconnect);
+use FS::Record qw(qsearch); # qsearchs);
+use FS::part_export;
#use FS::svc_acct;
#use FS::cust_svc;
-#lots of false laziness w/freeside-queued
-
my $user = shift or die &usage;
-#my $pid_file = "/var/run/freeside-sqlradius-radacctd.$user.pid";
-my $pid_file = "/var/run/freeside-sqlradius-radacctd.pid";
-
-&daemonize1;
-
-#sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; $kids--; }
-#$SIG{CHLD} = \&REAPER;
-
-$sigterm = 0;
-$sigint = 0;
-$SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
-$SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
-
-my $freeside_gid = scalar(getgrnam('freeside'))
- or die "can't setgid to freeside group\n";
-$) = $freeside_gid;
-$( = $freeside_gid;
-#if freebsd can't setuid(), presumably it can't setgid() either. grr fleabsd
-($(,$)) = ($),$();
-$) = $freeside_gid;
+#daemonize1('freeside-sqlradius-radacctd', $user); #keep unique pid files w/multi installs
+daemonize1('freeside-sqlradius-radacctd');
-$> = $FS::UID::freeside_uid;
-$< = $FS::UID::freeside_uid;
-#freebsd is sofa king broken, won't setuid()
-($<,$>) = ($>,$<);
-$> = $FS::UID::freeside_uid;
+drop_root();
#$ENV{HOME} = (getpwuid($>))[7]; #for ssh
+
adminsuidsetup $user;
-$log_file= "/usr/local/etc/freeside/sqlradius-radacctd-log.". $FS::UID::datasrc;
+logfile( "/usr/local/etc/freeside/sqlradius-radacctd-log.". $FS::UID::datasrc );
-&daemonize2;
+daemonize2();
-$SIG{__DIE__} = \&_die;
-$SIG{__WARN__} = \&_logmsg;
+#--
-warn "freeside-sqlradius-radacctd starting\n";
+ at part_export =
+ qsearch('part_export', { 'exporttype' => 'sqlradius' } );
+push @part_export,
+ qsearch('part_export', { 'exporttype' => 'sqlradius_withdomain' } );
-#eslaf
+ at part_export = grep { ! $_->option('ignore_accounting') } @part_export;
-#my $machine = shift or die &usage; #would need to be up higher for real
-my @exports = qsearch('part_export', { 'exporttype' => 'sqlradius' } );
+die "no sqlradius or sqlradius_withdomain exports without ignore_accounting"
+ unless @part_export;
while (1) {
- my %seen = ();
- foreach my $export ( @exports ) {
- next if $seen{$export->option('datasrc')}++;
- my $dbh = DBI->connect(
- map { $export->option($_) } qw( datasrc username password )
- ) or do {
- warn "can't connect to ". $export->option('datasrc'). ": ". $DBI::errstr;
- next;
- }
-
- # find old radacct position
- #$lastid = 0;
-
- # get new radacct records
- my $sth = $dbh->prepare('SELECT * FROM radacct WHERE radacctid > ?') or do {
- warn "can't select in radacct table from ". $export->option('datasrc').
- ": ". $dbh->errstr;
+ #fork off one kid per export (machine)
+ # _>{'_radacct_kid'} is an evil kludge
+ foreach my $part_export ( grep ! $_->{'_radacct_kid'}, @part_export ) {
+
+ defined( my $pid = fork ) or do {
+ warn "WARNING: can't fork to spawn child for ". $part_export->machine;
next;
};
- while ( my $radacct = $sth->fetchrow_arrayref({}) ) {
+ if ( $pid ) {
+ $part_export->{'_radacct_kid'} = $pid;
+ warn "child $pid spawned for ". $part_export->machine;
+ } else { #kid time
- my $session = new FS::session {
- portnum =>
- svcnum =>
- login =>
- #logout =>
- };
+ adminsuidsetup($user); #get our own db handle
- }
+ until ( sigint || sigterm ) {
+ $part_export->update_svc_acct();
+ sleep 1;
+ }
- # look for updated radacct records & replace them
+ warn "child for ". $part_export->machine. " done";
+ exit;
- }
+ } #eo kid
- sleep 5;
+ }
-}
+ #reap up any kids that died...
+ &reap_kids;
-#more false laziness w/freeside-queued
+ myshutdown() if sigterm() || sigint();
-sub usage {
- die "Usage:\n\n freeside-sqlradius-radacctd user\n";
+ sleep 5;
}
-sub _die {
- my $msg = shift;
- unlink $pid_file if -e $pid_file;
- _logmsg($msg);
-}
+#--
-sub _logmsg {
- chomp( my $msg = shift );
- my $log = new IO::File ">>$log_file";
- flock($log, LOCK_EX);
- seek($log, 0, 2);
- print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
- flock($log, LOCK_UN);
- close $log;
-}
+sub myshutdown {
+ &reap_kids;
-sub daemonize1 {
+ #kill all the kids
+ kill 'TERM', $_ foreach grep $_, map $_->{'_radacct_kid'}, @part_export;
- chdir "/" or die "Can't chdir to /: $!";
- open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
- defined(my $pid = fork) or die "Can't fork: $!";
- if ( $pid ) {
- print "freeside-sqlradius-radacctd started with pid $pid\n";
- #logging to $log_file\n";
- exit unless $pid_file;
- my $pidfh = new IO::File ">$pid_file" or exit;
- print $pidfh "$pid\n";
- exit;
+ my $wait = 12; #wait up to 1 minute
+ while ( ( grep $_->{'_radacct_kid'}, @part_export ) && $wait-- ) {
+ warn "waiting for children to terminate";
+ sleep 5;
+ &reap_kids;
}
- #open STDOUT, '>/dev/null'
- # or die "Can't write to /dev/null: $!";
- #setsid or die "Can't start a new session: $!";
- #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
-
+ warn "abandoning children" if grep $_->{'_radacct_kid'}, @part_export;
+ die "exiting";
}
-sub daemonize2 {
- open STDOUT, '>/dev/null'
- or die "Can't write to /dev/null: $!";
- setsid or die "Can't start a new session: $!";
- open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
+sub reap_kids {
+ #warn "reaping kids\n";
+ foreach my $part_export ( grep $_->{'_radacct_kid'}, @part_export ) {
+ my $pid = $part_export->{'_radacct_kid'};
+ my $kid = waitpid($pid, WNOHANG);
+ if ( $kid > 0 ) {
+ $part_export->{'_radacct_kid'} = '';
+ }
+ }
+ #warn "done reaping\n";
}
-
-#eslaf
+sub usage {
+ die "Usage:\n\n freeside-sqlradius-radacctd user\n";
+}
=head1 NAME
@@ -164,17 +121,24 @@
=head1 DESCRIPTION
-Imports records from an SQL radacct table in real-time into the session
-monitor.
-
-This enables per-minute or per-hour charges as well as the
-"View active NAS ports" function.
+Imports records from an the SQL radacct tables of all sqlradius and
+sqlradius_withdomain exports (except those with the ignore_accounting flag) and
+updates the svc_acct.seconds for each account. Runs as a daemon and updates
+the database in real-time.
B<username> is a username added by freeside-adduser.
-=head1 SEE ALSO
+=head1 RADIUS DATABASE CHANGES
-session.html from the base documentation.
+ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL;
+
+If you want to ignore the existing accountg records, also do:
+
+UPDATE TABLE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL;
+
+=head1 SEE ALSO
=cut
+
+1;
More information about the freeside-commits
mailing list