#!/usr/local/bin/perl ####################################################################### # Copyright 1998 Peter Janett # This script is written by Peter Janett, scripts@newmediaone.com. # This program CAN NOT be modified or distributed without the expressed # written consent of the author. # If you would like to modify or distribute this program, please email # us at scripts@newmediaone.com. ####################################################################### # This script is "Tipware", in that we are accepting tips for it's use. # Tips will help in the developent of new scripts, and offset costs of # supporting existing ones. Please mail your tip to the address below, # or our use your credit card at http://www.newmediaone.com/tips # Send your tips to: # New Media One Web Services # 3389 Hickok Pl. # Boulder, CO 80301 # Please send comments or suggestions to scripts@newmediaone.com. # This script works in conjuction with .htaccess and Apache servers, # to create custom error pages. It takes this a step further by # sending the webmaster an email, containing the attempted URL, browsers # used, and referring site, if available. # I included an auto setup routine, that should work with standard UNIX # setups. To run this setup, run this script, followed by "?setup". So, # if you installed this script in your cgi-bin, enter # http://www.yourdomain.com/cgi-bin/errorbot.cgi?setup into your browser. # This will create the nessesary files, including adding the ErrorDocument # lines to your .htaccess file. I have made every attempt to make this # script safe and server friendly, but I am not responsible for the results # of it's use. Script is provided as is, use at your own risk. ######################################################################## # I have attempted to make this work with a standard UNIX Apche server, # without any configuration. You may need to configure it yourself, or # you may choose to create and modify the files manually. The settings # below will be set automaticaly when the auto setup is run by entering # http://www.yourdomain.com/cgi-bin/errorbot.cgi?setup into your browser. # If this doesn't work, or you choose to set it up manually, the following # changes need to be made. ######################################################################## # Manual configuration instructions ######################################################################## # To disable the auto setup when configuring this script manually, make # to sure set "$disable_setup" to "yes" below. If you don't, no damage will # be done, but it prevents anyone from snooping around. The only thing # you MUST do to get email messages sent to you anytime there is an error # on your page is add these lines to your .htaccess file, in your root # directory. (If you don't already have a .htaccess file, create a text # file with the lines below it.) The other thing that may need to be # changed is the location sendmail on your system. ################## .htaccess file configuration #################### # You may need to adjust the path to this script in the # example lines below. Example lines: # ErrorDocument 401 /cgi-bin/errorbot.cgi?401 # ErrorDocument 403 /cgi-bin/errorbot.cgi?403 # ErrorDocument 404 /cgi-bin/errorbot.cgi?404 # ErrorDocument 500 /cgi-bin/errorbot.cgi?500 # (Note - Do not include the "#" at the begining of each line in your .htaccess # file.) ############################################################################ # The other thing you will probably want to do customize the error pages. # These are just standard HTML pages, which will now appear in place of the # generic "404 file not found", "500 script error" and "401 Access denied" # pages. If you didn't run the auto setup, create the HTML pages you want to # appear, and name them "error404.html", "error500.html", and "error401.html" # If you ran the auto setup, these pages already exist in your root directory. # Either way, you can edit these pages just as you would any other html page, # just make sure they are named "error404.html", "error500.html", and # "error401.html". If these html pages don't exist, or are deleted, this # script will output the default Error Robot pages when an error occours. ############################################################################ ############################################################################ # For most standard UNIX Apache setups, the settings below will not need to # be changed. You may want to look at the settings below, to make sure they # are correct for your system, and to set a few email preferences. ############################################################################ # First, we need to define a few basic variables # Diable Setup? Set to "yes" if you want the setup routine disabled $disable_setup = ''; # Location of sendmail. If you don't know this, ask your system administrator $sendmail = '/usr/lib/sendmail'; # This is the location of each type of error page # If you have trouble, replace $ENV{DOCUMENT_ROOT} with the server path to # your root http dir. $error404 = "$ENV{DOCUMENT_ROOT}/error404.html"; # File not found error page $error401 = "$ENV{DOCUMENT_ROOT}/error401.html"; # Resticted access error page $error500 = "$ENV{DOCUMENT_ROOT}/error500.html"; # Script error page $htaccess = "$ENV{DOCUMENT_ROOT}/.htaccess"; # Apache .htaccess file $errorbot_lock = "./errorbot.lock"; # The lock file for this script # Email address you want error messages sent. If left blank, emails will be # sent to the server administrator for the domain. # i.e. $email = 'webmaster@yourdomain.com'; $email = ''; # Do you want to be sent an email each time a password protected page is # accessed? # You have 3 choices here. # # Choose "0" (i.e. $send_401_messages = "0";) if you do not want to receive # any email from from accesses or errors to password protected pages. # # Choose "1" (i.e. $send_401_messages = "1";) if you want to receive an email # anytime there is a failed attempt (incorrect username or password is entered) # to access a password protected page. # # Choose "2" (i.e. $send_401_messages = "2";) if you want to receive an email # anytime there is an attempt to access a password protected page, regardless # if the access is sucessful (correct username and password are entered) OR # fails (incorrect username or password is entered). $send_401_messages = "1"; ######################################################################## ############ No configuing should be done beyond this point ############ ######################################################################## # Get the main URL, as a default redirect page and for setup $mainurl = 'http://'; $mainurl .= $ENV{"HTTP_HOST"}; # If no additional information is sent to the script, redirect to the main page if ($ENV{'QUERY_STRING'} eq ""){ print "Location: $mainurl\n\n"; exit; } # Use $ENV{"SERVER_ADMIN"} as email address, if none specified above if ($email eq ""){ $email = $ENV{"SERVER_ADMIN"}; } # End if ($email eq "") # Check to see if the setup is being run if ($ENV{'QUERY_STRING'} eq 'setup'){ #If setup has been disabled, redirect to the main page if ($disable_setup){ print "Location: $mainurl\n\n"; exit; } &setup; } # Send the webmaster an email based on option set in # "$send_401_messages" above if ($ENV{'QUERY_STRING'} eq '401'){ if ($send_401_messages eq "1"){ if ($ENV{'REMOTE_USER'} ne ""){ open (MAIL, "|$sendmail -t") || die "Can't open $mailprog!\n"; print MAIL "To: $email\n"; print MAIL "Reply-to: $email\n"; print MAIL "From: $email (Error Robot)\n"; print MAIL "Subject: [Error Robot] 401 - Failed restricted page access.\n"; print MAIL "X-Priority: 1 (Highest)\n\n"; print MAIL "401 - Failed restricted page access detected!\n\n"; print MAIL "User: $ENV{'REMOTE_USER'}\n"; print MAIL "Surfers IP: $ENV{'REMOTE_ADDR'}\n"; print MAIL "Attempted URL: $mainurl$ENV{'REDIRECT_URL'}\n"; print MAIL "Referring URL: $ENV{'HTTP_REFERER'}\n"; print MAIL "Browser used: $ENV{'HTTP_USER_AGENT'}\n"; close (MAIL); } # End if ($ENV{'REMOTE_USER'} ne "") } # End if ($send_401_messages eq "1") if ($send_401_messages eq "2"){ $subject = "Restricted page accessed"; if ($ENV{'REMOTE_USER'} ne ""){ $subject = "Failed restricted page access"; } # End if ($ENV{'REMOTE_USER'} ne "") open (MAIL, "|$sendmail -t") || die "Can't open $mailprog!\n"; print MAIL "To: $email\n"; print MAIL "Reply-to: $email\n"; print MAIL "From: $email (Error Robot)\n"; print MAIL "Subject: [Error Robot] 401 - $subject.\n"; print MAIL "X-Priority: 1 (Highest)\n\n"; print MAIL "401 - $subject detected!\n\n"; print MAIL "User: $ENV{'REMOTE_USER'}\n"; print MAIL "Surfers IP: $ENV{'REMOTE_ADDR'}\n"; print MAIL "Attempted URL: $mainurl$ENV{'REDIRECT_URL'}\n"; print MAIL "Referring URL: $ENV{'HTTP_REFERER'}\n"; print MAIL "Browser used: $ENV{'HTTP_USER_AGENT'}\n"; close (MAIL); } # End if ($send_401_messages eq "2") # Now, print the correct error page print "Content-type: text/html\n\n"; # If a custom error page exists, print it. If not, print a generic page. if (-s "$error401") { open(URL, "$error401") || die "I can't $!\n"; while() { print "$_"; } } # End (-s "$error401") else{ print<<"EEE"; 401 - Access Restricted
Access Restricted

