#!/usr/bin/python2.7 """Dynamic DNS script. Expects URLs from routers in the form https://info.colgarra.priv.at/dyndns/dyndns.py?username=&password=&hostname=&myip= """ import re import cgi import pwd from subprocess import call import ipaddr # Configuration PASSWORD = 'hygCithOrs5' ZONE = '.dyn.colgarra.priv.at' DEBUG = False # Just for debugging: if DEBUG: import cgitb cgitb.enable() fields = cgi.FieldStorage() # the following fields are supported by most dyndns providers # if a parameter is not provided, the .getvalue method returns None username = fields.getvalue('username') password = fields.getvalue('password') hostname = fields.getvalue('hostname') myip = fields.getvalue('myip') wildcard = fields.getvalue('wildcard') mx = fields.getvalue('mx') backmx = fields.getvalue('backmx') offline = fields.getvalue('offline') try: # check username user_info = pwd.getpwnam(username) # returns a key error if the user does not exist if user_info.pw_uid < 1000: raise RuntimeError('Invalid user name') # check password if password != PASSWORD: raise RuntimeError('Invalid password') # check hostname if re.match(r'[-0-9a-z]+(\.[-0-9a-z]+)*$', hostname) is None: raise RuntimeError('Invalid host name') # strip zone hostname = hostname.strip() if hostname.endswith(ZONE): hostname = hostname[:-len(ZONE)] # check IP address ip = ipaddr.IPAddress(myip) # throws axception if the IP address is not valid if isinstance(ip, ipaddr.IPv4Address): type = 'A' elif isinstance(ip, ipaddr.IPv6Address): type = 'AAAA' else: raise RuntimeError('Unknown IP address type') # access granted print "Content-Type: text/html" print call(['sudo', '/usr/local/bin/nsupdate_dyndns', hostname, myip, type]) print "OK" except: # access denied print "Content-Type: text/html" print "Status: 403 Forbidden" print print "Denied"