The cert "bootstrap" code now checks certs for validity
[freeradius.git] / dialup_admin / bin / truncate_radacct
1 #!/usr/bin/perl
2 #
3 # Delete sessions from the radacct table which are older than
4 # $back_days
5 # Works with mysql and postgresql
6 #
7 use POSIX;
8 use File::Temp;
9
10 $conf=shift||'/usr/local/dialup_admin/conf/admin.conf';
11 $back_days = 90;
12
13
14 open CONF, "<$conf"
15         or die "Could not open configuration file\n";
16 while(<CONF>){
17         chomp;
18         ($key,$val)=(split /:\s*/,$_);
19         $sql_type = $val if ($key eq 'sql_type');
20         $sql_server = $val if ($key eq 'sql_server');
21         $sql_username = $val if ($key eq 'sql_username');
22         $sql_password = $val if ($key eq 'sql_password');
23         $sql_database = $val if ($key eq 'sql_database');
24         $sql_accounting_table = $val if ($key eq 'sql_accounting_table');
25         $sqlcmd = $val if ($key eq 'sql_command');
26 }
27 close CONF;
28
29 die "sql_command directive is not set in admin.conf\n" if ($sqlcmd eq '');
30 die "sql command '$sqlcmd' not found or does not seem to be executable\n" if (! -x $sqlcmd);
31
32 if ($sql_type eq 'mysql'){
33         $sql_password = (!$sql_password) ? '' : "-p$sql_password";
34 }
35 $sql_password =~ s/(\W)/\\$1/g;
36
37 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
38 $date = POSIX::strftime("%Y-%m-%d %T",$sec,$min,$hour,($mday - $back_days),$mon,$year,$wday,$yday,$isdst);
39 print "$date\n";
40 if (POSIX::strftime("%Y-%m-%d %T",localtime) eq $date){
41         die "Could not set correct back date.\n";
42 }
43 $query = "";
44 $query = "LOCK TABLES $sql_accounting_table WRITE;" if ($sql_type eq 'mysql');
45 $query .= "DELETE FROM $sql_accounting_table WHERE AcctStopTime < '$date' AND AcctStopTime IS NOT NULL ;";
46 $query .= "UNLOCK TABLES;" if ($sql_type eq 'mysql');
47 print "$query\n";
48 my ($fh, $tmp_filename) = tempfile() or die "Could not open tmp file\n";
49 print $fh "ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS.FF TZH:TZM';\n" if ($sql_type eq 'oracle');
50 print $fh $query;
51 close $fh;
52 $command = "$sqlcmd -h$sql_server -u$sql_username $sql_password $sql_database < $tmp_filename" if ($sql_type eq 'mysql');
53 $command = "$sqlcmd  -U $sql_username -f  $tmp_filename $sql_database" if ($sql_type eq 'pg');
54 $command = "$sqlcmd  $sql_username/$pass" . "@" . "$sql_database <$tmpfile.$server" if ($sql_type eq 'oracle');
55 $command = "$sqlcmd '$sql_server' '$sql_port' '' '$sql_username' '$sql_password' < $tmp_filename" if ($sql_type eq 'sqlrelay');
56 `$command`;