# 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/>.
#
#
importsys
importos
importstat
importpwd
importjinja2
importipaddress
importnetifaces
fromvyos.configimportConfig
fromvyosimportConfigError
config_file=r'/etc/default/tftpd-hpa'
# Please be careful if you edit the template.
config_tmpl="""
### Autogenerated by tftp_server.py ###
# See manual at https://linux.die.net/man/8/tftpd
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="{{ directory }}"
{% if listen_ipv4 and listen_ipv6 -%}
TFTP_ADDRESS="{% for a in listen_ipv4 -%}{{ a }}:{{ port }}{{- " --address " if not loop.last -}}{% endfor -%} {% for a in listen_ipv6 %} --address [{{ a }}]:{{ port }}{% endfor -%}"
{% elif listen_ipv4 -%}
TFTP_ADDRESS="{% for a in listen_ipv4 -%}{{ a }}:{{ port }}{{- " --address " if not loop.last -}}{% endfor %} -4"
{% elif listen_ipv6 -%}
TFTP_ADDRESS="{% for a in listen_ipv6 -%}[{{ a }}]:{{ port }}{{- " --address " if not loop.last -}}{% endfor %} -6"
{%- endif %}
TFTP_OPTIONS="--secure {% if allow_upload %}--create --umask 000{% endif %}"
"""
default_config_data={
'directory':'',
'allow_upload':False,
'port':'69',
'listen_ipv4':[],
'listen_ipv6':[]
}
# Verify if an IP address is assigned to any interface, IPv4 and IPv6
defaddrok(ipaddr,ipversion):
# For every available interface on this system
forinterfaceinnetifaces.interfaces():
# If it has any IPv4 or IPv6 address (depending on ipversion) configured