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'
25 # Base class for our exceptions
26 class DynDnsError(Exception):
29 class AuthError(DynDnsError):
30 returncode = 'badauth'
32 class CredentialsMissing(AuthError):
35 class UsernameMissing(AuthError):
38 class UsernameInvalid(AuthError):
41 class PasswordMissing(AuthError):
44 class PasswordWrong(AuthError):
47 class AuthWrongMethod(AuthError):
48 returncode = 'wrongauthmethod' # not documented at dyn.com
50 class AuthBasicError(AuthError):
51 returncode = 'authbasicerror' # not documented at dyn.com
53 class HostnameError(DynDnsError):
54 returncode = 'notfqdn'
56 class HostnameMissing(HostnameError):
59 class PasswordWrong(HostnameError):
62 class MyipError(DynDnsError):
63 returncode = 'badip' # not documented at dyn.com
65 class MyipMissing(MyipError):
68 class MyipInvalid(MyipError):
71 class OfflineInvalid(DynDnsError):
72 returncode = 'badparam' # not documented at dyn.com
74 class NsupdateError(DynDnsError):
78 fields = cgi.FieldStorage()
80 # the following fields are supported by most dyndns providers
81 # if a parameter is not provided, the .getvalue method returns None
82 username = fields.getvalue('username')
83 password = fields.getvalue('password')
84 hostname = fields.getvalue('hostname')
85 myip = fields.getvalue('myip')
86 wildcard = fields.getvalue('wildcard')
87 mx = fields.getvalue('mx')
88 backmx = fields.getvalue('backmx')
89 offline = fields.getvalue('offline')
90 system = fields.getvalue('system')
91 url = fields.getvalue('url')
96 auth = os.environ.get('HTTP_AUTHORIZATION') # auth == 'Basic cGhpbGlwcDpka2ZhamRrZg=='
97 if auth: # empty string if HTTP_AUTHORIZATION not present
98 auth_parts = auth.split(' ')
100 if len(auth_parts) != 2 or auth_parts[0] != auth_method:
101 raise AuthWrongMethod()
103 auth_decoded = base64.b64decode(auth_parts[1]) # auth_decoded == 'philipp:dkfajdkf'
105 raise AuthBasicError()
106 auth_decoded_parts = auth_decoded.split(':')
107 if len(auth_decoded_parts) != 2:
108 raise AuthBasicError()
109 username, password = auth_decoded_parts
111 # check username and password
112 if username is None and password is None:
113 raise CredentialsMissing()
117 raise UsernameMissing()
119 user_info = pwd.getpwnam(username)
121 raise UsernameInvalid()
122 if user_info.pw_uid < 1000:
123 raise UsernameInvalid()
127 raise PasswordMissing()
128 if password != PASSWORD:
129 raise PasswordWrong()
133 raise HostnameMissing()
134 if re.match(r'[-0-9a-z]+(\.[-0-9a-z]+)*$', hostname) is None:
135 raise HostnameInvalid()
138 hostname = hostname.strip()
141 if offline is None or offline.lower() == 'no':
143 elif offline.lower() == 'yes':
146 raise OfflineInvalid()
151 # try HTTP_X_FORWARDED_FOR
152 myip = os.environ.get('HTTP_X_FORWARDED_FOR')
153 if not myip: # empty string if not present
155 myip = os.environ.get('REMOTE_ADDR')
156 if not myip: # empty string if not present
160 ipaddr.IPAddress(myip) # throws an exception if the IP address is not valid
165 call_params = ['sudo', 'tdyndns_update']
167 call_params.append('--delete')
169 call_params.extend(['--ip', myip])
170 call_params.append(hostname)
171 retcode = call(call_params)
173 raise NsupdateError()
176 print "Content-Type: text/html"
179 # Note: we should return 'nochg' in case the IP has not changed, however we don't know yet.
182 except CredentialsMissing as error:
183 print "Content-Type: text/html"
184 print "Status: 401 Unauthorized"
185 print "WWW-Authenticate: Basic realm='tdyndns'"
188 except DynDnsError as error:
189 print "Content-Type: text/html"
190 print "Status: 200 OK"
192 print error.returncode