3 # cleanup for F*EX service
5 # run this program via cron-job once at night!
7 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
14 use Digest::MD5 'md5_hex';
16 use constant DS => 60*60*24;
19 exit if $ENV{SCRIPT_NAME};
21 unless ($FEXLIB = $ENV{FEXLIB}) {
23 $FEXLIB = $ENV{FEXHOME}.'/lib';
24 } elsif (-f '/usr/share/fex/lib/fex.ph') {
25 $FEXLIB = '/usr/share/fex/lib';
27 $FEXLIB = dirname(dirname(abs_path($0))).'/lib';
29 $ENV{FEXLIB} = $FEXLIB;
31 die "$0: no FEXLIB\n" unless -r "$FEXLIB/fex.pp";
39 # use fex.ph for site configuration!
41 our ($spooldir,@logdir,$docdir);
42 our ($akeydir,$ukeydir,$dkeydir,$skeydir,$gkeydir,$xkeydir,$lockdir);
43 our ($durl,$debug,$autodelete,$hostname,$admin,$admin_pw,$bcc);
46 # load common code, local config : $HOME/lib/fex.ph
47 require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
49 my $logdir = $logdir[0];
52 # (needed for reminder and account reactivation e-mails)
53 foreach my $lf (glob "$FEXHOME/locale/*/lib/lf.pl") { require $lf }
55 # default locale functions (from fex.pp)
56 $notify{english} = \¬ify;
57 $reactivation{english} = \&reactivation;
61 $opt_v = $opt_V = $opt_d = 0;
63 $opt_v = $opt_d if $opt_d; # debug mode, no real action
66 $isodate = isodate($today);
68 chdir $spooldir or die "$0: $spooldir - $!\n";
69 # open L,">>$logdir/cleanup.log";
71 # clean up regular spool
72 opendir $spooldir,'.' or die "$0: $spooldir - $!\n";
73 while ($to = readdir $spooldir) {
75 next if $to !~ /@/ or $_ = readlink($to) and not /\//;
77 if (@demo and -f "$to/.demo" and time > lmtime("$to/.demo")+$demo[1]*DS) {
78 logdel($to,"demo user $to deleted");
81 unless (opendir TO,$to) {
82 warn "$0: $spooldir/$to - $!\n";
85 while ($from = readdir TO) {
87 if ($from eq '@GROUP') {
88 foreach $group (glob "$to/$from/*") {
89 if (readlink $group and not -f $group) {
90 logdel($group,"$group deleted (master has gone)");
94 if (-d "$to/$from" and $from !~ /^\./) {
95 unless (opendir FROM,"$to/$from") {
96 warn "$0: $spooldir/$to/$from - $!\n";
99 while ($file = readdir FROM) {
100 next if $file eq '.' or $file eq '..';
101 if (-d "$to/$from/$file" and $file !~ /^\./) {
102 cleanup($to,$from,$file);
103 rmdir "$to/$from/$file" unless $opt_d;
107 rmdir "$to/$from" unless $opt_d;
112 unless (-f "$to/\@PERSISTENT" or $to eq $admin) {
113 @glob = glob "$to/*/* $to/\@MAINUSER/* $to/\@GROUP/*";
114 unless (@glob or -f "$to/\@") {
115 logdel($to,"$to deleted");
118 if ($login_check and -l "$user/.login") {
119 my $lc = &$login_check(readlink("$user/.login"));
121 if (-f "$user/\@~" and not "$user/@") {
122 rename "$user/\@~","$user/@" unless $opt_d;
123 logv("$user reanimated (login_check)");
126 rename "$user/@","$user/\@~" unless $opt_d;
127 logv("$user deactivated (login_check)");
134 # clean up download key lookup directory
135 if (chdir $dkeydir and opendir D,'.') {
136 while ($file = readdir D) {
137 if ($link = readlink $file and
138 (not -l "$link/dkey" or readlink "$link/dkey" ne $file)) {
139 logdel($file,".dkeys/$file deleted");
145 # clean up upload key lookup directory
146 if (chdir $ukeydir and opendir D,'.') {
147 while ($file = readdir D) {
148 next if $file eq '.' or $file eq '..';
149 if (($link = readlink $file and not -e "$link/upload"
150 or -f $file and time > lmtime($file)+DS)) {
151 logdel($file,".ukeys/$file deleted");
157 # clean up authorization key lookup directory
158 if (chdir $akeydir and opendir D,'.') {
159 while ($file = readdir D) {
160 if (-l $file and time > (lmtime($file)||0)+DS) {
161 logdel($file,".akeys/$file deleted");
167 # clean up extra download key lookup directory
168 if (chdir $xkeydir and opendir D,'.') {
169 while ($file = readdir D) {
170 next if $file eq '.' or $file eq '..';
171 if (-l $file and not (-f "$file/upload" or -f "$file/data")) {
172 logdel($file,".xkeys/$file deleted");
178 # clean up lock directory
179 if (chdir $lockdir and opendir D,'.') {
180 while ($file = readdir D) {
181 if (-f $file and time > lmtime($file)+DS) {
182 logdel($file,".locks/$file deleted");
188 # clean up error directory
189 if (chdir "$spooldir/.error" and opendir D,'.') {
190 while ($file = readdir D) {
192 $mtime = lmtime($file);
193 if ($mtime and $today > 10*$keep_default*DS+$mtime) {
194 if ($opt_d) { print "unlink .error/$file\n" }
195 else { logdel($file,".error/$file deleted") }
202 # clean up debug directory
203 if (chdir "$spooldir/.debug" and opendir D,'.') {
204 while ($file = readdir D) {
206 $mtime = lmtime($file);
207 if ($mtime and $today > $keep_default*DS+$mtime) {
208 # logdel($file,".debug/$file deleted");
209 if ($opt_d) { print "unlink .debug/$file\n" }
210 else { unlink $file }
217 # clean up subuser keys directory
218 if (chdir $skeydir and opendir D,'.') {
219 while ($file = readdir D) {
220 if (-f $file and open F,$file) {
222 $from = $to = $id = '';
225 $from = $2 if $1 eq 'from';
226 $to = $2 if $1 eq 'to';
227 $id = $2 if $1 eq 'id';
231 if ($from and $to and $id and open F,"$spooldir/$to/\@SUBUSER") {
233 if (/^\Q$from:$id\E$/) {
241 logdel($file,".skeys/$file deleted");
248 # clean up orphan subuser links
250 foreach $subuser (glob '*/@MAINUSER/*') {
251 if ($skey = readlink $subuser and not -f "$skeydir/$skey") {
252 logdel($subuser,"$subuser deleted");
255 foreach $subuser (glob '*/@MAINUSER') {
256 unlink $subuser unless $opt_d;
261 foreach my $okey (glob '*/@OKEY/*') {
262 if (time > lmtime($okey)+30*DS) {
263 logdel($okey,"$okey deleted");
268 # clean up group keys directory
269 if (chdir $gkeydir and opendir D,'.') {
270 while ($gkey = readdir D) {
271 if (-f $gkey and open F,$gkey) {
273 $from = $group = $id = '';
276 $from = $2 if $1 eq 'from';
277 $group = $2 if $1 eq 'to';
278 $id = $2 if $1 eq 'id';
283 $gf = "$spooldir/$from/\@GROUP/$group";
284 if ($from and $group and $id and open F,$gf) {
286 if (/^\Q$from:$id\E$/) {
294 logdel($gkey,".gkeys/$gkey deleted");
295 logdel($gf,"$gf deleted") if -l $gf;
302 # clean up self registration directory
303 if (chdir "$spooldir/.reg" and opendir D,'.') {
304 while ($file = readdir D) {
306 $mtime = lmtime($file);
307 if ($mtime and $today > $mtime+DS) {
308 logdel($file,".reg/$file deleted");
315 # send account expiration warning
316 if ($account_expire and $account_expire =~ /^(\d+)/) {
318 if (chdir $spooldir) {
319 chomp($admin_pw = slurp("$admin/\@")||'');
321 warn "create new fex account for $admin\n";
322 $admin_pw = randstring(8);
323 system("$FEXHOME/bin/fac -u $admin $admin_pw");
325 my $fid = "$FEXHOME/.fex/id";
327 mkdir "$FEXHOME/.fex",0700;
328 if (open $fid,'>',$fid) {
329 if ($durl =~ m{(https?://.+?)/}) {
332 print {$fid} "$hostname\n";
334 print {$fid} "$admin\n";
335 print {$fid} "$admin_pw\n";
338 warn"$0: cannot create $fid - $!";
342 opendir $spooldir,'.';
343 while ($user = readdir $spooldir) {
344 next unless -f "$user/\@";
345 next if -e "$user/$admin/reactivation.txt";
346 next if -e "$user/\@PERSISTENT";
347 next if $user !~ /@/ or -l $user;
348 next if $user =~ /^(fexmaster|fexmail)/ or $user eq $admin;
349 next if -l "$user/.login";
351 if (time > lmtime($user)+$expire*DS) {
352 # print "$spooldir/$user\n";
353 local $locale = readlink "$user/\@LOCALE";
354 $locale = 'english' unless $locale and $reactivation{$locale};
355 &{$reactivation{$locale}}($expire,$user);
366 foreach $vhost (keys %vhost) {
367 my $fexlib = $vhost{$vhost}.'/lib';
368 if (-f "$fexlib/fex.ph") {
369 warn "run $0 for $vhost :\n" if -t or $opt_v;
370 my $cmd = "HTTP_HOST=$vhost FEXLIB=$fexlib $_0 -V @_ARGV";
371 if ($opt_d) { print "$cmd\n" }
377 if ($notify_newrelease and $notify_newrelease !~ /^no$/i
378 or not defined $notify_newrelease) {
379 $notify_newrelease ||= $admin;
381 $snew = $FEXHOME.'/doc/new';
382 $new = slurp($snew)||'';
383 $_ = slurp("$FEXHOME/doc/version")||'';
384 if (/(\d+)/) { $qn = "new?$hostname:$1" }
385 else { $qn = "new?$hostname:0" }
386 print "checking for new F*EX release\n" if $opt_v;
389 $newnew = `wget -qO- http://fex.belwue.de/$qn 2>/dev/null`;
390 last if $newnew =~ /release/;
391 # $newnew = `wget -qO- http://fex.rus.uni-stuttgart.de/$qn 2>/dev/null`;
392 # last if $newnew =~ /release/;
394 if ($newnew =~ /release/) {
395 if ($newnew ne $new) {
396 if (open $sendmail,"|$sendmail $notify_newrelease $bcc") {
398 'From: fex\@$hostname'
399 'To: $notify_newrelease'
400 'Subject: new F*EX release'
405 if (open $snew,'>',$snew) {
406 print {$snew} $newnew;
419 my ($to,$from,$file) = @_;
420 my ($data,$download,$notify,$mtime,$warn,$dir,$filename,$dkey,$delay);
422 my $keep = $keep_default;
423 my $kf = "$to/$from/$file/keep";
424 my $ef = "$to/$from/$file/error";
427 $keep = readlink $kf || readlink "$to/\@KEEP" || $keep_default;
429 $file = "$to/$from/$file";
430 $data = "$file/data";
431 $download = "$file/download";
432 $notify = "$file/notify";
434 if ($file =~ /\/ADDRESS_BOOK/) {
435 logdel($file,"$file deleted");
436 } elsif (-d $file and not -f $data) {
437 if ($mtime = lmtime("$file/upload")) {
438 if ($today > $mtime+DS) {
439 verbose("rmrf $file (today=$today mtime_upload=$mtime)");
440 logdel($file,"$file deleted");
442 } elsif ($mtime = lmtime("$file/error")) {
443 if ($today > 3*$keep*DS+$mtime) {
444 verbose("rmrf $file (today=$today mtime_error=$mtime keep=$keep)");
445 logdel($file,"$file deleted");
448 logdel($file,"$file deleted");
450 } elsif (-s $download and -s $data and autodelete($file) !~ /NO/i) {
451 $delay = autodelete($file);
452 $delay = 1 if $delay !~ /^\d+$/;
454 $mtime = lmtime($download);
455 if ($mtime and $today > $delay*DS+$mtime
456 and logdel($data,"$data deleted")) {
457 if (open $ef,'>',$ef) {
458 printf {$ef} "%s has been autodeleted after download at %s\n",
459 filename($file),isodate(lmtime($download));
464 my $reactivation = $file =~ m{/\Q$admin/reactivation.txt\E$};
465 $warn = $reactivation ? $keep-5 : $keep-2;
466 $mtime = lmtime("$file/filename") || lmtime($data) || 0;
467 if ($today > $mtime+$keep*DS) {
468 if ($account_expire and $reactivation) {
469 if ($account_expire =~ /delete/) {
470 logdel($to,"$to removed - expired");
472 if (open $sendmail,"|$sendmail $admin $bcc") {
473 $account_expire =~ /(\d+)/;
474 my $expire = $1 || 0;
476 'From: fex\@$hostname'
478 'Subject: user $to expired'
480 'F*EX user $to has been inactive for $expire days'
481 'and has ignored the account reactivation mail.'
482 'You may want to delete this account.'
487 warn "$0: cannot send mail - $!\n";
491 if ($file =~ /^anonymous.*\/afex_\d/ or $to =~ /^_.+_/) {
493 logdel($file,"$file deleted") and
494 verbose("rmrf $file (today=$today mtime_upload=$mtime)");
495 } elsif (logdel($data,"$data deleted")) {
496 verbose("unlink $data (today=$today mtime=$mtime keep=$keep)");
497 if (open $ef,'>',$ef) {
499 $filename =~ s:.*/::;
500 print $ef "$filename is expired";
506 elsif ($file !~ /STDFEX$/ and
507 $mtime+$warn*DS < $today and
508 $dkey = readlink("$file/dkey") and
511 (readlink("$to/\@REMINDER")||'yes') ne 'no')
513 my $locale = readlink "$to/\@LOCALE" || readlink "$file/\@LOCALE";
514 $locale = 'english' unless $locale and $notify{$locale};
515 if (open my $c,"$file/comment") {
516 chomp ($comment = <$c>||'');
519 if (&{$notify{$locale}}(
522 filename => filename($file),
525 warn => int(($mtime-$today)/DS)+$keep,
526 autodelete => autodelete($file),
528 open $notify,'>',$notify;
530 print "sent reminder for $file\n" if -t or $opt_v;
532 warn "$0: reminder notification for $file failed\n";
540 my $adf = "$file/autodelete";
544 $autodelete = readlink $adf || '';
545 } elsif (open $adf,$adf) {
546 chomp($autodelete = <$adf>||'');
550 return $autodelete||$::autodelete;
554 my ($file,$msg) = @_;
560 if ($status = rmrf($file)) {
563 logv("$file DEL FAILED : $!");
564 warn "$file DEL FAILED : $!\n" if -t or $opt_v;
575 print "$msg\n" if -t or $opt_v;
578 foreach my $ld (@logdir) {
579 if (open my $log,">>$ld/cleanup.log") {
580 print {$log} "$isodate $msg\n";
591 while ($_ = shift @_) {
600 my @s = lstat(shift);