oopen-server/roles/modify-ipt-server/tasks/ipt-server.yml
2025-04-21 11:04:04 +02:00

435 lines
14 KiB
YAML

---
# ===
# Install/Uodate git firewall repository
# ===
#- meta: end_play
# when:
# - inventory_hostname in groups['gateway_server'] or inventory_hostname in groups['no_ipt_firewall'] or git_firewall_repository is not defined or git_firewall_repository|length < 1
- name: Install/update firewall repository
git:
repo: '{{ git_firewall_repository.repo }}'
dest: '{{ git_firewall_repository.dest }}'
when: git_firewall_repository is defined and git_firewall_repository|length > 0
# ===
# Some Checks
# ===
- name: Check if file '/etc/ipt-firewall/main_ipv6.conf' exists
stat:
path: /etc/ipt-firewall/main_ipv6.conf
register: main_ipv6_exists
- name: Check if file '/etc/ipt-firewall/main_ipv4.conf' exists
stat:
path: /etc/ipt-firewall/main_ipv4.conf
register: main_ipv4_exists
- name: Check if file '/etc/ipt-firewall/interfaces_ipv6.conf' exists
stat:
path: /etc/ipt-firewall/interfaces_ipv6.conf
register: interfaces_ipv6_exists
- name: Check if file '/etc/ipt-firewall/interfaces_ipv4.conf' exists
stat:
path: /etc/ipt-firewall/interfaces_ipv4.conf
register: interfaces_ipv4_exists
- name: Check if file '/etc/munin/munin-node.conf' exists
stat:
path: /etc/munin/munin-node.conf
register: munin_node_exists
# ===
# Adjust/Correct some values..
# ===
- name: addjust line 'munin_remote_ip' (IPv4)
lineinfile:
path: /etc/ipt-firewall/main_ipv4.conf
regexp: '^munin_remote_ip='
line: 'munin_remote_ip="{{ munin_remote_ipv4 }}"'
when:
- main_ipv4_exists.stat.exists
notify:
- Restart IPv4 Firewall
- name: addjust line 'munin_remote_ip' (IPv6)
lineinfile:
path: /etc/ipt-firewall/main_ipv6.conf
regexp: '^munin_remote_ip='
line: 'munin_remote_ip="{{ munin_remote_ipv6 }}"'
when:
- main_ipv6_exists.stat.exists
notify:
- Restart IPv6 Firewall
- name: addjust line 'vpn_ifs' (IPv4)
lineinfile:
path: /etc/ipt-firewall/interfaces_ipv4.conf
regexp: '^vpn_ifs='
line: 'vpn_ifs="tun+"'
when:
- interfaces_ipv4_exists.stat.exists
notify:
- Restart IPv4 Firewall
- name: addjust line 'vpn_ifs' (IPv6)
lineinfile:
path: /etc/ipt-firewall/interfaces_ipv6.conf
regexp: '^vpn_ifs='
line: 'vpn_ifs="tun+"'
when:
- interfaces_ipv6_exists.stat.exists
notify:
- Restart IPv6 Firewall
# ===
# Add some Code Block.
# ===
# ---
# Add support for MNDP and mDNS Traffic
# ---
- name: Check if String 'drop_mndp=..' is present
shell: grep -q -E "^drop_mndp=" /etc/ipt-firewall/main_ipv4.conf
register: drop_mndp_ipv4_present
when: main_ipv4_exists.stat.exists
failed_when: "drop_mndp_ipv4_present.rc > 1"
changed_when: "drop_mndp_ipv4_present.rc > 0"
- name: Adjust file '/etc/ipt-firewall/main_ipv4.conf' (drop_mndp)
blockinfile:
path: /etc/ipt-firewall/main_ipv4.conf
insertafter: '^#?\s*drop_icmp'
block: |
# -------------
# --- Drop Mikrotik RouterOS Neighbor Discovery Protocol (MNDP) Traffic
# --- Drop Tinc VPN Traffic
# -------------
# Tinc VPN Traffic / Mikrotik RouterOS Neighbor Discovery Protocol (MNDP) Traffic
#
# Der UDP-Port 5678 wird üblicherweise von Tinc VPN verwendet. Tinc ist ein
# Open-Source-VPN-Softwarepaket, das für die Erstellung von Virtual Private
# Networks (VPNs) eingesetzt wird, bei denen Netzwerke über das Internet oder
# andere unsichere Netzwerke miteinander verbunden werden. Es nutzt diesen
# Port, um Verbindungen zwischen den Knoten (Nodes) des VPNs zu ermöglichen.
#
# Der UDP-Port 5678 wird auch von MikroTik RouterOS Neighbor Discovery Protocol
# (NDP) verwendet. Dieses Protokoll wird von MikroTik-Routern eingesetzt, um
# benachbarte Geräte im Netzwerk zu entdecken und automatisch zu erkennen. Es
# hilft dabei, die Kommunikation zwischen MikroTik-Geräten zu erleichtern, ohne
# dass eine manuelle IP-Konfiguration erforderlich ist.
#
# MikroTik Neighbor Discovery über UDP-Port 5678 ist speziell darauf ausgelegt,
# Router und Geräte im selben lokalen Netzwerk (LAN) zu identifizieren und
# Informationen über benachbarte MikroTik-Geräte auszutauschen. Dies ist besonders
# nützlich für die Verwaltung und Konfiguration von MikroTik-Geräten im Netzwerk.
#
# Zusammengefasst:
# Der UDP-Port 5678 wird sowohl für MikroTik RouterOS Neighbor Discovery als auch
# für Tinc VPN verwendet, je nachdem, welche Technologie zum Einsatz kommt.
#
drop_mndp=true
# -------------
# --- Drop Multicast DNS Traffic
# -------------
# Multicast Domain Name System (mDNS) protocol
#
# UDP Port 5353/
#
# Der UDP-Port 5353 wird hauptsächlich für Multicast DNS (mDNS) verwendet.
# mDNS ist ein Protokoll, das es Geräten ermöglicht, sich im lokalen Netzwerk
# selbst zu identifizieren und ohne zentrale DNS-Server Namen zu registrieren
# und aufzulösen. Dies wird häufig in lokalen Netzwerken eingesetzt, z.B. bei
# Geräten, die mit Apple's Bonjour oder Avahi (einer Open-Source-Implementierung
# von mDNS) kommunizieren.
#
# UDP port 5353 is mainly used for multicast DNS (mDNS). mDNS is a protocol that
# allows devices to identify themselves on the local network and register and
# resolve names without central DNS servers. This is often used in local
# networks, e.g. for devices that communicate using Apple's Bonjour or Avahi
# (an open-source implementation of mDNS).
#
drop_mdns=true
marker: "# Marker set by modify-ipt-server.yml (drop_mndp)"
when:
- main_ipv4_exists.stat.exists
- drop_mndp_ipv4_present is changed
notify:
- Restart IPv4 Firewall
- name: Check if String 'drop_mndp=..' is present
shell: grep -q -E "^drop_mndp=" /etc/ipt-firewall/main_ipv6.conf
register: drop_mndp_ipv6_present
when: main_ipv6_exists.stat.exists
failed_when: "drop_mndp_ipv6_present.rc > 1"
changed_when: "drop_mndp_ipv6_present.rc > 0"
- name: Adjust file '/etc/ipt-firewall/main_ipv6.conf' (drop_mndp)
blockinfile:
path: /etc/ipt-firewall/main_ipv6.conf
insertafter: '^#?\s*drop_icmp'
block: |
# -------------
# --- Drop Mikrotik RouterOS Neighbor Discovery Protocol (MNDP) Traffic
# --- Drop Tinc VPN Traffic
# -------------
# Tinc VPN Traffic / Mikrotik RouterOS Neighbor Discovery Protocol (MNDP) Traffic
#
# Der UDP-Port 5678 wird üblicherweise von Tinc VPN verwendet. Tinc ist ein
# Open-Source-VPN-Softwarepaket, das für die Erstellung von Virtual Private
# Networks (VPNs) eingesetzt wird, bei denen Netzwerke über das Internet oder
# andere unsichere Netzwerke miteinander verbunden werden. Es nutzt diesen
# Port, um Verbindungen zwischen den Knoten (Nodes) des VPNs zu ermöglichen.
#
# Der UDP-Port 5678 wird auch von MikroTik RouterOS Neighbor Discovery Protocol
# (NDP) verwendet. Dieses Protokoll wird von MikroTik-Routern eingesetzt, um
# benachbarte Geräte im Netzwerk zu entdecken und automatisch zu erkennen. Es
# hilft dabei, die Kommunikation zwischen MikroTik-Geräten zu erleichtern, ohne
# dass eine manuelle IP-Konfiguration erforderlich ist.
#
# MikroTik Neighbor Discovery über UDP-Port 5678 ist speziell darauf ausgelegt,
# Router und Geräte im selben lokalen Netzwerk (LAN) zu identifizieren und
# Informationen über benachbarte MikroTik-Geräte auszutauschen. Dies ist besonders
# nützlich für die Verwaltung und Konfiguration von MikroTik-Geräten im Netzwerk.
#
# Zusammengefasst:
# Der UDP-Port 5678 wird sowohl für MikroTik RouterOS Neighbor Discovery als auch
# für Tinc VPN verwendet, je nachdem, welche Technologie zum Einsatz kommt.
#
drop_mndp=true
# -------------
# --- Drop Multicast DNS Traffic
# -------------
# Multicast Domain Name System (mDNS) protocol
#
# UDP Port 5353/
#
# Der UDP-Port 5353 wird hauptsächlich für Multicast DNS (mDNS) verwendet.
# mDNS ist ein Protokoll, das es Geräten ermöglicht, sich im lokalen Netzwerk
# selbst zu identifizieren und ohne zentrale DNS-Server Namen zu registrieren
# und aufzulösen. Dies wird häufig in lokalen Netzwerken eingesetzt, z.B. bei
# Geräten, die mit Apple's Bonjour oder Avahi (einer Open-Source-Implementierung
# von mDNS) kommunizieren.
#
# UDP port 5353 is mainly used for multicast DNS (mDNS). mDNS is a protocol that
# allows devices to identify themselves on the local network and register and
# resolve names without central DNS servers. This is often used in local
# networks, e.g. for devices that communicate using Apple's Bonjour or Avahi
# (an open-source implementation of mDNS).
#
drop_mdns=true
marker: "# Marker set by modify-ipt-server.yml (drop_mndp)"
when:
- main_ipv6_exists.stat.exists
- drop_mndp_ipv6_present is changed
notify:
- Restart IPv6 Firewall
# ===
# Remove Marker set by blockinfile
# ===
- name: Remove marker IPv4
replace :
path: /etc/ipt-firewall/main_ipv4.conf
regexp: "^# Marker set by modify-ipt-server.yml.*$"
replace: ""
register: marker_ipv4_removed
#failed_when: "marker_ipv4_removed.rc > 1"
#changed_when: "marker_ipv4_removed.rc < 1"
when:
- main_ipv4_exists.stat.exists
- name: Remove marker IPv6
replace :
path: /etc/ipt-firewall/main_ipv6.conf
regexp: "^# Marker set by modify-ipt-server.yml.*$"
replace: ""
register: marker_ipv6_removed
#failed_when: "marker_ipv6_removed.rc > 1"
#changed_when: "marker_ipv6_removed.rc < 1"
when:
- main_ipv6_exists.stat.exists
- name: Remove marker IPv4 from interfaces_ipv4.conf
replace :
path: /etc/ipt-firewall/interfaces_ipv4.conf
regexp: "^# Marker set by modify-ipt-server.yml.*$"
replace: ""
when:
- interfaces_ipv4_exists.stat.exists
- name: Remove marker IPv6 from interfaces_ipv6.conf
replace :
path: /etc/ipt-firewall/interfaces_ipv6.conf
regexp: "^# Marker set by modify-ipt-server.yml.*$"
replace: ""
when:
- interfaces_ipv6_exists.stat.exists
# ===
# Add Code Line.
# ===
# ---
# Per IP Connection Limit (add a line)
# ---
- name: Check if String 'per_IP_connection_limit=..' is present
shell: grep -q -E "^per_IP_connection_limit=" /etc/ipt-firewall/main_ipv4.conf
register: per_ip_connection_limit_settings_ipv4_present
when: main_ipv4_exists.stat.exists
failed_when: "per_ip_connection_limit_settings_ipv4_present.rc > 1"
changed_when: "per_ip_connection_limit_settings_ipv4_present.rc > 0"
- name: Adjust file '/etc/ipt-firewall/main_ipv4.conf' (protect_settings)
lineinfile:
path: /etc/ipt-firewall/main_ipv4.conf
insertafter: '^#?\s*limit_connections_per_source_IP'
line: per_IP_connection_limit=$default_per_IP_connection_limit
when:
- main_ipv4_exists.stat.exists
- per_ip_connection_limit_settings_ipv4_present is changed
notify:
- Restart IPv4 Firewall
- name: Check if String 'per_IP_connection_limit=..' is present
shell: grep -q -E "^per_IP_connection_limit=" /etc/ipt-firewall/main_ipv6.conf
register: per_ip_connection_limit_settings_ipv6_present
when: main_ipv6_exists.stat.exists
failed_when: "per_ip_connection_limit_settings_ipv6_present.rc > 1"
changed_when: "per_ip_connection_limit_settings_ipv6_present.rc > 0"
- name: Adjust file '/etc/ipt-firewall/main_ipv6.conf' (protect_settings)
lineinfile:
dest: /etc/ipt-firewall/main_ipv6.conf
insertafter: '^#?\s*limit_connections_per_source_IP'
line: per_IP_connection_limit=$default_per_IP_connection_limit
when:
- main_ipv6_exists.stat.exists
- per_ip_connection_limit_settings_ipv6_present is changed
notify:
- Restart IPv6 Firewall
# ===
# Confiuration Files
# ===
- name: Check if configuration files are latest
shell: 'diff {{ git_firewall_repository.dest }}/conf/{{ item }} /etc/ipt-firewall/{{ item }} > /dev/null 2>&1'
changed_when: "diff_script_output.rc > 0"
# diff_output.rc
# 0 -> unchanged
# 1 -> changed
# 2 -> not present
failed_when: "diff_script_output.rc > 2"
when:
- git_firewall_repository is defined and git_firewall_repository|length > 0
loop:
- default_settings.conf
- include_functions.conf
- load_modules_ipv4.conf
- load_modules_ipv6.conf
- logging_ipv4.conf
- logging_ipv6.conf
- post_decalrations.conf
register: diff_script_output
- name: Ensure configuration files are latest
command: cp {{ git_firewall_repository.dest }}/conf/{{ item }} /etc/ipt-firewall/{{ item }}
loop:
- default_settings.conf
- include_functions.conf
- load_modules_ipv4.conf
- load_modules_ipv6.conf
- logging_ipv4.conf
- logging_ipv6.conf
- post_decalrations.conf
when:
- git_firewall_repository is defined and git_firewall_repository|length > 0
- diff_script_output.changed
notify:
- Restart IPv4 Firewall
- Restart IPv6 Firewall
# ===
# Firewall scripts
# ===
- name: Check if firewall scripts are latest
shell: 'diff {{ git_firewall_repository.dest }}/{{ item }} /usr/local/sbin/{{ item }} > /dev/null 2>&1'
changed_when: "diff_script_output.rc > 0"
# diff_output.rc
# 0 -> unchanged
# 1 -> changed
# 2 -> not present
failed_when: "diff_script_output.rc > 2"
when:
- git_firewall_repository is defined and git_firewall_repository|length > 0
loop:
- ipt-firewall-server
- ip6t-firewall-server
register: diff_script_output
- name: Ensure firewall scripts are latest
command: cp {{ git_firewall_repository.dest }}/{{ item }} /usr/local/sbin/{{ item }}
loop:
- ipt-firewall-server
- ip6t-firewall-server
when:
- git_firewall_repository is defined and git_firewall_repository|length > 0
- diff_script_output.changed
notify:
- Restart IPv4 Firewall
- Restart IPv6 Firewall
# ===
# Delete files that are no longer required
# ===
- name: Delete file '/etc/ipt-firewall/default_ports.conf' ..
file:
state: absent
path: /etc/ipt-firewall/default_ports.conf
- name: Delete file '/etc/ipt-firewall/ports.conf' ..
file:
state: absent
path: /etc/ipt-firewall/ports.conf