This is a protected area. The server administrator has been notified of this attempted access. If you have misplaced your password, please contact us at $email.

Back to our main page.



This page generated by "Error Robot".
Written by Peter Janett.
© 1997-98 New Media One Web Services
EEE } exit; } # End if ($ENV{'QUERY_STRING'} eq '401') # For all other type of errors, we start with a generic mail header open (MAIL, "|$sendmail -t") || die "Can't open $mailprog!\n"; print MAIL "To: $email\n"; print MAIL "Reply-to: $email\n"; print MAIL "From: $email (Error Robot)\n"; print MAIL "Subject: [Error Robot] - $ENV{'QUERY_STRING'} error detected!\n"; print MAIL "X-Priority: 1 (Highest)\n\n"; if ($ENV{'QUERY_STRING'} eq '404'){ print MAIL "404 File not found error!\n\n"; print MAIL "Surfers IP: $ENV{'REMOTE_ADDR'}\n"; print MAIL "Attempted URL: $mainurl$ENV{'REDIRECT_URL'}\n"; print MAIL "Referring URL: $ENV{'HTTP_REFERER'}\n"; print MAIL "Browser used: $ENV{'HTTP_USER_AGENT'}\n"; close (MAIL); # Now, print the correct error page print "Content-type: text/html\n\n"; # If a custom error page exists, print it. If not, print a generic page. if (-s "$error404") { open(URL, "$error404") || die "I can't $!\n"; while() { print "$_"; } } # End (-s "$error404") else{ print<<"EEE"; 404 - File Not Found
File Not Found

