#!/usr/bin/perl # Ri404 v1.0. Copyright 1998 Jon Hedley. All Rights Reserved. # http://www.cgi.tj # Do not distribute without express written permission of Jon Hedley. # Usage is free as long as you link to me: see http://www.cgi.tj/links/ # for the exact details. ################################################### # INSTALLATION NOTES # # # # 1) Set first line to path of the Perl 5 # # Interpreter. # # 2) Set $logpath to the world writable text # # file to log to. Comment it out to disable # # logging. # # 3) Set $mailprog to the path of your sendmail # # compatible mailer. # # 4) Set $email to the email address to send # # to. Comment out to disable email feature. # # 5) If you wish to disable ENV Report in email, # # comment out $send_env. # # 6) After the "END" token insert RAW HTML # # to be printed out to browser on 404 error. # ################################################### $logpath = '/home/yutopian/404/log.txt'; $mailprog = '/usr/sbin/sendmail'; # $email = 'info@yutopian.com'; $send_env = 'yes'; # First, Send Email if enabled: if ($email and $mailprog) { open(MAIL,"|$mailprog -t") or error("Could not open mailer: $!"); print MAIL "To: $email\n"; print MAIL "From: $email (Ri 404)\n"; print MAIL "Subject: Ri 404 Report\n"; print MAIL "X-Priority: 1 (Highest)\n\n"; print MAIL "Flags tripped for attempted access to $ENV{'REDIRECT_URL'}\n"; print MAIL "by $ENV{'REMOTE_HOST'}.\n\n"; if ($ENV{'HTTP_REFERER'}) { @terms = split(/\//,$ENV{'HTTP_REFERER'}); print MAIL "Visitor linked in from $ENV{'HTTP_REFERER'}. "; print MAIL "You may wish to contact the administrator of $terms[0]//$terms[2].\n\n"; } else {print MAIL "No referring URL was logged in association with this visit.\n\n";} if ($send_env) { print MAIL "Details follow:\n\n"; foreach $key (sort keys %ENV) {print MAIL "$key: $ENV{$key}\n";} } close(MAIL) or error("Could not send mail: $? $!"); } # Secondly, log error if enabled: if ($logpath) { unless (-e $logpath) {error("$logpath does not exist.")} unless (-W $logpath) {error("$logpath is not world writable.")} ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(time); $mon++; if (length($mday) == 1) {$mday = 0 . $mday} if (length($mon) == 1) {$mon = 0 . $mon} if (length($hour) == 1) {$hour = 0 . $hour} if (length($min) == 1) {$min = 0 . $min} $year = $year + 1900; $dt = "$year$mon$mday.$hour$min"; $line = "$dt\t $ENV{'REDIRECT_URL'}\t $ENV{'HTTP_REFERER'}\t $ENV{'REMOTE_HOST'}\n"; open(FILE, ">>$logpath") or error("Could not open $logpath for append: $!"); flock(FILE,2) or error("Could not lock file $logpath: $!"); print FILE $line; close(FILE); } # Finally, output HTML to browser (Content after "END" token below) print "Content-Type: text/html\n\n"; while () {print} sub error { my $error = $_[0]; print "Content-Type: text/html\n\n"; print "

Error

$error\n\n"; exit(0); } __END__