summaryrefslogtreecommitdiff
path: root/src/conf_mode/host_name.py
blob: b0a4648c76ceefaff1f2be5becac9fb3abd7f1fc (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python3
#
# Copyright (C) 2018 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/>.
#
#

"""
conf-mode script for 'system host-name' and 'system domain-name'.
"""

import os
import re
import sys
import copy
import glob
import subprocess
import argparse
import jinja2

from vyos.config import Config
from vyos import ConfigError


parser = argparse.ArgumentParser()
parser.add_argument("--dhclient", action="store_true",
                    help="Started from dhclient-script")

config_file_hosts = '/etc/hosts'
config_file_resolv = '/etc/resolv.conf'

config_tmpl_hosts = """
### Autogenerated by host_name.py ###
127.0.0.1       localhost
127.0.1.1       {{ hostname }}{% if domain_name %}.{{ domain_name }}{% endif %}

# The following lines are desirable for IPv6 capable hosts
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

# static hostname mappings
{%- if static_host_mapping['hostnames'] %}
{% for hn in static_host_mapping['hostnames'] -%}
{{static_host_mapping['hostnames'][hn]['ipaddr']}}\t{{static_host_mapping['hostnames'][hn]['alias']}}\t{{hn}}
{% endfor -%}
{%- endif %}

### modifications from other scripts should be added below

"""

config_tmpl_resolv = """
### Autogenerated by host_name.py ###
{% for ns in nameserver -%}
nameserver {{ ns }}
{% endfor -%}

{%- if domain_name %}
domain {{ domain_name }}
{%- endif %}

{%- if domain_search %}
search {{ domain_search | join(" ") }}
{%- endif %}

"""

default_config_data = {
    'hostname': 'vyos',
    'domain_name': '',
    'domain_search': [],
    'nameserver': [],
    'no_dhcp_ns': False
}

# borrowed from: https://github.com/donjajo/py-world/blob/master/resolvconfReader.py, THX!
def get_resolvers(file):
    resolv = {}
    try:
        with open(file, 'r') as resolvconf:
            lines = [line.split('#', 1)[0].rstrip()
                     for line in resolvconf.readlines()]
            resolvers = [line.split()[1]
                         for line in lines if 'nameserver' in line]
            domains = [line.split()[1] for line in lines if 'search' in line]
            resolv['resolvers'] = resolvers
            resolv['domains'] = domains
        return resolv
    except IOError:
        return []


def get_config(arguments):
    conf = Config()
    hosts = copy.deepcopy(default_config_data)

    if arguments.dhclient:
        conf.exists = conf.exists_effective
        conf.return_value = conf.return_effective_value
        conf.return_values = conf.return_effective_values

    if conf.exists("system host-name"):
        hosts['hostname'] = conf.return_value("system host-name")

    if conf.exists("system domain-name"):
        hosts['domain_name'] = conf.return_value("system domain-name")
        hosts['domain_search'].append(hosts['domain_name'])

    for search in conf.return_values("system domain-search domain"):
        hosts['domain_search'].append(search)

    if conf.exists("system name-server"):
        hosts['nameserver'] = conf.return_values("system name-server")

    if conf.exists("system disable-dhcp-nameservers"):
        hosts['no_dhcp_ns'] = conf.exists('system disable-dhcp-nameservers')

    # system static-host-mapping
    hosts['static_host_mapping'] = {'hostnames': {}}

    if conf.exists('system static-host-mapping host-name'):
        for hn in conf.list_nodes('system static-host-mapping host-name'):
            hosts['static_host_mapping']['hostnames'][hn] = {
                'ipaddr': conf.return_value('system static-host-mapping host-name ' + hn + ' inet'),
                'alias': ''
            }

            if conf.exists('system static-host-mapping host-name ' + hn + ' alias'):
                a = conf.return_values(
                    'system static-host-mapping host-name ' + hn + ' alias')
                hosts['static_host_mapping']['hostnames'][hn]['alias'] = " ".join(a)

    return hosts


def verify(config):
    if config is None:
        return None

    # pattern $VAR(@) "^[[:alnum:]][-.[:alnum:]]*[[:alnum:]]$" ; "invalid host name $VAR(@)"
    hostname_regex = re.compile("^[A-Za-z0-9][-.A-Za-z0-9]*[A-Za-z0-9]$")
    if not hostname_regex.match(config['hostname']):
        raise ConfigError('Invalid host name ' + config["hostname"])

    # pattern $VAR(@) "^.{1,63}$" ; "invalid host-name length"
    length = len(config['hostname'])
    if length < 1 or length > 63:
        raise ConfigError(
            'Invalid host-name length, must be less than 63 characters')

    # The search list is currently limited to six domains with a total of 256 characters.
    # https://linux.die.net/man/5/resolv.conf
    if len(config['domain_search']) > 6:
        raise ConfigError(
            'The search list is currently limited to six domains')

    tmp = ' '.join(config['domain_search'])
    if len(tmp) > 256:
        raise ConfigError(
            'The search list is currently limited to 256 characters')

    # static mappings alias hostname
    if config['static_host_mapping']['hostnames']:
        for hn in config['static_host_mapping']['hostnames']:
            if not config['static_host_mapping']['hostnames'][hn]['ipaddr']:
                raise ConfigError('IP address required for ' + hn)
            for hn_alias in config['static_host_mapping']['hostnames'][hn]['alias'].split(' '):
                if not hostname_regex.match(hn_alias) and len(hn_alias) != 0:
                    raise ConfigError('Invalid hostname alias ' + hn_alias)

    return None


def generate(config):
    if config is None:
        return None

    # If "system disable-dhcp-nameservers" is __configured__ all DNS resolvers
    # received via dhclient should not be added into the final 'resolv.conf'.
    #
    # We iterate over every resolver file and retrieve the received nameservers
    # for later adjustment of the system nameservers
    dhcp_ns = []
    dhcp_sd = []
    for file in glob.glob('/etc/resolv.conf.dhclient-new*'):
        for key, value in get_resolvers(file).items():
            ns = [r for r in value if key == 'resolvers']
            dhcp_ns.extend(ns)
            sd = [d for d in value if key == 'domains']
            dhcp_sd.extend(sd)

    if not config['no_dhcp_ns']:
        config['nameserver'] += dhcp_ns
        config['domain_search'] += dhcp_sd

    # We have third party scripts altering /etc/hosts, too.
    # One example are the DHCP hostname update scripts thus we need to cache in
    # every modification first - so changing domain-name, domain-search or hostname
    # during runtime works
    old_hosts = ""
    with open(config_file_hosts, 'r') as f:
        # Skips text before the beginning of our marker.
        # NOTE: Marker __MUST__ match the one specified in config_tmpl_hosts
        for line in f:
            if line.strip() == '### modifications from other scripts should be added below':
                break

        for line in f:
            # This additional line.strip() filters empty lines
            if line.strip():
                old_hosts += line

    # Add an additional newline
    old_hosts += '\n'

    tmpl = jinja2.Template(config_tmpl_hosts)
    config_text = tmpl.render(config)

    with open(config_file_hosts, 'w') as f:
        f.write(config_text)
        f.write(old_hosts)

    tmpl = jinja2.Template(config_tmpl_resolv)
    config_text = tmpl.render(config)
    with open(config_file_resolv, 'w') as f:
        f.write(config_text)

    return None


def apply(config):
    if config is None:
        return None

    fqdn = config['hostname']
    if config['domain_name']:
        fqdn += '.' + config['domain_name']

    # rsyslog runs into a race condition at boot time with systemd
    # restart rsyslog only if the hostname changed.
    hn = subprocess.check_output(['hostnamectl', '--static']).decode().strip()

    os.system("hostnamectl set-hostname --static {0}".format(fqdn.rstrip('.')))

    # Restart services that use the hostname
    if hn != fqdn:
        os.system("systemctl restart rsyslog.service")

    # If SNMP is running, restart it too
    if os.system("pgrep snmpd > /dev/null") == 0:
        os.system("systemctl restart snmpd.service")

    # restart pdns if it is used
    if os.system("/usr/bin/rec_control ping >/dev/null 2>&1") == 0:
        os.system("/etc/init.d/pdns-recursor restart >/dev/null")

    return None


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