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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
#!/usr/bin/perl -w
# cleanup for F*EX service
#
# run this program via cron-job once at night!
#
# Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
#
use Getopt::Std;
use File::Basename;
use IO::Socket::INET;
use Cwd 'abs_path';
use Digest::MD5 'md5_hex';
use constant DS => 60*60*24;
# do not run as CGI!
exit if $ENV{SCRIPT_NAME};
unless ($FEXLIB = $ENV{FEXLIB}) {
if ($ENV{FEXHOME}) {
$FEXLIB = $ENV{FEXHOME}.'/lib';
} elsif (-f '/usr/share/fex/lib/fex.ph') {
$FEXLIB = '/usr/share/fex/lib';
} else {
$FEXLIB = dirname(dirname(abs_path($0))).'/lib';
}
$ENV{FEXLIB} = $FEXLIB;
}
die "$0: no FEXLIB\n" unless -r "$FEXLIB/fex.pp";
# program name
$_0 = $0;
$0 =~ s:.*/::;
$| = 1;
# use fex.ph for site configuration!
our ($FEXHOME);
our ($spooldir,@logdir,$docdir);
our ($akeydir,$ukeydir,$dkeydir,$skeydir,$gkeydir,$xkeydir,$lockdir);
our ($durl,$debug,$autodelete,$hostname,$admin,$admin_pw,$bcc);
our $keep_default = 5;
our $purge = $keep_default*3;
# load common code, local config : $HOME/lib/fex.ph
require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
my $logdir = $logdir[0];
# localized functions
# (needed for reminder and account reactivation e-mails)
foreach my $lf (glob "$FEXHOME/locale/*/lib/lf.pl") { require $lf }
# default locale functions (from fex.pp)
$notify{english} = \¬ify;
$reactivation{english} = \&reactivation;
@_ARGV = @ARGV;
$opt_v = $opt_V = $opt_d = 0;
getopts('vVd');
$opt_v = $opt_d if $opt_d; # debug mode, no real action
$today = time;
$isodate = isodate($today);
chdir $spooldir or die "$0: $spooldir - $!\n";
# open L,">>$logdir/cleanup.log";
# clean up regular spool
opendir $spooldir,'.' or die "$0: $spooldir - $!\n";
while ($to = readdir $spooldir) {
next if $to =~ /^\./;
next if $to !~ /@/ or $_ = readlink($to) and not /\//;
next unless -d $to;
if (@demo and -f "$to/.demo" and time > lmtime("$to/.demo")+$demo[1]*DS) {
logdel($to,"demo user $to deleted");
next;
}
unless (opendir TO,$to) {
warn "$0: $spooldir/$to - $!\n";
next;
}
while ($from = readdir TO) {
next if $from !~ /@/;
if ($from eq '@GROUP') {
foreach $group (glob "$to/$from/*") {
if (readlink $group and not -f $group) {
logdel($group,"$group deleted (master has gone)");
}
}
} else {
if (-d "$to/$from" and $from !~ /^\./) {
unless (opendir FROM,"$to/$from") {
warn "$0: $spooldir/$to/$from - $!\n";
next;
}
while ($file = readdir FROM) {
next if $file eq '.' or $file eq '..';
if (-d "$to/$from/$file" and $file !~ /^\./) {
cleanup($to,$from,$file);
rmdir "$to/$from/$file" unless $opt_d;
}
}
closedir FROM;
rmdir "$to/$from" unless $opt_d;
}
}
}
closedir TO;
unless (-f "$to/\@PERSISTENT" or $to eq $admin) {
@glob = glob "$to/*/* $to/\@MAINUSER/* $to/\@GROUP/*";
unless (@glob or -f "$to/\@") {
logdel($to,"$to deleted");
}
$user = $to;
if ($login_check and -l "$user/.login") {
my $lc = &$login_check(readlink("$user/.login"));
if ($lc) {
if (-f "$user/\@~" and not "$user/@") {
rename "$user/\@~","$user/@" unless $opt_d;
logv("$user reanimated (login_check)");
}
} else {
rename "$user/@","$user/\@~" unless $opt_d;
logv("$user deactivated (login_check)");
}
}
}
}
closedir $spooldir;
# clean up download key lookup directory
if (chdir $dkeydir and opendir D,'.') {
while ($file = readdir D) {
if ($link = readlink $file and
(not -l "$link/dkey" or readlink "$link/dkey" ne $file)) {
logdel($file,".dkeys/$file deleted");
}
}
closedir D;
}
# clean up upload key lookup directory
if (chdir $ukeydir and opendir D,'.') {
while ($file = readdir D) {
next if $file eq '.' or $file eq '..';
if (($link = readlink $file and not -e "$link/upload"
or -f $file and time > lmtime($file)+DS)) {
logdel($file,".ukeys/$file deleted");
}
}
closedir D;
}
# clean up authorization key lookup directory
if (chdir $akeydir and opendir D,'.') {
while ($file = readdir D) {
if (-l $file and time > (lmtime($file)||0)+DS) {
logdel($file,".akeys/$file deleted");
}
}
closedir D;
}
# clean up extra download key lookup directory
if (chdir $xkeydir and opendir D,'.') {
while ($file = readdir D) {
next if $file eq '.' or $file eq '..';
if (-l $file and not (-f "$file/upload" or -f "$file/data")) {
logdel($file,".xkeys/$file deleted");
}
}
closedir D;
}
# clean up lock directory
if (chdir $lockdir and opendir D,'.') {
while ($file = readdir D) {
if (-f $file and time > lmtime($file)+DS) {
logdel($file,".locks/$file deleted");
}
}
closedir D;
}
# clean up error directory
if (chdir "$spooldir/.error" and opendir D,'.') {
while ($file = readdir D) {
if (-f $file) {
$mtime = lmtime($file);
if ($mtime and $today > 10*$keep_default*DS+$mtime) {
if ($opt_d) { print "unlink .error/$file\n" }
else { logdel($file,".error/$file deleted") }
}
}
}
closedir D;
}
# clean up debug directory
if (chdir "$spooldir/.debug" and opendir D,'.') {
while ($file = readdir D) {
if (-f $file) {
$mtime = lmtime($file);
if ($mtime and $today > $keep_default*DS+$mtime) {
# logdel($file,".debug/$file deleted");
if ($opt_d) { print "unlink .debug/$file\n" }
else { unlink $file }
}
}
}
closedir D;
}
# clean up subuser keys directory
if (chdir $skeydir and opendir D,'.') {
while ($file = readdir D) {
if (-f $file and open F,$file) {
$delete = 1;
$from = $to = $id = '';
while (<F>) {
if (/^(\w+)=(.+)/) {
$from = $2 if $1 eq 'from';
$to = $2 if $1 eq 'to';
$id = $2 if $1 eq 'id';
}
}
close F;
if ($from and $to and $id and open F,"$spooldir/$to/\@SUBUSER") {
while (<F>) {
if (/^\Q$from:$id\E$/) {
$delete = 0;
last;
}
}
close F;
}
if ($delete) {
logdel($file,".skeys/$file deleted");
}
}
}
closedir D;
}
# clean up orphan subuser links
chdir $spooldir;
foreach $subuser (glob '*/@MAINUSER/*') {
if ($skey = readlink $subuser and not -f "$skeydir/$skey") {
logdel($subuser,"$subuser deleted");
}
}
foreach $subuser (glob '*/@MAINUSER') {
unlink $subuser unless $opt_d;
}
# clean up old OKEYs
chdir $spooldir;
foreach my $okey (glob '*/@OKEY/*') {
if (time > lmtime($okey)+30*DS) {
logdel($okey,"$okey deleted");
}
}
# clean up group keys directory
if (chdir $gkeydir and opendir D,'.') {
while ($gkey = readdir D) {
if (-f $gkey and open F,$gkey) {
$delete = 1;
$from = $group = $id = '';
while (<F>) {
if (/^(\w+)=(.+)/) {
$from = $2 if $1 eq 'from';
$group = $2 if $1 eq 'to';
$id = $2 if $1 eq 'id';
}
}
close F;
$group =~ s/^@//;
$gf = "$spooldir/$from/\@GROUP/$group";
if ($from and $group and $id and open F,$gf) {
while (<F>) {
if (/^\Q$from:$id\E$/) {
$delete = 0;
last;
}
}
close F;
}
if ($delete) {
logdel($gkey,".gkeys/$gkey deleted");
logdel($gf,"$gf deleted") if -l $gf;
}
}
}
closedir D;
}
# clean up self registration directory
if (chdir "$spooldir/.reg" and opendir D,'.') {
while ($file = readdir D) {
if (-f $file) {
$mtime = lmtime($file);
if ($mtime and $today > $mtime+DS) {
logdel($file,".reg/$file deleted");
}
}
}
closedir D;
}
# send account expiration warning
if ($account_expire and $account_expire =~ /^(\d+)/) {
my $expire = $1;
if (chdir $spooldir) {
chomp($admin_pw = slurp("$admin/\@")||'');
unless ($admin_pw) {
warn "create new fex account for $admin\n";
$admin_pw = randstring(8);
system("$FEXHOME/bin/fac -u $admin $admin_pw");
}
my $fid = "$FEXHOME/.fex/id";
unless (-f $fid) {
mkdir "$FEXHOME/.fex",0700;
if (open $fid,'>',$fid) {
if ($durl =~ m{(https?://.+?)/}) {
print {$fid} "$1\n";
} else {
print {$fid} "$hostname\n";
}
print {$fid} "$admin\n";
print {$fid} "$admin_pw\n";
close $fid;
} else {
warn"$0: cannot create $fid - $!";
}
}
chmod 0600,$fid;
opendir $spooldir,'.';
while ($user = readdir $spooldir) {
next unless -f "$user/\@";
next if -e "$user/$admin/reactivation.txt";
next if -e "$user/\@PERSISTENT";
next if $user !~ /@/ or -l $user;
next if $user =~ /^(fexmaster|fexmail)/ or $user eq $admin;
next if -l "$user/.login";
if (time > lmtime($user)+$expire*DS) {
# print "$spooldir/$user\n";
local $locale = readlink "$user/\@LOCALE";
$locale = 'english' unless $locale and $reactivation{$locale};
&{$reactivation{$locale}}($expire,$user);
sleep 1;
}
}
closedir $spooldir;
}
}
# vhosts
exit if $opt_V;
if (%vhost) {
foreach $vhost (keys %vhost) {
my $fexlib = $vhost{$vhost}.'/lib';
if (-f "$fexlib/fex.ph") {
warn "run $0 for $vhost :\n" if -t or $opt_v;
my $cmd = "HTTP_HOST=$vhost FEXLIB=$fexlib $_0 -V @_ARGV";
if ($opt_d) { print "$cmd\n" }
else { system $cmd }
}
}
}
if ($notify_newrelease and $notify_newrelease !~ /^no$/i
or not defined $notify_newrelease) {
$notify_newrelease ||= $admin;
$newnew = $new = '';
$snew = $FEXHOME.'/doc/new';
$new = slurp($snew)||'';
$_ = slurp("$FEXHOME/doc/version")||'';
if (/(\d+)/) { $qn = "new?$hostname:$1" }
else { $qn = "new?$hostname:0" }
print "checking for new F*EX release\n" if $opt_v;
for (1..3) {
sleep rand(10);
$newnew = `wget -qO- http://fex.belwue.de/$qn 2>/dev/null`;
last if $newnew =~ /release/;
# $newnew = `wget -qO- http://fex.rus.uni-stuttgart.de/$qn 2>/dev/null`;
# last if $newnew =~ /release/;
};
if ($newnew =~ /release/) {
if ($newnew ne $new) {
if (open $sendmail,"|$sendmail $notify_newrelease $bcc") {
pq($sendmail,qq(
'From: fex\@$hostname'
'To: $notify_newrelease'
'Subject: new F*EX release'
''
'$newnew'
));
close $sendmail;
if (open $snew,'>',$snew) {
print {$snew} $newnew;
close $snew;
}
}
}
}
}
exit;
# file clean up
sub cleanup {
my ($to,$from,$file) = @_;
my ($data,$download,$notify,$mtime,$warn,$dir,$filename,$dkey,$delay);
my $keep = $keep_default;
my $purge = $::purge || 3*$keep;
my $comment = '';
my $kf = "$to/$from/$file/keep";
my $ef = "$to/$from/$file/error";
local $_;
$keep = readlink $kf || readlink "$to/\@KEEP" || $keep_default;
$file = "$to/$from/$file";
$data = "$file/data";
$download = "$file/download";
$notify = "$file/notify";
if ($file =~ /\/ADDRESS_BOOK/) {
logdel($file,"$file deleted");
} elsif (-d $file and not -f $data) {
if ($mtime = lmtime("$file/upload")) {
if ($today > $mtime+DS) {
verbose("rmrf $file (today=$today mtime_upload=$mtime)");
logdel($file,"$file deleted");
}
} elsif ($mtime = lmtime("$file/error")) {
$purge = $1*$keep if $purge =~ /(\d+).*keep/;
if ($today > $purge*DS+$mtime) {
verbose("rmrf $file (today=$today mtime_error=$mtime keep=$keep purge=$purge)");
logdel($file,"$file deleted");
}
} else {
logdel($file,"$file deleted");
}
} elsif (-s $download and -s $data and autodelete($file) !~ /NO/i) {
$delay = autodelete($file);
$delay = 1 if $delay !~ /^\d+$/;
$delay--;
$mtime = lmtime($download);
if ($mtime and $today > $delay*DS+$mtime
and logdel($data,"$data deleted")) {
if (open $ef,'>',$ef) {
printf {$ef} "%s has been autodeleted after download at %s\n",
filename($file),isodate(lmtime($download));
close $ef;
}
}
} elsif (-f $data) {
my $reactivation = $file =~ m{/\Q$admin/reactivation.txt\E$};
$warn = $reactivation ? $keep-5 : $keep-2;
$mtime = lmtime("$file/filename") || lmtime($data) || 0;
if ($today > $mtime+$keep*DS) {
if ($account_expire and $reactivation) {
if ($account_expire =~ /delete/) {
logdel($to,"$to removed - expired");
} else {
if (open $sendmail,"|$sendmail $admin $bcc") {
$account_expire =~ /(\d+)/;
my $expire = $1 || 0;
pq($sendmail,qq(
'From: fex\@$hostname'
'To: $admin'
'Subject: user $to expired'
''
'F*EX user $to has been inactive for $expire days'
'and has ignored the account reactivation mail.'
'You may want to delete this account.'
));
close $sendmail;
unlink $data;
} else {
warn "$0: cannot send mail - $!\n";
}
}
} else {
if ($file =~ /^anonymous.*\/afex_\d/ or $to =~ /^_.+_/) {
# also _fexmail_*
logdel($file,"$file deleted") and
verbose("rmrf $file (today=$today mtime_upload=$mtime)");
} elsif (logdel($data,"$data deleted")) {
verbose("unlink $data (today=$today mtime=$mtime keep=$keep)");
if (open $ef,'>',$ef) {
$filename = $file;
$filename =~ s:.*/::;
print $ef "$filename is expired";
close $ef;
}
}
}
}
elsif ($file !~ /STDFEX$/ and
$mtime+$warn*DS < $today and
$dkey = readlink("$file/dkey") and
not -s $download and
not -f $notify and
(readlink("$to/\@REMINDER")||'yes') ne 'no')
{
my $locale = readlink "$to/\@LOCALE" || readlink "$file/\@LOCALE";
$locale = 'english' unless $locale and $notify{$locale};
if (open my $c,"$file/comment") {
chomp ($comment = <$c>||'');
close $c;
}
if (&{$notify{$locale}}(
status => 'remind',
dkey => $dkey,
filename => filename($file),
keep => $keep,
comment => $comment,
warn => int(($mtime-$today)/DS)+$keep,
autodelete => autodelete($file),
)) {
open $notify,'>',$notify;
close $notify;
print "sent reminder for $file\n" if -t or $opt_v;
} else {
warn "$0: reminder notification for $file failed\n";
}
}
}
}
sub autodelete {
my $file = shift;
my $adf = "$file/autodelete";
my $autodelete;
if (-l $adf) {
$autodelete = readlink $adf || '';
} elsif (open $adf,$adf) {
chomp($autodelete = <$adf>||'');
close $adf;
}
return $autodelete||$::autodelete;
}
sub logdel {
my ($file,$msg) = @_;
my $status = 0;
if ($opt_d) {
print "$msg\n";
} else {
if ($status = rmrf($file)) {
logv($msg);
} else {
logv("$file DEL FAILED : $!");
warn "$file DEL FAILED : $!\n" if -t or $opt_v;
}
}
return $status;
}
sub logv {
my $msg = shift;
print "$msg\n" if -t or $opt_v;
unless ($opt_d) {
foreach my $ld (@logdir) {
if (open my $log,">>$ld/cleanup.log") {
print {$log} "$isodate $msg\n";
close $log;
}
}
}
}
sub verbose {
local $_;
if ($opt_v) {
while ($_ = shift @_) {
s/\n*$/\n/;
print;
}
}
}
sub lmtime {
my @s = lstat(shift);
return @s?$s[9]:0;
}
|