From e75d09515b69aa10f69f4af615631a3734bddc6d Mon Sep 17 00:00:00 2001 From: kkalev Date: Tue, 4 Jan 2005 14:04:00 +0000 Subject: [PATCH] * If date calculation fails, abort * Add a backup_radacct script --- Changelog | 2 ++ bin/backup_radacct | 48 ++++++++++++++++++++++++++++++++++++++++++++ bin/clean_radacct | 3 +++ bin/truncate_radacct | 3 +++ 4 files changed, 56 insertions(+) create mode 100755 bin/backup_radacct diff --git a/Changelog b/Changelog index 91a591f..e4c4a57 100644 --- a/Changelog +++ b/Changelog @@ -9,6 +9,8 @@ Ver 1.78: * Add a header with the page encoding before sending any page (header added in config.php3) This closes Bug #153 * Fix a problem when reading username.mappings +* If date calculation fails, abort +* Add a backup_radacct script Ver 1.75: * A LOT of security related fixes. Now dialupadmin should hopefully be secure enough to be accessed by normal users (not administrators). diff --git a/bin/backup_radacct b/bin/backup_radacct new file mode 100755 index 0000000..2194f46 --- /dev/null +++ b/bin/backup_radacct @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use POSIX; + +$conf=shift||'/data/local/dialupadmin/conf/admin.conf'; +$back_days = 80; +$backup_directory = "/logs/radiusd/accounting"; + +open CONF, "<$conf" + or die "Could not open configuration file\n"; +while(){ + chomp; + ($key,$val)=(split /:\s*/,$_); + $sql_server = $val if ($key eq 'sql_server'); + $sql_type = $val if ($key eq 'sql_type'); + $sql_port = $val if ($key eq 'sql_port'); + $sql_username = $val if ($key eq 'sql_username'); + $sql_password = $val if ($key eq 'sql_password'); + $sql_database = $val if ($key eq 'sql_database'); + $sql_accounting_table = $val if ($key eq 'sql_accounting_table'); + $sqlcmd = $val if ($key eq 'sql_command'); +} +close CONF; + +die "sql_command directive is not set in admin.conf\n" if ($sqlcmd eq ''); +die "Could not find sql binary. Please make sure that the \$sqlcmd variable points to the right location\n" if (! -x $sqlcmd); +if ($sql_type eq 'mysql'){ + $sql_password = ($sql_password eq '') ? '' : "-p$sql_password"; +} +$sql_password =~ s/(\W)/\\$1/g; + +($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; +$date = POSIX::strftime("%Y-%m-%d",$sec,$min,$hour,($mday - ($back_days - 1)),$mon,$year,$wday,$yday,$isdst); +$date2 = POSIX::strftime("%Y-%m-%d",$sec,$min,$hour,($mday - ($back_days + 1)),$mon,$year,$wday,$yday,$isdst); +$date3 = POSIX::strftime("%Y%m%d",$sec,$min,$hour,($mday - $back_days),$mon,$year,$wday,$yday,$isdst); +if (POSIX::strftime("%Y-%m-%d %T",localtime) eq $date){ + die "Could not set correct back date.\n"; +} + +$query = "SELECT * FROM $sql_accounting_table WHERE AcctStopTime < '$date' AND AcctStopTime > '$date2';"; +print "$query\n"; +open TMP, ">/tmp/backup_radacct.query" + or die "Could not open tmp file\n"; +print TMP $query; +close TMP; +$comm = "$sqlcmd -B -h $sql_server -u $sql_username $sql_password $sql_database $backup_directory/$date3" if ($sql_type eq 'mysql'); +$comm = "$sqlcmd -U $sql_username -f /tmp/truncate_radacct.query $sql_database >$backup_directory/$date3" if ($sql_type eq 'pg'); +`$comm`; +`/usr/local/bin/gzip -9 $backup_directory/$date3`; diff --git a/bin/clean_radacct b/bin/clean_radacct index 3789e49..c82f664 100755 --- a/bin/clean_radacct +++ b/bin/clean_radacct @@ -34,6 +34,9 @@ $sql_password =~ s/(\W)/\\$1/g; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $date = POSIX::strftime("%Y-%m-%d %T",$sec,$min,$hour,($mday - $back_days),$mon,$year,$wday,$yday,$isdst); print "$date\n"; +if (POSIX::strftime("%Y-%m-%d %T",localtime) eq $date){ + die "Could not set correct back date.\n"; +} $query = "DELETE FROM $sql_accounting_table WHERE AcctStopTime IS NULL AND AcctStartTime < '$date';"; print "$query\n"; diff --git a/bin/truncate_radacct b/bin/truncate_radacct index da82037..48e2d22 100755 --- a/bin/truncate_radacct +++ b/bin/truncate_radacct @@ -34,6 +34,9 @@ $sql_password =~ s/(\W)/\\$1/g; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $date = POSIX::strftime("%Y-%m-%d %T",$sec,$min,$hour,($mday - $back_days),$mon,$year,$wday,$yday,$isdst); print "$date\n"; +if (POSIX::strftime("%Y-%m-%d %T",localtime) eq $date){ + die "Could not set correct back date.\n"; +} $query = ""; $query = "LOCK TABLES $sql_accounting_table WRITE;" if ($sql_type eq 'mysql'); $query .= "DELETE FROM $sql_accounting_table WHERE AcctStopTime < '$date' AND AcctStopTime IS NOT NULL ;"; -- 2.39.5