be more verbose when asked for it
[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         addr = socket.getaddrinfo(domain, None, family=family)
51         return addr[0][4][0]
52     except socket.gaierror: # domain not found
53         return ""
54
55 def getCurIPv4(domain):
56     return getCurIP(domain, socket.AF_INET)
57
58 def getCurIPv6(domain):
59     return getCurIP(domain, socket.AF_INET6)
60
61 def update_domain(server, domain, ipv4, ipv6, password, verbose):
62     '''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.'''
63     assert ipv4 is not None or ipv6 is not None
64     
65     # check what the domain is currently mapped to
66     curIPv4 = getCurIPv4(domain)
67     curIPv6 = getCurIPv6(domain)
68     if verbose:
69         print("Current status of domain {0} is: IPv4 address '{1}', IPv6 address '{2}'".format(domain, curIPv4, curIPv6))
70     
71     # check if there's something to do
72     needUpdate = (ipv4 is not None and curIPv4 != ipv4) or (ipv6 is not None and curIPv6 != ipv6)
73     if not needUpdate:
74         if verbose:
75             print("Everything alread up-to-date, nothing to do")
76         return True
77
78     # we need to update the IP
79     url = 'https://'+server+'/update?password='+urllib.parse.quote(password)+'&domain='+urllib.parse.quote(domain)
80     expected = "good"
81     if ipv4 is not None:
82         url += '&ip='+urllib.parse.quote(ipv4)
83         expected += " "+ipv4
84     if ipv6 is not None:
85         url += '&ipv6='+urllib.parse.quote(ipv6)
86         expected += " "+ipv6
87     result = urlopen(url)
88     
89     # did everything go as planned?
90     if result == expected:
91         if verbose:
92             print("Successfully updated domain",domain)
93         # all went all right
94         return True
95     else:
96         # Something went wrong
97         print("Unexpected answer from server",server,"while updating",domain)
98         print(result)
99         return False
100
101 if __name__ == "__main__":
102     # allow overwriting some values on the command-line
103     parser = argparse.ArgumentParser(description='Update a domain managed by a dyn-nsupdate server')
104     parser.add_argument("-p", "--password",
105                         dest="password", default=password,
106                         help="The password used to update the domains")
107     parser.add_argument("-v", "--verbose",
108                         action="store_true", dest="verbose",
109                         help="Be more verbose")
110     parser.add_argument("domains",  metavar='DOMAIN', nargs='*', default=domains,
111                         help="The domains to update")
112     args = parser.parse_args()
113
114     # get our own IPv4
115     if haveIPv4:
116         myIPv4 = getMyIP(serverIPv4)
117         if args.verbose:
118             print("My IPv4 is",myIPv4)
119     else:
120         myIPv4 = None
121     # and IPv6
122     if haveIPv6:
123         myIPv6 = getMyIP(serverIPv6)
124         if args.verbose:
125             print("My IPv6 is",myIPv6)
126     else:
127         myIPv6 = None
128
129     # update all the domains
130     exitcode = 0
131     for domain in args.domains:
132         if not update_domain(server, domain, myIPv4, myIPv6, args.password, verbose=args.verbose):
133             exitcode = 1
134     sys.exit(exitcode)