The link you followed may be inaccurate or outdated. We have logged this error and will look into it right away.

Back to our main page.



This page generated by "Error Robot".
Written by Peter Janett.
© 1997-98 New Media One Web Services
EEE } exit; } elsif ($ENV{'QUERY_STRING'} eq '500'){ print MAIL "500 Script Error!\n\n"; print MAIL "Surfers IP: $ENV{'REMOTE_ADDR'}\n"; print MAIL "Attempted URL: $mainurl$ENV{'REDIRECT_URL'}\n"; print MAIL "Referring URL: $ENV{'HTTP_REFERER'}\n"; print MAIL "Browser used: $ENV{'HTTP_USER_AGENT'}\n"; close (MAIL); # Now, print the correct error page print "Content-type: text/html\n\n"; # If a custom error page exists, print it. If not, print a generic page. if (-s "$error500") { open(URL, "$error500") || die "I can't $!\n"; while() { print "$_"; } } # End (-s "$error500") else{ print<<"EEE"; 500 - Internal Server Error
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, $email and inform them of the time the error occurred, and anything you might have done that may have caused the error.

Back to our main page.



This page generated by "Error Robot".
Written by Peter Janett.
© 1997-98 New Media One Web Services
EEE } exit; } elsif ($ENV{'QUERY_STRING'} eq '403'){ print MAIL "403 Misc. Error!\n\n"; print MAIL "Surfers IP: $ENV{'REMOTE_ADDR'}\n"; print MAIL "Attempted URL: $mainurl$ENV{'REDIRECT_URL'}\n"; print MAIL "Referring URL: $ENV{'HTTP_REFERER'}\n"; print MAIL "Browser used: $ENV{'HTTP_USER_AGENT'}\n"; close (MAIL); # Now, print the correct error page print "Content-type: text/html\n\n"; # If a custom error page exists, print it. If not, print a generic page. if (-s "$error404") { open(URL, "$error404") || die "I can't $!\n"; while() { print "$_"; } } # End (-s "$error404") else{ print<<"EEE"; 404 - File Not Found
File Not Found

The link you followed may be inaccurate or outdated. We have logged this error and will look into it right away.

Back to our main page.



This page generated by "Error Robot".
Written by Peter Janett.
© 1997-98 New Media One Web Services
EEE } exit; } else { exit; print MAIL "Unknown Error!\n\n"; print MAIL "Surfers IP: $ENV{'REMOTE_ADDR'}\n"; print MAIL "Attempted URL: $mainurl$ENV{'REDIRECT_URL'}\n"; print MAIL "Referring URL: $ENV{'HTTP_REFERER'}\n"; print MAIL "Browser used: $ENV{'HTTP_USER_AGENT'}\n"; close (MAIL); # Now, print the correct error page print "Content-type: text/html\n\n"; # If a custom error page exists, print it. If not, print a generic page. if (-s "$error404") { open(URL, "$error404") || die "I can't $!\n"; while() { print "$_"; } } # End (-s "$error404") else{ print<<"EEE"; 404 - File Not Found
File Not Found

The link you followed may be inaccurate or outdated. We have logged this error and will look into it right away.

Back to our main page.



This page generated by "Error Robot".
Written by Peter Janett.
© 1997-98 New Media One Web Services
EEE } exit; } #################################################################### # Setup subroutine #################################################################### sub setup{ # The first thing we do is check to make sure setup hasn't been run # before. We do this by checking for the existence of a lock file, # errorbot.lock if (-s "$errorbot_lock") { print "Content-type: text/html\n\n"; print<<"EEE"; Error Robot Setup
Error Robot Setup

