/usr/sbin/opendmarc-expire is in opendmarc 1.2.0+dfsg-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | #!/usr/bin/perl
#
# Copyright (c) 2010-2012, The Trusted Domain Project. All rights reserved.
#
# Script to age out OpenDMARC aggregate report data
###
### Setup
###
use strict;
use warnings;
use DBI;
use File::Basename;
use Getopt::Long;
use IO::Handle;
use POSIX;
require DBD::mysql;
# general
my $progname = basename($0);
my $version = "1.2.0";
my $verbose = 0;
my $helponly = 0;
my $showversion = 0;
my $minmsg;
my $dbi_s;
my $dbi_h;
my $dbi_a;
# DB parameters
my $def_dbhost = "localhost";
my $def_dbname = "opendmarc";
my $def_dbuser = "opendmarc";
my $def_dbpasswd = "opendmarc";
my $def_dbport = "3306";
my $dbhost;
my $dbname;
my $dbuser;
my $dbpasswd;
my $dbport;
my $dbscheme = "mysql";
my $def_maxage = 180;
my $rows;
my $maxage;
###
### NO user-serviceable parts beyond this point
###
sub usage
{
print STDERR "$progname: usage: $progname [options]\n";
print STDERR "\t--dbhost=host database host [$def_dbhost]\n";
print STDERR "\t--dbname=name database name [$def_dbname]\n";
print STDERR "\t--dbpasswd=passwd database password [$def_dbpasswd]\n";
print STDERR "\t--dbport=port database port [$def_dbport]\n";
print STDERR "\t--dbuser=user database user [$def_dbuser]\n";
print STDERR "\t--expire=days expiration time, in days [$def_maxage]\n";
print STDERR "\t--help print help and exit\n";
print STDERR "\t--verbose verbose output\n";
print STDERR "\t--version print version and exit\n";
}
# parse command line arguments
my $opt_retval = &Getopt::Long::GetOptions ('dbhost=s' => \$dbhost,
'dbname=s' => \$dbname,
'dbpasswd=s' => \$dbpasswd,
'dbport=s' => \$dbport,
'dbuser=s' => \$dbuser,
'expire=i' => \$maxage,
'help!' => \$helponly,
'verbose!' => \$verbose,
'version!' => \$showversion,
);
if (!$opt_retval || $helponly)
{
usage();
if ($helponly)
{
exit(0);
}
else
{
exit(1);
}
}
if ($showversion)
{
print STDOUT "$progname v$version\n";
exit(0);
}
# apply defaults
if (!defined($dbhost))
{
if (defined($ENV{'OPENDMARC_DBHOST'}))
{
$dbhost = $ENV{'OPENDMARC_DBHOST'};
}
else
{
$dbhost = $def_dbhost;
}
}
if (!defined($dbname))
{
if (defined($ENV{'OPENDMARC_DB'}))
{
$dbname = $ENV{'OPENDMARC_DB'};
}
else
{
$dbname = $def_dbname;
}
}
if (!defined($dbpasswd))
{
if (defined($ENV{'OPENDMARC_PASSWORD'}))
{
$dbpasswd = $ENV{'OPENDMARC_PASSWORD'};
}
else
{
$dbpasswd = $def_dbpasswd;
}
}
if (!defined($dbport))
{
if (defined($ENV{'OPENDMARC_PORT'}))
{
$dbport = $ENV{'OPENDMARC_PORT'};
}
else
{
$dbport = $def_dbport;
}
}
if (!defined($dbuser))
{
if (defined($ENV{'OPENDMARC_USER'}))
{
$dbuser = $ENV{'OPENDMARC_USER'};
}
else
{
$dbuser = $def_dbuser;
}
}
if (!defined($maxage))
{
if (defined($ENV{'OPENDMARC_MAXAGE'}))
{
$maxage = $ENV{'OPENDMARC_MAXAGE'};
}
else
{
$maxage = $def_maxage;
}
}
# sanity check
if ($maxage <= 0)
{
print STDERR "$progname: invalid expiration time\n";
exit(1);
}
#
# Let's go!
#
if ($verbose)
{
print STDERR "$progname: started at " . localtime() . "\n";
}
my $dbi_dsn = "DBI:" . $dbscheme . ":database=" . $dbname .
";host=" . $dbhost . ";port=" . $dbport;
$dbi_h = DBI->connect($dbi_dsn, $dbuser, $dbpasswd, { PrintError => 0 });
if (!defined($dbi_h))
{
print STDERR "$progname: unable to connect to database: $DBI::errstr\n";
exit(1);
}
if ($verbose)
{
print STDERR "$progname: connected to database\n";
}
#
# Expire messages
#
if ($verbose)
{
print STDERR "$progname: expiring messages older than $maxage day(s)\n";
}
$dbi_s = $dbi_h->prepare("DELETE FROM messages WHERE date <= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL ? DAY)");
$rows = $dbi_s->execute($maxage);
if (!$rows)
{
print STDERR "$progname: DELETE failed: " . $dbi_h->errstr;
$dbi_s->finish;
$dbi_h->disconnect;
exit(1);
}
elsif ($verbose)
{
if ($rows eq "0E0")
{
print STDOUT "$progname: no rows deleted\n";
}
else
{
print STDOUT "$progname: $rows row(s) deleted\n";
}
}
$dbi_s->finish;
#
# Expire signatures
#
$dbi_s = $dbi_h->prepare("SELECT MIN(id) FROM messages");
if (!$dbi_s->execute)
{
print STDERR "$progname: SELECT failed: " . $dbi_h->errstr;
$dbi_s->finish;
$dbi_h->disconnect;
exit(1);
}
while ($dbi_a = $dbi_s->fetchrow_arrayref())
{
$minmsg = $dbi_a->[0];
}
$dbi_s->finish;
if ($verbose)
{
print STDERR "$progname: expiring signatures on expired messages (id < $minmsg)\n";
}
$dbi_s = $dbi_h->prepare("DELETE FROM signatures WHERE message < ?");
$rows = $dbi_s->execute($minmsg);
if (!$rows)
{
print STDERR "$progname: DELETE failed: " . $dbi_h->errstr;
$dbi_s->finish;
$dbi_h->disconnect;
exit(1);
}
elsif ($verbose)
{
if ($rows eq "0E0")
{
print STDOUT "$progname: no rows deleted\n";
}
else
{
print STDOUT "$progname: $rows row(s) deleted\n";
}
}
$dbi_s->finish;
#
# Expire request data
#
if ($verbose)
{
print STDERR "$progname: expiring request data older than $maxage days\n";
}
$dbi_s = $dbi_h->prepare("DELETE FROM requests WHERE lastsent <= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL ? DAY) AND NOT lastsent = '0000-00-00 00:00:00'");
$rows = $dbi_s->execute($maxage);
if (!$rows)
{
print STDERR "$progname: DELETE failed: " . $dbi_h->errstr . "\n";
$dbi_s->finish;
$dbi_h->disconnect;
exit(1);
}
elsif ($verbose)
{
if ($rows eq "0E0")
{
print STDOUT "$progname: no rows deleted\n";
}
else
{
print STDOUT "$progname: $rows row(s) deleted\n";
}
}
$dbi_s->finish;
#
# All done!
#
if ($verbose)
{
print STDERR "$progname: terminating at " . localtime() . "\n";
}
$dbi_h->disconnect;
exit(0);
|