update client script to handle IPv6
[dyn-nsupdate.git] / client-scripts / dyn-ns-client
1 #!/usr/bin/python3
2 # Copyright (c) 2014, Ralf Jung <post@ralfj.de>
3 # All rights reserved.
4
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7
8 # 1. Redistributions of source code must retain the above copyright notice, this
9 #    list of conditions and the following disclaimer. 
10 # 2. Redistributions in binary form must reproduce the above copyright notice,
11 #    this list of conditions and the following disclaimer in the documentation
12 #    and/or other materials provided with the distribution.
13
14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25 # The views and conclusions contained in the software and documentation are those
26 # of the authors and should not be interpreted as representing official policies, 
27 # either expressed or implied, of the FreeBSD Project.
28
29 import urllib.request, socket, sys, argparse
30
31 # configuration variables
32 domains = ['domain.dyn.ralfj.de'] # list of domains to update
33 password = 'yourpassword'
34 haveIPv4 = True
35 haveIPv6 = False
36
37 serverIPv4 = 'ipv4.ns.ralfj.de' # Only needed if haveIPv4 is True. This server should NOT have an AAAA record!
38 serverIPv6 = 'ipv6.ns.ralfj.de' # Only needed if haveIPv6 is True. This server should NOT have an A record!
39 server     = 'ns.ralfj.de'
40 # END of configuration variables
41
42 def urlopen(url):
43     return urllib.request.urlopen(url).read().decode('utf-8').strip()
44
45 def getMyIP(server):
46     return urlopen('https://'+server+'/checkip')
47
48 def getCurIP(domain, family):
49     try:
50         return socket.getaddrinfo(domain, None, family=family)[0][4][0]
51     except socket.gaierror: # domain not found
52         return ""
53
54 def getCurIPv4(domain):
55     return getCurIP(domain, socket.AF_INET)
56
57 def getCurIPv6(domain):
58     return getCurIP(domain, socket.AF_INET6)
59
60 def update_domain(server, domain, ipv4, ipv6, password, verbose):
61     '''Update the given domain, using the server, password. ipv4 or ipv6 can be None to not update that record. Returns True on success, False on failure.'''
62     assert ipv4 is not None or ipv6 is not None
63     
64     # check what the domain is currently mapped to
65     curIPv4 = getCurIPv4(domain)
66     curIPv6 = getCurIPv6(domain)
67     
68     # check if there's something to do
69     needUpdate = (ipv4 is not None and curIPv4 != ipv4) or (ipv6 is not None and curIPv6 != ipv6)
70     if not needUpdate:
71         if verbose:
72             print("Everything alread up-to-date, nothing to do")
73         return True
74
75     # we need to update the IP
76     url = 'https://'+server+'/update?password='+urllib.parse.quote(password)+'&domain='+urllib.parse.quote(domain)
77     expected = "good"
78     if ipv4 is not None:
79         url += '&ip='+urllib.parse.quote(ipv4)
80         expected += " "+ipv4
81     if ipv6 is not None:
82         url += '&ipv6='+urllib.parse.quote(ipv6)
83         expected += " "+ipv6
84     result = urlopen(url)
85     
86     # did everything go as planned?
87     if result == expected:
88         if verbose:
89             print("Successfully updated domain",domain)
90         # all went all right
91         return True
92     else:
93         # Something went wrong
94         print("Unexpected answer from server",server,"while updating",domain)
95         print(result)
96         return False
97
98 if __name__ == "__main__":
99     # allow overwriting some values on the command-line
100     parser = argparse.ArgumentParser(description='Update a domain managed by a dyn-nsupdate server')
101     parser.add_argument("-p", "--password",
102                         dest="password", default=password,
103                         help="The password used to update the domains")
104     parser.add_argument("-v", "--verbose",
105                         action="store_true", dest="verbose",
106                         help="Be more verbose")
107     parser.add_argument("domains",  metavar='DOMAIN', nargs='*', default=domains,
108                         help="The domains to update")
109     args = parser.parse_args()
110
111     # get our own IPs
112     myIPv4 = getMyIP(serverIPv4) if haveIPv4 else None
113     myIPv6 = getMyIP(serverIPv6) if haveIPv6 else None
114
115     # update all the domains
116     exitcode = 0
117     for domain in args.domains:
118         if not update_domain(server, domain, myIPv4, myIPv6, args.password, verbose=args.verbose):
119             exitcode = 1
120     sys.exit(exitcode)