It appears that the Error Robot setup has been run before. For security reasons, in order to run the setup again, you need to delete the lock file, errorbot.lock, located in the same directory as this script, then run this setup again.

Back to our main page.



This page generated by "Error Robot".
Written by Peter Janett.
© 1997-98 New Media One Web Services
EEE exit; } # End if (-s "$errorbot_lock") # First, we check to see what server software is running $server = "$ENV{'SERVER_SOFTWARE'}"; # Start to print the results to the browser print "Content-type: text/html\n\n"; print<<"EEE"; Error Robot Setup
Error Robot Setup

Actions completed:

    EEE # Check to see if .htaccess exists if (-s "$htaccess") { # Open and grab contents of .htaccess open (URL, "$htaccess") or die ("can't open URL: $!"); @htlines = ; close (URL); # Open and overwrite .htaccess open (URL, ">$htaccess") or die ("can't open URL: $!"); foreach (@htlines) { # If an ErrorDocument call already exists, comment it out if ($_ =~ "ErrorDocument") { $_ = "# The following line has been comment by Error Robot\n#" . $_ ; } # Print commented out line to .htaccess print URL; } # Lastly, add the new lines print URL "ErrorDocument 401 $ENV{'SCRIPT_NAME'}?401\n"; print URL "ErrorDocument 403 $ENV{'SCRIPT_NAME'}?403\n"; print URL "ErrorDocument 404 $ENV{'SCRIPT_NAME'}?404\n"; print URL "ErrorDocument 500 $ENV{'SCRIPT_NAME'}?500\n"; close (URL); # Print "modified" message to browser print "
  • Modified .htaccess\n"; } # End (-s "$htaccess") # Create an .htaccess file if there isn't one else{ # Double check, just to make sure there is no .htaccess file # Check to see if .htaccess exists unless (-s "$htaccess") { # Create .htaccess file open (URL, ">$htaccess") or die ("can't open URL: $!"); # Add the new lines print URL "ErrorDocument 401 $ENV{'SCRIPT_NAME'}?401\n"; print URL "ErrorDocument 403 $ENV{'SCRIPT_NAME'}?403\n"; print URL "ErrorDocument 404 $ENV{'SCRIPT_NAME'}?404\n"; print URL "ErrorDocument 500 $ENV{'SCRIPT_NAME'}?500\n"; close (URL); # Print "modified" message to browser print "
  • Created .htaccess\n"; } # End unless (-s "$htaccess") } # End else ################################################################### # Add 401 page ################################################################### # Check to see if $error401 exists unless (-s "$error401") { # Write $error401 page open (URL, ">$error401") or die ("can't open URL: $!"); print URL<<"EEE"; 401 - Access Restricted
    Access Restricted

    This is a protected area. The server administrator has been notified of this attempted access. If you have misplaced your password, please contact us at $email.

    Back to our main page.



    This page generated by "Error Robot".
    Written by Peter Janett.
    © 1997-98 New Media One Web Services
    EEE close (URL); # Print "created" message to browser print "
  • Created 401 - Restricted access error page\n"; } # End (-s "$htaccess") else{ print "
  • 401 - Restricted access error page already exists\n"; } # End else ################################################################### # Add 404 page ################################################################### # Check to see if $error404 exists unless (-s "$error404") { # Write $error404 page open (URL, ">$error404") or die ("can't open URL: $!"); print URL<<"EEE"; 404 - File Not Found
    File Not Found

    The link you followed may be inaccurate or outdated. We have logged this error and will look into it right away.

    Back to our main page.



    This page generated by "Error Robot".
    Written by Peter Janett.
    © 1997-98 New Media One Web Services
    EEE close (URL); # Print "created" message to browser print "
  • Created 404 - File not found error page\n"; } # End (-s "$error404") else{ print "
  • 404 - File not found page already exists\n"; } # End else ################################################################### # Add 500 page ################################################################### # Check to see if $error500 exists unless (-s "$error500") { # Write $error500 page open (URL, ">$error500") or die ("can't open URL: $!"); print URL<<"EEE"; 500 - Internal Server Error
    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, $email and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    Back to our main page.



    This page generated by "Error Robot".
    Written by Peter Janett.
    © 1997-98 New Media One Web Services
    EEE close (URL); # Print "created" message to browser print "
  • Created 500 - Server error page\n"; } # End (-s "$error404") else{ print "
  • 500 - Server error page already exists\n"; } # End else ################################################################### # Add errorbot_lock page, to prevent setup from being rerun ################################################################### # Check to see if $errorbot_lock exists unless (-s "$errorbot_lock") { # Write $errorbot_lock page open (URL, ">$errorbot_lock") or die ("can't open URL: $!"); print URL<<"EEE"; Error Robot Lock File
    Error Robot Lock File

    This is a simple lock file for the "Error Robot".

    Back to our main page.



    This page generated by "Error Robot".
    Written by Peter Janett.
    © 1997-98 New Media One Web Services
    EEE close (URL); # Print "created" message to browser print "
  • Error Robot lock file created\n"; } # End (-s "$errorbot_lock") else{ print "
  • Error Robot lock file already exists\n"; } # End else print<<"EEE";

