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) {
74 next if $to !~ /@/ or $_ = readlink($to) and not /\//;
75 if (@demo and -f "$to/.demo" and time > lmtime("$to/.demo")+$demo[1]*DS) {
76 logdel($to,"demo user $to deleted");
79 if (-d $to and $to !~ /^\./) {
80 unless (opendir TO,$to) {
81 warn "$0: $spooldir/$to - $!\n";
84 while ($from = readdir TO) {
86 if ($from eq '@GROUP') {
87 foreach $group (glob "$to/$from/*") {
88 if (readlink $group and not -f $group) {
89 logdel($group,"$group deleted (master has gone)");
93 if (-d "$to/$from" and $from !~ /^\./) {
94 unless (opendir FROM,"$to/$from") {
95 warn "$0: $spooldir/$to/$from - $!\n";
98 while ($file = readdir FROM) {
99 next if $file eq '.' or $file eq '..';
100 if (-d "$to/$from/$file" and $file !~ /^\./) {
101 cleanup($to,$from,$file);
102 rmdir "$to/$from/$file" unless $opt_d;
106 rmdir "$to/$from" unless $opt_d;
111 unless (-f "$to/\@PERSISTENT" or $to eq $admin) {
112 @glob = glob "$to/*/* $to/\@MAINUSER/* $to/\@GROUP/*";
113 unless (@glob or -f "$to/\@") {
114 logdel($to,"$to deleted");
117 if ($login_check and -l "$user/.login") {
118 my $lc = &$login_check(readlink("$user/.login"));
120 if (-f "$user/\@~" and not "$user/@") {
121 rename "$user/\@~","$user/@" unless $opt_d;
122 logv("$user reanimated (login_check)");
125 rename "$user/@","$user/\@~" unless $opt_d;
126 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 my $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" }
388 $newnew = `wget -qO- http://fex.belwue.de/$qn 2>/dev/null`;
389 last if $newnew =~ /release/;
390 # $newnew = `wget -qO- http://fex.rus.uni-stuttgart.de/$qn 2>/dev/null`;
391 # last if $newnew =~ /release/;
393 if ($newnew =~ /release/) {
394 if ($newnew ne $new) {
395 if (open $sendmail,"|$sendmail $notify_newrelease $bcc") {
397 'From: fex\@$hostname'
398 'To: $notify_newrelease'
399 'Subject: new F*EX release'
404 if (open $snew,'>',$snew) {
405 print {$snew} $newnew;
418 my ($to,$from,$file) = @_;
419 my ($data,$download,$notify,$mtime,$warn,$dir,$filename,$dkey,$delay);
421 my $keep = $keep_default;
422 my $kf = "$to/$from/$file/keep";
423 my $ef = "$to/$from/$file/error";
426 $keep = readlink $kf || readlink "$to/\@KEEP" || $keep_default;
428 $file = "$to/$from/$file";
429 $data = "$file/data";
430 $download = "$file/download";
431 $notify = "$file/notify";
433 if ($file =~ /\/ADDRESS_BOOK/) {
434 logdel($file,"$file deleted");
435 } elsif (-d $file and not -f $data) {
436 if ($mtime = lmtime("$file/upload")) {
437 if ($today > $mtime+DS) {
438 verbose("rmrf $file (today=$today mtime_upload=$mtime)");
439 logdel($file,"$file deleted");
441 } elsif ($mtime = lmtime("$file/error")) {
442 if ($today > 3*$keep*DS+$mtime) {
443 verbose("rmrf $file (today=$today mtime_error=$mtime keep=$keep)");
444 logdel($file,"$file deleted");
447 logdel($file,"$file deleted");
449 } elsif (-s $download and -s $data and autodelete($file) !~ /NO/i) {
450 $delay = autodelete($file);
451 $delay = 1 if $delay !~ /^\d+$/;
453 $mtime = lmtime($download);
454 if ($mtime and $today > $delay*DS+$mtime
455 and logdel($data,"$data deleted")) {
456 if (open $ef,'>',$ef) {
457 printf {$ef} "%s has been autodeleted after download at %s\n",
458 filename($file),isodate(lmtime($download));
463 my $reactivation = $file =~ m{/\Q$admin/reactivation.txt\E$};
464 $warn = $reactivation ? $keep-5 : $keep-2;
465 $mtime = lmtime("$file/filename") || lmtime($data) || 0;
466 if ($today > $mtime+$keep*DS) {
467 if ($account_expire and $reactivation) {
468 if ($account_expire =~ /delete/) {
469 logdel($to,"$to removed - expired");
471 if (open $sendmail,"|$sendmail $admin $bcc") {
472 $account_expire =~ /(\d+)/;
473 my $expire = $1 || 0;
475 'From: fex\@$hostname'
477 'Subject: user $to expired'
479 'F*EX user $to has been inactive for $expire days'
480 'and has ignored the account reactivation mail.'
481 'You may want to delete this account.'
486 warn "$0: cannot send mail - $!\n";
490 if ($file =~ /^anonymous.*\/afex_\d/ or $to =~ /^_.+_/) {
492 logdel($file,"$file deleted") and
493 verbose("rmrf $file (today=$today mtime_upload=$mtime)");
494 } elsif (logdel($data,"$data deleted")) {
495 verbose("unlink $data (today=$today mtime=$mtime keep=$keep)");
496 if (open $ef,'>',$ef) {
498 $filename =~ s:.*/::;
499 print $ef "$filename is expired";
505 elsif ($file !~ /STDFEX$/ and
506 $mtime+$warn*DS < $today and
507 $dkey = readlink("$file/dkey") and
510 (readlink("$to/\@REMINDER")||'yes') ne 'no')
512 my $locale = readlink "$to/\@LOCALE" || readlink "$file/\@LOCALE";
513 $locale = 'english' unless $locale and $notify{$locale};
514 if (open my $c,"$file/comment") {
515 chomp ($comment = <$c>||'');
518 if (&{$notify{$locale}}(
521 filename => filename($file),
524 warn => int(($mtime-$today)/DS)+$keep,
525 autodelete => autodelete($file),
527 open $notify,'>',$notify;
529 print "sent reminder for $file\n" if -t or $opt_v;
531 warn "$0: reminder notification for $file failed\n";
539 my $adf = "$file/autodelete";
543 $autodelete = readlink $adf || '';
544 } elsif (open $adf,$adf) {
545 chomp($autodelete = <$adf>||'');
549 return $autodelete||$::autodelete;
553 my ($file,$msg) = @_;
559 if ($status = rmrf($file)) {
562 logv("$file DEL FAILED : $!");
563 warn "$file DEL FAILED : $!\n" if -t or $opt_v;
574 print "$msg\n" if -t or $opt_v;
577 foreach my $ld (@logdir) {
578 if (open my $log,">>$ld/cleanup.log") {
579 print {$log} "$isodate $msg\n";
590 while ($_ = shift @_) {
599 my @s = lstat(shift);