Newer
Older
tperimeter / tperimeter.py
@tundra tundra on 29 Apr 2006 2 KB Program name change.
#!/usr/bin/env python
# tperimiter.py
# Copyright (c) 2006 TundraWare Inc.  All Rights Reserved.
# For Updates See:  http://www.tundraware.com/Software/tperimiter

# Program Information

PROGNAME = "perimiter.py"
RCSID = "$Id: tperimeter.py,v 1.102 2006/04/29 07:48:35 tundra Exp $"

import os, re, sys

#####
# Constants
#####

Basedir  = "/www/cgi-bin/perimeter/requests/" # Base directory to store requests
IPQuad   = r"(\d{1,3}\.){3}\d{1,3}$"          # Regex for IP Quads


#####
# Lookup Tables
#####

Forbidden = ["0.0.0.0", "255.255.255.255"]    # Forbidden IP addresses
Services  = ["imapd", "leafnode", "sshd"]     # Services we can open up


#####
# Print Routine
#####

def Print(s):

    print "[User@%s] %s" % (requestor, s)

#####
# Error/Abort Routine
#####

def Error(s):
    Print("ERROR: %s!<br>" % s)
    Print( "Please Try Again ...<br><br>")
    sys.exit(1)

# End of 'Error()'
    

#####
# Print The HTML Header
####

print "Content-type: text/html\n\n"

#####
# Process Passed Parameters
# Save locally in a variable of the same name
#####

try:
    requestor = os.environ["REMOTE_ADDR"]
    service = address = ""
    for argument in os.environ["QUERY_STRING"].split("&"):
        exec('%s="%s"' % tuple(argument.split("=")))

except:
    Error("Internal Program Error")

#####
# Validate Number And Content Of Passed Parameters
#####

# Make sure both parameters were passed

if not (service and address):
    Error("You Must Specify Both A Service Name And Address")


# Make sure requested service is one of the ones allowed

elif service not in Services:
    Error("You Are Not Permitted To Change Access To %s" % service)


# Make sure address is in quad format

if not re.match(IPQuad, address):
    Error("You Must Specify Address In IP Quad Format")
    

# Make sure each quad element is in range

for q in address.split("."):
    if not (0 <= int(q) <= 255):
        Error("IP Address Component Is Out Of Range (%s Not Between 0-255)" % q)

# Make sure address is not on the forbidden list

if address in Forbidden:
    Error("You Are Not Permitted To Enable Access For Address: %s" % address)


#####
# Output Content
#####

Print("Requested...<br>Access To Service: %s<br>For Address: %s<br>" % (service, address))


# Make sure there is a directory present to receive the request

reqdir = Basedir + service

try:
    os.makedirs(reqdir)
except:
    pass

# Now write the request there

reqfil = reqdir + os.sep + address
f=open(reqfil, "w")
f.close()