Server software detected: $server
Error email will be sent to: $email
To edit your error pages, edit the following HTML pages on your site:

If you would like to change the email address that errors are sent to, or be notified any time a password protected page is accessed, you can manually change these settings in the script.

You should also receive an email containing this information.



This page generated by "Error Robot".
Written by Peter Janett.
© 1997-98 New Media One Web Services
EEE # Lastly, send an email outlineing the setup settings open (MAIL, "|$sendmail -t") || die "Can't open $mailprog!\n"; print MAIL "To: $email\n"; print MAIL "Reply-to: $email\n"; print MAIL "From: $email (Error Robot)\n"; print MAIL "Subject: Error Robot setup completed\n\n"; print MAIL "Thanks for setting up the Error Robot script!\n\n"; print MAIL "Your settings have been automatically set to the default settings.\n"; print MAIL "These settings can be changed by manually configuring the script.\n\n"; print MAIL "Error email will be sent to: $email\n"; print MAIL "To edit your error pages, edit the following HTML pages on your site:\n\n"; print MAIL "$mainurl/error404.html\n"; print MAIL "(file not found error page)\n\n"; print MAIL "$mainurl/error401.html\n"; print MAIL "(restricted access error page)\n\n"; print MAIL "$mainurl/error500.html\n"; print MAIL "(server error page)\n\n"; print MAIL "A friendly reminder - this script is \"tipware\". This means the author is\n"; print MAIL "accepting tips for this script. Tips will help in the developent of new\n"; print MAIL "scripts, and offset costs of supporting this and other scripts.\n"; print MAIL "To send a tip, visit http://www.newmediaone.com/tips\n"; close (MAIL); exit; } # End sub setup exit;