summaryrefslogtreecommitdiff
path: root/src/conf_mode/host_name.py
blob: 4fe7b4cbdcdfda84fba3c716b04c9ee04bbfcb1b (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
#!/usr/bin/env python3
#
# Copyright (C) 2018-2020 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 argparse
import jinja2

import vyos.util
import vyos.hostsd_client

from vyos.config import Config
from vyos import ConfigError
from vyos.util import cmd, call, run, process_named_running

from vyos import airbag
airbag.enable()

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

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

    if conf.exists("system host-name"):
        hosts['hostname'] = conf.return_value("system host-name")
        # This may happen if the config is not loaded yet,
        # e.g. if run by cloud-init
        if not hosts['hostname']:
            hosts['hostname'] = default_config_data['hostname']

    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'] = True

    # system static-host-mapping
    for hn in conf.list_nodes('system static-host-mapping host-name'):
        hosts['static_host_mapping'][hn] = {}
        hosts['static_host_mapping'][hn]['address'] = conf.return_value(f'system static-host-mapping host-name {hn} inet')
        hosts['static_host_mapping'][hn]['aliases'] = conf.return_values(f'system static-host-mapping host-name {hn} alias')

    return hosts


def verify(conf, hosts):
    if hosts 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(hosts['hostname']):
        raise ConfigError('Invalid host name ' + hosts["hostname"])

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

    all_static_host_mapping_addresses = []
    # static mappings alias hostname
    for host, hostprops in hosts['static_host_mapping'].items():
        if not hostprops['address']:
            raise ConfigError(f'IP address required for static-host-mapping "{host}"')
        if hostprops['address'] in all_static_host_mapping_addresses:
            raise ConfigError((
                f'static-host-mapping "{host}" address "{hostprops["address"]}"'
                f'already used in another static-host-mapping'))
        all_static_host_mapping_addresses.append(hostprops['address'])
        for a in hostprops['aliases']:
            if not hostname_regex.match(a) and len(a) != 0:
                raise ConfigError(f'Invalid alias "{a}" in static-host-mapping "{host}"')

    # TODO: add warnings for nameservers_dhcp_interfaces if interface doesn't
    # exist or doesn't have address dhcp(v6)

    return None


def generate(config):
    pass

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

    ## Send the updated data to vyos-hostsd

    # vyos-hostsd uses "tags" to identify data sources
    tag = "static"

    try:
        client = vyos.hostsd_client.Client()

        # Check if disable-dhcp-nameservers is configured, and if yes - delete DNS servers added by DHCP
        if config['no_dhcp_ns']:
            client.delete_name_servers('dhcp-.+')

        client.set_host_name(config['hostname'], config['domain_name'], config['domain_search'])

        client.delete_name_servers(tag)
        client.add_name_servers(tag, config['nameserver'])

        client.delete_hosts(tag)
        client.add_hosts(tag, config['static_host_mapping'])
    except vyos.hostsd_client.VyOSHostsdError as e:
        raise ConfigError(str(e))

    ## Actually update the hostname -- vyos-hostsd doesn't do that

    # No domain name -- the Debian way.
    hostname_new = config['hostname']

    # rsyslog runs into a race condition at boot time with systemd
    # restart rsyslog only if the hostname changed.
    hostname_old = cmd('hostnamectl --static')
    call(f'hostnamectl set-hostname --static {hostname_new}')

    # Restart services that use the hostname
    if hostname_new != hostname_old:
        call("systemctl restart rsyslog.service")

    # If SNMP is running, restart it too
    if process_named_running('snmpd'):
        call('systemctl restart snmpd.service')

    # restart pdns if it is used - we check for the control dir to not raise
    # an exception on system startup
    #
    #   File "/usr/lib/python3/dist-packages/vyos/configsession.py", line 128, in __run_command
    #     raise ConfigSessionError(output)
    # vyos.configsession.ConfigSessionError: [ system domain-name vyos.io ]
    # Fatal: Unable to generate local temporary file in directory '/run/powerdns': No such file or directory
    if os.path.isdir('/run/powerdns'):
        ret = run('/usr/bin/rec_control --socket-dir=/run/powerdns ping')
        if ret == 0:
            call('systemctl restart pdns-recursor.service')

    return None


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