summaryrefslogtreecommitdiff
path: root/src/conf_mode/dynamic_dns.py
blob: d4e890ebb55f9d15d3fff89f9e4ad182ed90ab25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
#
# Copyright (C) 2018-2019 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import jinja2

from vyos.config import Config
from vyos import ConfigError

config_file = r'/etc/ddclient/ddclient.conf'
cache_file = r'/var/cache/ddclient/ddclient.cache'

config_tmpl = """
### Autogenerated by dynamic_dns.py ###
daemon=1m
syslog=yes
ssl=yes
pid=/var/run/ddclient/ddclient.pid
cache=/var/cache/ddclient/ddclient.cache

{% for interface in interfaces -%}

#
# ddclient configuration for interface "{{ interface.interface }}":
#
{% if interface.web_url -%}
use=web, web='{{ interface.web_url}}' {%- if interface.web_skip %}, web-skip='{{ interface.web_skip }}'{% endif %}
{% else -%}
use=if, if={{ interface.interface }}
{% endif -%}

{% for rfc in interface.rfc2136 -%}
{% for record in rfc.record %}
# RFC2136 dynamic DNS configuration for {{ record }}.{{ rfc.zone }}
server={{ rfc.server }}
protocol=nsupdate
password={{ rfc.keyfile }}
ttl={{ rfc.ttl }}
zone={{ rfc.zone }}
{{ record }}
{% endfor -%}
{% endfor -%}

{% for srv in interface.service %}
{% for host in srv.host %}
# DynDNS provider configuration for {{ host }}
protocol={{ srv.protocol }}
max-interval=28d
login={{ srv.login }}
password='{{ srv.password }}'
{% if srv.server -%}
server={{ srv.server }}
{% endif -%}
{{ host }}
{% endfor %}
{% endfor %}

{% endfor %}
"""

# Mapping of service name to service protocol
default_service_protocol = {
    'afraid': 'freedns',
    'changeip': 'changeip',
    'cloudflare': 'cloudflare',
    'dnspark': 'dnspark',
    'dslreports': 'dslreports1',
    'dyndns': 'dyndns2',
    'easydns': 'easydns',
    'namecheap': 'namecheap',
    'noip': 'noip',
    'sitelutions': 'sitelutions',
    'zoneedit': 'zoneedit1'
}

default_config_data = {
    'interfaces': [],
}

def get_config():
    dyndns = default_config_data
    conf = Config()
    if not conf.exists('service dns dynamic'):
        return None
    else:
        conf.set_level('service dns dynamic')

    for interface in conf.list_nodes('interface'):
        node = {
            'interface': interface,
            'rfc2136': [],
            'service': [],
            'web_skip': '',
            'web_url': ''
        }

        # set config level to e.g. "service dns dynamic interface eth0"
        conf.set_level('service dns dynamic interface {0}'.format(interface))

        # Handle RFC2136 - Dynamic Updates in the Domain Name System
        for rfc2136 in conf.list_nodes('rfc2136'):
            rfc = {
                'name': rfc2136,
                'keyfile': '',
                'record': [],
                'server': '',
                'ttl': '600',
                'zone': ''
            }

            if conf.exists('rfc2136 {0} key'.format(rfc2136)):
                rfc['keyfile'] = conf.return_value('rfc2136 {0} key'.format(rfc2136))

            if conf.exists('rfc2136 {0} record'.format(rfc2136)):
                rfc['record'] = conf.return_values('rfc2136 {0} record'.format(rfc2136))

            if conf.exists('rfc2136 {0} server'.format(rfc2136)):
                rfc['server'] = conf.return_value('rfc2136 {0} server'.format(rfc2136))

            if conf.exists('rfc2136 {0} ttl'.format(rfc2136)):
                rfc['ttl'] = conf.return_value('rfc2136 {0} ttl'.format(rfc2136))

            if conf.exists('rfc2136 {0} zone'.format(rfc2136)):
                rfc['zone'] = conf.return_value('rfc2136 {0} zone'.format(rfc2136))

            node['rfc2136'].append(rfc)

        # Handle DynDNS service providers
        for service in conf.list_nodes('service'):
            srv = {
                'provider': service,
                'host': [],
                'login': '',
                'password': '',
                'protocol': '',
                'server': '',
                'custom' : False
            }

            # preload protocol from default service mapping
            if service in default_service_protocol.keys():
                srv['protocol'] = default_service_protocol[service]
            else:
                srv['custom'] = True

            if conf.exists('service {0} login'.format(service)):
                srv['login'] = conf.return_value('service {0} login'.format(service))

            if conf.exists('service {0} host-name'.format(service)):
                srv['host'] = conf.return_values('service {0} host-name'.format(service))

            if conf.exists('service {0} protocol'.format(service)):
                srv['protocol'] = conf.return_value('service {0} protocol'.format(service))

            if conf.exists('service {0} password'.format(service)):
                srv['password'] = conf.return_value('service {0} password'.format(service))

            if conf.exists('service {0} server'.format(service)):
                srv['server'] = conf.return_value('service {0} server'.format(service))

            node['service'].append(srv)

        # Additional settings in CLI
        if conf.exists('use-web skip'):
            node['web_skip'] = conf.return_value('use-web skip')

        if conf.exists('use-web url'):
            node['web_url'] = conf.return_value('use-web url')

        dyndns['interfaces'].append(node)

    return dyndns

def verify(dyndns):
    # bail out early - looks like removal from running config
    if dyndns is None:
        return None

    # A 'node' corresponds to an interface
    for node in dyndns['interfaces']:

        # RFC2136 - configuration validation
        for rfc2136 in node['rfc2136']:
            if not rfc2136['record']:
                raise ConfigError('Set key for service "{0}" to send DDNS updates for interface "{1}"'.format(rfc2136['name'], node['interface']))

            if not rfc2136['zone']:
                raise ConfigError('Set zone for service "{0}" to send DDNS updates for interface "{1}"'.format(rfc2136['name'], node['interface']))

            if not rfc2136['keyfile']:
                raise ConfigError('Set keyfile for service "{0}" to send DDNS updates for interface "{1}"'.format(rfc2136['name'], node['interface']))
            else:
                if not os.path.isfile(rfc2136['keyfile']):
                    raise ConfigError('Keyfile for service "{0}" to send DDNS updates for interface "{1}" does not exist'.format(rfc2136['name'], node['interface']))

            if not rfc2136['server']:
                raise ConfigError('Set server for service "{0}" to send DDNS updates for interface "{1}"'.format(rfc2136['name'], node['interface']))

        # DynDNS service provider - configuration validation
        for service in node['service']:
            if not service['host']:
                raise ConfigError('Set host-name for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface']))

            if not service['login']:
                raise ConfigError('Set login for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface']))

            if not service['password']:
                raise ConfigError('Set password for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface']))

            if service['custom'] is True:
                if not service['protocol']:
                    raise ConfigError('Set protocol for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface']))

                if not service['server']:
                    raise ConfigError('Set server for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface']))

    return None

def generate(dyndns):
    # bail out early - looks like removal from running config
    if dyndns is None:
        return None

    tmpl = jinja2.Template(config_tmpl)

    config_text = tmpl.render(dyndns)
    with open(config_file, 'w') as f:
        f.write(config_text)

    return None

def apply(dyndns):
    if os.path.exists(cache_file):
        os.unlink(cache_file)

    if dyndns is None:
        os.system('/etc/init.d/ddclient stop')
    else:
        os.system('/etc/init.d/ddclient restart')

    return None

if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        sys.exit(1)