2 """Dynamic DNS script. Expects URLs from routers in the form
3 http://dyndns.colgarra.priv.at/nic/update??username=<username>&password=<pass>&hostname=<domain>&myip=<ipaddr>
10 from subprocess import call
15 PASSWORD = 'hygCithOrs5'
16 ZONE = '.dyn.colgarra.priv.at'
26 # Base class for our exceptions
27 class DynDnsError(Exception):
30 class AuthError(DynDnsError):
31 returncode = 'badauth'
33 class CredentialsMissing(AuthError):
36 class UsernameMissing(AuthError):
39 class UsernameInvalid(AuthError):
42 class PasswordMissing(AuthError):
45 class PasswordWrong(AuthError):
48 class AuthWrongMethod(AuthError):
49 returncode = 'wrongauthmethod' # not documented at dyn.com
51 class AuthBasicError(AuthError):
52 returncode = 'authbasicerror' # not documented at dyn.com
54 class HostnameError(DynDnsError):
55 returncode = 'notfqdn'
57 class HostnameMissing(HostnameError):
60 class PasswordWrong(HostnameError):
63 class MyipError(DynDnsError):
64 returncode = 'badip' # not documented at dyn.com
66 class MyipMissing(MyipError):
69 class MyipInvalid(MyipError):
72 class OfflineInvalid(DynDnsError):
73 returncode = 'badparam' # not documented at dyn.com
76 fields = cgi.FieldStorage()
78 # the following fields are supported by most dyndns providers
79 # if a parameter is not provided, the .getvalue method returns None
80 username = fields.getvalue('username')
81 password = fields.getvalue('password')
82 hostname = fields.getvalue('hostname')
83 myip = fields.getvalue('myip')
84 wildcard = fields.getvalue('wildcard')
85 mx = fields.getvalue('mx')
86 backmx = fields.getvalue('backmx')
87 offline = fields.getvalue('offline')
88 system = fields.getvalue('system')
89 url = fields.getvalue('url')
93 auth = os.environ.get('HTTP_AUTHORIZATION') # auth == 'Basic cGhpbGlwcDpka2ZhamRrZg=='
94 if auth: # empty string if HTTP_AUTHORIZATION not present
95 auth_parts = auth.split(' ')
97 if len(auth_parts) != 2 or auth_parts[0] != auth_method:
98 raise AuthWrongMethod()
100 auth_decoded = base64.b64decode(auth_parts[1]) # auth_decoded == 'philipp:dkfajdkf'
102 raise AuthBasicError()
103 auth_decoded_parts = auth_decoded.split(':')
104 if len(auth_decoded_parts) != 2:
105 raise AuthBasicError()
106 username, password = auth_decoded_parts
110 # check username and password
111 if username is None and password is None:
112 raise CredentialsMissing()
116 raise UsernameMissing()
118 user_info = pwd.getpwnam(username)
120 raise UsernameInvalid()
121 if user_info.pw_uid < 1000:
122 raise UsernameInvalid()
126 raise PasswordMissing()
127 if password != PASSWORD:
128 raise PasswordWrong()
132 raise HostnameMissing()
133 if re.match(r'[-0-9a-z]+(\.[-0-9a-z]+)*$', hostname) is None:
134 raise HostnameInvalid()
137 hostname = hostname.strip()
138 if hostname.endswith(ZONE):
139 hostname = hostname[:-len(ZONE)]
142 if offline is None or offline.lower() == 'no':
144 elif offline.lower() == 'yes':
147 raise OfflineInvalid()
154 ip = ipaddr.IPAddress(myip) # throws an exception if the IP address is not valid
157 if isinstance(ip, ipaddr.IPv4Address):
159 elif isinstance(ip, ipaddr.IPv6Address):
162 raise MyipInvalid() # should never happen
166 call(['sudo', '/usr/local/bin/nsupdate_dyndns_del', hostname, 'A'])
167 call(['sudo', '/usr/local/bin/nsupdate_dyndns_del', hostname, 'AAAA'])
169 call(['sudo', '/usr/local/bin/nsupdate_dyndns', hostname, myip, iptype])
172 print "Content-Type: text/html"
175 # Note: we should return 'nochg' in case the IP has not changed, however we don't know yet.
178 except CredentialsMissing as error:
179 print "Content-Type: text/html"
180 print "Status: 401 Unauthorized"
181 print "WWW-Authenticate: Basic realm='tdyndns'"
184 except DynDnsError as error:
185 print "Content-Type: text/html"
186 print "Status: 200 OK"
188 print error.returncode