summaryrefslogtreecommitdiff
path: root/src/conf_mode/service_suricata.py
blob: 69b369e0b70f40a4c11b4e7eb5b4d56b097ada39 (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
#!/usr/bin/env python3
#
# Copyright (C) 2024 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

from sys import exit

from vyos.base import Warning
from vyos.config import Config
from vyos.template import render
from vyos.utils.process import call
from vyos import ConfigError
from vyos import airbag
airbag.enable()

config_file = '/run/suricata/suricata.yaml'
rotate_file = '/etc/logrotate.d/suricata'

def get_config(config=None):
    if config:
        conf = config
    else:
        conf = Config()
    base = ['service', 'suricata']

    if not conf.exists(base):
        return None

    suricata = conf.get_config_dict(base, key_mangling=('-', '_'),
                                    get_first_key=True, with_recursive_defaults=True)

    return suricata

# https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
def topological_sort(source):
    sorted_nodes = []
    permanent_marks = set()
    temporary_marks = set()

    def visit(n, v):
        if n in permanent_marks:
            return
        if n in temporary_marks:
            raise ConfigError('At least one cycle exists in the referenced groups')

        temporary_marks.add(n)

        for m in v.get('group', []):
            m = m.lstrip('!')
            if m not in source:
                raise ConfigError(f'Undefined referenced group "{m}"')
            visit(m, source[m])

        temporary_marks.remove(n)
        permanent_marks.add(n)
        sorted_nodes.append((n, v))

    while len(permanent_marks) < len(source):
        n = next(n for n in source.keys() if n not in permanent_marks)
        visit(n, source[n])

    return sorted_nodes

def verify(suricata):
    if not suricata:
        return None

    if 'interface' not in suricata:
        raise ConfigError('No interfaces configured!')

    if 'address_group' not in suricata:
        raise ConfigError('No address-group configured!')

    if 'port_group' not in suricata:
        raise ConfigError('No port-group configured!')

    try:
        topological_sort(suricata['address_group'])
    except (ConfigError,StopIteration) as e:
        raise ConfigError(f'Invalid address-group: {e}')

    try:
        topological_sort(suricata['port_group'])
    except (ConfigError,StopIteration) as e:
        raise ConfigError(f'Invalid port-group: {e}')

def generate(suricata):
    if not suricata:
        for file in [config_file, rotate_file]:
            if os.path.isfile(file):
                os.unlink(file)

        return None

    # Config-related formatters
    def to_var(s:str):
        return s.replace('-','_').upper()

    def to_val(s:str):
        return s.replace('-',':')

    def to_ref(s:str):
        if s[0] == '!':
            return '!$' + to_var(s[1:])
        return '$' + to_var(s)

    def to_config(kind:str):
        def format_group(group):
            (name, value) = group
            property = [to_val(property) for property in value.get(kind,[])]
            group = [to_ref(group) for group in value.get('group',[])]
            return (to_var(name), property + group)
        return format_group

    # Format the address group
    suricata['address_group'] = map(to_config('address'),
                                    topological_sort(suricata['address_group']))

    # Format the port group
    suricata['port_group'] = map(to_config('port'),
                                    topological_sort(suricata['port_group']))

    render(config_file, 'ids/suricata.j2', {'suricata': suricata})
    render(rotate_file, 'ids/suricata_logrotate.j2', suricata)
    return None

def apply(suricata):
    systemd_service = 'suricata.service'
    if not suricata or 'interface' not in suricata:
        # Stop suricata service if removed
        call(f'systemctl stop {systemd_service}')
    else:
        Warning('To fetch the latest rules, use "update suricata"; '
                'To periodically fetch the latest rules, '
                'use the task scheduler!')
        call(f'systemctl restart {systemd_service}')

    return None

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