]> ToastFreeware Gitweb - toast/tdyndns.git/blobdiff - cgi-bin/dyndns.py
update docs after renames
[toast/tdyndns.git] / cgi-bin / dyndns.py
index b2d33a710970c1603997db970ce51fa12730b055..5cbf1cee2d2ece5b71606fe3a94f0c478a111abf 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/python2.7
 """Dynamic DNS script. Expects URLs from routers in the form
-http://dyndns.colgarra.priv.at/nic/update??username=<username>&password=<pass>&hostname=<domain>&myip=<ipaddr>
+http://dyndns.colgarra.priv.at/nic/update?username=<username>&password=<pass>&hostname=<domain>&myip=<ipaddr>
 """
 import os
 import re
@@ -13,7 +13,6 @@ import ipaddr
 
 # Configuration
 PASSWORD = 'hygCithOrs5'
-ZONE = '.dyn.colgarra.priv.at'
 DEBUG = False
 
 
@@ -72,6 +71,9 @@ class MyipInvalid(MyipError):
 class OfflineInvalid(DynDnsError):
        returncode = 'badparam' # not documented at dyn.com
 
+class NsupdateError(DynDnsError):
+       returncode = 'nohost'
+
 
 fields = cgi.FieldStorage()
 
@@ -89,24 +91,23 @@ system   = fields.getvalue('system')
 url      = fields.getvalue('url')
 
 
-# Optional Auth Basic
-auth = os.environ.get('HTTP_AUTHORIZATION') # auth == 'Basic cGhpbGlwcDpka2ZhamRrZg=='
-if auth: # empty string if HTTP_AUTHORIZATION not present
-       auth_parts = auth.split(' ')
-       auth_method = 'Basic'
-       if len(auth_parts) != 2 or auth_parts[0] != auth_method:
-               raise AuthWrongMethod()
-       try:
-               auth_decoded = base64.b64decode(auth_parts[1]) # auth_decoded == 'philipp:dkfajdkf'
-       except TypeError:
-               raise AuthBasicError()
-       auth_decoded_parts = auth_decoded.split(':')
-       if len(auth_decoded_parts) != 2:
-               raise AuthBasicError()
-       username, password = auth_decoded_parts
-
-
 try:
+       # Optional Auth Basic
+       auth = os.environ.get('HTTP_AUTHORIZATION') # auth == 'Basic cGhpbGlwcDpka2ZhamRrZg=='
+       if auth: # empty string if HTTP_AUTHORIZATION not present
+               auth_parts = auth.split(' ')
+               auth_method = 'Basic'
+               if len(auth_parts) != 2 or auth_parts[0] != auth_method:
+                       raise AuthWrongMethod()
+               try:
+                       auth_decoded = base64.b64decode(auth_parts[1]) # auth_decoded == 'philipp:dkfajdkf'
+               except TypeError:
+                       raise AuthBasicError()
+               auth_decoded_parts = auth_decoded.split(':')
+               if len(auth_decoded_parts) != 2:
+                       raise AuthBasicError()
+               username, password = auth_decoded_parts
+
        # check username and password
        if username is None and password is None:
                raise CredentialsMissing()
@@ -135,8 +136,6 @@ try:
 
        # strip zone
        hostname = hostname.strip()
-       if hostname.endswith(ZONE):
-               hostname = hostname[:-len(ZONE)]
 
        # check offline
        if offline is None or offline.lower() == 'no':
@@ -149,24 +148,29 @@ try:
        # check IP address
        if not offline:
                if myip is None:
-                       raise MyipMissing()
+                       # try HTTP_X_FORWARDED_FOR
+                       myip = os.environ.get('HTTP_X_FORWARDED_FOR')
+                       if not myip: # empty string if not present
+                               # try REMOTE_ADDR
+                               myip = os.environ.get('REMOTE_ADDR')
+                               if not myip: # empty string if not present
+                                       raise MyipMissing()
+       if not myip is None:
                try:
-                       ip = ipaddr.IPAddress(myip) # throws an exception if the IP address is not valid
+                       ipaddr.IPAddress(myip) # throws an exception if the IP address is not valid
                except ValueError:
                        raise MyipInvalid()
-               if isinstance(ip, ipaddr.IPv4Address):
-                       iptype = 'A'
-               elif isinstance(ip, ipaddr.IPv6Address):
-                       iptype = 'AAAA'
-               else:
-                       raise MyipInvalid() # should never happen
 
        # update bind
+       call_params = ['sudo', 'nsupdate_dyndns']
        if offline:
-               call(['sudo', '/usr/local/bin/nsupdate_dyndns_del', hostname, 'A'])
-               call(['sudo', '/usr/local/bin/nsupdate_dyndns_del', hostname, 'AAAA'])
-       else:
-               call(['sudo', '/usr/local/bin/nsupdate_dyndns', hostname, myip, iptype])
+               call_params.append('--delete')
+       if myip is not None:
+               call_params.extend(['--ip', myip])
+       call_params.append(hostname)
+       retcode = call(call_params)
+       if retcode != 0:
+               raise NsupdateError()
 
        # return success
        print "Content-Type: text/html"