Debian Codegraph @

src/foomuuri

raw
1 #!/usr/bin/python3
2 # pylint: disable=too-many-lines
3
4 """Foomuuri - Multizone bidirectional nftables firewall.
5
6 Copyright 2023-2026, Kim B. Heino, Foobar Oy <[email protected]> and contributors.
7
8 License: GPL-2.0-or-later
9 """
10
11 import collections
12 import concurrent.futures
13 import contextlib
14 import dataclasses
15 import datetime
16 import io
17 import ipaddress
18 import itertools
19 import json
20 import os
21 import pathlib
22 import re
23 import select
24 import shlex
25 import signal
26 import socket
27 import subprocess
28 import sys
29 import syslog
30 import time
31 import typing
32 import unicodedata
33
34 # dbus is optional but highly recommended for dynamic zone/interface changes
35 try:
36 import dbus
37 import dbus.mainloop.glib
38 import dbus.service
39
40 HAVE_DBUS = True
41 except ImportError:
42 HAVE_DBUS = False
43
44 # lxml is optional, used by iplist xml and html filters
45 try:
46 import lxml.etree
47
48 HAVE_LXML = True
49 except ImportError:
50 HAVE_LXML = False
51
52 # systemd notify support is optional
53 try:
54 import systemd.daemon
55
56 HAVE_SYSTEMD = True
57 except ImportError:
58 HAVE_SYSTEMD = False
59
60 VERSION = '0.33'
61
62
63 class Validators:
64 """Validators collection."""
65
66 @staticmethod
67 def str_zone_name(value: str):
68 """Assert value is a valid zone name."""
69 return all(
70 [value.isascii(), re.fullmatch(r'[a-zA-Z][a-zA-Z_0-9./]*', value)]
71 )
72
73 @staticmethod
74 def str_iplist_name(value: str):
75 """Assert value is a valid iplist name, including @."""
76 return all(
77 [
78 value.isascii(),
79 re.fullmatch(r'@[a-zA-Z_][-a-zA-Z_0-9./]*', value),
80 ]
81 )
82
83 @staticmethod
84 def str_interface_name(value: str):
85 r"""Assert value is a valid network interface name.
86
87 Linux accepts also \\ and " characters. Reject them here as they
88 would break JSONs without quoting.
89 """
90 if any(letter <= ' ' for letter in value):
91 return False # Reject whitespace/control chars as Linux does
92 if '/' in value:
93 return False # Linux rejects this
94 if '\\' in value or '"' in value:
95 return False # Linux accepts these
96 return 1 <= len(value) <= 15 # IFNAMSIZ=16 including NUL
97
98 @staticmethod
99 def str_words(value: str) -> bool:
100 """Assert value is one or more words."""
101 return len(value.split()) >= 1
102
103 @staticmethod
104 def has_elements(value: typing.Union[set, list]) -> bool:
105 """Assert value is non-empty list or set."""
106 return len(value) >= 1
107
108 @staticmethod
109 def int_positive_or_zero(value: int):
110 """Assert value is not negative."""
111 return value >= 0
112
113 @staticmethod
114 def int_positive(value: int):
115 """Assert value is positive."""
116 return value > 0
117
118 @staticmethod
119 def str_yes_no(value: str):
120 """Assert value is either 'yes' or 'no'."""
121 return value in {'yes', 'no'}
122
123
124 class Converters: # pylint: disable=too-few-public-methods
125 """Type converters collection."""
126
127 @staticmethod
128 def str_yes_no_to_bool(value: str) -> bool:
129 """Convert str 'yes'/'no' to bool True/False, or raise ValueError."""
130 if value == 'yes':
131 return True
132 if value == 'no':
133 return False
134 raise ValueError
135
136
137 @dataclasses.dataclass
138 class TypedConfig:
139 """Storage class with simple attribute type enforcement and validation."""
140
141 def __setattr__(self, name, value):
142 """Set attribute value."""
143 if (field := self._get_field(name)) is None:
144 raise AttributeError(f'{name}: unknown attribute')
145 if not isinstance(value, field.type):
146 raise TypeError(
147 f'{name}: expected value type {field.type}, got {type(value)}'
148 )
149 validator = field.metadata.get('validate')
150 if validator and not validator(value):
151 raise ValueError(f'{name}: value failed validation')
152 super().__setattr__(name, value)
153
154 def get(self, name, default=None):
155 """Get attribute value."""
156 return getattr(self, name, default)
157
158 def __getitem__(self, name):
159 """Get attribute value via index operator."""
160 return getattr(self, name)
161
162 def __setitem__(self, name, value):
163 """Set attribute value."""
164 self.__setattr__(name, value)
165
166 def __iter__(self):
167 """Return attributes list terator."""
168 # pylint: disable=no-member
169 return iter(list(self.__dataclass_fields__))
170
171 def _check_set_append_args(self, name: str, value: str) -> None:
172 """Verify attribute name and value sanity."""
173 if name not in self:
174 raise AttributeError(f'{name}: unknown attribute')
175 if not isinstance(value, str):
176 raise TypeError(
177 f'{name}: expected value type {str}, got {type(value)}'
178 )
179
180 def _convert_set_append_value(self, name: str, value: str):
181 """Convert value to attribute's type."""
182 field = self._get_field(name)
183
184 # "Built-in" value converters. Executed when
185 # convert_str is not defined in field metadata.
186 implicit_converters = {
187 int: int,
188 set: lambda v: set(v.split()),
189 list: str.split,
190 pathlib.PosixPath: pathlib.PosixPath,
191 }
192
193 if converter := (
194 field.metadata.get('convert_str')
195 or implicit_converters.get(field.type)
196 ):
197 return converter(value)
198 if isinstance(value, field.type):
199 return value
200 raise TypeError(
201 f'{name}: value conversion to {field.type} not implemented'
202 )
203
204 def set_from_str(self, name, value: str) -> None:
205 """Set attribute value from str with type conversion."""
206 self._check_set_append_args(name, value)
207 new_value = self._convert_set_append_value(name, value)
208 setattr(self, name, new_value)
209
210 def append_from_str(self, name, value: str) -> None:
211 """Appends attribute value from str with type conversion."""
212 self._check_set_append_args(name, value)
213 new_value = self._convert_set_append_value(name, value)
214
215 field = self._get_field(name)
216
217 # "Built-in" implicit appenders.
218 implicit_appenders = {
219 str: lambda value, append: f'{value} {append}',
220 set: lambda value, append: value | append,
221 list: lambda value, append: value + append,
222 }
223
224 if (appender := implicit_appenders.get(field.type)) and isinstance(
225 new_value, field.type
226 ):
227 new_value = appender(self[name], new_value)
228 else:
229 raise TypeError(
230 f'{name}: appending of {field.type} not implemented'
231 )
232
233 setattr(self, name, new_value)
234
235 def _get_field(self, name, default=None):
236 """Get dataclass field by attribute name."""
237 # pylint: disable=no-member
238 return self.__dataclass_fields__.get(name, default)
239
240
241 @dataclasses.dataclass
242 class Config(TypedConfig):
243 """Parsed foomuuri{} section variables from config files."""
244
245 # pylint: disable=too-many-instance-attributes
246
247 # Basic settings
248 log_rate: str = '1/second burst 3'
249 log_input: str = 'yes'
250 log_output: str = 'yes'
251 log_forward: str = 'yes'
252 log_rpfilter: str = 'yes'
253 log_invalid: str = 'no'
254 log_smurfs: str = 'no'
255 log_prefix: str = '$(szone)-$(dzone) $(statement)'
256 log_level: str = 'level info flags skuid'
257 localhost_zone: str = dataclasses.field(
258 default='localhost', metadata={'validate': Validators.str_zone_name}
259 )
260 dbus_zone: str = dataclasses.field(
261 default='public', metadata={'validate': Validators.str_zone_name}
262 )
263 rpfilter: set = dataclasses.field(
264 default_factory=lambda: {'yes'},
265 metadata={'validate': Validators.has_elements},
266 )
267 flowtable: set = dataclasses.field(default_factory=set)
268 counter: set = dataclasses.field(
269 default_factory=lambda: {'no'},
270 metadata={'validate': Validators.has_elements},
271 )
272 set_size: int = dataclasses.field(
273 default=65535, metadata={'validate': Validators.int_positive}
274 )
275 recursion_limit: int = dataclasses.field(
276 default=10000, metadata={'validate': Validators.int_positive}
277 )
278 priority_offset: int = 5
279 dbus_firewalld: str = dataclasses.field(
280 default='no', metadata={'validate': Validators.str_yes_no}
281 )
282 nft_bin: list = dataclasses.field(
283 default_factory=lambda: ['nft'],
284 metadata={
285 'convert_str': shlex.split,
286 'validate': Validators.has_elements,
287 },
288 )
289 try_reload_timeout: int = dataclasses.field(
290 default=15, metadata={'validate': Validators.int_positive}
291 )
292
293 # Directories and files. Files are relative to state_dir.
294 etc_dir: pathlib.PosixPath = dataclasses.field(
295 default_factory=lambda: pathlib.PosixPath('/etc/foomuuri')
296 )
297 share_dir: pathlib.PosixPath = dataclasses.field(
298 default_factory=lambda: pathlib.PosixPath('/usr/share/foomuuri')
299 )
300 state_dir: pathlib.PosixPath = dataclasses.field(
301 default_factory=lambda: pathlib.PosixPath('/var/lib/foomuuri')
302 )
303 run_dir: pathlib.PosixPath = dataclasses.field(
304 default_factory=lambda: pathlib.PosixPath('/run/foomuuri')
305 )
306 good_file: str = 'good.fw'
307 next_file: str = 'next.fw'
308 resolve_file: str = 'resolve.fw' # old iplist/resolve
309 iplist_file: str = 'iplist.fw' # old iplist/resolve
310 iplist_manual_file: str = 'iplist-manual.fw' # old iplist/resolve
311 iplist_cache_file: str = 'iplist-cache.json'
312 zone_file: str = 'zone'
313 monitor_statistics_file: str = 'monitor.statistics'
314
315 # Variable in foomuuri{}, or command line
316 fork: int = 0 # --fork
317 verbose: int = 0 # --verbose
318 syslog: int = 0 # --syslog
319
320
321 @dataclasses.dataclass
322 class Internal(TypedConfig):
323 """Internal variables and command line options."""
324
325 # pylint: disable=too-many-instance-attributes
326
327 # Command line
328 command: str = '' # Command to run: "start" etc
329 parameters: list = dataclasses.field(
330 default_factory=list
331 ) # Parameters for command
332 force: int = 0 # --force
333
334 # Other internally used variables
335 # Added without default value to prevent uninitialized use
336 iplist_missing_ok: set = dataclasses.field(init=False)
337 keep_going: int = dataclasses.field(init=False)
338 post_start: list = dataclasses.field(init=False)
339 post_stop: list = dataclasses.field(init=False)
340 pre_start: list = dataclasses.field(init=False)
341 pre_stop: list = dataclasses.field(init=False)
342 priority_offset: str = dataclasses.field(init=False)
343 catch_all_interface_zone: str = dataclasses.field(init=False)
344 root_power: bool = dataclasses.field(init=False) # Running as "root"?
345
346 # Url loader
347 # Constants
348 url_chunk_size: int = 1_048_576
349 url_get_timeout: dict = dataclasses.field(
350 default_factory=lambda: {'connect': 3.05, 'read': 3.05}
351 ) # Timeouts (seconds)
352 url_get_retries: dict = dataclasses.field(
353 default_factory=lambda: {
354 'connect': 3,
355 'redirect': 3,
356 'read': 3,
357 'raise_on_redirect': False,
358 }
359 ) # Retries count and redirect behaviour
360
361
362 @dataclasses.dataclass
363 class IPListOptions(TypedConfig):
364 """iplist{} options container."""
365
366 # pylint: disable=too-many-instance-attributes
367
368 dns_timeout: int = dataclasses.field(
369 default=86400,
370 metadata={
371 'convert_str': lambda d: parse_duration(d, 0),
372 'validate': Validators.int_positive,
373 },
374 ) # 24h
375 dns_refresh: int = dataclasses.field(
376 default=900,
377 metadata={
378 'convert_str': lambda d: parse_duration(d, 0),
379 'validate': Validators.int_positive,
380 },
381 ) # 15m
382 url_timeout: int = dataclasses.field(
383 default=864000,
384 metadata={
385 'convert_str': lambda d: parse_duration(d, 0),
386 'validate': Validators.int_positive,
387 },
388 ) # 10d
389 url_refresh: int = dataclasses.field(
390 default=86400,
391 metadata={
392 'convert_str': lambda d: parse_duration(d, 0),
393 'validate': Validators.int_positive,
394 },
395 ) # 1d
396 element_timeout: int = dataclasses.field(
397 default=0,
398 metadata={
399 'convert_str': lambda d: parse_duration(d, -1),
400 'validate': Validators.int_positive_or_zero,
401 },
402 ) # disabled
403 url_max_size: int = dataclasses.field(
404 default=33_554_432, metadata={'validate': Validators.int_positive}
405 ) # 32MiB
406 start: bool = dataclasses.field(
407 default=True, metadata={'convert_str': Converters.str_yes_no_to_bool}
408 ) # load on start
409 merge: bool = dataclasses.field(
410 default=True, metadata={'convert_str': Converters.str_yes_no_to_bool}
411 ) # merge
412 dynamic: bool = dataclasses.field(
413 default=False, metadata={'convert_str': Converters.str_yes_no_to_bool}
414 ) # dynamic flag, implies merge=no and no interval
415
416 def parse_option(self, option: str):
417 """Parse and set iplist option value from option.
418
419 Option is either a a string in option=value format,
420 or specific word.
421 """
422 shortcuts = {
423 '-start': ('start', False),
424 '-merge': ('merge', False),
425 }
426
427 if shortcut := shortcuts.get(option):
428 name, value = shortcut
429 self[name] = value
430 return True
431
432 if len(assignment := option.split('=', maxsplit=1)) == 2:
433 name, value = assignment
434 # Only try setting if name is a valid identifier
435 if name.isidentifier():
436 self.set_from_str(name, value)
437 return True
438
439 return False
440
441
442 @dataclasses.dataclass(eq=False)
443 class IPList:
444 """Container of iplist options and sources."""
445
446 options: IPListOptions = dataclasses.field(default_factory=IPListOptions)
447 sources: list = dataclasses.field(default_factory=list)
448
449
450 @dataclasses.dataclass(eq=False)
451 class IPLists:
452 """Mapping of iplist name to IPList."""
453
454 _entries: dict[str, IPList] = dataclasses.field(default_factory=dict)
455
456 def __getitem__(self, name):
457 """Get iplist value via index operator."""
458 return self._entries[name]
459
460 def __setitem__(self, name, value):
461 """Set iplist value via index operator."""
462 self._entries[name] = value
463
464 def __iter__(self):
465 """Iterate over iplist names."""
466 return iter(self._entries)
467
468 def items(self):
469 """Return iterable of iplist mapping."""
470 return self._entries.items()
471
472 def values(self):
473 """Return iterable of iplist mapping values."""
474 return self._entries.values()
475
476 def filter_names_by(self, option: str, required_value) -> set[str]:
477 """Return set of iplist names with required option value."""
478 return {
479 name
480 for name, value in self._entries.items()
481 if value.options[option] == required_value
482 }
483
484
485 class IPListSourceOptions: # pylint: disable=too-few-public-methods
486 """Container of iplist runtime source options."""
487
488 timeout: int = sys.maxsize
489 refresh: int = sys.maxsize
490 max_size: int = sys.maxsize
491
492 def update_if_less(self, name: str, new_value: int) -> None:
493 """Update attribute value if new value is less than current."""
494 setattr(self, name, min(new_value, getattr(self, name)))
495
496
497 CONFIG = Config()
498 CONFIG_OVERRIDE = {}
499 INTERNAL = Internal()
500
501 OUT = [] # Generated nftables ruleset / commands
502 LOGRATES = {} # Lograte names and limits
503 HELPERS = [] # List of helpers: (helper-object, protocol, ports)
504
505
506 def fail(error: str, fatal: bool = True) -> int:
507 """Exit with error message."""
508 if CONFIG.verbose >= -1: # Double-quiet will suppress output
509 print(f'Error: {error}', flush=True)
510 if CONFIG.syslog:
511 syslog.syslog(syslog.LOG_ERR, f'Error: {error}')
512 if fatal:
513 sys.exit(1)
514 return 1
515
516
517 def warning(text: str) -> None:
518 """Print warning message."""
519 if CONFIG.verbose >= 0:
520 print(f'Warning: {text}', flush=True)
521 if CONFIG.syslog:
522 syslog.syslog(syslog.LOG_WARNING, f'Warning: {text}')
523
524
525 def verbose(line: str, level: int = 1) -> None:
526 """Print line if --verbose was given in command line."""
527 if CONFIG.verbose >= level:
528 print(line, flush=True)
529 if CONFIG.syslog:
530 syslog.syslog(syslog.LOG_NOTICE, line)
531
532
533 def int_to_human(number: int) -> str:
534 """Format int in human readable way."""
535 return f'{number:,}'
536
537
538 def print_table(
539 data: list[tuple],
540 align: str = '<',
541 header: bool = False,
542 sep: str = ' ',
543 ) -> None:
544 """Simplistic formatted table printer."""
545 if not data or (header and len(data) == 1):
546 return
547
548 if not set(align).issubset(set('^<>')):
549 raise ValueError('Supported alignment: left <, right >, center ^')
550
551 columns = len(data[0])
552 width = [
553 max(len(str(row[column])) for row in data) for column in range(columns)
554 ]
555
556 if len(align) == 1:
557 align *= columns
558 elif len(align) != columns:
559 raise ValueError('Align value does not match column count')
560
561 for r, row in enumerate(data):
562 print(
563 sep.join(
564 [
565 f'{column:{align[c]}{width[c]}}'
566 for c, column in enumerate(row)
567 ]
568 ).rstrip(),
569 )
570
571 if header and not r:
572 print(
573 sep.join([f'{"-" * width[c]}' for c in range(columns)]),
574 )
575
576
577 def out(line: str) -> None:
578 """Add single line to ruleset."""
579 OUT.append(line)
580
581
582 def notify(message: str) -> None:
583 """Send systemd notify message."""
584 if HAVE_SYSTEMD:
585 try:
586 systemd.daemon.notify(message)
587 except OSError as error:
588 warning(f'Failed to send systemd notify "{message}": {error}')
589
590
591 def state_file(key):
592 """Helper function to return CONFIG.state_dir / CONFIG.key.
593
594 All xxx_file variables are relative to state_dir.
595 Values "../filename" and "/full/path/filename" works too.
596 """
597 return CONFIG.state_dir / CONFIG[key]
598
599
600 def join_args(args: list) -> str:
601 """Helper function to return joined string of stringified args."""
602 if isinstance(args, list):
603 return shlex.join(map(str, args))
604 raise TypeError(f'args type must be a {list}, not a {type(args)}')
605
606
607 def syslog_open_or_close() -> None:
608 """Initialize or stop syslogging."""
609 if CONFIG.syslog:
610 syslog.openlog('foomuuri', syslog.LOG_PID, syslog.LOG_DAEMON)
611 else:
612 syslog.closelog()
613
614
615 def run_program_rc(
616 args: list, *, env=None, stdin=None, print_output=True, quiet=False
617 ) -> int:
618 """Run external program and return its errorcode. Print its output."""
619 if not args:
620 return 0
621 verbose(join_args(args))
622 try:
623 proc = subprocess.run(
624 args,
625 check=False,
626 stdout=subprocess.PIPE,
627 stderr=subprocess.STDOUT,
628 encoding='utf-8',
629 env=env,
630 input=stdin,
631 timeout=60,
632 )
633 except (OSError, subprocess.TimeoutExpired) as error:
634 fail(f'Failed to run command "{join_args(args)}": {error}', False)
635 return 1
636 output = proc.stdout.rstrip()
637 if (
638 output
639 and (proc.returncode or print_output or CONFIG.verbose > 0)
640 and not quiet
641 ):
642 verbose(output, 0)
643 return proc.returncode
644
645
646 def run_program_pipe(
647 args: list[str],
648 text_input: typing.Optional[str],
649 check_error: bool = True,
650 ) -> typing.Optional[str]:
651 """Run external program with stdin, return its output."""
652 if not args:
653 return ''
654 try:
655 proc = subprocess.run(
656 args,
657 check=False,
658 input=text_input,
659 stdout=subprocess.PIPE,
660 stderr=subprocess.STDOUT,
661 encoding='utf-8',
662 timeout=60,
663 )
664 except (OSError, subprocess.TimeoutExpired) as error:
665 fail(f'Failed to run command "{join_args(args)}": {error}')
666 if proc.returncode and check_error:
667 warning(
668 f'Failed to run command "{join_args(args)}": '
669 f'return code {proc.returncode}'
670 )
671 return None
672 return proc.stdout
673
674
675 def run_program_shell(args: str, fileline: str) -> str:
676 """Run external program as shell command and return its output.
677
678 Command failure is fatal. Parameter args must be a string, not list.
679 """
680 if not args:
681 return ''
682 try:
683 proc = subprocess.run(
684 args,
685 check=False,
686 stdout=subprocess.PIPE,
687 stderr=subprocess.STDOUT,
688 encoding='utf-8',
689 shell=True,
690 timeout=60,
691 )
692 except (OSError, subprocess.TimeoutExpired) as error:
693 fail(f'{fileline}Failed to run command "{args}": {error}')
694 if proc.returncode:
695 fail(
696 f'{fileline}Failed to run command "{args}": '
697 f'return code {proc.returncode}'
698 )
699 return proc.stdout.rstrip()
700
701
702 def nft_command(cmd, **kwargs):
703 """Run "nft cmd", wrapper to run_program_rc()."""
704 return run_program_rc(CONFIG.nft_bin + [cmd], **kwargs)
705
706
707 def nft_json(cmd: str) -> typing.Optional[dict]:
708 """Run "nft --json cmd", return its output as json, ignore errors."""
709 if not cmd:
710 return {}
711 args = CONFIG.nft_bin + ['--json', cmd]
712 verbose(join_args(args), 2)
713 try:
714 proc = subprocess.run(
715 args,
716 check=False,
717 stdout=subprocess.PIPE,
718 stderr=subprocess.STDOUT,
719 encoding='utf-8',
720 timeout=60,
721 )
722 except (OSError, subprocess.TimeoutExpired):
723 return None
724 if proc.returncode:
725 if CONFIG.verbose >= 1 or proc.stdout.startswith('JSON support'):
726 warning(
727 f'Failed to run command "{join_args(args)}": '
728 f'{proc.stdout.strip()}'
729 )
730 return None
731 try:
732 verbose(proc.stdout, 2)
733 return json.loads(proc.stdout)
734 except json.decoder.JSONDecodeError:
735 return None
736
737
738 def daemonize():
739 """Fork as a background daemon. Silently ignore errors."""
740 # Enabled in config?
741 if not CONFIG.fork:
742 return
743 CONFIG.fork = 0 # Fork only once!
744
745 # Fork #1
746 try:
747 pid = os.fork()
748 if pid > 0:
749 sys.exit(0)
750 except OSError:
751 return
752
753 # Run in a new session. Don't do "os.chdir('/')" so that relative paths
754 # work in development.
755 os.umask(0o022)
756 os.setsid()
757
758 # Fork #2
759 try:
760 pid = os.fork()
761 if pid > 0:
762 sys.exit(0)
763 except OSError:
764 return
765
766 # Redirect stdin/out/err
767 for stream in (sys.stdin, sys.stdout, sys.stderr):
768 devnull = os.open(os.devnull, os.O_RDWR)
769 os.dup2(devnull, stream.fileno())
770
771
772 def shell_expansion(content, fileline):
773 """Expand $(shell command) in configuration file.
774
775 This is the first expansion done. Failure in command is fatal.
776 """
777 while '$(shell ' in content:
778 prefix, postfix = content.split('$(shell ', 1)
779 if ')' not in postfix:
780 postfix = postfix.splitlines()[0]
781 fail(f'{fileline}"$(shell" without ")" in command: {postfix}')
782 shell, postfix = postfix.split(')', 1)
783 content = f'{prefix}{run_program_shell(shell, fileline)}{postfix}'
784 return content
785
786
787 def find_config_files(basedir, mask):
788 """Find all config files in basedir, ignoring hidden and backups."""
789 files = sorted(basedir.rglob(mask))
790 return [item for item in files if not item.name.startswith(('.', '#'))]
791
792
793 def read_config(require_etc_config: bool):
794 """Read all config files to config dict: section -> lines[].
795
796 Files are read in alphabetical order, ignoring backup and hidden files.
797 """
798 # pylint: disable=too-many-branches
799 # pylint: disable=too-many-locals
800 # pylint: disable=too-many-statements
801
802 # Find all config files
803 share_config = find_config_files(CONFIG.share_dir, '*.conf')
804 etc_config = find_config_files(CONFIG.etc_dir, '*.conf')
805 if not etc_config and require_etc_config:
806 fail(
807 f'No configuration files "{CONFIG.etc_dir}/*.conf" found\n'
808 '\n'
809 'See https://foomuuri.foobar.fi/latest/example/host-firewall/ '
810 'for example\n'
811 'configuration.'
812 )
813
814 # There characters will combine to single word in shlex
815 wordchars = ''.join(
816 chr(letter)
817 for letter in range(33, 256)
818 # excludes: " # ' ; { }
819 if letter not in {34, 35, 39, 59, 123, 125}
820 )
821
822 # Read all config files
823 config = {} # Final config dict
824 section = None # Currently open section name
825 section_line = {} # Section_name -> filename_line for error messages
826 for filename in share_config + etc_config:
827 try:
828 content = filename.read_text(encoding='utf-8')
829 except PermissionError as error:
830 fail(f"File {filename}: Can't read: {error}")
831
832 # Expand $(shell in configuration file. Do this for whole file instead
833 # of single line so that command can return multiple lines.
834 content = shell_expansion(content, f'File {filename}: ')
835
836 # Parse single config file content
837 continuation = ''
838 for linenumber, line in enumerate(content.splitlines()):
839 if line == '# foomuuri: not-conf':
840 break # sysctl's 50-foomuuri.conf is not my config
841
842 # Combine lines if there is \ at end of line
843 if line.endswith('\\'):
844 continuation += line[:-1] + ' '
845 continue
846 line = continuation + line
847 continuation = ''
848
849 # Parse single line to list of words. Keep " as is, it can be
850 # used to avoid macro expansion.
851 fileline = f'File {filename} line {linenumber + 1}: '
852 try:
853 lexer = shlex.shlex(line, punctuation_chars=';{')
854 lexer.wordchars = wordchars
855 tokens = list(lexer)
856 except ValueError as error:
857 fail(f"{fileline}Can't parse line: {error}")
858 if not tokens:
859 continue
860
861 # "}" is end of section
862 if len(tokens) == 1 and tokens[0] == '}': # End of section
863 if not section:
864 fail(f'{fileline}Extra "}}"')
865 section = None
866
867 # Section start:
868 # - "foo {"
869 # - "template foo {" / "target foo" / "group foo"
870 # - "prerouting {"
871 # - "prerouting filter mangle - 10 {"
872 elif len(tokens) >= 2 and tokens[-1] == '{':
873 if section:
874 fail(
875 f'{fileline}New "{" ".join(tokens)}" while section '
876 f'"{section}" is still open'
877 )
878 if (
879 tokens[0] in {'template', 'target', 'group'}
880 and len(tokens) != 3
881 ):
882 fail(
883 f'{fileline}Section "{tokens[0]}" must have single '
884 f' word name: {" ".join(tokens)}'
885 )
886 if len(tokens) > 2 and tokens[0] not in {
887 'template',
888 'target',
889 'group',
890 'snat',
891 'dnat',
892 'prerouting',
893 'postrouting',
894 'forward',
895 'input',
896 'output',
897 }:
898 fail(
899 f'{fileline}Section "{tokens[0]}" does not take '
900 f'parameters: {" ".join(tokens)}'
901 )
902
903 section = ' '.join(tokens[:-1])
904 if section.startswith('_'): # _name is protected
905 fail(f'{fileline}Unknown section: {section}')
906 if section not in config:
907 config[section] = []
908 section_line[section] = fileline
909
910 # "foo" which is not inside section
911 elif not section:
912 fail(f'{fileline}Unknown line: {" ".join(tokens)}')
913
914 # "foo" inside section
915 else:
916 config[section].append((fileline, tokens)) # ty: ignore
917
918 # End of file checks
919 if continuation:
920 fail(f'File {filename}: Continuation "\\" at end of file')
921 if section:
922 fail(
923 f'File {filename}: Section "{section}" is missing "}}" at '
924 f'end of file'
925 )
926
927 # Include section_name -> filename_line to config for error messages
928 config['_section_line'] = section_line
929 return config
930
931
932 def parse_config_templates(config, macros, macroline):
933 """Parse "template foo { ... }" rules and convert them to macros."""
934 names = [item for item in config if item.startswith('template ')]
935 for name in names:
936 lines = config.pop(name)
937 fileline = config['_section_line'][name]
938 if not lines:
939 fail(f'{fileline}Template "{name[9:]}" is empty')
940 macro_name = f'_template_{name[9:]}'
941 macroline[macro_name] = fileline
942 macros[macro_name] = []
943 for line in lines:
944 if macros[macro_name]:
945 macros[macro_name].append(';')
946 macros[macro_name].extend(line[1])
947
948
949 def parse_config_macros(config):
950 """Parse macro{} from config. Recursively expand macro in macro{}."""
951 # Parse macro{} to dict
952 macros = {}
953 macroline = {}
954 parse_config_templates(config, macros, macroline)
955 for fileline, macro in config.pop('macro', []):
956 key = macro[0]
957 value = macro[1:]
958 if not value:
959 fail(f'{fileline}Macro "{key}" does not have value')
960 macroline[key] = fileline
961 if value[0] == '+': # append
962 macros[key] = macros.get(key, []) + value[1:]
963 else:
964 if INTERNAL.command == 'check' and macros.get(key):
965 warning(
966 f'{fileline}Overwriting macro "{key}" '
967 f'with value "{" ".join(value)}"'
968 )
969 macros[key] = value # overwrite
970
971 # Expand macro in macro{}. Keep going as long as there was some expansion
972 # done.
973 while True:
974 found = False
975 for check, cvalue in macros.items():
976 for macro, mvalue in macros.items():
977 try:
978 pos = mvalue.index(check) # Full word expansion only
979 except ValueError:
980 continue
981 if check == macro: # Macro "foo" expands to "foo bar"
982 fail(
983 f'{macroline[macro]}Macro "{macro}" expands to '
984 f'itself: {" ".join(mvalue)}'
985 )
986 # Expand macro
987 macros[macro] = mvalue[:pos] + cvalue + mvalue[pos + 1 :]
988 found = True
989 if not found: # No new expansion was done
990 return macros
991
992
993 def macro_isdigit(word, separator):
994 """Check if word contains number after separator."""
995 if word.count(separator) != 1:
996 return False
997 return word.split(separator)[1].isdigit()
998
999
1000 def change_template_to_macro_in_rule(fileline, line, macros):
1001 """Change "template foo" to "_template_foo"."""
1002 pos = 0
1003 while pos < len(line) - 1:
1004 if line[pos] == 'template':
1005 del line[pos]
1006 macro_name = f'_template_{line[pos]}'
1007 if macro_name not in macros:
1008 fail(f'{fileline}Unknown template name: {line[pos]}')
1009 line[pos] = macro_name
1010 pos += 1
1011
1012
1013 def expand_single_line(fileline, line, macros):
1014 """Expand first macro in line. Repeat call to expand all.
1015
1016 Expansion can return multiple lines.
1017 Returns None if no expansion was done.
1018 """
1019 # Change "template foo" to macro call
1020 change_template_to_macro_in_rule(fileline, line, macros)
1021
1022 # Iterate words in line
1023 for pos, word in enumerate(line):
1024 # Cleanup "-macro", "macro/24", "[macro]:123" and "macro:123"
1025 # to prefix/macro/suffix parts
1026 word_prefix = word_suffix = ''
1027 if word[0] == '-' and len(word) > 1: # Negative IP address
1028 word = word[1:]
1029 word_prefix = '-'
1030 if macro_isdigit(word, '/'): # Netmask
1031 word, word_suffix = word.split('/')
1032 word_suffix = f'/{word_suffix}'
1033 if (
1034 word[0] == '['
1035 and macro_isdigit(word, ']:')
1036 and not word_prefix
1037 and not word_suffix
1038 ): # [IPv6]:port
1039 word, word_suffix = word[1:].split(']:')
1040 word_prefix = '['
1041 word_suffix = f']:{word_suffix}'
1042 if macro_isdigit(word, ':') and not word_suffix: # IPv4:port
1043 word, word_suffix = word.split(':')
1044 word_suffix = f':{word_suffix}'
1045
1046 # Check cleaned value
1047 if word not in macros:
1048 continue
1049
1050 # Found, add prefix/suffix to all macro's value
1051 mvalue = []
1052 for item in macros[word]:
1053 if item == ';':
1054 mvalue.append(item)
1055 else:
1056 mvalue.append(f'{word_prefix}{item}{word_suffix}')
1057
1058 # Expand mvalue to line and return list of expanded lines
1059 prefix = line[:pos]
1060 suffix = line[pos + 1 :]
1061 return [
1062 (fileline, prefix + list(group) + suffix)
1063 for is_split, group in itertools.groupby(
1064 mvalue, lambda spl: spl == ';'
1065 )
1066 if not is_split
1067 ]
1068 return None
1069
1070
1071 def expand_macros(config):
1072 """Expand all macros in all config sections."""
1073 macros = parse_config_macros(config)
1074 for section, orig_lines in config.items():
1075 if section.startswith('_'):
1076 continue # Internally used section, skip
1077
1078 if section in {'foomuuri', 'zone'}:
1079 # Don't expand macros in these sections, print warning instead.
1080 # This is for security reasons. Bad things will happen if
1081 # there is "xyz" in these sections and macro "xyz" is added
1082 # to default macros config.
1083 for line in orig_lines:
1084 for item in line[1]:
1085 if item in macros:
1086 warning(
1087 f'{line[0]}Macro "{item}" is not expanded in '
1088 f'"{section}" section: {" ".join(line[1])}'
1089 )
1090 continue
1091
1092 new_lines = []
1093 recursion_limit = collections.Counter()
1094 while orig_lines:
1095 # Get next line and expand macros there
1096 fileline, line = orig_lines.pop(0)
1097 expanded = expand_single_line(fileline, line, macros)
1098
1099 # Repeat call if some expansion was done
1100 if expanded:
1101 orig_lines = expanded + orig_lines
1102 else: # Not found, go to next line
1103 new_lines.append((fileline, line))
1104
1105 # Check for expansion loop
1106 recursion_limit.update([fileline])
1107 if recursion_limit[fileline] > CONFIG.recursion_limit:
1108 fail(
1109 f'{fileline}Possible macro or template loop '
1110 f'in "{section}": {" ".join(line[:10])} ...'
1111 )
1112 config[section] = new_lines
1113
1114
1115 def remove_quotes(config):
1116 """Change "foo" and 'foo' to foo in config files.
1117
1118 This function is called after macro expansion so that "ssh" is
1119 changed to ssh, not tcp 22.
1120 """
1121 for section, lines in config.items():
1122 if section.startswith('_'):
1123 continue
1124 for _dummy_fileline, line in lines:
1125 for index, item in enumerate(line):
1126 if len(item) >= 2 and (
1127 (item[0] == item[-1] == '"')
1128 or (item[0] == item[-1] == "'")
1129 ):
1130 line[index] = item[1:-1]
1131
1132
1133 def parse_config_foomuuri(config):
1134 """Parse foomuuri{} from config to CONFIG{}."""
1135 # pylint: disable=too-many-branches
1136 for fileline, line in config.pop('foomuuri', []):
1137 name = line[0]
1138 value = ' '.join(line[1:])
1139 if name == 'try-reload_timeout': # backward compatibility
1140 name = 'try_reload_timeout'
1141 warning(
1142 f'{fileline}foomuuri{{}} option "{line[0]}" is obsolete, '
1143 f'use "{name}" instead'
1144 )
1145 if name not in CONFIG:
1146 fail(f'{fileline}Unknown foomuuri{{}} option: {" ".join(line)}')
1147 try:
1148 if value.startswith('+ '): # append
1149 CONFIG.append_from_str(name, value[2:])
1150 else:
1151 CONFIG.set_from_str(name, value) # overwrite
1152 except (ValueError, TypeError):
1153 fail(
1154 f'{fileline}Invalid foomuuri{{}} option value:'
1155 f' {" ".join(line)}'
1156 )
1157
1158 # Overrides from CLI arguments
1159 for name, value in CONFIG_OVERRIDE.items():
1160 CONFIG.set_from_str(name, str(value))
1161
1162 # Reinitialize syslogging, foomuuri{} may have a different syslog setting
1163 syslog_open_or_close()
1164
1165 # Convert chain priority offset to nft
1166 if CONFIG.priority_offset == 0:
1167 INTERNAL.priority_offset = ''
1168 elif CONFIG.priority_offset > 0:
1169 INTERNAL.priority_offset = f' + {CONFIG.priority_offset}'
1170 else:
1171 INTERNAL.priority_offset = f' - {-CONFIG.priority_offset}'
1172
1173 # Add "packets" to log rates
1174 for key in list(CONFIG):
1175 if key.startswith('log_') and re.match(
1176 r'^\d+/(second|minute|hour) burst \d+$', CONFIG[key]
1177 ):
1178 CONFIG[key] += ' packets'
1179
1180
1181 def parse_config_zones(config):
1182 """Parse zone{} from config."""
1183 zones = {}
1184 INTERNAL.catch_all_interface_zone = ''
1185 for fileline, line in config.pop('zone', []):
1186 if not Validators.str_zone_name(line[0]):
1187 fail(f'{fileline}Invalid zone name: {" ".join(line)}')
1188 if line[0] in zones:
1189 fail(f'{fileline}Zone is already defined: {line[0]}')
1190 interface = [item for item in line[1:] if item != '*']
1191 zones[line[0]] = {'interface': interface}
1192 if '*' in line[1:]:
1193 if INTERNAL.catch_all_interface_zone:
1194 fail(
1195 f'{fileline}Catch all interface "*" is already assigned '
1196 f'to zone "{INTERNAL.catch_all_interface_zone}": '
1197 f'{" ".join(line)}'
1198 )
1199 INTERNAL.catch_all_interface_zone = line[0]
1200 return zones
1201
1202
1203 def parse_config_zonemap(config, zones):
1204 """Parse zonemap{} rules from config."""
1205 zonemap = []
1206 for fileline, line in config.pop('zonemap', []):
1207 rule = parse_rule_line((fileline, line))
1208 if not rule['new_dzone'] and not rule['new_szone']:
1209 fail(
1210 f'{fileline}Zonemap without "new_dzone" or "new_szone" '
1211 f'has no effect: {" ".join(line)}'
1212 )
1213 for key in ('szone', 'dzone', 'new_szone', 'new_dzone'):
1214 value = rule[key]
1215 if value and value not in zones:
1216 fail(
1217 f'{fileline}Zonemap "{key}" has unknown zone "{value}": '
1218 f'{" ".join(line)}'
1219 )
1220 zonemap.append(rule)
1221 return zonemap
1222
1223
1224 def parse_config_special_chains(config):
1225 """Parse snat{}, dnat{}, prerouting{} etc. rules from config."""
1226 chain_rules = {
1227 # output+mangle is needed for mark restore in output_special_chains()
1228 ('output', 'route', 'mangle' + INTERNAL.priority_offset): [],
1229 }
1230 for prefix, def_type, def_priority in (
1231 ('snat', 'nat', 'srcnat'),
1232 ('dnat', 'nat', 'dstnat'),
1233 ('prerouting', 'filter', 'mangle'),
1234 ('postrouting', 'filter', 'mangle'),
1235 ('forward', 'filter', 'mangle'),
1236 ('input', 'filter', 'mangle'),
1237 ('output', 'route', 'mangle'),
1238 ('invalid', None, None),
1239 ('rpfilter', None, None),
1240 ('smurfs', None, None),
1241 ):
1242 for section in list(config):
1243 if not (section == prefix or section.startswith(f'{prefix} ')):
1244 continue
1245
1246 fileline = config['_section_line'][section]
1247 items = section.split(' ', 2)
1248 if len(items) == 2:
1249 fail(
1250 f'{fileline}Invalid section: type or priority missing: '
1251 f'{section}'
1252 )
1253
1254 if len(items) == 1:
1255 ftype = def_type
1256 priority = None
1257 if def_priority:
1258 priority = def_priority + INTERNAL.priority_offset
1259 else:
1260 ftype = items[1]
1261 priority = items[2]
1262 if ftype not in {'filter', 'nat', 'route'}:
1263 fail(f'{fileline}Invalid section type: {section}')
1264
1265 lines = config.pop(section)
1266 chain_rules[prefix, ftype, priority] = [
1267 parse_rule_line(line) for line in lines
1268 ] # ty: ignore[invalid-assignment]
1269 return chain_rules
1270
1271
1272 def parse_config_hook(config):
1273 """Parse hook{} from config."""
1274 for fileline, line in config.pop('hook', []):
1275 if line[0] not in {
1276 'pre_start',
1277 'post_start',
1278 'pre_stop',
1279 'post_stop',
1280 }:
1281 fail(f'{fileline}Unknown hook: {" ".join(line)}')
1282 INTERNAL[line[0]] = line[1:]
1283
1284
1285 def minimal_config(require_etc_config=True):
1286 """Read and parse minimal config."""
1287 config = read_config(require_etc_config)
1288 expand_macros(config)
1289 remove_quotes(config)
1290 parse_config_foomuuri(config)
1291 return config
1292
1293
1294 def is_ipv4_address(value, allow_network=True):
1295 """Is value IPv4 address, network or interval."""
1296 if value.count('-') == 1: # Interval "IP-IP"
1297 try:
1298 addr_from, addr_to = value.split('-')
1299 return ipaddress.IPv4Address(addr_from) <= ipaddress.IPv4Address(
1300 addr_to
1301 )
1302 except ValueError:
1303 return False
1304
1305 try: # Address "IP"
1306 return isinstance(ipaddress.ip_address(value), ipaddress.IPv4Address)
1307 except ValueError:
1308 try: # Network "IP/mask"
1309 return (
1310 isinstance(
1311 ipaddress.ip_network(value, strict=False),
1312 ipaddress.IPv4Network,
1313 )
1314 and allow_network
1315 )
1316 except ValueError:
1317 return False
1318
1319
1320 def is_ipv6_address(value, allow_network=True):
1321 """Is value IPv6 address, network or interval."""
1322 # pylint: disable=too-many-return-statements
1323 if value.count('/-') == 1: # Suffix mask "IP/-mask"
1324 addr, maskstr = value.split('/-')
1325 try:
1326 mask = int(maskstr)
1327 return 0 <= mask <= 128 and ipaddress.IPv6Address(addr)
1328 except ValueError:
1329 return False
1330
1331 if value.count('-') == 1: # Interval "IP-IP"
1332 try:
1333 addr_from, addr_to = value.split('-')
1334 return ipaddress.IPv6Address(addr_from) <= ipaddress.IPv6Address(
1335 addr_to
1336 )
1337 except ValueError:
1338 return False
1339
1340 # Python's ipaddress library doesn't handle "[ipv6]" notation.
1341 # Strip [] before validating the address. nft handles [] fine, it will
1342 # strip them.
1343 if value.startswith('['):
1344 if value.endswith(']'): # "[ipv6]"
1345 value = value[1:-1]
1346 elif ']/' in value: # "[ipv6]/56"
1347 value = value[1:].replace(']/', '/')
1348
1349 try: # Address "IP"
1350 return isinstance(ipaddress.ip_address(value), ipaddress.IPv6Address)
1351 except ValueError:
1352 try: # Network "IP/mask"
1353 return (
1354 isinstance(
1355 ipaddress.ip_network(value, strict=False),
1356 ipaddress.IPv6Network,
1357 )
1358 and allow_network
1359 )
1360 except ValueError:
1361 return False
1362
1363
1364 def is_ip_address(value, *, allow_negative=True):
1365 """Check if value is IPv4 or IPv6 address, network or interval.
1366
1367 Return 4, 6, or 0 if not detected.
1368 """
1369 if value.startswith('-') and allow_negative:
1370 value = value[1:] # Negative is handled in single_or_set()
1371 if is_ipv4_address(value):
1372 return 4
1373 if is_ipv6_address(value):
1374 return 6
1375 return 0
1376
1377
1378 def is_port(value, protocol):
1379 """Check if value is port: "1", "1-2" or "1,2", or any combination.
1380
1381 This is used in parse_rule_line so protocol must specified.
1382 Allow special keywords for protocol icmp/icmpv6.
1383 """
1384 if not protocol:
1385 return False
1386 for item in value.split(','):
1387 if protocol in {'icmp', 'icmpv6'} and (
1388 # See: "nft describe icmp type; nft describe icmpv6 type"
1389 item == 'redirect' or re.match(r'^[a-z2]{2,}-[-a-z]{5,}$', item)
1390 ):
1391 continue
1392 for number in item.split('-'):
1393 if not number.isnumeric():
1394 return False
1395 return True
1396
1397
1398 def rule_item_only_one(rule, keyword, value):
1399 """Verify that keyword is set only once, or set to same value."""
1400 old = rule.get(f'_only_one_{keyword}', value)
1401 if old != value:
1402 fail(
1403 f"{rule['fileline']}Rule's {keyword} is already set to "
1404 f'"{old}": {" ".join(rule["line"])}'
1405 )
1406 rule[f'_only_one_{keyword}'] = value
1407
1408
1409 def verify_rule_sanity(rule, fileline):
1410 """Do some basic verify that single rule is valid."""
1411 # pylint: disable=too-many-branches
1412 for key, value in rule.items():
1413 if (
1414 value == '' # noqa: RUF100,PLC1901 -- it can be None too
1415 and key
1416 not in {'nat_to', 'queue', 'counter', 'log', 'fileline', 'line'}
1417 ):
1418 fail(f'{fileline}"{key}" without value is not valid')
1419
1420 for key in (
1421 'saddr_rate_name',
1422 'daddr_rate_name',
1423 'saddr_daddr_rate_name',
1424 'helper',
1425 'counter',
1426 'mss',
1427 ):
1428 value = rule[key] or ''
1429 if ' ' in value:
1430 fail(f'{fileline}"{key}" must be single word: {value}')
1431
1432 for key in ('iplist_add', 'iplist_update', 'iplist_delete'):
1433 value = rule[key]
1434 if value is None:
1435 continue
1436 if value.count(' ') != 1 or not value.startswith(
1437 ('saddr @', 'daddr @')
1438 ):
1439 fail(
1440 f'{fileline}"{key}" must be addrtype and iplist name: {value}'
1441 )
1442
1443 for key in ('global_rate', 'saddr_rate', 'daddr_rate', 'saddr_daddr_rate'):
1444 if not rule[key]:
1445 continue
1446 # Limit by packets:
1447 #
1448 # 3/second
1449 # 3/second burst 5
1450 # 3/second burst 5 packets
1451 # over 3/second
1452 # over 3/second burst 5
1453 # over 3/second burst 5 packets
1454 #
1455 # Limit by bytes:
1456 #
1457 # 10 mbytes/second
1458 # 10 mbytes/second burst 12000 kbytes
1459 # over 10 mbytes/second
1460 # over 10 mbytes/second burst 12000 kbytes
1461 #
1462 # Limit by connection count
1463 #
1464 # ct count 8
1465 # ct count over 8
1466 if re.match(
1467 r'^(over )?\d+/(second|minute|hour) burst \d+$', rule[key]
1468 ):
1469 rule[key] += ' packets' # Add missing "packets"
1470 if not (
1471 re.match(
1472 r'^(over )?'
1473 r'\d+/(second|minute|hour)'
1474 r'( burst \d+ packets)?$',
1475 rule[key],
1476 )
1477 or re.match(
1478 r'^(over )?'
1479 r'\d+ [km]?bytes/(second|minute|hour)'
1480 r'( burst \d+ [km]?bytes)?$',
1481 rule[key],
1482 )
1483 or re.match(r'^ct count (over )?\d+$', rule[key])
1484 ):
1485 fail(f'{fileline}Invalid "{key}" value: {rule[key]}')
1486
1487 for basic, extra in (
1488 ('protocol', 'sport'),
1489 ('protocol', 'dport'),
1490 ('saddr_rate', 'saddr_rate_name'),
1491 ('saddr_rate', 'saddr_rate_mask'),
1492 ('daddr_rate', 'daddr_rate_name'),
1493 ('daddr_rate', 'daddr_rate_mask'),
1494 ('saddr_daddr_rate', 'saddr_daddr_rate_name'),
1495 ('saddr_daddr_rate', 'saddr_daddr_rate_mask'),
1496 ):
1497 if rule[extra] and not rule[basic]:
1498 fail(f'{fileline}"{extra}" without "{basic}" is not valid')
1499
1500 if not rule['nat_to'] and rule['statement'] in {
1501 'snat',
1502 'dnat',
1503 'snat_prefix',
1504 'dnat_prefix',
1505 }:
1506 fail(f'{fileline}"{rule["statement"]}" without address is not valid')
1507
1508 if rule['ct_status'] and rule['ct_status'] not in {
1509 'expected',
1510 'seen-reply',
1511 'assured',
1512 'confirmed',
1513 'snat',
1514 'dnat',
1515 'dying',
1516 }:
1517 fail(f'{fileline}"Invalid "ct_status" value: {rule["ct_status"]}')
1518
1519
1520 def parse_rule_line(fileline_line):
1521 """Parse single config section line to rule dict.
1522
1523 This parser is quite relaxed. Words can be in almost any order. For
1524 example, all following entries are equal:
1525 tcp 22 log <- preferred
1526 tcp 22 accept log
1527 accept tcp 22 log
1528 log tcp accept 22
1529 """
1530 # pylint: disable=too-many-branches
1531 # pylint: disable=too-many-statements
1532 fileline, line = fileline_line
1533 ret = {
1534 # Basic rules
1535 'statement': 'accept',
1536 'cast': 'unicast',
1537 'protocol': None,
1538 'saddr': None,
1539 'sport': None,
1540 'daddr': None,
1541 'dport': None,
1542 'oifname': None,
1543 'iifname': None,
1544 'mac_saddr': None,
1545 'mac_daddr': None,
1546 # Is this IPv4/6 specific rule?
1547 'ipv4': False,
1548 'ipv6': False,
1549 # Rate limits
1550 'global_rate': None,
1551 'saddr_rate': None,
1552 'saddr_rate_mask': None,
1553 'saddr_rate_name': None,
1554 'daddr_rate': None,
1555 'daddr_rate_mask': None,
1556 'daddr_rate_name': None,
1557 'saddr_daddr_rate': None,
1558 'saddr_daddr_rate_mask': None,
1559 'saddr_daddr_rate_name': None,
1560 # User limits
1561 'uid': None,
1562 'gid': None,
1563 # Zonemap specific rules
1564 'szone': None,
1565 'dzone': None,
1566 'new_szone': None,
1567 'new_dzone': None,
1568 # Misc rules
1569 'nat_to': None, # snat/dnat to
1570 'queue': None, # optional queue flags
1571 'counter': None,
1572 'helper': None,
1573 'sipsec': None,
1574 'dipsec': None,
1575 'log': None,
1576 'log_level': None,
1577 'nft': None,
1578 'mss': None,
1579 'template': None,
1580 'tproxy': None,
1581 'mark_set': None,
1582 'mark_match': None,
1583 'priority_set': None,
1584 'priority_match': None,
1585 'iplist_add': None,
1586 'iplist_update': None,
1587 'iplist_delete': None,
1588 'dscp': None,
1589 'cgroup': None,
1590 'ct_status': None,
1591 'time': None,
1592 'after_conntrack': True,
1593 'warning': None, # Print warning to user
1594 # Internal housekeeping
1595 'plain': True, # Plain "log" or "counter" without anything else
1596 'fileline': fileline, # For error messages
1597 'line': line, # Original line for error messages
1598 }
1599
1600 keyword = None
1601 for item in line:
1602 if item == ';':
1603 fail(
1604 f'{fileline}";" is not supported in rule, split it to '
1605 f'separate lines: {" ".join(line)}'
1606 )
1607
1608 # "tcp 22" is shortcut for "tcp dport 22"
1609 if not keyword and is_port(item, ret['protocol']):
1610 keyword = 'dport'
1611 ret['plain'] = False
1612 if ret[keyword] is None:
1613 ret[keyword] = ''
1614
1615 # Version 0.29 deprecates 'to' after 'snat' statement, ignore it
1616 if keyword == 'nat_to' and not ret['nat_to'] and item == 'to':
1617 continue
1618
1619 # First item after start keyword is always a parameter for it, except
1620 # for "log" or "counter". Log will have good default value if not
1621 # defined. Counter without parameter will create anonymous counter.
1622 if keyword and not ret[keyword] and keyword not in {'log', 'counter'}:
1623 ret[keyword] = item
1624 if keyword == 'protocol': # Single word only
1625 keyword = None
1626
1627 # Non-start keywords
1628 elif item in {
1629 'accept',
1630 'drop',
1631 'return',
1632 'continue',
1633 'masquerade',
1634 'notrack',
1635 'nftrace',
1636 }:
1637 rule_item_only_one(ret, 'statement', item)
1638 ret['statement'] = item
1639 ret['plain'] = False
1640 keyword = None
1641 elif item == 'reject':
1642 rule_item_only_one(ret, 'statement', item)
1643 ret['statement'] = 'reject with icmpx admin-prohibited'
1644 ret['plain'] = False
1645 keyword = None
1646 elif item in {'multicast', 'broadcast'}:
1647 rule_item_only_one(ret, 'cast', item)
1648 ret['cast'] = item
1649 ret['plain'] = False
1650 keyword = None
1651 elif item in {'tcp', 'udp', 'icmp', 'icmpv6', 'igmp', 'esp'}:
1652 # "igmp" and "esp" are for backward compability (v0.21)
1653 rule_item_only_one(ret, 'protocol', item)
1654 ret['protocol'] = item
1655 ret['plain'] = False
1656 keyword = None
1657 elif item in {'ipv4', 'ipv6'}:
1658 ret[item] = True
1659 ret['plain'] = False
1660 keyword = None
1661 elif item in {'sipsec', 'dipsec'}:
1662 ret[item] = 'exists'
1663 ret['plain'] = False
1664 keyword = None
1665 elif item in {'-sipsec', '-dipsec'}:
1666 ret[item[1:]] = 'missing'
1667 ret['plain'] = False
1668 keyword = None
1669 elif item in {'conntrack', '-conntrack'}:
1670 rule_item_only_one(ret, 'conntrack', item)
1671 ret['after_conntrack'] = item == 'conntrack'
1672 ret['plain'] = False
1673 keyword = None
1674
1675 # Start keywords
1676 elif item in {'snat', 'dnat', 'snat_prefix', 'dnat_prefix'}:
1677 rule_item_only_one(ret, 'statement', item)
1678 ret['statement'] = item
1679 ret['plain'] = False
1680 keyword = 'nat_to'
1681 ret[keyword] = ret[keyword] or ''
1682 elif item in {
1683 'protocol',
1684 'saddr',
1685 'sport',
1686 'daddr',
1687 'dport',
1688 'oifname',
1689 'iifname',
1690 'mac_saddr',
1691 'mac_daddr',
1692 'global_rate',
1693 'saddr_rate',
1694 'saddr_rate_mask',
1695 'saddr_rate_name',
1696 'daddr_rate',
1697 'daddr_rate_mask',
1698 'daddr_rate_name',
1699 'saddr_daddr_rate',
1700 'saddr_daddr_rate_mask',
1701 'saddr_daddr_rate_name',
1702 'uid',
1703 'gid',
1704 'szone',
1705 'dzone',
1706 'new_szone',
1707 'new_dzone',
1708 'counter',
1709 'helper',
1710 'log',
1711 'log_level',
1712 'nft',
1713 'mss',
1714 'template',
1715 'queue',
1716 'tproxy',
1717 'mark_set',
1718 'mark_match',
1719 'priority_set',
1720 'priority_match',
1721 'iplist_add',
1722 'iplist_update',
1723 'iplist_delete',
1724 'dscp',
1725 'cgroup',
1726 'ct_status',
1727 'time',
1728 'warning',
1729 }:
1730 keyword = item
1731 ret[keyword] = ret[keyword] or ''
1732 if item == 'queue': # statement and start keyword
1733 rule_item_only_one(ret, 'statement', item)
1734 ret['statement'] = item
1735 if item not in {'counter', 'log', 'log_level'}:
1736 ret['plain'] = False
1737
1738 # More parameters for keyword
1739 elif keyword:
1740 if ret[keyword]:
1741 ret[keyword] += ' ' # ty: ignore[unsupported-operator]
1742 ret[keyword] += item
1743
1744 # Unknown word after non-start keyword
1745 else:
1746 fail(f"{fileline}Can't parse line: {' '.join(line)}")
1747
1748 # Use no-op statement "continue" for plain "log" or "counter" rule,
1749 # everything else defaults to "accept". Also mark them as -conntrack
1750 # so that they really log/count everything.
1751 if ret['plain']:
1752 ret['statement'] = 'continue'
1753 ret['after_conntrack'] = False
1754 verify_rule_sanity(ret, fileline)
1755
1756 # Print warning, used mostly in old macros
1757 if ret['warning']:
1758 warning(f'{fileline}{ret["warning"]}')
1759 return ret
1760
1761
1762 def parse_config_rules(config):
1763 """Parse "zone-zone" rules from config.
1764
1765 All other sections must be already parsed and removed from config.
1766 """
1767 rules = {}
1768 for section, lines in config.items():
1769 if section.startswith('_') or section in {'resolve', 'iplist'}:
1770 continue
1771 try:
1772 szone, dzone = section.split('-')
1773 except ValueError:
1774 fail(
1775 f'{config["_section_line"][section]}Unknown section: {section}'
1776 )
1777 rules[szone, dzone] = [parse_rule_line(line) for line in lines]
1778
1779 # Allow multicast + broadcast if last rule is "accept".
1780 # These can be added to the end as they are processed in
1781 # correct order in output_zone().
1782 if rules[szone, dzone] and rules[szone, dzone][-1]['line'] == [
1783 'accept'
1784 ]:
1785 rules[szone, dzone].extend(
1786 [
1787 parse_rule_line(('', ['multicast'])),
1788 parse_rule_line(('', ['broadcast'])),
1789 ]
1790 )
1791
1792 return rules
1793
1794
1795 def filter_any_zonelist(rule, srcdst):
1796 """Parse rule[szone] list and return pos/neg boolean + zonelist."""
1797 if not rule[srcdst]:
1798 return False, [] # "not in empty list" == everything
1799
1800 inside = True
1801 zonelist = []
1802 for item in rule[srcdst].split():
1803 if item.startswith('-'):
1804 if inside and zonelist:
1805 fail(
1806 f'{rule["fileline"]}Can\'t mix "+" and "-" items: '
1807 f'{rule[srcdst]}'
1808 )
1809 inside = False
1810 zonelist.append(item[1:])
1811 else:
1812 if not inside:
1813 fail(
1814 f'{rule["fileline"]}Can\'t mix "+" and "-" items: '
1815 f'{rule[srcdst]}'
1816 )
1817 zonelist.append(item)
1818 return inside, zonelist
1819
1820
1821 def insert_single_any(any_rules, rules, szone, dzone):
1822 """Insert single any_rules to rules[(szone, dzone)]."""
1823 if szone == dzone == CONFIG.localhost_zone:
1824 return # Don't insert to localhost-localhost
1825
1826 # Filter out "szone -public" when adding to zone "public-xxx"
1827 filtered = []
1828 for rule in any_rules:
1829 inside, zonelist = filter_any_zonelist(rule, 'szone')
1830 if (szone in zonelist) != inside:
1831 continue
1832 inside, zonelist = filter_any_zonelist(rule, 'dzone')
1833 if (dzone in zonelist) != inside:
1834 continue
1835 filtered.append(rule)
1836
1837 # Insert filtered rules to beginning
1838 if filtered:
1839 rules[szone, dzone] = filtered + rules.get((szone, dzone), [])
1840
1841
1842 def insert_any_zones(zones, rules):
1843 """Insert "any-zone", "zone-any" and "any-any" rules to "zone-zone" rules.
1844
1845 These are inserted to beginning of zone-zone rules.
1846 """
1847 for zone in zones:
1848 any_rules = rules.pop(('any', zone), []) # any-zone
1849 for szone in zones:
1850 insert_single_any(any_rules, rules, szone, zone)
1851
1852 any_rules = rules.pop((zone, 'any'), []) # zone-any
1853 for dzone in zones:
1854 insert_single_any(any_rules, rules, zone, dzone)
1855
1856 any_rules = rules.pop(('any', 'any'), []) # any-any
1857 for szone in zones:
1858 for dzone in zones:
1859 insert_single_any(any_rules, rules, szone, dzone)
1860
1861
1862 def verify_config(config, zones, rules):
1863 """Verify config data."""
1864 if not zones:
1865 fail('No zones defined in section zone{}')
1866
1867 localhost = CONFIG.localhost_zone
1868 if localhost not in zones:
1869 zones[localhost] = {'interface': []}
1870 warning(
1871 f'{config["_section_line"]["zone"]}Zone "{localhost}" '
1872 f'is missing from zone{{}}, adding it'
1873 )
1874
1875 if zones[localhost]['interface']:
1876 fail(
1877 f'{config["_section_line"]["zone"]}Zone "{localhost}" has '
1878 f'interfaces "{" ".join(zones[localhost]["interface"])}", '
1879 f'it must be empty'
1880 )
1881
1882 if CONFIG.dbus_zone not in zones:
1883 warning(
1884 f'Config option dbus_zone value '
1885 f'"{CONFIG.dbus_zone}" is missing from zone{{}}'
1886 )
1887
1888 # All zone-zone pairs must be known
1889 for szone, dzone in rules:
1890 if szone not in zones or dzone not in zones:
1891 fileline = config['_section_line'][f'{szone}-{dzone}']
1892 fail(f'{fileline}Unknown zone-zone: {szone}-{dzone}')
1893
1894 # Make sure all zone-zone pairs are defined. They are needed for
1895 # "ct established" return packets and for dynamic interface-to-zone
1896 # binding via D-Bus.
1897 # Add final rule to all zone-zone pairs, even if there already is
1898 # one. It will be optimized out later.
1899 for szone in zones:
1900 for dzone in zones:
1901 if (szone, dzone) not in rules:
1902 rules[szone, dzone] = []
1903 if szone == dzone == localhost:
1904 rule = ['accept'] # localhost-localhost is accept
1905 elif szone == localhost:
1906 rule = ['reject', 'log'] # localhost-foo is reject
1907 else:
1908 rule = ['drop', 'log'] # everything else is drop
1909 rules[szone, dzone].append(parse_rule_line(('', rule)))
1910
1911
1912 def output_rate_names(rules):
1913 """Output empty saddr_rate sets to ruleset."""
1914 counter = 1
1915 already_added = set()
1916 for rulelist in rules.values():
1917 for rule in rulelist:
1918 for rate in ('saddr_rate', 'daddr_rate', 'saddr_daddr_rate'):
1919 if not rule[rate]:
1920 continue
1921
1922 # Rule with rate found. It can be pre-named or anonymous.
1923 setname = rule[f'{rate}_name']
1924 if setname in already_added:
1925 continue # Pre-named and already added
1926 if not setname: # Anonymous - invent a name for it
1927 setname = rule[f'{rate}_name'] = f'_rate_set_{counter}'
1928 counter += 1
1929 already_added.add(setname)
1930
1931 # Output empty sets for IPv4 and IPv6. These will have
1932 # one minute timeout, unless rate is specified per hour.
1933 for ipv in (4, 6):
1934 out(f'set {setname}_{ipv} {{')
1935 if rate == 'saddr_daddr_rate':
1936 out(f'type ipv{ipv}_addr . ipv{ipv}_addr')
1937 else:
1938 out(f'type ipv{ipv}_addr')
1939 out(f'size {CONFIG.set_size}')
1940 if rule[rate].startswith('ct '):
1941 out('flags dynamic')
1942 elif 'hour' in rule[rate]:
1943 out('flags dynamic,timeout')
1944 out('timeout 1h')
1945 out('gc-interval 5m') # Default is timeout/4
1946 else:
1947 out('flags dynamic,timeout')
1948 out('timeout 1m')
1949 out('}')
1950
1951
1952 def suffix_mask(value, compare):
1953 """Convert suffix mask "IP/-mask" to nft."""
1954 if not compare:
1955 compare = '== '
1956 addr, mask = value.split('/-')
1957 mask = f'{pow(2, int(mask)) - 1:x}' # As long hex
1958 splitted = '' # Convert to ":1234" parts
1959 while mask:
1960 splitted = f':{mask[-4:]}{splitted}'
1961 mask = mask[:-4]
1962 return f'& :{splitted} {compare}{addr}'
1963
1964
1965 def single_or_set(
1966 data: typing.Union[str, list, set],
1967 fileline: str = '',
1968 quote: bool = False,
1969 ) -> str:
1970 """Convert data to nft string containing single item or set."""
1971 # Convert to list
1972 if isinstance(data, str):
1973 values = data.split()
1974 elif isinstance(data, list):
1975 values = data
1976 else:
1977 values = list(data)
1978
1979 # Handle negative: add "!=" to final rule
1980 neg = ''
1981 for index, value in enumerate(values):
1982 if value.startswith('-'):
1983 if index and not neg:
1984 fail(
1985 f'{fileline}Can\'t mix "+" and "-" items: '
1986 f'{" ".join(values)}'
1987 )
1988 neg = '!= '
1989 values[index] = value.removeprefix('-')
1990 elif neg:
1991 fail(f'{fileline}Can\'t mix "+" and "-" items: {" ".join(values)}')
1992
1993 # Quote interface names and similar items. "inet" is a reserved word
1994 # but can also be an interface name. It must be quoted.
1995 if quote:
1996 values = [
1997 value if value.isnumeric() else f'"{value}"' for value in values
1998 ]
1999
2000 # Single item
2001 if len(values) == 1 and ' ' not in values[0]:
2002 if '/-' in values[0]:
2003 return suffix_mask(values[0], neg)
2004 return neg + values[0]
2005
2006 # nft doesn't support "saddr { @foo, @bar }"
2007 if any(value.startswith('@') for value in values):
2008 fail(f'{fileline}Only one @iplist can be used: {" ".join(values)}')
2009
2010 # Multiple, use "{set}"
2011 return f'{neg}{{ {", ".join(sorted(set(values)))} }}'
2012
2013
2014 def netmask_to_and(masklist, ipv, fileline):
2015 """Parse "masklist 24 56" rule and return "and 255.255.255.0 " string.
2016
2017 First value is mask for IPv4 and second is for IPv6.
2018 """
2019 if not masklist:
2020 return ''
2021 masks = [int(item) for item in masklist.split() if item.isnumeric()]
2022 if len(masks) != 2 or masks[0] > 32 or masks[1] > 128:
2023 fail(f'{fileline}Invalid rate_mask: {masklist}')
2024
2025 if ipv == 4:
2026 ipaddr = ipaddress.IPv4Network(f'0.0.0.0/{masks[0]}')
2027 return f'and {ipaddr.netmask} '
2028
2029 ipaddr = ipaddress.IPv6Network(f'::/{masks[1]}')
2030 return f'and {ipaddr.netmask} '
2031
2032
2033 def limit_rate_or_ct(rate):
2034 """Return "limit rate x" or "ct count x" according to rate."""
2035 if rate.startswith('ct '):
2036 return f'{rate} '
2037 return f'limit rate {rate} '
2038
2039
2040 def rule_rate_limit(rule, ipv):
2041 """Return rule's rate limits as nft update-command."""
2042 if rule['global_rate']:
2043 return limit_rate_or_ct(rule['global_rate'])
2044
2045 ret = ''
2046 for rate in ('saddr_rate', 'daddr_rate', 'saddr_daddr_rate'):
2047 rate_limit = rule[rate]
2048 if not rate_limit:
2049 continue
2050 rate_name = rule[f'{rate}_name']
2051 rate_mask = rule[f'{rate}_mask']
2052
2053 # "update @foo { ip saddr "
2054 ret += 'add' if rate_limit.startswith('ct ') else 'update'
2055 ret += f' @{rate_name}_{ipv} {{ '
2056 if 'saddr' in rate:
2057 ret += f'{"ip" if ipv == 4 else "ip6"} saddr '
2058 ret += netmask_to_and(rate_mask, ipv, rule['fileline'])
2059 if rate == 'saddr_daddr_rate':
2060 ret += '. '
2061 if 'daddr' in rate:
2062 ret += f'{"ip" if ipv == 4 else "ip6"} daddr '
2063 ret += netmask_to_and(rate_mask, ipv, rule['fileline'])
2064 # "limit rate 3/second } "
2065 ret += f'{limit_rate_or_ct(rate_limit)}}} '
2066 return ret
2067
2068
2069 def mark_set_argument(rule):
2070 """Convert "x" to "x", "x/y" to "mark and ~y or x"."""
2071 value = rule['mark_set']
2072 x_y = value.split('/')
2073 if len(x_y) > 2:
2074 fail(f'{rule["fileline"]}Invalid "mark_set" value: {value}')
2075 if len(x_y) == 1:
2076 return value
2077
2078 # Convert "/0xff00" to "/0xffff00ff" so that same mask is used in
2079 # set and match operations in config. Nftables requires 0xffff00ff.
2080 try:
2081 mask = int(x_y[1], 0) # dec or hex
2082 except ValueError:
2083 fail(f'{rule["fileline"]}Invalid "mark_set" value: {value}')
2084 return f'meta mark & {hex(0xFFFFFFFF ^ mask)} | {x_y[0]}'
2085
2086
2087 def mark_match(rule):
2088 """Convert "x" to "== x", "x/y" to "and y == x".
2089
2090 Negative "-x" can also be used to get "!= x".
2091 """
2092 value = rule['mark_match']
2093 if not value:
2094 return ''
2095 check = '=='
2096 if value.startswith('-'):
2097 check = '!='
2098 value = value[1:]
2099 x_y = value.split('/')
2100 if len(x_y) > 2:
2101 fail(
2102 f'{rule["fileline"]}Invalid "mark_match" value: '
2103 f'{rule["mark_match"]}'
2104 )
2105 if len(x_y) == 1:
2106 return f'meta mark {check} {value} '
2107 return f'meta mark & {x_y[1]} {check} {x_y[0]} '
2108
2109
2110 def time_only_one(rule, oldvalue, newvalue):
2111 """Return newvalue if oldvalue is not set, else fail."""
2112 if oldvalue or not newvalue:
2113 fail(f'{rule["fileline"]}Invalid "time" value: {rule["time"]}')
2114 return newvalue
2115
2116
2117 def time_match_single(rule, compare, value):
2118 """Return single time compare+value as nft."""
2119 if not compare and not value:
2120 return ''
2121 time_only_one(rule, None, value) # Check that value is set
2122
2123 # Parse value to day/hour/time
2124 wday = None
2125 hour = None
2126 date = None
2127 for item in value:
2128 if item.lower() in {
2129 'monday',
2130 'tuesday',
2131 'wednesday',
2132 'thursday',
2133 'friday',
2134 'saturday',
2135 'sunday',
2136 }:
2137 wday = time_only_one(rule, wday, item.capitalize())
2138 elif (
2139 re.fullmatch(r'\d\d:\d\d(:\d\d)?', item) # hh:mm:ss, hh:mm
2140 or re.fullmatch(r'\d\d:\d\d-\d\d:\d\d', item) # hh:mm-hh:mm
2141 ):
2142 hour = time_only_one(rule, hour, item)
2143 elif re.fullmatch(r'\d\d\d\d-\d\d-\d\d', item): # yyyy-mm-dd
2144 date = time_only_one(rule, date, item)
2145 else:
2146 fail(f'{rule["fileline"]}Invalid "time" value: {rule["time"]}')
2147
2148 # Output as nft
2149 if date and hour: # Combine if both set
2150 date += f' {hour}'
2151 hour = None
2152 ret = ''
2153 if wday:
2154 ret += f'day {compare}"{wday}" '
2155 if date:
2156 ret += f'time {compare}"{date}" '
2157 if hour:
2158 if '-' in hour: # hh:mm-hh:mm may not be in ", others must
2159 ret += f'hour {compare}{hour} '
2160 else:
2161 ret += f'hour {compare}"{hour}" '
2162 return ret
2163
2164
2165 def time_match(rule):
2166 """Convert "time Saturday" to nft, supporting time/day/hour."""
2167 if not rule['time']:
2168 return ''
2169 compare = ''
2170 value = []
2171 ret = ''
2172 for item in rule['time'].split():
2173 if item in {'==', '!=', '<', '>', '<=', '>='}:
2174 ret += time_match_single(rule, compare, value)
2175 compare = '' if item == '==' else f'{item} '
2176 value = []
2177 else:
2178 value.append(item)
2179 ret += time_match_single(rule, compare, value)
2180 return ret
2181
2182
2183 def cgroup_match(rule):
2184 """Parse cgroup to nft."""
2185 cgroup = rule['cgroup']
2186 if not cgroup:
2187 return ''
2188
2189 # cgroupv2: non-numeric, single value only
2190 if not re.match(r'[-0-9]', cgroup[0]) and ' ' not in cgroup:
2191 level = cgroup.count('/') + 1
2192 return f'socket cgroupv2 level {level} "{cgroup}" '
2193
2194 # cgroup
2195 return f'meta cgroup {single_or_set(cgroup, rule["fileline"])} '
2196
2197
2198 def rule_statement(
2199 szone, dzone, rule, ipv, *, force_statement=None, do_lograte=True
2200 ):
2201 """Return rule's rate, log and statement as nft command."""
2202 # pylint: disable=too-many-arguments
2203
2204 # Map internal statement to nft statement
2205 if rule['mss']:
2206 mss = 'rt mtu' if rule['mss'] == 'pmtu' else rule['mss']
2207 statement = f'tcp flags syn tcp option maxseg size set {mss}'
2208 else:
2209 statement = force_statement or {
2210 'snat_prefix': 'snat',
2211 'dnat_prefix': 'dnat',
2212 'nftrace': 'meta nftrace set 1',
2213 }.get(rule['statement'], rule['statement'])
2214
2215 # queue can have optional flags
2216 if statement == 'queue' and rule[statement]:
2217 statement = f'{statement} {rule[statement]}'
2218
2219 # Rate, counter and log goes before statement
2220 prefix = rule_rate_limit(rule, ipv)
2221
2222 # "counter" in single rule line adds counter to it.
2223 #
2224 # "foomuuri { counter xxx }" can be used to add counters to all rules:
2225 # yes - add to all rules in all zone-zone
2226 # zone-zone - add to all rules in single zone-zone
2227 # zone-any - add to all rules in all zone-*
2228 # any-zone - add to all rules in all *-zone
2229 # Multiple zone-pairs can be defined
2230 checklist = {'yes', f'{szone}-{dzone}', f'{szone}-any', f'any-{dzone}'}
2231 if not rule['nft'] and (
2232 rule['counter'] is not None # "counter" in this single rule
2233 or not checklist.isdisjoint(CONFIG.counter) # zone level enable
2234 ):
2235 prefix += 'counter '
2236 if rule['counter']:
2237 prefix += f'name "{rule["counter"]}" '
2238
2239 # "log" in rule will log all packets matching this rule. This is usually
2240 # used in final "drop log" rule. Default log prefix is
2241 # "zone-zone STATEMENT".
2242 if rule['log'] is not None or rule['log_level'] is not None:
2243 log_prefix = CONFIG.log_prefix
2244 if rule['log']:
2245 if rule['log'].startswith('+ '):
2246 log_prefix += rule['log'][2:]
2247 else:
2248 log_prefix = rule['log']
2249 log_prefix = log_prefix.replace('$(szone)', szone)
2250 log_prefix = log_prefix.replace('$(dzone)', dzone)
2251 log_prefix = log_prefix.replace(
2252 '$(statement)', statement.split()[0].upper()
2253 )
2254 log_level = (
2255 CONFIG.log_level
2256 if rule['log_level'] is None
2257 else rule['log_level']
2258 )
2259 log_nft = f'log prefix "{log_prefix} " {log_level}'
2260 if do_lograte and CONFIG.log_rate:
2261 # Limit maximum amount of logging to "foomuuri { log_rate }".
2262 # This is important to avoid overlogging (DoS or filesystem full).
2263 rate = (
2264 f'update @_lograte_set_{ipv} '
2265 f'{{ {"ip" if ipv == 4 else "ip6"} saddr '
2266 f'limit rate {CONFIG.log_rate} }} '
2267 )
2268 if statement == 'continue':
2269 return f'{prefix}{rate}{log_nft}'
2270 logname = f'lograte_{len(LOGRATES) + 1}'
2271 LOGRATES[logname] = (f'{rate}{log_nft}', statement)
2272 return f'{prefix}jump {logname}'
2273 prefix += f'{log_nft} '
2274
2275 return prefix + statement
2276
2277
2278 def output_icmp(szone, dzone, rules, ipv):
2279 """Find and parse icmp and icmpv6 rules.
2280
2281 These must be handled before ct as "ct established" would accept
2282 ping floods. Default is to drop pings.
2283 """
2284 has_ping_rule = False
2285 has_match_all = False
2286 if ipv == 4:
2287 icmp = 'icmp'
2288 ping = '8'
2289 else:
2290 icmp = 'icmpv6'
2291 ping = '128'
2292 for rule in rules:
2293 if rule['protocol'] != icmp:
2294 continue
2295
2296 match = rule_matchers(rule, ipv, skip_icmp=False)
2297 if match is None:
2298 continue
2299 statement = rule_statement(szone, dzone, rule, ipv)
2300 out(f'{match}{statement}')
2301
2302 # Continue to next rule if this wasn't type echo-request (ping) or all
2303 if (
2304 rule['dport']
2305 and ping not in rule['dport'].split()
2306 and 'echo-request' not in rule['dport'].split()
2307 ):
2308 continue
2309
2310 # This rule was for ping, usually accepting non-flood pings. Add
2311 # explicit rule to drop overflow and all other pings.
2312 # Skip adding drop rule if this rule is "match all" as then drop is
2313 # never reached. This simple check doesn't catch all types of
2314 # "match all".
2315 has_ping_rule = True
2316 if match in {
2317 'ip protocol icmp ',
2318 'ip6 nexthdr icmpv6 ',
2319 f'{icmp} type {ping} ',
2320 f'{icmp} type echo-request ',
2321 } and statement.startswith(
2322 ('accept', 'drop', 'reject', 'jump', 'queue')
2323 ):
2324 has_match_all = True
2325 elif has_match_all: # Specific ping rule after match-all rule
2326 warning(f'{rule["fileline"]}Unreachable ping rule')
2327
2328 # Overflow-pings must be dropped before ct
2329 if has_ping_rule and not has_match_all:
2330 out(f'{icmp} type echo-request drop')
2331
2332 # Allow needed icmp
2333 out(f'jump allow_icmp_{ipv}')
2334
2335
2336 def parse_iplist(rule, direction, ipv):
2337 """Parse IP address list in rule[direction] to nft rule."""
2338 iplist = rule[direction]
2339 if not iplist:
2340 return ''
2341
2342 ips = []
2343 for item in iplist.split():
2344 if item.startswith(('@', '-@')): # "@foo" to "@foo_4"
2345 ips.append(f'{item}_{ipv}')
2346 else:
2347 ipv_addr = is_ip_address(item)
2348 if ipv_addr == ipv: # Address for this ipv - add to list
2349 ips.append(item)
2350 elif not ipv_addr: # Invalid IP address
2351 fail(
2352 f'{rule["fileline"]}Invalid IP address "{item}" in: '
2353 f'{iplist}'
2354 )
2355
2356 # No matching addresses for this ipv family
2357 if not ips:
2358 raise ValueError
2359
2360 # Return "ip saddr 10.2.3.4 " string
2361 return (
2362 f'{"ip" if ipv == 4 else "ip6"} {direction} '
2363 f'{single_or_set(ips, rule["fileline"])} '
2364 )
2365
2366
2367 def parse_maclist(rule, direction):
2368 """Parse MAC address list in rule[direction] to nft rule."""
2369 maclist = rule[direction]
2370 if not maclist:
2371 return ''
2372
2373 macs = []
2374 for item in maclist.split():
2375 item = item.lower()
2376 if re.match(r'^(-)?([0-9a-f]{2}:){5}[0-9a-f]{2}$', item):
2377 macs.append(item)
2378 else:
2379 fail(
2380 f'{rule["fileline"]}Invalid MAC address "{item}" in: {maclist}'
2381 )
2382
2383 # Return "ether saddr 0a:00:27:00:00:00 " string
2384 return f'ether {direction[4:]} {single_or_set(macs, rule["fileline"])} '
2385
2386
2387 def parse_interface_names(rule):
2388 """Parse iifname/oifname to nft rule.
2389
2390 Interface "lo" is special:
2391 - "iifname lo" or "iif lo" doesn't work in most chains. This is also
2392 visible in kernel logging: "IN=<empty>". Use "iif 0" instead. It works
2393 on all chains.
2394 - "oifname lo" never works. "oif 0" works on all but prerouting (dnat)
2395 as all packets have "OUT=<empty>" there. Use "fib daddr type local"
2396 which works on all chains.
2397 """
2398 ret = ''
2399 for key in ('iifname', 'oifname'):
2400 if not rule[key]:
2401 continue
2402 if key == 'iifname' and rule[key] == 'lo':
2403 ret += 'iif 0 '
2404 elif key == 'oifname' and rule[key] == 'lo':
2405 ret += 'fib daddr type local '
2406 else:
2407 ret += f'{key} '
2408 ret += single_or_set(rule[key], rule['fileline'], quote=True)
2409 ret += ' '
2410 return ret
2411
2412
2413 def parse_protocol_ports(rule, ipv, skip_icmp=True):
2414 """Parse tcp/udp sport/dport to nft rule.
2415
2416 This can also handle rules like:
2417 - "tcp" without dport to nft "protocol tcp"
2418 - "protocol esp" to nft "protocol esp"
2419 - "protocol esp 123" to nft "esp spi 123"
2420 - "protocol vlan 123" to nft "vlan id 123"
2421 """
2422 protocol = rule['protocol']
2423 if not protocol or (skip_icmp and protocol in {'icmp', 'icmpv6'}):
2424 # Protocol is empty for rules like "drop log" and "dnat"
2425 # icmp is handled in output_icmp()
2426 return ''
2427
2428 # ICMP is for IPv4, ICMPv6 is for IPv6.
2429 # IPv6 uses Multicast Listener Discovery ICMP instead of IGMP.
2430 if (ipv == 4 and protocol == 'icmpv6') or (
2431 ipv == 6 and protocol in {'icmp', 'igmp'}
2432 ):
2433 return None
2434
2435 ports = ''
2436 for key in ('sport', 'dport'):
2437 if rule[key]:
2438 protokey = key
2439 if key == 'dport': # Change "dport" to protocol-specific key
2440 protokey = {
2441 'ip': 'protocol',
2442 'ip6': 'nexthdr',
2443 'ah': 'spi',
2444 'esp': 'spi',
2445 'comp': 'nexthdr',
2446 'icmp': 'type',
2447 'icmpv6': 'type',
2448 'dst': 'nexthdr',
2449 'frag': 'nexthdr',
2450 'hbh': 'nexthdr',
2451 'mh': 'nexthdr',
2452 'rt': 'nexthdr',
2453 'vlan': 'id',
2454 'arp': 'htype',
2455 }.get(protocol, protokey)
2456 ports += (
2457 f'{protocol} {protokey} '
2458 f'{single_or_set(rule[key], rule["fileline"])} '
2459 )
2460 if ports:
2461 return ports
2462 return f'{"ip protocol" if ipv == 4 else "ip6 nexthdr"} {protocol} '
2463
2464
2465 def parse_iplist_to_single_ip(rule, key, value, ipv):
2466 """Parse rule's iplist to single ipaddress, or None."""
2467 target = []
2468 for check in value.split():
2469 if check.count(':') == 1 and is_ip_address(check.split(':')[0]) == 4:
2470 check_ipv = 4 # IPv4 address with port
2471 elif (
2472 check.startswith('[')
2473 and ']:' in check
2474 and is_ip_address(check[1:].split(']:')[0]) == 6
2475 ):
2476 check_ipv = 6 # IPv6 address with port
2477 else:
2478 check_ipv = is_ip_address(check)
2479 if check_ipv == 0:
2480 fail(
2481 f'{rule["fileline"]}Invalid IP address in '
2482 f'"{key}" target: {check}'
2483 )
2484 if check_ipv == ipv:
2485 target.append(check)
2486 if not target: # Nothing found for this ipv, don't generate rule
2487 return None
2488 if len(target) > 1:
2489 fail(f'{rule["fileline"]}Multiple "{key}" targets: {" ".join(target)}')
2490 return target[0]
2491
2492
2493 def parse_nat_to(rule, ipv):
2494 """Parse snat/dnat "to" rule to nft rule."""
2495 if not rule['nat_to']:
2496 return ''
2497
2498 # "to" can be IPv4, IPv6 or both, find correct one
2499 target = parse_iplist_to_single_ip(
2500 rule, rule['statement'], rule['nat_to'], ipv
2501 )
2502 if not target:
2503 return None
2504
2505 # Generate rule
2506 to_text = 'to'
2507 if rule['statement'] in {'snat_prefix', 'dnat_prefix'}:
2508 to_text = 'prefix to'
2509 return f' {"ip" if ipv == 4 else "ip6"} {to_text} {target}'
2510
2511
2512 def rule_matchers( # noqa: PLR0914 -- Too many local variables
2513 rule,
2514 ipv,
2515 *,
2516 cast=None,
2517 skip_options=True,
2518 skip_icmp=True,
2519 ):
2520 """Parse rule's matchers to nft rule.
2521
2522 Return value "None" is "no match, skip rule".
2523 """
2524 # pylint: disable=too-many-branches
2525 # pylint: disable=too-many-locals
2526 # pylint: disable=too-many-return-statements
2527 # pylint: disable=too-many-statements
2528 if cast is not None and rule['cast'] != cast:
2529 return None # multi/broadcast doesn't match
2530 if ipv == 6 and rule['cast'] == 'broadcast':
2531 return None # broadcast is ipv4 only
2532 if skip_icmp and rule['protocol'] in {'icmp', 'icmpv6'}:
2533 return None
2534 if skip_options and rule['mss']: # Handled in output_options()
2535 return None
2536
2537 # IPv4/6 specific rule?
2538 if ipv == 4 and rule['ipv6'] and not rule['ipv4']:
2539 return None
2540 if ipv == 6 and rule['ipv4'] and not rule['ipv6']:
2541 return None
2542
2543 # Convert matchers to nft
2544 castmeta = ''
2545 if cast and rule['cast'] != 'unicast':
2546 castmeta = f'fib daddr type {rule["cast"]} '
2547
2548 ipsecmeta = ''
2549 if rule['sipsec']:
2550 ipsecmeta += f'meta ipsec {rule["sipsec"]} '
2551 if rule['dipsec']:
2552 ipsecmeta += f'rt ipsec {rule["dipsec"]} '
2553
2554 ct_status = ''
2555 if rule['ct_status']:
2556 ct_status = f'ct status {rule["ct_status"]} '
2557
2558 priority_match = ''
2559 if rule['priority_match']:
2560 priority_match = f'meta priority "{rule["priority_match"]}" '
2561
2562 uid = ''
2563 if rule['uid']:
2564 uid = (
2565 f'meta skuid '
2566 f'{single_or_set(rule["uid"], rule["fileline"], quote=True)} '
2567 )
2568 gid = ''
2569 if rule['gid']:
2570 gid = (
2571 f'meta skgid '
2572 f'{single_or_set(rule["gid"], rule["fileline"], quote=True)} '
2573 )
2574
2575 meta_set = ''
2576 if rule['mark_set']:
2577 meta_set += (
2578 f'meta mark set {mark_set_argument(rule)} ct mark set meta mark '
2579 )
2580 if rule['priority_set']:
2581 meta_set += f'meta priority set "{rule["priority_set"]}" '
2582
2583 iplist_mangle = ''
2584 for operation in ('add', 'update', 'delete'):
2585 value = rule[f'iplist_{operation}']
2586 if value and ' ' in value:
2587 addr, iplist_name = value.split(' ', 1)
2588 iplist_mangle += (
2589 f'{operation} {iplist_name}_{ipv} '
2590 f'{{ {"ip" if ipv == 4 else "ip6"} {addr} }} '
2591 )
2592
2593 tproxy = ''
2594 if rule['tproxy']:
2595 tproxy_to = parse_iplist_to_single_ip(
2596 rule, 'tproxy', rule['tproxy'], ipv
2597 )
2598 if not tproxy_to:
2599 return None
2600 tproxy = f'tproxy {"ip" if ipv == 4 else "ip6"} to {tproxy_to} '
2601
2602 dscp = ''
2603 if rule['dscp']:
2604 dscp = (
2605 f'{"ip" if ipv == 4 else "ip6"} dscp '
2606 f'{single_or_set(rule["dscp"], rule["fileline"])} '
2607 )
2608
2609 ifname = parse_interface_names(rule)
2610
2611 addrlist = ''
2612 try:
2613 addrlist += parse_iplist(rule, 'saddr', ipv)
2614 addrlist += parse_iplist(rule, 'daddr', ipv)
2615 addrlist += parse_maclist(rule, 'mac_saddr')
2616 addrlist += parse_maclist(rule, 'mac_daddr')
2617 except ValueError:
2618 return None
2619
2620 proto_ports = parse_protocol_ports(rule, ipv, skip_icmp=skip_icmp)
2621 if proto_ports is None:
2622 return None
2623
2624 # Return matcher string
2625 return (
2626 f'{ipsecmeta}{ct_status}{castmeta}{ifname}{addrlist}{proto_ports}'
2627 f'{cgroup_match(rule)}{time_match(rule)}{mark_match(rule)}'
2628 f'{priority_match}{dscp}{uid}{gid}{tproxy}{meta_set}'
2629 f'{iplist_mangle}'
2630 )
2631
2632
2633 def output_cast(cast, szone, dzone, rules, ipv, *, after_conntrack=True):
2634 """Output all uni/multi/broadcast rules for single zone-zone."""
2635 # pylint: disable=too-many-arguments
2636 has_mark_restore = False
2637 for rule in rules:
2638 if rule['after_conntrack'] != after_conntrack:
2639 continue
2640 match = rule_matchers(rule, ipv, cast=cast)
2641 if match is None:
2642 continue
2643 if not match and cast is None and rule['cast'] != 'unicast':
2644 continue # Don't convert "multicast accept" to nft "accept"
2645 if rule['statement'] in {'snat', 'dnat', 'snat_prefix', 'dnat_prefix'}:
2646 fail(
2647 f'{rule["fileline"]}Statement "{rule["statement"]}" can\'t '
2648 f'be used in "{szone}-{dzone}" section'
2649 )
2650 statement = rule_statement(
2651 szone, dzone, rule, ipv, force_statement=rule['nft']
2652 )
2653
2654 # Automatically restore mark from conntrack before mark match or set
2655 # is used. Mark set needs it too for OR-operations.
2656 if not has_mark_restore and (rule['mark_match'] or rule['mark_set']):
2657 has_mark_restore = True
2658 out('meta mark set ct mark')
2659
2660 out(f'{match}{statement}')
2661
2662 # Does this rule need kernel helper?
2663 if rule['helper']:
2664 if rule['helper'].count('-') != 1:
2665 fail(
2666 f'{rule["fileline"]}Invalid helper name: {rule["helper"]}'
2667 )
2668 HELPERS.append((rule['helper'], rule['protocol'], rule['dport']))
2669 kernelname = rule['helper'].split('-')[0].replace('_', '-')
2670 out(f'ct helper "{kernelname}" {rule["statement"]}')
2671
2672
2673 def output_zonemap(zonemap, szone, dzone, ipv):
2674 """Output zonemap{} rules for this szone-dzone."""
2675 for rule in zonemap:
2676 if rule['szone'] and szone not in rule['szone'].split():
2677 continue
2678 if rule['dzone'] and dzone not in rule['dzone'].split():
2679 continue
2680 new_szone = rule['new_szone'] or szone
2681 new_dzone = rule['new_dzone'] or dzone
2682 if new_szone == szone and new_dzone == dzone:
2683 continue
2684
2685 match = rule_matchers(rule, ipv, skip_icmp=False)
2686 if match is None:
2687 continue
2688 out(f'{match}jump {new_szone}-{new_dzone}_{ipv}')
2689
2690
2691 def output_options(szone, dzone, rules, ipv):
2692 """Output "mss" etc options as first rules."""
2693 for rule in rules:
2694 if rule['mss']:
2695 match = rule_matchers(rule, ipv, skip_options=False)
2696 if match is None:
2697 continue
2698 statement = rule_statement(
2699 szone, dzone, rule, ipv, do_lograte=False
2700 )
2701 out(f'{match}{statement}')
2702
2703
2704 def output_zone(zonemap, szone, dzone, rules, ipv):
2705 """Output single zone-zone_ipv4 nft chain."""
2706 # Header + zonemap jumps + options
2707 out(f'chain {szone}-{dzone}_{ipv} {{')
2708 output_zonemap(zonemap, szone, dzone, ipv)
2709 output_options(szone, dzone, rules, ipv)
2710
2711 # Rules with "-conntrack" or plain "log"/"counter" rule. Output these
2712 # before conntrack and icmp so that they see all traffic.
2713 output_cast(None, szone, dzone, rules, ipv, after_conntrack=False)
2714
2715 # ICMP is special, keep it before ct
2716 output_icmp(szone, dzone, rules, ipv)
2717
2718 # Connection tracking
2719 out('ct state vmap {')
2720 out('established : accept,')
2721 out('related : accept,')
2722 out('invalid : jump invalid_drop,')
2723 out(f'new : jump smurfs_{ipv},')
2724 out(f'untracked : jump smurfs_{ipv}')
2725 out('}')
2726
2727 # Allow outgoing IGMP multicast membership reports and incoming IGMP
2728 # multicast query.
2729 output_chain = szone == CONFIG.localhost_zone
2730 input_chain = dzone == CONFIG.localhost_zone
2731 if ipv == 4 and output_chain and not input_chain:
2732 out('ip protocol igmp ip daddr 224.0.0.22 accept') # membership report
2733 if ipv == 4 and input_chain and not output_chain:
2734 out('ip protocol igmp ip daddr 224.0.0.1 accept') # query
2735
2736 # Broadcast and multicast.
2737 #
2738 # "fib daddr type" works only for incoming packets so skip these if
2739 # szone=localhost (outgoing).
2740 #
2741 # "fib daddr type" works also for forwarding packets so write those rules
2742 # too (see below for zonemap reason). Multicast can't really be forwarded
2743 # as it is local to ip/netmask. Proxying is ok, where software listens one
2744 # interface and writes it to another.
2745 if not output_chain:
2746 output_cast('multicast', szone, dzone, rules, ipv)
2747 output_cast('broadcast', szone, dzone, rules, ipv)
2748 if ipv == 4:
2749 out('fib daddr type { broadcast, multicast } drop')
2750 else:
2751 out('fib daddr type multicast drop')
2752
2753 # Unicast.
2754 #
2755 # Broadcast/multicast is already handled above for incoming packets so
2756 # output only unicast here for incoming.
2757 #
2758 # For forward/output add multicast rules without multicast matcher. This
2759 # means that for forward packets both with and without "fib daddr type"
2760 # rules will be outputted. This is needed for complex zonemap mangling
2761 # where szone=myservice might actually be from localhost.
2762 output_cast('unicast' if input_chain else None, szone, dzone, rules, ipv)
2763 out('}')
2764
2765
2766 def output_zone_vmaps(zones, rules):
2767 """Output interface verdict maps to jump to correct zone-zone."""
2768 # pylint: disable=too-many-branches
2769
2770 # Vmap must have interval-flag if there is wildcard-interface.
2771 localhost = CONFIG.localhost_zone
2772 has_wildcard = False
2773 for value in zones.values():
2774 for interface in value['interface']:
2775 if '*' in interface:
2776 has_wildcard = True
2777
2778 # Incoming zones
2779 out('map input_zones {')
2780 out('type ifname : verdict')
2781 if has_wildcard:
2782 out('flags interval')
2783 out('elements = {')
2784 out('"lo" : accept,')
2785 for zone, value in zones.items():
2786 for interface in value['interface']:
2787 out(f'"{interface}" : jump {zone}-{localhost},')
2788 out('}')
2789 out('}')
2790
2791 # Outgoing zones
2792 out('map output_zones {')
2793 out('type ifname : verdict')
2794 if has_wildcard:
2795 out('flags interval')
2796 out('elements = {')
2797 if len(rules[localhost, localhost]) > 1: # Jump to lo-lo if it has rules
2798 out(f'"lo" : jump {localhost}-{localhost},')
2799 else:
2800 out('"lo" : accept,')
2801 for zone, value in zones.items():
2802 for interface in value['interface']:
2803 out(f'"{interface}" : jump {localhost}-{zone},')
2804 out('}')
2805 out('}')
2806
2807 # Forwarding zones
2808 out('map forward_zones {')
2809 out('type ifname . ifname : verdict')
2810 if has_wildcard:
2811 out('flags interval')
2812 out('elements = {')
2813 out('"lo" . "lo" : accept,')
2814 for szone, svalue in zones.items():
2815 for dzone, dvalue in zones.items():
2816 for sinterface in svalue['interface']:
2817 for dinterface in dvalue['interface']:
2818 out(
2819 f'"{sinterface}" . "{dinterface}" : '
2820 f'jump {szone}-{dzone},'
2821 )
2822 out('}')
2823 out('}')
2824
2825
2826 def output_zone2zone_rules(rules, zonemap):
2827 """Output all zone-zone rules for both IPv4 and IPv6."""
2828 for szone, dzone in rules:
2829 # Split to zone-zone_4 and zone-zone_6
2830 out(f'chain {szone}-{dzone} {{')
2831 out('meta nfproto vmap {')
2832 out(f'ipv4 : jump {szone}-{dzone}_4,')
2833 out(f'ipv6 : jump {szone}-{dzone}_6')
2834 out('}')
2835 out('}')
2836
2837 # IPv4 and IPv6 chains
2838 output_zone(zonemap, szone, dzone, rules[szone, dzone], 4)
2839 output_zone(zonemap, szone, dzone, rules[szone, dzone], 6)
2840
2841
2842 def output_rule_section_no_header(rules, section):
2843 """Output snat, dnat, prerouting, postrouting, etc. rules inside chain."""
2844 if not rules:
2845 return
2846
2847 has_mark_restore = False
2848 ip_merger = set()
2849 for rule in rules:
2850 for ipv in (4, 6):
2851 to_rule = parse_nat_to(rule, ipv)
2852 if to_rule is None: # ipv == 6 and "nat to IPv4" mismatch
2853 continue
2854 match = rule_matchers(
2855 rule, ipv, skip_options=False, skip_icmp=False
2856 )
2857 if match is None:
2858 continue
2859 statement = rule_statement(
2860 section.upper(),
2861 '',
2862 rule,
2863 ipv,
2864 force_statement=rule['nft'],
2865 do_lograte=False,
2866 )
2867
2868 # Automatically restore mark from conntrack when needed
2869 if not has_mark_restore and (
2870 rule['mark_match'] or rule['mark_set']
2871 ):
2872 has_mark_restore = True
2873 out('meta mark set ct mark')
2874
2875 # There are no separate IPv4/IPv6 chains so merge possible rules
2876 full_rule = f'{match}{statement}{to_rule}'
2877 if full_rule in ip_merger:
2878 continue
2879 ip_merger.add(full_rule)
2880 out(full_rule)
2881
2882
2883 def output_special_chains(chain_rules):
2884 """Output snat, dnat, prerouting postrouting, etc. chain + rules."""
2885 for prefix in (
2886 'snat',
2887 'dnat',
2888 'prerouting',
2889 'postrouting',
2890 'forward',
2891 'input',
2892 'output',
2893 ):
2894 for (section, ftype, priority), rules in chain_rules.items():
2895 if prefix != section:
2896 continue
2897
2898 # Output-chain must restore mark from conntrack if mark_set was
2899 # used for locally generated packets (usually in multiple ISP's
2900 # prerouting).
2901 #
2902 # Simply restore mark if mark_set was used in any chain. For that
2903 # reason output-chain should be outputted last.
2904 need_mark_restore = section == 'output' and any(
2905 'ct mark set' in line for line in OUT
2906 )
2907
2908 if not rules and not need_mark_restore:
2909 continue
2910
2911 hook_chain = {
2912 'snat': 'postrouting',
2913 'dnat': 'prerouting',
2914 }.get(section, section)
2915 hook_nft = f'type {ftype} hook {hook_chain} priority {priority}'
2916 chain_name = f'{ftype}_{hook_chain}_{priority}'
2917 chain_name = chain_name.replace(INTERNAL.priority_offset, '')
2918 chain_name = chain_name.replace('+', '')
2919 chain_name = chain_name.replace(' ', '_')
2920 chain_name = chain_name.replace('__', '_')
2921 out(f'chain {chain_name} {{')
2922 out(hook_nft)
2923 if need_mark_restore:
2924 out('meta mark set ct mark')
2925 output_rule_section_no_header(rules, section)
2926 out('}')
2927
2928
2929 def output_static_chain_logging(chain, statement):
2930 """Output logging rules for input/output/forward/invalid/smurfs chains."""
2931 lograte = CONFIG[f'log_{chain}'] # Enabled in foomuuri{}
2932 if lograte == 'no':
2933 return
2934 if lograte == 'yes': # "yes" means use standard log rate
2935 lograte = CONFIG.log_rate
2936 flags = ''
2937 if chain == 'invalid' and 'group ' not in CONFIG.log_level:
2938 flags = ' flags ip options' # flags and group are mutually exclusive
2939 flags += f' {CONFIG.log_level}'
2940 chain = chain.upper()
2941 statement = statement.upper()
2942
2943 if not lograte:
2944 out(f'log prefix "{chain} {statement} "{flags}')
2945 return
2946
2947 out(
2948 f'update @_lograte_set_4 {{ ip saddr limit rate {lograte} }} '
2949 f'log prefix "{chain} {statement} "{flags}'
2950 )
2951 out(
2952 f'update @_lograte_set_6 {{ ip6 saddr limit rate {lograte} }} '
2953 f'log prefix "{chain} {statement} "{flags}'
2954 )
2955
2956
2957 def output_header(chain_rules):
2958 """Output generic nft header."""
2959 # pylint: disable=too-many-branches
2960 # pylint: disable=too-many-statements
2961
2962 # Delete current foomuuri table and add new
2963 out('table inet foomuuri')
2964 out('delete table inet foomuuri')
2965 out('')
2966 out('table inet foomuuri {')
2967 out('')
2968
2969 # Insert include files
2970 for filename in find_config_files(
2971 CONFIG.share_dir, '*.nft'
2972 ) + find_config_files(CONFIG.etc_dir, '*.nft'):
2973 try:
2974 lines = filename.read_text('utf-8').splitlines()
2975 except PermissionError as error:
2976 fail(f"File {filename}: Can't read: {error}")
2977 for line in lines:
2978 line = line.strip()
2979 if line:
2980 out(line)
2981
2982 # Logging chains
2983 for chain in ('invalid', 'smurfs', 'rpfilter'):
2984 out(f'chain {chain}_drop {{')
2985 output_rule_section_no_header(
2986 chain_rules.get((chain, None, None), []), chain
2987 )
2988 if chain == 'rpfilter':
2989 out('udp sport 67 udp dport 68 return')
2990 output_static_chain_logging(chain, 'drop')
2991 out('drop')
2992 out('}')
2993
2994 # input/output/forward jump chains
2995 out('chain input {')
2996 out(f'type filter hook input priority filter{INTERNAL.priority_offset}')
2997 out('iifname vmap @input_zones')
2998 if INTERNAL.catch_all_interface_zone:
2999 out(
3000 f'jump {INTERNAL.catch_all_interface_zone}-{CONFIG.localhost_zone}'
3001 )
3002 output_static_chain_logging('input', 'drop')
3003 out('drop')
3004 out('}')
3005
3006 out('chain output {')
3007 out(f'type filter hook output priority filter{INTERNAL.priority_offset}')
3008 out('oifname vmap @output_zones')
3009 if INTERNAL.catch_all_interface_zone:
3010 out(
3011 f'jump {CONFIG.localhost_zone}-{INTERNAL.catch_all_interface_zone}'
3012 )
3013 # IGMP membership report and IPv6 equivalent must be allowed here too as
3014 # D-Bus interface change event might not be processed yet.
3015 out('ip protocol igmp ip daddr 224.0.0.22 accept')
3016 out('ip6 saddr :: icmpv6 type mld2-listener-report accept')
3017 output_static_chain_logging('output', 'reject')
3018 out('reject with icmpx admin-prohibited')
3019 out('}')
3020
3021 flowtable = {item for item in CONFIG.flowtable if '=' not in item}
3022 if len(flowtable) > 1:
3023 out('flowtable fastpath {')
3024 out(f'hook ingress priority filter{INTERNAL.priority_offset}')
3025 out(f'devices = {single_or_set(flowtable, quote=True)}')
3026 if 'hw_offload=yes' in CONFIG.flowtable:
3027 out('flags offload')
3028 if 'counter=yes' in CONFIG.flowtable:
3029 out('counter')
3030 out('}')
3031
3032 out('chain forward {')
3033 out(f'type filter hook forward priority filter{INTERNAL.priority_offset}')
3034 if len(flowtable) > 1:
3035 out('flow add @fastpath')
3036 out('iifname . oifname vmap @forward_zones')
3037 if INTERNAL.catch_all_interface_zone:
3038 out(
3039 f'jump {INTERNAL.catch_all_interface_zone}-'
3040 f'{INTERNAL.catch_all_interface_zone}'
3041 )
3042 output_static_chain_logging('forward', 'drop')
3043 out('drop')
3044 out('}')
3045
3046
3047 # Rules starting with these matchers can be merged.
3048 # List them in preferred order.
3049 MERGES = [
3050 'fib daddr type multicast udp dport',
3051 'fib daddr type broadcast udp dport',
3052 'udp dport',
3053 'udp sport',
3054 'tcp dport',
3055 'tcp sport',
3056 'ct helper',
3057 ]
3058
3059
3060 def merge_accepts(accepts, linenum):
3061 """Sort and merge found accept rules."""
3062 merge = {key: [] for key in MERGES[::-1]}
3063 ret = 0
3064 for accept in accepts:
3065 for key, ports in merge.items():
3066 # Check if accept line is:
3067 # "tcp dport number accept"
3068 # "tcp dport { number, number } accept"
3069 # "tcp dport number-number accept"
3070 regex = f'^{key} (\\{{ )?([-\\d, ]+)( \\}})? accept$'
3071 match = re.match(regex, accept)
3072 if match: # Add "22" from "tcp dport 22 accept" to merged
3073 ports.append(match.group(2))
3074 break
3075 else: # Can't merge, output as is
3076 OUT.insert(linenum + ret, accept)
3077 ret += 1
3078
3079 # Output merged accept ports in preferred order
3080 for key, ports in merge.items():
3081 if ports:
3082 OUT.insert(linenum, f'{key} {single_or_set(ports)} accept')
3083 ret += 1
3084 return ret
3085
3086
3087 def optimize_accepts():
3088 """Optimize ruleset accepts.
3089
3090 This will change multiple accepts to single accept using set.
3091 """
3092 # Remove duplicate accept rules
3093 already_seen = set()
3094 linenum = 0
3095 while linenum < len(OUT):
3096 line = OUT[linenum]
3097 if line in already_seen: # Already in seen, delete from ruleset
3098 del OUT[linenum]
3099 continue
3100 if line.endswith(' accept'): # Add accept to seen
3101 already_seen.add(line)
3102 else: # Something else, reset seen
3103 already_seen = set()
3104 linenum += 1
3105
3106 # Merge multiple "tcp daddr x accept" rules to single
3107 # rule "tcp daddr { x, y, z } accept".
3108 accepts = []
3109 linenum = 0
3110 while linenum < len(OUT):
3111 line = OUT[linenum]
3112 if line == 'continue': # No-op line generated by plain "counter"
3113 del OUT[linenum]
3114 elif line.endswith(' accept') and line.startswith(
3115 tuple(MERGES) + ('ip ', 'ip6 ')
3116 ):
3117 # Line is "tcp dport ... accept"
3118 accepts.append(line)
3119 del OUT[linenum]
3120 else:
3121 # Line is something else, output merged accepts
3122 linenum += merge_accepts(accepts, linenum) + 1
3123 accepts = []
3124
3125
3126 def optimize_jumps():
3127 """Optimize lograte jumps in ruleset.
3128
3129 This will change zone-zone's final "drop log" rule to optimized version.
3130 """
3131 linenum = 0
3132 while linenum < len(OUT):
3133 line = OUT[linenum]
3134 if line.startswith('jump lograte_'):
3135 logname = line.split()[1]
3136 log_nft, statement = LOGRATES.pop(logname)
3137 OUT[linenum] = log_nft
3138 OUT.insert(linenum + 1, statement)
3139 linenum += 1
3140
3141
3142 def optimize_final_rules():
3143 """Remove unreachable rules from chain after "drop" without matcher."""
3144 linenum = 0
3145 while linenum < len(OUT):
3146 if OUT[linenum].startswith(('accept', 'drop', 'reject', 'queue')):
3147 while OUT[linenum + 1] != '}':
3148 del OUT[linenum + 1]
3149 linenum += 1
3150
3151
3152 def output_logrates():
3153 """Output non-optimized lograte entries as chains."""
3154 for logname, (log_nft, statement) in LOGRATES.items():
3155 out(f'chain {logname} {{')
3156 out(log_nft)
3157 out(statement)
3158 out('}')
3159
3160 # Output empty lograte sets used by "foomuuri { log_rate }".
3161 for ipv in (4, 6):
3162 out(f'set _lograte_set_{ipv} {{')
3163 out(f'type ipv{ipv}_addr')
3164 out(f'size {CONFIG.set_size}')
3165 out('flags dynamic,timeout')
3166 out('timeout 1m')
3167 out('}')
3168
3169
3170 def output_iplist_sets(iplists: IPLists) -> None:
3171 """Output empty iplist{} sets."""
3172 for name in sorted(iplists):
3173 for ipv in (4, 6):
3174 out(f'set {name[1:]}_{ipv} {{')
3175 out(f'type ipv{ipv}_addr')
3176 if iplists[name].options.dynamic:
3177 out(f'size {CONFIG.set_size}')
3178 out('flags dynamic,timeout')
3179 else:
3180 out('flags interval,timeout')
3181 if iplists[name].options.merge:
3182 out('auto-merge')
3183 if iplists[name].options.element_timeout:
3184 out(f'timeout {iplists[name].options.element_timeout}s')
3185 if iplists[name].options.element_timeout > 20 * 60:
3186 out('gc-interval 5m')
3187 out('}')
3188
3189
3190 def output_named_counters(rules, chain_rules):
3191 """Output named counters."""
3192 # Collect all counter names
3193 names = set()
3194 for rulelist in list(rules.values()) + list(chain_rules.values()):
3195 for rule in rulelist:
3196 if rule['counter']:
3197 names.add(rule['counter'])
3198
3199 # Output counters
3200 for name in sorted(names):
3201 out(f'counter {name} {{')
3202 out('}')
3203
3204
3205 def output_helpers():
3206 """Output helpers."""
3207 # Convert helper list to helper->proto->set(ports) dict
3208 helpers = {}
3209 for name, proto, ports in HELPERS:
3210 if name not in helpers:
3211 helpers[name] = {}
3212 if proto not in helpers[name]:
3213 helpers[name][proto] = set()
3214 for port in ports.split():
3215 helpers[name][proto].add(port)
3216 if not helpers:
3217 return
3218
3219 # Output "ct helper" lines
3220 for name, protos in helpers.items():
3221 kernelname = name.split('-')[0].replace('_', '-')
3222 out(f'ct helper {name} {{')
3223 for proto in protos:
3224 out(f'type "{kernelname}" protocol {proto}')
3225 out('}')
3226
3227 # Output prerouting
3228 out('chain helper {')
3229 out(
3230 f'type filter hook prerouting priority filter'
3231 f'{INTERNAL.priority_offset}'
3232 )
3233 for name, protos in helpers.items():
3234 for proto, ports in protos.items():
3235 out(
3236 f'{proto} dport {single_or_set(" ".join(ports))} '
3237 f'ct helper set "{name}"'
3238 )
3239 out('}')
3240
3241
3242 def output_rpfilter():
3243 """Prerouting chain to check rpfilter."""
3244 if 'no' in CONFIG.rpfilter:
3245 return
3246 out('chain rpfilter {')
3247 out(
3248 f'type filter hook prerouting priority filter'
3249 f'{INTERNAL.priority_offset}'
3250 )
3251 interfaces = ''
3252 if 'yes' not in CONFIG.rpfilter: # Specific interfaces?
3253 interfaces = f'iifname {single_or_set(CONFIG.rpfilter, quote=True)} '
3254 out(
3255 f'{interfaces}fib saddr . mark . iif oif 0 meta ipsec missing '
3256 f'jump rpfilter_drop'
3257 )
3258 out('}')
3259
3260
3261 def output_footer():
3262 """Output generic ruleset footer."""
3263 out('}')
3264
3265
3266 def save_file(filename, lines):
3267 """Write lines to file. Use temporary file to make this atomic."""
3268 if str(filename) == '/dev/null':
3269 return
3270 if isinstance(lines, dict):
3271 text = json.dumps(lines, indent=2, sort_keys=True) + '\n'
3272 else:
3273 text = '\n'.join(lines) + '\n'
3274 tmpfile = filename.with_suffix(f'{filename.suffix}.{os.getpid()}')
3275 try:
3276 tmpfile.unlink(missing_ok=True)
3277 tmpfile.write_text(text, 'utf-8')
3278 tmpfile.chmod(0o600)
3279 tmpfile.rename(filename)
3280 except PermissionError as error:
3281 fail(f"File {filename}: Can't write: {error}")
3282 except FileNotFoundError as error:
3283 warning(f"File {filename}: Can't write: {error}")
3284
3285
3286 def env_cleanup(text):
3287 """Allow only letters and numbers in text for environment variable."""
3288 # Convert ä->a as isalpha('ä') is true
3289 value = unicodedata.normalize('NFKD', text)
3290 value = value.encode('ASCII', 'ignore').decode('utf-8')
3291
3292 # Remove non-alphanumeric chars
3293 return ''.join(char if char.isalnum() else '_' for char in value)
3294
3295
3296 def save_final(filename):
3297 """Save final ruleset to file."""
3298 # Convert to indented lines
3299 indent = 0
3300 lines = []
3301 for line in OUT:
3302 if line.startswith('}'):
3303 indent -= 1
3304 if line:
3305 line = '\t' * indent + line
3306 lines.append(line)
3307 if line == '\t}':
3308 lines.append('')
3309 if line.endswith('{'):
3310 indent += 1
3311
3312 # Save to "next" file
3313 if filename:
3314 save_file(filename, lines)
3315 return lines
3316
3317
3318 def signal_childs():
3319 """Signal foomuuri-dbus and foomuuri-monitor to reload."""
3320 for child in ('dbus', 'monitor'):
3321 # Read pid
3322 filename = CONFIG.run_dir / f'foomuuri-{child}.pid'
3323 try:
3324 pid = int(filename.read_text(encoding='utf-8'))
3325 except PermissionError as error:
3326 fail(f"File {filename}: Can't read: {error}")
3327 except (FileNotFoundError, ValueError):
3328 continue
3329
3330 # Send reload-signal
3331 with contextlib.suppress(OSError):
3332 os.kill(pid, signal.SIGHUP)
3333
3334
3335 def apply_final():
3336 """Use final ruleset."""
3337 # Check config
3338 good_file = state_file('good_file')
3339 next_file = state_file('next_file')
3340 if INTERNAL.command == 'check':
3341 if INTERNAL.root_power: # "nft check" requires root
3342 ret = run_program_rc(
3343 CONFIG.nft_bin + ['--check', '--file', next_file]
3344 )
3345 else:
3346 ret = 0
3347 warning('Not running as "root", skipping "nft check"')
3348 if ret:
3349 fail(f'Nftables failed to check ruleset, error code {ret}', False)
3350 else:
3351 verbose('check success', 0)
3352 return ret
3353
3354 # Run pre_start / pre_stop hook
3355 run_program_rc(INTERNAL.get(f'pre_{INTERNAL.command}'))
3356
3357 # Load "next"
3358 ret = run_program_rc(
3359 CONFIG.nft_bin + ['--file', next_file], print_output=False
3360 )
3361
3362 # Check failure
3363 if ret:
3364 fail(f'Failed to load ruleset to nftables, error code {ret}', False)
3365 return 1
3366
3367 # Success. Rename "next" to "good", signal dbus to reload and run hook
3368 if INTERNAL.command == 'start':
3369 good_file.unlink(missing_ok=True)
3370 next_file.rename(good_file)
3371 signal_childs()
3372 run_program_rc(INTERNAL.get(f'post_{INTERNAL.command}'))
3373 verbose(f'{INTERNAL.command} success', 0)
3374 return 0
3375
3376
3377 def print_assembled_config(config, zones, zonemap, rules, chain_rules) -> None:
3378 """Print assembled configuration in foomuuri.conf format."""
3379
3380 def print_section(name: str, lines: list, indent: int = 2) -> None:
3381 """Section print helper."""
3382 if lines:
3383 print(f'{name} {{')
3384 print(*[f'{" " * indent}{line}' for line in lines], sep='\n')
3385 print('}')
3386
3387 print('Assembled configuration << EOF')
3388
3389 print_section(
3390 'foomuuri',
3391 [
3392 f'{option} {value}'
3393 for option, value in dataclasses.asdict(CONFIG).items()
3394 ],
3395 )
3396
3397 print_section(
3398 'zone',
3399 [
3400 f'{zone} {" ".join(statement["interface"])}'
3401 for zone, statement in zones.items()
3402 ],
3403 )
3404
3405 print_section(
3406 'zonemap', [f'{" ".join(statement["line"])}' for statement in zonemap]
3407 )
3408
3409 for chains, statements in chain_rules.items():
3410 print_section(
3411 ' '.join(chains),
3412 [f'{" ".join(statement["line"])}' for statement in statements],
3413 )
3414
3415 print_section(
3416 'iplist', [f'{" ".join(line[1])}' for line in config.get('iplist', [])]
3417 )
3418
3419 for zone2zone, statements in rules.items():
3420 print_section(
3421 '-'.join(zone2zone),
3422 [f'{" ".join(statement["line"])}' for statement in statements],
3423 )
3424
3425 print_section(
3426 'hook',
3427 [
3428 f'{hook} {join_args(INTERNAL.get(hook))}'
3429 for hook in (
3430 filter(lambda k: k.startswith(('pre_', 'post_')), INTERNAL)
3431 )
3432 if INTERNAL.get(hook)
3433 ],
3434 )
3435
3436 print('EOF')
3437
3438
3439 def command_start():
3440 """Process "start" or "check" command."""
3441 # Read full config
3442 config = minimal_config()
3443 zones = parse_config_zones(config)
3444 zonemap = parse_config_zonemap(config, zones)
3445 iplists = parse_config_iplist(config)
3446 chain_rules = parse_config_special_chains(config)
3447 parse_config_groups(config, parse_config_targets(config))
3448 parse_config_hook(config)
3449 rules = parse_config_rules(config) # Also verify for unknown sections
3450 insert_any_zones(zones, rules)
3451 verify_config(config, zones, rules)
3452
3453 # Generate output
3454 output_header(chain_rules)
3455 output_rate_names(rules)
3456 output_zone_vmaps(zones, rules)
3457 output_zone2zone_rules(rules, zonemap)
3458 output_special_chains(chain_rules)
3459 optimize_jumps()
3460 optimize_final_rules()
3461 optimize_accepts()
3462 output_logrates()
3463 output_iplist_sets(iplists)
3464 output_named_counters(rules, chain_rules)
3465 output_helpers()
3466 output_rpfilter()
3467 output_footer()
3468
3469 # Add iplist elements from cache to output. Include only iplists
3470 # that are marked with "start=yes".
3471 if iplists_add_on_start := iplists.filter_names_by('start', True):
3472 command_iplist_refresh(
3473 iplists=iplists,
3474 currently_active=set(iplists),
3475 override_update_only=iplists_add_on_start,
3476 )
3477
3478 # Print assembled configuration
3479 if CONFIG.verbose >= 2:
3480 print_assembled_config(config, zones, zonemap, rules, chain_rules)
3481
3482 # Save known zones to file
3483 save_file(state_file('zone_file'), zones.keys())
3484
3485 # Save and apply generated ruleset
3486 save_final(state_file('next_file'))
3487 return apply_final()
3488
3489
3490 def command_stop():
3491 """Process "stop" command. This will remove all foomuuri rules."""
3492 config = minimal_config() # Needed for pre_stop and post_stop hooks
3493 parse_config_hook(config)
3494 out('table inet foomuuri')
3495 out('delete table inet foomuuri')
3496 save_final(state_file('next_file'))
3497 return apply_final()
3498
3499
3500 def command_block():
3501 """Load "block all traffic" ruleset."""
3502 minimal_config()
3503 return run_program_rc(
3504 CONFIG.nft_bin + ['--file', CONFIG.share_dir / 'block.fw']
3505 )
3506
3507
3508 def parse_active_interface_zone():
3509 """Parse current interface->zone mapping from active nft ruleset."""
3510 data = nft_json('list map inet foomuuri input_zones')
3511 if not data:
3512 return {}
3513 ret = {}
3514 for item in data['nftables']:
3515 if 'map' in item:
3516 for interface, rule in item['map']['elem']:
3517 if interface != 'lo':
3518 ret[interface] = rule['jump']['target'].split('-')[0]
3519 return ret
3520
3521
3522 def command_status():
3523 """Print if Foomuuri is running, zone<->mapping, etc."""
3524 # Get minimal config and interface status
3525 config = minimal_config()
3526 zones = parse_config_zones(config)
3527 zone_interface = parse_active_interface_zone()
3528 if INTERNAL.catch_all_interface_zone:
3529 zone_interface['*'] = INTERNAL.catch_all_interface_zone
3530
3531 nft_tables = nft_json('list tables') or {}
3532
3533 tables = {
3534 t['table'].get('name')
3535 for t in nft_tables.get('nftables', {})
3536 if t.get('table')
3537 }
3538
3539 # Running
3540 if 'foomuuri' not in tables:
3541 fail('Foomuuri is not running')
3542 return 1
3543 print('Foomuuri is running')
3544
3545 # D-Bus, Monitor
3546 for child in ('dbus', 'monitor'):
3547 filename = CONFIG.run_dir / f'foomuuri-{child}.pid'
3548 try:
3549 pid = int(filename.read_text(encoding='utf-8'))
3550 os.kill(pid, 0)
3551 print(f'Foomuuri-{child} is running, PID {pid}')
3552 except (
3553 FileNotFoundError,
3554 ValueError,
3555 PermissionError,
3556 ProcessLookupError,
3557 ):
3558 print(f'Foomuuri-{child} is not running')
3559
3560 # Check for multiple firewalls
3561 if 'firewalld' in tables:
3562 print()
3563 warning('Firewalld is running')
3564
3565 # Zones
3566 if not zone_interface:
3567 print()
3568 print('Warning: There are no interfaces assigned to any zones')
3569
3570 print()
3571 print('zone {')
3572 table = []
3573 for zone in zones:
3574 interfaces = [
3575 interface
3576 for interface, int_zones in zone_interface.items()
3577 if zone == int_zones
3578 ]
3579 table.append(('', zone, f'{" ".join(interfaces)}'))
3580 print_table(data=table, header=False)
3581 print('}')
3582 return 0
3583
3584
3585 if HAVE_DBUS:
3586
3587 class FoomuuriDbusException(dbus.DBusException):
3588 """Exception class for D-Bus interface."""
3589
3590 _dbus_error_name = 'fi.foobar.Foomuuri1.exception'
3591
3592 class DbusCommon(dbus.service.Object):
3593 """D-Bus server and common functions."""
3594
3595 polkit = None
3596
3597 def __init__(self, conn, object_path, zones):
3598 """Initialize dbus.service.Object."""
3599 dbus.service.Object.__init__(
3600 self, conn=conn, object_path=object_path
3601 )
3602 # Save config data: zone list is static.
3603 self.zones = zones
3604
3605 @staticmethod
3606 def clean_out():
3607 """Remove all entries from current OUT[] variable."""
3608 while OUT:
3609 del OUT[0]
3610
3611 @staticmethod
3612 def apply_out():
3613 """Apply current OUT commands."""
3614 lines = '\n'.join(save_final(None))
3615 return run_program_rc(
3616 CONFIG.nft_bin + ['--file', '-'], stdin=lines
3617 )
3618
3619 def verify_polkit_privilege(self, sender, privilege):
3620 """Verify authorization from PolicyKit."""
3621 # Initialize polkit connections
3622 if not self.polkit:
3623 try:
3624 self.polkit = dbus.Interface(
3625 dbus.SystemBus().get_object(
3626 'org.freedesktop.PolicyKit1',
3627 '/org/freedesktop/PolicyKit1/Authority',
3628 ),
3629 'org.freedesktop.PolicyKit1.Authority',
3630 )
3631 except dbus.DBusException:
3632 # No polkit, fallback to check that UID == 0
3633 iface = dbus.Interface(
3634 dbus.SystemBus().get_object(
3635 'org.freedesktop.DBus', '/org/freedesktop/DBus'
3636 ),
3637 'org.freedesktop.DBus',
3638 )
3639 with contextlib.suppress(ValueError):
3640 if int(iface.GetConnectionUnixUser(sender)) == 0:
3641 return
3642 raise FoomuuriDbusException(
3643 'Authorization failed'
3644 ) from None
3645
3646 # Verify authorization from polkit
3647 try:
3648 result = self.polkit.CheckAuthorization(
3649 ('system-bus-name', {'name': sender}),
3650 privilege,
3651 {},
3652 1,
3653 '',
3654 timeout=60,
3655 )
3656 is_authorized = result[0]
3657 if is_authorized:
3658 return
3659 except dbus.DBusException as error:
3660 warning(str(error))
3661 self.polkit = None # Reconnect on next call
3662 raise FoomuuriDbusException('Authorization failed') from None
3663
3664 @staticmethod
3665 def remove_interface(interface_zone, interface):
3666 """Remove interface from all zones."""
3667 # Get interface's current zone
3668 zone = interface_zone.get(interface)
3669 if not zone:
3670 return ''
3671
3672 # Remove from input and output
3673 out(
3674 f'delete element inet foomuuri input_zones '
3675 f'{{ "{interface}" : jump '
3676 f'{zone}-{CONFIG.localhost_zone} }}'
3677 )
3678 out(
3679 f'delete element inet foomuuri output_zones '
3680 f'{{ "{interface}" : jump '
3681 f'{CONFIG.localhost_zone}-{zone} }}'
3682 )
3683
3684 # Remove from forward
3685 for other, otherzone in interface_zone.items():
3686 out(
3687 f'delete element inet foomuuri forward_zones '
3688 f'{{ "{other}" . "{interface}" : jump '
3689 f'{otherzone}-{zone} }}'
3690 )
3691 if other != interface:
3692 out(
3693 f'delete element inet foomuuri forward_zones '
3694 f'{{ "{interface}" . "{other}" : '
3695 f'jump {zone}-{otherzone} }}'
3696 )
3697 return zone
3698
3699 @staticmethod
3700 def add_interface(interface_zone, interface, zone):
3701 """Add interface to zone.
3702
3703 It must be already removed from other zones.
3704 """
3705 # Add to input and output
3706 out(
3707 f'add element inet foomuuri input_zones '
3708 f'{{ "{interface}" : jump '
3709 f'{zone}-{CONFIG.localhost_zone} }}'
3710 )
3711 out(
3712 f'add element inet foomuuri output_zones '
3713 f'{{ "{interface}" : jump '
3714 f'{CONFIG.localhost_zone}-{zone} }}'
3715 )
3716
3717 # Add to forward
3718 for other, otherzone in interface_zone.items():
3719 if other != interface:
3720 out(
3721 f'add element inet foomuuri forward_zones '
3722 f'{{ "{other}" . "{interface}" : '
3723 f'jump {otherzone}-{zone} }}'
3724 )
3725 out(
3726 f'add element inet foomuuri forward_zones '
3727 f'{{ "{interface}" . "{other}" : '
3728 f'jump {zone}-{otherzone} }}'
3729 )
3730 out(
3731 f'add element inet foomuuri forward_zones '
3732 f'{{ "{interface}" . "{interface}" : jump {zone}-{zone} }}'
3733 )
3734
3735 def change_interface_zone(self, interface, new_zone):
3736 """Change interface to new_zone, or delete if new_zone is empty."""
3737 interface, new_zone = str(interface), str(new_zone)
3738 if new_zone and new_zone not in self.zones:
3739 warning(f'Zone "{new_zone}" is unknown')
3740 raise FoomuuriDbusException(f'Zone "{new_zone}" is unknown')
3741 if interface == 'lo':
3742 # Interface "lo" must stay in "localhost" zone.
3743 # Other interfaces can be added to "localhost" only if
3744 # "localhost-localhost" section is defined.
3745 if new_zone != CONFIG.localhost_zone:
3746 warning(
3747 f'Can\'t change interface "lo" to zone "{new_zone}"'
3748 )
3749 raise FoomuuriDbusException(
3750 f'Can\'t change interface "lo" to zone "{new_zone}"'
3751 )
3752 return '', ''
3753 interface_zone = parse_active_interface_zone()
3754 self.clean_out()
3755 old_zone = self.remove_interface(interface_zone, interface)
3756 if new_zone:
3757 self.add_interface(interface_zone, interface, new_zone)
3758 if self.apply_out():
3759 raise FoomuuriDbusException(
3760 'Failed to modify interface. See '
3761 'journal for more information.'
3762 )
3763 return old_zone, new_zone
3764
3765 def parse_default_zone(self, interface, zone):
3766 """Return zone, or dbus_zone if empty."""
3767 interface, zone = str(interface), str(zone)
3768 if zone:
3769 return zone
3770
3771 # Fallback to zones section, or to foomuuri.dbus_zone
3772 for key, value in self.zones.items():
3773 if interface in value['interface']:
3774 return key
3775 return CONFIG.dbus_zone
3776
3777 def method_get_zones(self):
3778 """Get list of available zones.
3779
3780 "localhost" can't have any interfaces so don't include it.
3781 """
3782 return [
3783 name for name in self.zones if name != CONFIG.localhost_zone
3784 ]
3785
3786 def method_remove_interface(self, zone, interface):
3787 """Remove interface from zone, or from all if zone is empty.
3788
3789 This is currently always handled as "from all".
3790 """
3791 if not Validators.str_interface_name(interface):
3792 raise FoomuuriDbusException(
3793 f'Invalid interface name: {interface}'
3794 )
3795 verbose(f'Interface "{interface}" remove from zone "{zone}"', 0)
3796 return self.change_interface_zone(interface, '')[0]
3797
3798 def method_add_interface(self, zone, interface):
3799 """Add interface to zone.
3800
3801 There can be only one zone per interface so it will be removed
3802 from previous zone if needed.
3803 """
3804 if not Validators.str_interface_name(interface):
3805 raise FoomuuriDbusException(
3806 f'Invalid interface name: {interface}'
3807 )
3808 zone = self.parse_default_zone(interface, zone)
3809 verbose(f'Interface "{interface}" add to zone "{zone}"', 0)
3810 return self.change_interface_zone(interface, zone)[1]
3811
3812 def method_change_zone_of_interface(self, zone, interface):
3813 """Change interface to zone."""
3814 if not Validators.str_interface_name(interface):
3815 raise FoomuuriDbusException(
3816 f'Invalid interface name: {interface}'
3817 )
3818 zone = self.parse_default_zone(interface, zone)
3819 verbose(f'Interface "{interface}" change to zone "{zone}"', 0)
3820 return self.change_interface_zone(interface, zone)[0]
3821
3822 class DbusFoomuuri(DbusCommon):
3823 """D-Bus server for Foomuuri."""
3824
3825 # pylint: disable=invalid-name # dbus method names
3826
3827 @dbus.service.method(
3828 'fi.foobar.Foomuuri1.zone',
3829 in_signature='',
3830 out_signature='as',
3831 sender_keyword='sender',
3832 )
3833 def getZones(self, sender=None):
3834 """Get list of available zones."""
3835 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.info')
3836 return self.method_get_zones()
3837
3838 @dbus.service.method(
3839 'fi.foobar.Foomuuri1.zone',
3840 in_signature='ss',
3841 out_signature='s',
3842 sender_keyword='sender',
3843 )
3844 def removeInterface(self, zone, interface, sender=None):
3845 """Remove interface from zone, or from all if zone is empty.
3846
3847 Return: previous zone
3848 """
3849 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.config')
3850 return self.method_remove_interface(zone, interface)
3851
3852 @dbus.service.method(
3853 'fi.foobar.Foomuuri1.zone',
3854 in_signature='ss',
3855 out_signature='s',
3856 sender_keyword='sender',
3857 )
3858 def addInterface(self, zone, interface, sender=None):
3859 """Add interface to zone.
3860
3861 Return: new zone
3862 """
3863 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.config')
3864 return self.method_add_interface(zone, interface)
3865
3866 @dbus.service.method(
3867 'fi.foobar.Foomuuri1.zone',
3868 in_signature='ss',
3869 out_signature='s',
3870 sender_keyword='sender',
3871 )
3872 def changeZoneOfInterface(self, zone, interface, sender=None):
3873 """Change interface to zone.
3874
3875 Return: previous zone
3876 """
3877 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.config')
3878 return self.method_change_zone_of_interface(zone, interface)
3879
3880 class DbusFirewalld(DbusCommon):
3881 """D-Bus server for firewalld emulation."""
3882
3883 # pylint: disable=invalid-name # dbus method names
3884
3885 @dbus.service.method(
3886 'org.fedoraproject.FirewallD1.zone',
3887 in_signature='',
3888 out_signature='as',
3889 sender_keyword='sender',
3890 )
3891 def getZones(self, sender=None):
3892 """Get list of available zones."""
3893 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.info')
3894 return self.method_get_zones()
3895
3896 @dbus.service.method(
3897 'org.fedoraproject.FirewallD1.zone',
3898 in_signature='ss',
3899 out_signature='s',
3900 sender_keyword='sender',
3901 )
3902 def removeInterface(self, zone, interface, sender=None):
3903 """Remove interface from zone, or from all if zone is empty.
3904
3905 Return: previous zone
3906 """
3907 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.config')
3908 return self.method_remove_interface(zone, interface)
3909
3910 @dbus.service.method(
3911 'org.fedoraproject.FirewallD1.zone',
3912 in_signature='ss',
3913 out_signature='s',
3914 sender_keyword='sender',
3915 )
3916 def addInterface(self, zone, interface, sender=None):
3917 """Add interface to zone.
3918
3919 Return: new zone
3920 """
3921 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.config')
3922 return self.method_add_interface(zone, interface)
3923
3924 @dbus.service.method(
3925 'org.fedoraproject.FirewallD1.zone',
3926 in_signature='ss',
3927 out_signature='s',
3928 sender_keyword='sender',
3929 )
3930 def changeZoneOfInterface(self, zone, interface, sender=None):
3931 """Change interface to zone.
3932
3933 Return: previous zone
3934 """
3935 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.config')
3936 return self.method_change_zone_of_interface(zone, interface)
3937
3938 @dbus.service.method(
3939 'org.fedoraproject.FirewallD1.zone',
3940 in_signature='s',
3941 out_signature='s',
3942 sender_keyword='sender',
3943 )
3944 def getZoneOfSource(self, source, sender=None):
3945 """Get current zone of source.
3946
3947 These calls are used by netavark (run by Podman) to set
3948 container's IP address to "trusted" zone. There is no such thing
3949 in Foomuuri.
3950 """
3951 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.info')
3952 if not is_ip_address(source):
3953 raise FoomuuriDbusException('Invalid source address')
3954 return '' # It's not in any zone
3955
3956 @dbus.service.method(
3957 'org.fedoraproject.FirewallD1.zone',
3958 in_signature='ss',
3959 out_signature='s',
3960 sender_keyword='sender',
3961 )
3962 def changeZoneOfSource(self, zone, source, sender=None):
3963 """Change source to zone. See above getZoneOfSource()."""
3964 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.config')
3965 raise FoomuuriDbusException(
3966 f'Not implemented: change {source} to "{zone}"'
3967 )
3968
3969 @dbus.service.method(
3970 'org.fedoraproject.FirewallD1.zone',
3971 in_signature='ss',
3972 out_signature='s',
3973 sender_keyword='sender',
3974 )
3975 def removeSource(self, zone, source, sender=None):
3976 """Remove source from zone. See above getZoneOfSource()."""
3977 self.verify_polkit_privilege(sender, 'fi.foobar.Foomuuri1.config')
3978 raise FoomuuriDbusException(
3979 f'Not implemented: remove {source} from "{zone}"'
3980 )
3981
3982 def command_dbus():
3983 """Start D-Bus daemon."""
3984 # Import gi here as it is heavy module to load.
3985 try:
3986 # ruff: noqa: PLC0415 (`import` should be at the top-level)
3987 # pylint: disable=import-outside-toplevel
3988 import gi.repository.GLib
3989 except ImportError:
3990 fail('No python-gobject / python-gi installed')
3991
3992 INTERNAL.keep_going = 1
3993 while INTERNAL.keep_going:
3994 # Read minimal config
3995 config = minimal_config()
3996 zones = parse_config_zones(config)
3997 daemonize()
3998
3999 # Initialize D-Bus
4000 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
4001 bus = dbus.SystemBus()
4002
4003 # Foomuuri D-Bus calls
4004 try:
4005 foomuuri_name = dbus.service.BusName(
4006 'fi.foobar.Foomuuri1', bus
4007 )
4008 foomuuri_name.get_name() # Dummy call to get rid of pylint
4009 except dbus.exceptions.DBusException:
4010 fail("Can't bind to system D-Bus: fi.foobar.Foomuuri1")
4011 foomuuri_object = DbusFoomuuri(bus, '/fi/foobar/Foomuuri1', zones)
4012
4013 # Firewalld emulation calls, if enabled in foomuuri{} config
4014 firewalld_object = None
4015 if CONFIG.dbus_firewalld == 'yes':
4016 try:
4017 firewalld_name = dbus.service.BusName(
4018 'org.fedoraproject.FirewallD1', bus
4019 )
4020 firewalld_name.get_name() # Dummy call for pylint
4021 except dbus.exceptions.DBusException:
4022 fail(
4023 "Can't bind to system D-Bus: "
4024 'org.federaproject.FirewallD1'
4025 )
4026 firewalld_object = DbusFirewalld(
4027 bus, '/org/fedoraproject/FirewallD1', zones
4028 )
4029
4030 # Define reload/stop signal handler
4031 mainloop = gi.repository.GLib.MainLoop()
4032
4033 def signal_handler(sig, _dummy_frame, mainloop=mainloop):
4034 if sig == signal.SIGINT:
4035 INTERNAL.keep_going = 0
4036 notify('STOPPING=1')
4037 else:
4038 notify('RELOADING=1')
4039 mainloop.quit()
4040
4041 signal.signal(signal.SIGHUP, signal_handler)
4042 signal.signal(signal.SIGINT, signal_handler)
4043 save_file(CONFIG.run_dir / 'foomuuri-dbus.pid', [str(os.getpid())])
4044
4045 # Start processing messages
4046 verbose('D-Bus handler ready', 0)
4047 notify('READY=1')
4048 mainloop.run()
4049
4050 # Reload signal received. Disconnect from D-Bus.
4051 foomuuri_object.remove_from_connection()
4052 del foomuuri_object
4053 del foomuuri_name
4054 if firewalld_object:
4055 firewalld_object.remove_from_connection()
4056 del firewalld_object
4057 del firewalld_name
4058 del bus
4059
4060 def command_set():
4061 """Modify runtime config by calling D-Bus methods."""
4062 if (
4063 len(INTERNAL.parameters) != 4
4064 or INTERNAL.parameters[0] != 'interface'
4065 or INTERNAL.parameters[2] != 'zone'
4066 ):
4067 command_help()
4068 interface = INTERNAL.parameters[1]
4069 zone = INTERNAL.parameters[3]
4070 try:
4071 bus = dbus.SystemBus()
4072 obj = bus.get_object('fi.foobar.Foomuuri1', '/fi/foobar/Foomuuri1')
4073 except dbus.exceptions.DBusException as error:
4074 fail(f"Can't connect to Foomuuri D-Bus: {error}")
4075 try:
4076 if zone in {'', '-'}:
4077 obj.removeInterface('', interface)
4078 else:
4079 obj.changeZoneOfInterface(zone, interface)
4080 except dbus.exceptions.DBusException as error:
4081 fail(f"Can't modify interface: {error}")
4082
4083 else: # HAVE_DBUS is false
4084
4085 def command_dbus():
4086 """Start D-Bus daemon."""
4087 fail('No python-dbus installed')
4088
4089 def command_set():
4090 """Modify runtime config by calling D-Bus methods."""
4091 fail('No python-dbus installed')
4092
4093
4094 def remove_filters(text):
4095 """Remove tailing flags and filters from hostname or URL."""
4096 return text.split('|')[0]
4097
4098
4099 def iplist_verify_filters(name, values, fileline):
4100 """Verify that iplist entry's filter is known."""
4101 for value in values:
4102 for spec in value.split('|')[1:]:
4103 if not spec.startswith(
4104 ('missing-ok', 'shell:', 'json:', 'html:', 'xml:')
4105 ):
4106 warning(
4107 f'{fileline}Unknown filter "|{spec}" in iplist '
4108 f'"{name} {value}"'
4109 )
4110
4111
4112 def resolve_one_hostname(hostname):
4113 """Resolve hostname and return its IP addresses."""
4114 ret = set()
4115 with contextlib.suppress(socket.gaierror):
4116 for item in socket.getaddrinfo(remove_filters(hostname), None):
4117 if item[4] and item[4][0]:
4118 ret.add(item[4][0])
4119 return ret
4120
4121
4122 def resolve_all_hostnames(resolve, cache):
4123 """Resolve hostnames to IP addresses and save them to cache."""
4124 # Collect list of hostnames to resolve
4125 now = int(time.time())
4126 todo = set()
4127 for hostname, options in resolve.items():
4128 cachevalue = cache.get(hostname, {}).get('ip', {})
4129 cacherefresh = cache.get(hostname, {}).get('refresh')
4130 if cachevalue and cacherefresh:
4131 if now >= cacherefresh + options.timeout: # Expired entry
4132 cache[hostname]['ip'] = {}
4133 cache[hostname]['dirty'] = True
4134 elif now < cacherefresh + options.refresh and INTERNAL.force < 0:
4135 verbose(f'Using cached value for "{remove_filters(hostname)}"')
4136 continue
4137 todo.add(hostname)
4138 if not todo:
4139 return
4140
4141 # Resolve them
4142 verbose(f'DNS lookup for: {", ".join(todo)}')
4143 with concurrent.futures.ThreadPoolExecutor() as executor:
4144 jobs = {
4145 executor.submit(resolve_one_hostname, hostname): hostname
4146 for hostname in todo
4147 }
4148 for future in concurrent.futures.as_completed(jobs):
4149 hostname = jobs[future]
4150 addrlist = future.result()
4151 if addrlist:
4152 # Append/update addresses, don't replace whole list.
4153 # Some cloud services will change server IP a lot, and some
4154 # will return only partial list of addresses. By appending
4155 # old addresses will still work until lookup timeout.
4156 iplist_cache_init(cache, hostname)
4157 cache[hostname]['dirty'] = True
4158 cache[hostname]['refresh'] = now
4159 expire = now + resolve[hostname].timeout
4160 for addr in addrlist:
4161 cache[hostname]['ip'][addr] = expire
4162 verbose(
4163 f'Hostname "{remove_filters(hostname)}" resolved '
4164 f'to: {", ".join(addrlist)}'
4165 )
4166 elif '|missing-ok' in hostname:
4167 INTERNAL.iplist_missing_ok.add(hostname)
4168 else:
4169 warning(
4170 f'No IP address found for hostname '
4171 f'"{remove_filters(hostname)}" in iplist'
4172 )
4173
4174
4175 def active_sets():
4176 """Return set names in currently active firewall."""
4177 ret = set()
4178 data = nft_json('list sets table inet foomuuri')
4179 if not data:
4180 return ret
4181 for item in data['nftables']:
4182 if 'set' in item:
4183 ret.add(f'@{item["set"]["name"][:-2]}')
4184 return ret
4185
4186
4187 def get_url(url: str, max_size: int) -> typing.Optional[str]:
4188 """Download URL and return it as text, or None if failure."""
4189
4190 def get_response_content(response, max_size: int) -> io.BytesIO:
4191 """Helper function to get response content."""
4192 if response.status != 200:
4193 raise ValueError(f'status code: {response.status}')
4194
4195 content_length = int(response.headers.get('content-length', 0))
4196 if content_length > max_size:
4197 raise ValueError(
4198 f'content length {content_length} exceeds {max_size}'
4199 )
4200
4201 content = io.BytesIO()
4202 for chunk in response.stream(INTERNAL.url_chunk_size):
4203 content.write(chunk)
4204 if content.tell() > max_size:
4205 raise ValueError(f'content length exceeds {max_size}')
4206
4207 return content
4208
4209 try:
4210 # urllib3 is optional, needed to download iplist with HTTP GET.
4211 # Import it here as it is heavy module to load.
4212 # ruff: noqa: PLC0415 (`import` should be at the top-level of a file)
4213 # pylint: disable=import-outside-toplevel
4214 import urllib3
4215
4216 with urllib3.PoolManager().request(
4217 method='GET',
4218 url=remove_filters(url),
4219 decode_content=False,
4220 preload_content=False,
4221 retries=urllib3.Retry(**INTERNAL.url_get_retries),
4222 timeout=urllib3.Timeout(**INTERNAL.url_get_timeout),
4223 ) as response:
4224 return (
4225 get_response_content(response, max_size)
4226 .getvalue()
4227 .decode('utf-8', errors='ignore')
4228 )
4229 except ImportError:
4230 warning(
4231 f'Can\'t download iplist URL "{remove_filters(url)}": '
4232 f'No python-urllib3 installed'
4233 )
4234 except (ValueError, urllib3.exceptions.HTTPError) as error:
4235 if '|missing-ok' in url:
4236 INTERNAL.iplist_missing_ok.add(url)
4237 else:
4238 warning(
4239 f'Can\'t download iplist URL "{remove_filters(url)}": {error}'
4240 )
4241 finally:
4242 with contextlib.suppress(NameError):
4243 response.drain_conn()
4244 response.release_conn()
4245
4246
4247 def get_file(wildcard):
4248 """Read all files and return them as single text."""
4249 wildpath = pathlib.Path(remove_filters(wildcard))
4250 filenames = sorted(wildpath.parent.glob(wildpath.name))
4251 if not filenames:
4252 if '|missing-ok' in wildcard:
4253 INTERNAL.iplist_missing_ok.add(wildcard)
4254 else:
4255 warning(
4256 f'Can\'t read iplist file "{remove_filters(wildcard)}": '
4257 f'No such file'
4258 )
4259 return None
4260 text = ''
4261 for filename in filenames:
4262 try:
4263 content = filename.read_text(encoding='utf-8')
4264 except PermissionError as error:
4265 warning(f'Can\'t read iplist file "{filename}": {error}')
4266 return None
4267 text = text + '\n' + content
4268 return text
4269
4270
4271 def parse_duration(timespec, fallback):
4272 """Parse timespec 1w2d3h4m5s to seconds."""
4273 units = (
4274 ('w', 'weeks'),
4275 ('d', 'days'),
4276 ('h', 'hours'),
4277 ('m', 'minutes'),
4278 ('s', 'seconds'),
4279 )
4280 delta = collections.defaultdict(int)
4281 timespec = timespec.replace(' ', '').replace('_', '')
4282
4283 for unit in units:
4284 pattern = rf'^(\d+){unit[0]}'
4285 if match := re.match(pattern, timespec):
4286 delta[unit[1]] = int(match[1])
4287 timespec = re.sub(pattern, '', timespec, count=1)
4288
4289 if delta and not timespec:
4290 with contextlib.suppress(OverflowError):
4291 return int(datetime.timedelta(**delta).total_seconds())
4292
4293 return fallback
4294
4295
4296 def seconds_to_duration(seconds: int) -> str:
4297 """Convert seconds to duration string."""
4298 days, seconds = divmod(seconds, 86400)
4299 hours, seconds = divmod(seconds, 3600)
4300 minutes, seconds = divmod(seconds, 60)
4301 if days:
4302 return f'{days}d{hours:02d}h{minutes:02d}m{seconds:02d}s'
4303 if hours:
4304 return f'{hours}h{minutes:02d}m{seconds:02d}s'
4305 if minutes:
4306 return f'{minutes}m{seconds:02d}s'
4307 return f'{seconds}s'
4308
4309
4310 def iterate_set_elements(data):
4311 """Iterate elements from "nft --json list set" output."""
4312 for toplevel in (data or {}).get('nftables', []):
4313 for elem in toplevel.get('set', {}).get('elem', []):
4314 if isinstance(elem, dict) and 'elem' in elem:
4315 ipaddr = elem['elem']['val']
4316 expire = elem['elem'].get('expires', 864000) # 10 days
4317 else:
4318 ipaddr = elem
4319 expire = 864000
4320 if isinstance(ipaddr, str):
4321 yield ipaddr, expire
4322 elif 'range' in ipaddr:
4323 yield f'{ipaddr["range"][0]}-{ipaddr["range"][1]}', expire
4324 else:
4325 yield (
4326 (f'{ipaddr["prefix"]["addr"]}/{ipaddr["prefix"]["len"]}'),
4327 expire,
4328 )
4329
4330
4331 def command_iplist_status():
4332 """List iplist(s) name and entries count."""
4333 config = minimal_config()
4334 known = parse_config_iplist(config)
4335 table = [('Iplist', 'Entries')]
4336 error = 0
4337 for setname in INTERNAL.parameters[1:] or sorted(known):
4338 setname = setname.removeprefix('@')
4339 if setname.endswith(('_4', '_6')):
4340 data4 = nft_json(f'list set inet foomuuri {setname}')
4341 data6 = {}
4342 else:
4343 data4 = nft_json(f'list set inet foomuuri {setname}_4')
4344 data6 = nft_json(f'list set inet foomuuri {setname}_6')
4345 if data4 is None and data6 is None:
4346 warning(f'Unknown iplist: @{setname}')
4347 error = 1
4348 continue
4349 count = len(
4350 list(
4351 itertools.chain(
4352 iterate_set_elements(data4), iterate_set_elements(data6)
4353 )
4354 )
4355 )
4356 table.append((f'@{setname}', int_to_human(count)))
4357
4358 print_table(data=table, align='<>', header=True)
4359 return error
4360
4361
4362 def command_iplist_list():
4363 """List iplist entries."""
4364 config = minimal_config()
4365 known = parse_config_iplist(config)
4366 table = [('Iplist', 'IPAddress', 'Timeout')]
4367 error = 0
4368 for setname in INTERNAL.parameters[1:] or sorted(known):
4369 setname = setname.removeprefix('@')
4370 if setname.endswith(('_4', '_6')):
4371 data4 = nft_json(f'list set inet foomuuri {setname}')
4372 data6 = {}
4373 else:
4374 data4 = nft_json(f'list set inet foomuuri {setname}_4')
4375 data6 = nft_json(f'list set inet foomuuri {setname}_6')
4376 if data4 is None and data6 is None:
4377 warning(f'Unknown iplist: @{setname}')
4378 error = 1
4379 continue
4380 elems = sorted(iterate_set_elements(data4)) + sorted(
4381 iterate_set_elements(data6)
4382 )
4383 if not elems:
4384 table.append((f'@{setname}', '', ''))
4385 else:
4386 table.extend(
4387 (f'@{setname}', elem, seconds_to_duration(timeout))
4388 for elem, timeout in elems
4389 )
4390
4391 print_table(data=table, align='<<>', header=True)
4392 return error
4393
4394
4395 def command_iplist_add():
4396 """Add entries to iplist."""
4397 config = minimal_config()
4398 iplists = parse_config_iplist(config)
4399 cache = read_iplist_cache(iplists)
4400 setname = INTERNAL.parameters[1]
4401 if not setname.startswith('@'):
4402 setname = f'@{setname}'
4403 if setname not in iplists:
4404 fail(f'Unknown iplist name: {setname}')
4405 timeout = max(
4406 iplists[setname].options.url_timeout,
4407 iplists[setname].options.dns_timeout,
4408 )
4409 now = int(time.time())
4410 expire = now + timeout
4411
4412 # Add entries to cache and apply it
4413 iplist_cache_init(cache, f'manual.{setname}')
4414 for address in INTERNAL.parameters[2:]:
4415 ipv = is_ip_address(address, allow_negative=False)
4416 if ipv:
4417 cache[f'manual.{setname}']['ip'][address] = expire
4418 cache[f'manual.{setname}']['dirty'] = True
4419 else:
4420 timeout = parse_duration(address, 0)
4421 if not timeout:
4422 fail(f'Invalid IP address {address}')
4423 expire = now + timeout
4424 if timeout > 630720000: # More than 20 years is "forever"
4425 expire = 4000000000 # year 2096
4426
4427 return iplist_output_values(iplists, cache, {setname}, {setname})
4428
4429
4430 def command_iplist_del():
4431 """Delete entries from iplist."""
4432 config = minimal_config()
4433 iplists = parse_config_iplist(config)
4434 cache = read_iplist_cache(iplists)
4435 setname = INTERNAL.parameters[1]
4436 if not setname.startswith('@'):
4437 setname = f'@{setname}'
4438 if setname not in iplists:
4439 fail(f'Unknown iplist name: {setname}')
4440
4441 # Delete entries from cache and apply it
4442 iplist_cache_init(cache, f'manual.{setname}')
4443 for address in INTERNAL.parameters[2:]:
4444 cache[f'manual.{setname}']['ip'].pop(address, None)
4445 cache[f'manual.{setname}']['dirty'] = True
4446
4447 return iplist_output_values(iplists, cache, {setname}, {setname})
4448
4449
4450 def command_iplist_flush():
4451 """Delete all IP addresses from all or specific iplist(s)."""
4452 config = minimal_config()
4453 iplists = parse_config_iplist(config)
4454 cache = read_iplist_cache(iplists)
4455 update_only = set()
4456 target = set(INTERNAL.parameters[1:] or iplists)
4457
4458 for setname in target:
4459 if not setname.startswith('@'):
4460 setname = f'@{setname}'
4461 if setname not in iplists:
4462 fail(f'Unknown iplist name: {setname}')
4463
4464 update_only.add(setname)
4465 iplist_cache_init(cache, f'manual.{setname}')
4466 cache[f'manual.{setname}']['ip'] = {}
4467 cache[f'manual.{setname}']['dirty'] = True
4468
4469 return iplist_output_values(iplists, cache, update_only, update_only)
4470
4471
4472 def lxml_parser(url, parser, path, text):
4473 """Parse html/xml text with lxml."""
4474 # pylint: disable=c-extension-no-member
4475 try:
4476 root = lxml.etree.fromstring(text, parser)
4477 nodes = root.xpath(path)
4478 except lxml.etree.LxmlError as error:
4479 warning(f'Failed to parse iplist xml/html "{url}": {error}')
4480 return None
4481 return '\n'.join(nodes)
4482
4483
4484 def iplist_url_filters(url, text):
4485 """Apply filters to downloaded iplist content."""
4486 for spec in url.split('|')[1:]:
4487 if not text:
4488 break
4489
4490 # pylint: disable=c-extension-no-member
4491 if spec.startswith('shell:'):
4492 text = run_program_pipe([spec[6:]], text)
4493 elif spec.startswith('json:'):
4494 text = run_program_pipe(['jq', '--raw-output', spec[5:]], text)
4495 elif spec.startswith('html:'):
4496 if HAVE_LXML:
4497 text = lxml_parser(
4498 url, lxml.etree.HTMLParser(), spec[5:], text
4499 )
4500 else:
4501 warning('No python-lxml installed')
4502 elif spec.startswith('xml:'):
4503 if HAVE_LXML:
4504 text = lxml_parser(url, lxml.etree.XMLParser(), spec[4:], text)
4505 else:
4506 warning('No python-lxml installed')
4507 return text
4508
4509
4510 def iplist_cleanup_file(url: str, text: str) -> list[str]:
4511 """Parse downloaded iplist file to list of IP addresses."""
4512 # Apply filters
4513 text = iplist_url_filters(url, text)
4514 if not text:
4515 return []
4516
4517 # Collect IP addresses
4518 addresses = []
4519 for line in text.splitlines():
4520 for value in line.split():
4521 if value.startswith(('#', ';')): # Remove comments
4522 break
4523 if is_ip_address(value, allow_negative=False):
4524 addresses.append(value)
4525 else:
4526 warning(f'Unknown entry in iplist "{url}": {value}')
4527 return addresses
4528
4529
4530 def get_url_or_file(
4531 url: str, options: IPListSourceOptions, cache: dict
4532 ) -> None:
4533 """Get URL or file content with cache check."""
4534 now = int(time.time())
4535 cachevalue = cache.get(url, {}).get('ip', {})
4536 cacherefresh = cache.get(url, {}).get('refresh')
4537 if cachevalue and cacherefresh:
4538 if now >= cacherefresh + options.timeout: # Expired entry
4539 cache[url]['ip'] = {}
4540 cache[url]['dirty'] = True
4541 elif now < cacherefresh + options.refresh and INTERNAL.force < 0:
4542 verbose(f'Using cached value for "{url}"')
4543 return
4544
4545 value = (
4546 get_url(url, options.max_size)
4547 if url.startswith(('https:', 'http:'))
4548 else get_file(url)
4549 )
4550
4551 if value is not None:
4552 # Replace list, don't append. These lists are always complete
4553 # address lists.
4554 expire = now + options.timeout
4555 cache[url] = {
4556 'ip': dict.fromkeys(iplist_cleanup_file(url, value), expire),
4557 'dirty': True,
4558 'refresh': now,
4559 }
4560 verbose(
4561 f'Iplist content for "{url}" refreshed, '
4562 f'{len(cache[url]["ip"])} entries'
4563 )
4564
4565
4566 def str_to_time(timestamp):
4567 """Convert time string to unix timestamp."""
4568 obj = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
4569 obj = obj.replace(tzinfo=datetime.timezone.utc)
4570 return int(obj.timestamp())
4571
4572
4573 def read_old_iplist_format(cache):
4574 """Read old iplist.fw (v0.27) files and convert them to new cache format.
4575
4576 Example entries:
4577
4578 # add element inet foomuuri fi_6 { 2a13:aec0::/32 timeout 10d }
4579 add element inet foomuuri foo_4 { 10.0.9.98 timeout 24h }
4580 add element inet foomuuri dynamic_4 { 10.0.0.4 timeout
4581 798169s } # 2024-12-11T17:16:28
4582 """
4583 for file_key in ('resolve_file', 'iplist_file', 'iplist_manual_file'):
4584 try:
4585 manual = state_file(file_key).read_text('utf-8')
4586 except OSError:
4587 continue # Silently ignore all errors
4588
4589 for line in manual.splitlines():
4590 line = line.removeprefix('# ')
4591 items = line.split()
4592 if (
4593 not line.startswith('add element inet')
4594 or len(items) not in {10, 12}
4595 or not is_ip_address(items[6])
4596 ):
4597 continue
4598 setname = f'manual.@{items[4][:-2]}'
4599
4600 if len(items) == 12:
4601 # iplist_manual_file has expire stamp.
4602 timeout = str_to_time(items[11])
4603 else:
4604 timeout = int(time.time()) + parse_duration(items[8], 0)
4605 iplist_cache_init(cache, setname)
4606 cache[setname]['ip'][items[6]] = timeout
4607
4608
4609 def read_iplist_cache(iplist):
4610 """Read current iplist values from cache."""
4611 cache = {}
4612 filename = state_file('iplist_cache_file')
4613 try:
4614 cache = json.loads(filename.read_text('utf-8'))
4615 except json.decoder.JSONDecodeError as error:
4616 warning(f'File {filename}: Broken JSON format: {error}')
4617 except PermissionError as error:
4618 fail(f"File {filename}: Can't read: {error}")
4619 except FileNotFoundError:
4620 read_old_iplist_format(cache)
4621
4622 now = int(time.time())
4623 for key in list(cache):
4624 if (
4625 (key.startswith('@') and key not in iplist)
4626 or (key.startswith('manual.@') and key[7:] not in iplist)
4627 or 'ip' not in cache[key]
4628 ):
4629 # Iplist in cache, not in config
4630 del cache[key]
4631 verbose(f'Deleting iplist "{key}" from cache, not in config')
4632 continue
4633
4634 # Check top level refresh. Missing or invalid value is ok.
4635 try:
4636 if isinstance(cache[key]['refresh'], str):
4637 cache[key]['refresh'] = str_to_time(cache[key]['refresh'])
4638 except KeyError:
4639 cache[key]['refresh'] = now
4640
4641 # Check IP address timeouts
4642 dellist = set()
4643 for addr, timeout in cache[key]['ip'].items():
4644 if isinstance(timeout, str):
4645 timeout = str_to_time(timeout)
4646 cache[key]['ip'][addr] = timeout
4647 if now >= timeout:
4648 dellist.add(addr)
4649 for addr in dellist:
4650 verbose(f'Deleting expired iplist "{key}" entry "{addr}"')
4651 del cache[key]['ip'][addr]
4652
4653 # Don't keep empty sets
4654 if not cache[key]['ip']:
4655 del cache[key]
4656 return cache
4657
4658
4659 def write_iplist_cache(cache):
4660 """Write iplist cache to disk for next run."""
4661 for key in cache:
4662 cache[key].pop('dirty', None)
4663 if key.startswith(('@', 'manual.@')):
4664 del cache[key]['refresh']
4665
4666 save_file(state_file('iplist_cache_file'), cache)
4667
4668
4669 def parse_config_iplist(config) -> IPLists:
4670 """Parse iplist{} sections from config."""
4671
4672 # pylint: disable=too-many-branches
4673 def parse_option(
4674 options: IPListOptions, option: str, fileline: str
4675 ) -> bool:
4676 """Option parser helper."""
4677 try:
4678 return options.parse_option(option)
4679 except AttributeError:
4680 fail(f'{fileline}Invalid iplist{{}} option name: {option}')
4681 except ValueError:
4682 fail(f'{fileline}Invalid iplist{{}} option value: {option}')
4683 return False
4684
4685 default_options = IPListOptions()
4686 iplists = IPLists()
4687
4688 if config.get('resolve'):
4689 warning('Section "resolve" is obsolete, use "iplist" instead')
4690
4691 for fileline, line in config.get('resolve', []) + config.get('iplist', []):
4692 # Parse old default option syntax
4693 if line[0] in {'timeout', 'refresh'}:
4694 option = line[0]
4695 value = ' '.join(line[1:])
4696 warning(
4697 f'{fileline}Iplist "{option}" is obsolete, use '
4698 f'"dns_{option}" or "url_{option}" instead'
4699 )
4700 parse_option(default_options, f'dns_{option}={value}', fileline)
4701 parse_option(default_options, f'url_{option}={value}', fileline)
4702 continue
4703
4704 # Parse one or more 'equal-sign'-style default options in single line
4705 for index, default_option in enumerate(line):
4706 if not parse_option(default_options, default_option, fileline):
4707 if index == 0:
4708 break
4709 fail(f'{fileline}Unsupported syntax: {" ".join(line)}')
4710 else:
4711 continue
4712
4713 # Parse 'space-separated'-style default option
4714 option = f'{line[0]}={" ".join(line[1:])}'
4715 if parse_option(default_options, option, fileline):
4716 continue
4717
4718 # Is it an iplist?
4719 if not Validators.str_iplist_name(name := line[0]):
4720 fail(f'{fileline}Invalid iplist name: {" ".join(line)}')
4721
4722 if name not in iplists:
4723 # Create new iplist with default_options collected so far
4724 iplists[name] = IPList(
4725 options=IPListOptions(*dataclasses.astuple(default_options))
4726 )
4727
4728 if len(line) >= 3 and line[1] == '+':
4729 # Append iplist sources
4730 options = line[2:]
4731 else:
4732 if iplists[name].sources:
4733 # Overwrite iplist sources
4734 iplists[name].sources.clear()
4735 warning(
4736 f'{fileline}Overwriting iplist "{name}" with value '
4737 f'"{" ".join(line[1:])}"'
4738 )
4739 options = line[1:]
4740
4741 # Parse the rest of iplist definition
4742 for option in options:
4743 # Ignore insignificant tokens
4744 if option in {'-', '+'}:
4745 continue
4746
4747 # Inline option?
4748 if parse_option(iplists[name].options, option, fileline):
4749 continue
4750
4751 # Add as a source
4752 iplists[name].sources.append(option)
4753
4754 iplist_verify_filters(name, iplists[name].sources, fileline)
4755 if CONFIG.verbose >= 2:
4756 for name, iplist in iplists.items():
4757 verbose(f'{name:9s} {" ".join(iplist.sources)}')
4758 INTERNAL.iplist_missing_ok = set()
4759 return iplists
4760
4761
4762 def iplist_collect_resolve_list(iplists: IPLists) -> tuple:
4763 """Collect URLs and hostnames to be resolved."""
4764 if INTERNAL.command != 'iplist':
4765 # Don't resolve anything if called as "foomuuri start".
4766 # Use cached entries only.
4767 return {}, {}
4768
4769 urls = collections.defaultdict(IPListSourceOptions)
4770 hostnames = collections.defaultdict(IPListSourceOptions)
4771 for iplist in iplists.values():
4772 for source in iplist.sources:
4773 if source.startswith(('https://', 'http://', '.', '/')):
4774 for option in ('timeout', 'refresh', 'max_size'):
4775 urls[source].update_if_less(
4776 option, iplist.options[f'url_{option}']
4777 )
4778 elif not is_ip_address(source, allow_negative=False):
4779 for option in ('timeout', 'refresh'):
4780 hostnames[source].update_if_less(
4781 option, iplist.options[f'dns_{option}']
4782 )
4783 if CONFIG.verbose >= 2:
4784 verbose(f'URLs: {urls}')
4785 verbose(f'Hostnames: {hostnames}')
4786 return urls, hostnames
4787
4788
4789 def iplist_cache_init(cache, setname):
4790 """Add empty setname to cache."""
4791 if setname in cache:
4792 return
4793 cache[setname] = {
4794 'ip': {},
4795 'refresh': int(time.time()),
4796 }
4797
4798
4799 def iplist_cache_copy_ip(cache, source, destination):
4800 """Copy IP addresses from cache[source] to cache[destination]."""
4801 if source not in cache:
4802 return
4803 if cache[source].get('dirty'):
4804 cache[destination]['dirty'] = True
4805 for address, timeout in cache[source]['ip'].items():
4806 # Choose maximum timeout if both dynamic (resolved from hostname)
4807 # and manually added address exists.
4808 old_timeout = cache[destination]['ip'].get(address, timeout)
4809 cache[destination]['ip'][address] = max(old_timeout, timeout)
4810
4811
4812 def iplist_output_add_element(cache, setname, now):
4813 """Output nft commands to update iplist values to sets.
4814
4815 Auto-merge in nftables set requires that adds are done set by set, not
4816 mixing foo_4 and foo_6 adds.
4817 """
4818 # Collect addresses to IPv4 and IPv6 sets
4819 updates = {
4820 4: set(),
4821 6: set(),
4822 }
4823 for address in cache[setname]['ip']:
4824 if ':' in address:
4825 updates[6].add(address)
4826 else:
4827 updates[4].add(address)
4828
4829 # Output add to sets
4830 out('')
4831 for ipv in (4, 6):
4832 out(f'flush set inet foomuuri {setname[1:]}_{ipv}')
4833 if not updates[ipv]:
4834 continue
4835
4836 out(f'add element inet foomuuri {setname[1:]}_{ipv} {{')
4837 for address in sorted(updates[ipv]):
4838 timeout = cache[setname]['ip'][address]
4839 seconds = max(1, timeout - now)
4840 if seconds > 630720000: # 20 years, add without timeout
4841 out(f'{address},')
4842 else:
4843 out(f'{address} timeout {seconds}s,')
4844 out('}')
4845
4846
4847 def iplist_output_values(
4848 iplists: IPLists,
4849 cache: dict,
4850 currently_active: set[str],
4851 update_only: set[str],
4852 ) -> int:
4853 """Output and apply nft commands to add iplist entries."""
4854 now = int(time.time())
4855 error = 0
4856 for setname in sorted(iplists):
4857 setvalues = iplists[setname].sources
4858
4859 # Collect new values to cache[@setname]
4860 cache[setname] = {
4861 'ip': {},
4862 'refresh': now,
4863 }
4864 for value in setvalues:
4865 # Static IP address doesn't need refresh, so timeout is forever
4866 if is_ip_address(value, allow_negative=False):
4867 cache[setname]['ip'][value] = 4000000000 # forever
4868 cache[setname]['dirty'] = True
4869 continue
4870
4871 # Add IPs from URL/hostname
4872 iplist_cache_copy_ip(cache, value, setname)
4873
4874 # Add manually added IPs
4875 iplist_cache_copy_ip(cache, f'manual.{setname}', setname)
4876
4877 # Check that this set should be updated
4878 if update_only and setname not in update_only:
4879 continue # Filtered away by command line parameter
4880 if setname not in currently_active:
4881 warning(
4882 f'Iplist "{setname}" does not exist in currently active '
4883 f'firewall'
4884 )
4885 continue
4886 if not cache[setname].get('dirty') and INTERNAL.force < 0:
4887 verbose(f'Iplist "{setname}" is clean, skipping update')
4888 continue
4889
4890 # Print error if set had values, but result is empty
4891 if (
4892 setvalues
4893 and not cache[setname]['ip']
4894 and INTERNAL.command == 'iplist'
4895 ):
4896 if all(name in INTERNAL.iplist_missing_ok for name in setvalues):
4897 warning(f'Iplist "{setname}" is empty')
4898 else:
4899 fail(f'Iplist "{setname}" is empty', False)
4900 error = 2
4901 else:
4902 verbose(
4903 f'Updating iplist "{setname}", '
4904 f'{len(cache[setname]["ip"])} entries'
4905 )
4906
4907 # Add elements to sets
4908 iplist_output_add_element(cache, setname, now)
4909
4910 return iplist_apply_values(cache) | error
4911
4912
4913 def iplist_apply_values(cache):
4914 """Save iplist changes to disk and use nft to apply it."""
4915 if INTERNAL.command != 'iplist' or not OUT:
4916 return 0
4917
4918 write_iplist_cache(cache)
4919 lines = '\n'.join(save_final(None))
4920 ret = run_program_rc(CONFIG.nft_bin + ['--file', '-'], stdin=lines)
4921 if ret:
4922 fail(f'Failed to update iplists, nftables error code {ret}', False)
4923 return ret
4924
4925
4926 def command_iplist_refresh(
4927 iplists: typing.Optional[IPLists] = None,
4928 currently_active: typing.Optional[set[str]] = None,
4929 override_update_only: typing.Optional[set[str]] = None,
4930 ) -> int:
4931 """Refresh iplist{} entries."""
4932 # Read minimal config and parse it
4933 if not iplists:
4934 config = minimal_config()
4935 iplists = parse_config_iplist(config)
4936 urls, hostnames = iplist_collect_resolve_list(iplists)
4937
4938 # Read URLs and files to cache
4939 cache = read_iplist_cache(iplists)
4940 for url, options in urls.items():
4941 get_url_or_file(url, options, cache)
4942
4943 # Resolve hostnames to cache
4944 resolve_all_hostnames(hostnames, cache)
4945
4946 # Read active set names from nft if not provided by "foomuuri start"
4947 if currently_active is None:
4948 currently_active = active_sets()
4949
4950 # Special case for testing (non-root) or faked nft_bin
4951 if not currently_active and (
4952 not INTERNAL.root_power or CONFIG.nft_bin == ['true']
4953 ):
4954 currently_active = set(iplists)
4955
4956 # Parse which iplists should be updated.
4957 if override_update_only:
4958 update_only = override_update_only
4959 else:
4960 # Default is to update all iplists (empty set).
4961 # This can be filtered with "foomuuri iplist refresh setname...".
4962 update_only = {
4963 name if name.startswith('@') else f'@{name}'
4964 for name in INTERNAL.parameters[1:]
4965 }
4966
4967 # Make sure systemd's TimeoutStartSec doesn't trigger while updating
4968 # ruleset or writing cache file.
4969 notify('EXTEND_TIMEOUT_USEC=120000000') # 2 minutes
4970
4971 # Apply updates to ruleset
4972 ret = iplist_output_values(iplists, cache, currently_active, update_only)
4973 notify('READY=1')
4974 notify('STOPPING=1')
4975 return ret
4976
4977
4978 def command_counter_list() -> int:
4979 """List named counters."""
4980 # Read config if available for nft binary path.
4981 minimal_config(require_etc_config=False)
4982 list_params = INTERNAL.parameters[1:]
4983
4984 # Parse counter data from kernel
4985 data = nft_json('list counters table inet foomuuri')
4986 if not data:
4987 fail("Can't read counter data from kernel")
4988 return 1
4989 counters = {}
4990 for toplevel in data.get('nftables', []):
4991 counter = toplevel.get('counter')
4992 name = (counter or {}).get('name')
4993 if not name:
4994 continue
4995 packet_value = int_to_human(counter.get('packets', 0))
4996 byte_value = int_to_human(counter.get('bytes', 0))
4997 counters[name] = (packet_value, byte_value)
4998 if not counters:
4999 fail('No named counters defined in currently active firewall')
5000
5001 # Print counter data
5002 table = [('Counter', 'Packets', 'Bytes')]
5003 error = 0
5004 for name in list_params or counters:
5005 values = counters.get(name)
5006 if not values:
5007 warning(f'Unknown counter: {name}')
5008 error = 1
5009 continue
5010 table.append((name, values[0], values[1]))
5011 print_table(data=table, align='<>>', header=True)
5012 return error
5013
5014
5015 def command_macro_list() -> int:
5016 """List default system macros and user defined macros.
5017
5018 Don't require config files so that this can be run before initial
5019 configuration.
5020 """
5021 macros = parse_config_macros(read_config(require_etc_config=False))
5022 list_params = INTERNAL.parameters[1:]
5023 print('macro {')
5024 table = [
5025 ('', macro, f'{" ".join(macros[macro])}')
5026 for macro in sorted(macros)
5027 if not macro.startswith('_template_')
5028 and (
5029 not list_params
5030 or macro in list_params
5031 or any(item in list_params for item in macros[macro])
5032 )
5033 ]
5034 print_table(data=table, header=False)
5035 print('}')
5036 return 0
5037
5038
5039 def command_ruleset_list() -> int:
5040 """List currently active ruleset."""
5041 # List full ruleset if no zone-zone specified. Read config if available
5042 # for nft binary path.
5043 list_params = INTERNAL.parameters[1:]
5044 if not list_params:
5045 minimal_config(require_etc_config=False)
5046 return nft_command('list ruleset')
5047
5048 # Read config for correct nft_bin value and zone-zone names
5049 config = minimal_config()
5050 zones = parse_config_zones(config)
5051 zonepairs = [f'{szone}-{dzone}' for szone in zones for dzone in zones]
5052
5053 # List zone-zone rules
5054 error = 0
5055 for zone in list_params:
5056 if zone not in zonepairs:
5057 warning(f'Unknown zone-zone: {zone}')
5058 error = 1
5059 continue
5060 for ipv in (4, 6):
5061 error |= nft_command(f'list chain inet foomuuri {zone}_{ipv}')
5062 return error
5063
5064
5065 def command_subcommand() -> int:
5066 """Parse "foomuuri ruleset|iplist|..." subcommand."""
5067 # Use default subcommand if not specified in command line
5068 if not INTERNAL.parameters:
5069 INTERNAL.parameters = [
5070 {
5071 'counter': 'list',
5072 'iplist': 'status',
5073 'macro': 'list',
5074 'ruleset': 'list',
5075 }.get(INTERNAL.command)
5076 ]
5077
5078 # Find correct handler. min_args includes "list" argument.
5079 handler, min_args = {
5080 ('counter', 'list'): (command_counter_list, 1),
5081 ('iplist', 'add'): (command_iplist_add, 3),
5082 ('iplist', 'del'): (command_iplist_del, 3),
5083 ('iplist', 'flush'): (command_iplist_flush, 1),
5084 ('iplist', 'list'): (command_iplist_list, 1),
5085 ('iplist', 'refresh'): (command_iplist_refresh, 1),
5086 ('iplist', 'status'): (command_iplist_status, 1),
5087 ('macro', 'list'): (command_macro_list, 1),
5088 ('ruleset', 'list'): (command_ruleset_list, 1),
5089 }.get((INTERNAL.command, INTERNAL.parameters[0]), (None, 0))
5090 if handler and len(INTERNAL.parameters) >= min_args:
5091 return handler()
5092 return command_help()
5093
5094
5095 def command_list() -> int:
5096 """Backward compability command handler."""
5097 if not INTERNAL.parameters:
5098 warning(
5099 'Command "foomuuri list" is obsolete, use '
5100 '"foomuuri ruleset list" instead'
5101 )
5102 return command_ruleset_list()
5103 if INTERNAL.parameters[0] == 'macro':
5104 warning(
5105 'Command "foomuuri list macro" is obsolete, use '
5106 '"foomuuri macro list" instead'
5107 )
5108 return command_macro_list()
5109 if INTERNAL.parameters[0] == 'counter':
5110 warning(
5111 'Command "foomuuri list counter" is obsolete, use '
5112 '"foomuuri counter list" instead'
5113 )
5114 return command_counter_list()
5115 warning(
5116 'Command "foomuuri list" is obsolete, use '
5117 '"foomuuri ruleset list" instead'
5118 )
5119 INTERNAL.parameters = ['list'] + INTERNAL.parameters
5120 return command_ruleset_list()
5121
5122
5123 def command_reload():
5124 """Run start and refresh iplist."""
5125 # Use same args
5126 args = [sys.argv[0]]
5127 args.extend(arg for arg in sys.argv[1:] if arg.startswith('--'))
5128
5129 # Run commands
5130 for sub, fatal in ((['start'], True), (['iplist', 'refresh'], False)):
5131 ret = run_program_rc(args + sub)
5132 if ret and fatal:
5133 return ret
5134 return 0
5135
5136
5137 def alarm_handler(_dummy_signum, _dummy_frame):
5138 """Signal handler to catch input timeout."""
5139 raise KeyboardInterrupt
5140
5141
5142 def input_timeout(prompt, timeout):
5143 """Ask something from user with timeout."""
5144 signal.signal(signal.SIGALRM, alarm_handler)
5145 signal.signal(signal.SIGHUP, alarm_handler)
5146 signal.signal(signal.SIGINT, alarm_handler)
5147 signal.signal(signal.SIGTERM, alarm_handler)
5148 signal.alarm(timeout)
5149 try:
5150 return input(prompt)
5151 except (KeyboardInterrupt, EOFError):
5152 print()
5153 print('Timeout')
5154 finally:
5155 signal.alarm(0)
5156 return ''
5157
5158
5159 def command_try_reload():
5160 """Reload, ask confirmation, optionally revert to previous rules.
5161
5162 Revert is not fail safe. Things can break if:
5163 - Zone is added/deleted to config and NetworkManager sends new
5164 "change to zone" request via D-Bus. This happens only if user
5165 reconfigures NetworkManager or attach ethernet cable or similar.
5166 - Needed entry is removed from iplist config and foomuuri-iplist.timer
5167 updates iplist contents. There is a long expiry time for entries so
5168 there is plenty of time to fix config.
5169
5170 It is recommended to fix config immediately and run "foomuuri reload" or
5171 "foomuuri try-reload".
5172 """
5173 # Get copy of currently active ruleset
5174 minimal_config()
5175 old_rules = run_program_pipe(
5176 CONFIG.nft_bin + ['list', 'table', 'inet', 'foomuuri'],
5177 None,
5178 check_error=False,
5179 )
5180 if not old_rules or 'netlink: Error' in old_rules:
5181 # Not running as root or similar error
5182 if old_rules:
5183 print(old_rules.strip())
5184 fail('Failed to read old ruleset')
5185 return 1
5186
5187 # Load new ruleset
5188 error = command_reload()
5189 if error:
5190 return error
5191
5192 # Verify from user if ruleset is ok
5193 timeout = CONFIG.try_reload_timeout
5194 print(f'You have {timeout} seconds to accept the changes.')
5195 answer = input_timeout(
5196 'Can you establish NEW connections (yes/no)? ', timeout
5197 )
5198 if answer.lower().startswith('y'):
5199 return 0
5200
5201 # Revert to old ruleset and return errorcode 2. Revert to empty
5202 # ruleset if Foomuuri was not running.
5203 if not old_rules.startswith('table inet foomuuri {'):
5204 old_rules = ''
5205 warning('Revert to empty ruleset. Foomuuri was not running.')
5206 else:
5207 warning('Revert to old config')
5208 save_file(
5209 state_file('good_file'),
5210 ['table inet foomuuri', 'delete table inet foomuuri', '', old_rules],
5211 )
5212 return (
5213 run_program_rc(CONFIG.nft_bin + ['--file', state_file('good_file')])
5214 + 2
5215 )
5216
5217
5218 def seconds_to_human(seconds):
5219 """Convert seconds int to human readable "19 days, 18:37" format."""
5220 day = seconds // 86400
5221 hour = (seconds // 3600) % 24
5222 minute = (seconds // 60) % 60
5223 second = seconds % 60
5224 if day:
5225 return f'{day} days, {hour:02d}:{minute:02d}:{second:02d}'
5226 return f'{hour:02d}:{minute:02d}:{second:02d}'
5227
5228
5229 def monitor_state_command(targets, groups, cfg, grouptarget, name):
5230 """Run command if group/target state changes."""
5231 # pylint: disable=too-many-locals
5232
5233 # Log state change
5234 updown = 'up' if cfg['state'] else 'down'
5235 now = time.time()
5236 prev = cfg.get('state_time')
5237 history = None
5238 if prev:
5239 seconds = int(now - prev + 0.5)
5240 extra = f'previous change was {seconds_to_human(seconds)} ago'
5241 if grouptarget == 'target':
5242 for cons in range(cfg['history_size']):
5243 if cfg['history'][-1 - cons] != cfg['state']:
5244 break
5245 historycount = cfg['history'].count(cfg['state'])
5246 extra = (
5247 f'{extra}, consecutive_{updown} {cons}, '
5248 f'history_{updown} {historycount}'
5249 )
5250 history = ''.join('.' if item else '!' for item in cfg['history'])
5251 else:
5252 extra = 'startup change'
5253 verbose(f'{grouptarget} {name} changed state to {updown}, {extra}', 0)
5254 if history:
5255 verbose(f'{grouptarget} {name} history: {history}', 0)
5256 cfg['state_time'] = cfg['last_down_interval'] = now
5257 monitor_run_command(
5258 targets, groups, cfg, grouptarget, name, updown, extra, history
5259 )
5260
5261
5262 def monitor_run_command( # noqa: RUF100,PLR0917
5263 targets,
5264 groups,
5265 cfg,
5266 grouptarget,
5267 name,
5268 updown,
5269 extra,
5270 history,
5271 ):
5272 """Run monitor's external command if configured.
5273
5274 It will receive current state change event info and all states in
5275 environment variables.
5276 """
5277 # pylint: disable=too-many-arguments
5278 # pylint: disable=too-many-positional-arguments
5279 proc = cfg[f'command_{updown}']
5280 if not proc:
5281 return
5282 env = {
5283 # Change state event
5284 'FOOMUURI_CHANGE_TYPE': grouptarget,
5285 'FOOMUURI_CHANGE_NAME': env_cleanup(name),
5286 'FOOMUURI_CHANGE_STATE': updown,
5287 'FOOMUURI_CHANGE_LOG': extra,
5288 'FOOMUURI_CHANGE_HISTORY': history or '',
5289 # List of configured targets
5290 'FOOMUURI_ALL_TARGET': ' '.join(env_cleanup(item) for item in targets),
5291 # List of configured groups
5292 'FOOMUURI_ALL_GROUP': ' '.join(env_cleanup(item) for item in groups),
5293 }
5294 for target, icfg in targets.items():
5295 env[f'FOOMUURI_TARGET_{env_cleanup(target)}'] = (
5296 'up' if icfg['state'] else 'down'
5297 )
5298 for group, icfg in groups.items():
5299 env[f'FOOMUURI_GROUP_{env_cleanup(group)}'] = (
5300 'up' if icfg.get('state', True) else 'down'
5301 )
5302 run_program_rc(proc, env=env)
5303
5304
5305 def monitor_down_interval(targets, groups, cfg, grouptarget, name):
5306 """Group/target is still down, run command in regular intervals."""
5307 now = time.time()
5308 if now < cfg['last_down_interval'] + cfg['down_interval']:
5309 return
5310 cfg['last_down_interval'] = now
5311 seconds = int(now - cfg['state_time'] + 0.5)
5312 extra = f'previous change was {seconds_to_human(seconds)} ago'
5313 verbose(f'{grouptarget} {name} is still down, {extra}', 0)
5314 monitor_run_command(
5315 targets, groups, cfg, grouptarget, name, 'down_interval', extra, None
5316 )
5317
5318
5319 def monitor_update_groups(targets, groups):
5320 """Update all group statuses.
5321
5322 On startup make decision and send event for all groups after first reply
5323 from any target.
5324 """
5325 for group, cfg in groups.items():
5326 any_up = any(targets[target]['state'] for target in cfg['target'])
5327 state = cfg.get('state', not any_up) # Undef in startup
5328 if not any_up:
5329 if state: # Change from up to down
5330 cfg['state'] = False
5331 monitor_state_command(targets, groups, cfg, 'group', group)
5332 else: # Still down, check interval
5333 monitor_down_interval(targets, groups, cfg, 'group', group)
5334 elif any_up and not state: # Change from down to up
5335 cfg['state'] = True
5336 monitor_state_command(targets, groups, cfg, 'group', group)
5337
5338
5339 def monitor_update_target(targets, groups, target, state):
5340 """Add state to target's history and change its state."""
5341 # Add new state to end of history
5342 cfg = targets[target]
5343 startup_change = False
5344 if 'history' not in cfg:
5345 # First reply ever, fill history and force change event
5346 cfg['history'] = [True] * cfg['history_size']
5347 startup_change = True
5348 cfg['history'] = cfg['history'][1:] + [state]
5349
5350 # Target state can't change if added state is same as current state
5351 if cfg['state'] == state and not startup_change:
5352 if not state:
5353 monitor_down_interval(targets, groups, cfg, 'target', target)
5354 return
5355
5356 # Check if target state is changed
5357 count_up = sum(cfg['history'])
5358 if cfg['state']:
5359 # Currently up. Target goes down if:
5360 # - history has too many downs
5361 # OR
5362 # - last n items were down
5363 if cfg['history_size'] - count_up >= cfg['history_down'] or not any(
5364 cfg['history'][-cfg['consecutive_down'] :]
5365 ):
5366 cfg['state'] = False
5367 monitor_state_command(targets, groups, cfg, 'target', target)
5368 elif startup_change: # Always send an event on startup
5369 monitor_state_command(targets, groups, cfg, 'target', target)
5370
5371 elif count_up >= cfg['history_up'] and all(
5372 cfg['history'][-cfg['consecutive_up'] :]
5373 ):
5374 # Currently down. Target goes up if:
5375 # - history has enough ups
5376 # AND
5377 # - last n items were up
5378 cfg['state'] = True
5379 monitor_state_command(targets, groups, cfg, 'target', target)
5380
5381
5382 def monitor_parse_line(targets, groups, target, line):
5383 """Parse result line from monitor's command."""
5384 verbose(f'target {target}: {line}', 2)
5385 stat_ms = None # Value for statistics, assume error
5386
5387 if line in {'OK', 'ERROR'}:
5388 # Generic "OK" or "ERROR" reply
5389 state = line == 'OK'
5390 if state:
5391 stat_ms = 1 # There is no time, use 1
5392
5393 elif line.startswith('ICMP Unreachable (Communication Administratively'):
5394 # fping reply if packet is rejected
5395 state = False
5396
5397 elif 'xmt/rcv/%loss' in line:
5398 # fping with "--squiet"
5399 if '0/0/0%' in line:
5400 return # --squiet is less than --interval, ignore line
5401 state = '/100%' not in line # It's up if loss is less than 100%
5402 if state:
5403 with contextlib.suppress(ValueError):
5404 stat_ms = float(line.split('/')[-1])
5405
5406 else:
5407 # fping with "--interval"
5408 match = re.match(r'^[^ ]+ : \[\d+\], (.+)$', line)
5409 if not match:
5410 return # No match, ignore line
5411 result = match.group(1)
5412 state = ' bytes, ' in result and ' ms (' in result
5413 if state:
5414 with contextlib.suppress(ValueError):
5415 stat_ms = float(result.split()[2])
5416
5417 # Update target state
5418 monitor_update_target(targets, groups, target, state)
5419
5420 # Add result to statistics
5421 cfg = targets[target]
5422 cfg['time'] = (cfg['time'] + [stat_ms])[-cfg['statistics_size'] :]
5423
5424
5425 def command_option_values(cmd):
5426 """Quick and dirty parser for "--foo n" and "--foo=n"."""
5427 ret = {}
5428 for index, item in enumerate(cmd):
5429 if not item.startswith('--'):
5430 continue
5431 if '=' in item:
5432 item, value = item.split('=', 1)
5433 else:
5434 try:
5435 value = cmd[index + 1]
5436 except IndexError:
5437 value = None
5438 ret[item] = value
5439 return ret
5440
5441
5442 def mangle_fping_command(target, cfg):
5443 """Mangle fping's command line to contain needed options."""
5444 cmd = cfg['command']
5445 if 'fping' not in cmd[0]:
5446 return cmd
5447 options = command_option_values(cmd)
5448
5449 # --squiet works even if interface is down or missing. Automatically
5450 # use it if missing (1s) or --interval=n000 is specified.
5451 # Use plain --interval if value is not exact second.
5452 squiet = options.get('--squiet', '')
5453 interval = options.get('--interval', '')
5454 if not squiet and not interval: # Nothing, use 1s / 1000ms
5455 cmd = cmd[:1] + ['--squiet=1', '--interval=1000'] + cmd[1:]
5456 elif squiet and not interval: # squiet specified, use it as interval
5457 cmd = cmd[:1] + [f'--interval={squiet}000'] + cmd[1:]
5458 elif not squiet and interval.endswith('000'): # Use interval as squiet
5459 cmd = cmd[:1] + [f'--squiet={interval[:-3]}'] + cmd[1:]
5460 elif squiet and interval:
5461 with contextlib.suppress(ValueError):
5462 if int(squiet) * 1000 != int(interval):
5463 warning(
5464 f'Mismatching "--squiet" and "--interval" values in '
5465 f'target "{target}": {" ".join(cmd)}'
5466 )
5467
5468 # Add --loop if missing to get continous reports
5469 if '--loop' not in options:
5470 cmd = cmd[:1] + ['--loop'] + cmd[1:]
5471
5472 return cmd
5473
5474
5475 def monitor_start_targets(targets, groups):
5476 """Start pinging all targets."""
5477 started_something = False
5478 for target, cfg in targets.items():
5479 # Check if target is already running, or still waiting for restart
5480 if cfg.get('proc') or cfg['proc_restart'] > time.time():
5481 continue
5482
5483 # Assume it is up on first startup
5484 if 'state' not in cfg:
5485 cfg['state'] = True
5486
5487 # Start command (pylint: disable=consider-using-with)
5488 cmd = mangle_fping_command(target, cfg)
5489 verbose(f'target {target} command: {" ".join(map(str, cmd))}', 2)
5490 try:
5491 cfg['proc'] = subprocess.Popen(
5492 cmd,
5493 stdin=subprocess.DEVNULL,
5494 stdout=subprocess.PIPE,
5495 stderr=subprocess.STDOUT,
5496 encoding='utf-8',
5497 )
5498 verbose(f'target {target} monitoring command started')
5499 started_something = True
5500 except OSError as error:
5501 verbose(f'target {target} monitoring command failed: {error}', 0)
5502 cfg['proc'] = None
5503 cfg['proc_restart'] = time.time() + 30
5504
5505 # Mark it as down immediately
5506 cfg['history'] = [False] * cfg['history_size']
5507 monitor_update_target(targets, groups, target, False)
5508
5509 # Small initial sleep so that first read_target() will receive more
5510 # replies in one go. This helps to get more "target up" events before
5511 # "group up" events.
5512 if started_something:
5513 time.sleep(0.5)
5514
5515
5516 def monitor_close_target(targets, groups, target):
5517 """Target monitoring command died. Mark it as down."""
5518 # Terminate proc and schedule restart
5519 verbose(
5520 f'target {target} monitoring command died, restarting it in 30s', 0
5521 )
5522 cfg = targets[target]
5523 cfg['proc'].terminate()
5524 cfg['proc'].wait(timeout=0.1)
5525 cfg['proc'] = None
5526 cfg['proc_restart'] = time.time() + 30
5527 cfg['time'] = []
5528
5529 # Mark it as down immediately
5530 cfg['history'] = [False] * cfg['history_size']
5531 monitor_update_target(targets, groups, target, False)
5532
5533
5534 def monitor_read_targets(targets, groups):
5535 """Read incoming lines from all targets."""
5536 # (Re)Start targets if not running
5537 monitor_start_targets(targets, groups)
5538
5539 # Wait for incoming lines from any client, up to 10 seconds
5540 readfd = [item['proc'].stdout for item in targets.values() if item['proc']]
5541 poll = select.select(readfd, [], [], 10.0)
5542
5543 # Read all incoming lines
5544 for readfd in poll[0]:
5545 for target, cfg in targets.items():
5546 if cfg['proc'] and cfg['proc'].stdout == readfd:
5547 line = readfd.readline()
5548 if line:
5549 monitor_parse_line(targets, groups, target, line.strip())
5550 else:
5551 monitor_close_target(targets, groups, target)
5552 break
5553
5554 # Update all groups
5555 monitor_update_groups(targets, groups)
5556
5557
5558 def monitor_terminate_targets(targets):
5559 """Terminate all subprocesses."""
5560 for cfg in targets.values():
5561 if cfg['proc']:
5562 cfg['proc'].terminate()
5563 for cfg in targets.values():
5564 if cfg['proc']:
5565 cfg['proc'].wait(timeout=0.1)
5566
5567
5568 def parse_config_targets(config):
5569 """Parse "target foo { ... }" entries from config."""
5570 targets = {}
5571 names = [item for item in config if item.startswith('target ')]
5572 for name in names:
5573 name = name[7:]
5574 cfg = targets[name] = {
5575 'command': [],
5576 'command_up': [],
5577 'command_down': [],
5578 'command_down_interval': [],
5579 'consecutive_up': 20, # last n were UP => UP
5580 'consecutive_down': 10, # last n were DOWN => DOWN
5581 'history_up': 80, # count of UPs => n => UP
5582 'history_down': 30, # count of DOWNs >= n => DOWN
5583 'history_size': 100,
5584 'statistics_size': 300, # Keep last 300 results
5585 'statistics_interval': 60, # Write it once a minute
5586 'down_interval': 600, # How often to run command_down_interval
5587 # Internal items, don't set in config:
5588 # 'state': True, # Up or down
5589 # 'state_time': time(), # Timestamp for last state change
5590 # 'last_down_interval': time(), # Still down timestamp
5591 # 'history': [True] * history_size,
5592 # 'proc': Popen()
5593 'proc_restart': 0, # Start immediately
5594 'time': [], # Statistics
5595 }
5596 fileline = ''
5597 for fileline, line in config.pop(f'target {name}'):
5598 if line[0] not in cfg:
5599 fail(f'{fileline}Unknown keyword: {" ".join(line)}')
5600 if isinstance(cfg[line[0]], list):
5601 cfg[line[0]] = line[1:]
5602 else:
5603 try:
5604 cfg[line[0]] = int(' '.join(line[1:]))
5605 except ValueError:
5606 fail(f'{fileline}Invalid value: {" ".join(line)}')
5607
5608 # Verify section
5609 if not cfg['command']:
5610 fail(f'{fileline}Missing "command" keyword in "target {name}"')
5611 for key in (
5612 'consecutive_up',
5613 'consecutive_down',
5614 'history_up',
5615 'history_down',
5616 ):
5617 if (
5618 cfg[key] # ty: ignore[unsupported-operator]
5619 > cfg['history_size']
5620 ):
5621 fail(
5622 f'{fileline}{key} is larger than history_size in '
5623 f'"target {name}": {cfg[key]} > {cfg["history_size"]}'
5624 )
5625 if (
5626 cfg['history_size'] # ty: ignore[unsupported-operator]
5627 - cfg['history_up']
5628 >= cfg['history_down']
5629 ):
5630 warning(
5631 f'{fileline}Possible up-down loop in '
5632 f'"target {name}": history_size {cfg["history_size"]} - '
5633 f'history_up {cfg["history_up"]} >= '
5634 f'history_down {cfg["history_down"]}'
5635 )
5636
5637 return targets
5638
5639
5640 def parse_config_groups(config, targets):
5641 """Parse "group foo { ... }" entries from config."""
5642 groups = {}
5643 names = [item for item in config if item.startswith('group ')]
5644 for name in names:
5645 name = name[6:]
5646 cfg = groups[name] = {
5647 'target': [],
5648 'command_up': [],
5649 'command_down': [],
5650 'command_down_interval': [],
5651 'down_interval': 600, # How often to run command_down_interval
5652 }
5653 fileline = ''
5654 for fileline, line in config.pop(f'group {name}'):
5655 if line[0] not in cfg:
5656 fail(f'{fileline}Unknown keyword: {" ".join(line)}')
5657 if isinstance(cfg[line[0]], list):
5658 cfg[line[0]] = line[1:]
5659 else:
5660 try:
5661 cfg[line[0]] = int(' '.join(line[1:]))
5662 except ValueError:
5663 fail(f'{fileline}Invalid value: {" ".join(line)}')
5664
5665 # Verify section
5666 target = cfg.get('target')
5667 if not target:
5668 fail(f'{fileline}Missing "target" keyword in "group {name}"')
5669 for item in target: # ty: ignore[not-iterable]
5670 if item not in targets:
5671 fail(f'{fileline}Undefined target "{item}" in "group {name}"')
5672
5673 return groups
5674
5675
5676 def monitor_write_statistics(targets, groups):
5677 """Write statistics file."""
5678 stats = {}
5679 for group, cfg in groups.items():
5680 stats[group] = {
5681 'type': 'group',
5682 # Config
5683 'target': cfg['target'],
5684 # State
5685 'state': cfg['state'],
5686 'history': [], # Dummy fields to be compatible with targets
5687 'time': [],
5688 }
5689 for target, cfg in targets.items():
5690 stats[target] = {
5691 'type': 'target',
5692 # Config
5693 'consecutive_up': cfg['consecutive_up'],
5694 'consecutive_down': cfg['consecutive_down'],
5695 'history_up': cfg['history_up'],
5696 'history_down': cfg['history_down'],
5697 # State
5698 'state': cfg['state'],
5699 'history': cfg.get('history', []),
5700 'time': cfg['time'], # fping time
5701 }
5702 save_file(state_file('monitor_statistics_file'), stats)
5703
5704
5705 def command_monitor():
5706 """Monitor targets and run command when their state changes up or down."""
5707 INTERNAL.keep_going = 1
5708 while INTERNAL.keep_going:
5709 # Read minimal config
5710 config = minimal_config()
5711 targets = parse_config_targets(config)
5712 groups = parse_config_groups(config, targets)
5713
5714 # Exit silently with OK if no targets defined in configuration
5715 if not targets:
5716 notify('READY=1')
5717 notify('STOPPING=1')
5718 return 0
5719 daemonize()
5720
5721 # Define reload/stop signal handler
5722 def signal_handler(sig, _dummy_frame):
5723 if sig == signal.SIGINT:
5724 INTERNAL.keep_going = 0
5725 notify('STOPPING=1')
5726 else:
5727 INTERNAL.keep_going = 2
5728 notify('RELOADING=1')
5729
5730 INTERNAL.keep_going = 1
5731 signal.signal(signal.SIGHUP, signal_handler)
5732 signal.signal(signal.SIGINT, signal_handler)
5733 save_file(CONFIG.run_dir / 'foomuuri-monitor.pid', [str(os.getpid())])
5734
5735 # Start monitoring
5736 verbose('Target monitor ready', 0)
5737 notify('READY=1')
5738 stat_interval = min(
5739 cfg['statistics_interval'] for cfg in targets.values()
5740 )
5741 next_stat = time.time() + stat_interval / 5
5742 while INTERNAL.keep_going == 1:
5743 monitor_read_targets(targets, groups)
5744
5745 # Periodic statistics file update
5746 if stat_interval and time.time() >= next_stat:
5747 next_stat += stat_interval
5748 monitor_write_statistics(targets, groups)
5749
5750 monitor_terminate_targets(targets)
5751
5752 return 0
5753
5754
5755 def command_help(error=True):
5756 """Print command line help."""
5757 print(f"""Foomuuri {VERSION}
5758
5759 Usage: {sys.argv[0]} [OPTIONS] COMMAND
5760
5761 Available COMMANDs:
5762
5763 start Load configuration files and generate ruleset
5764 stop Remove ruleset
5765 reload Same as start, followed by iplist refresh
5766 try-reload Same as reload, ask confirmation to keep new config
5767 status Show current status: running, zone-interface mapping
5768 check Verify configuration files
5769 block Load "block all traffic" ruleset
5770 ruleset list [ZONE-ZONE]...
5771 List whole active ruleset or for ZONE-ZONE only
5772 macro list [NAME | VALUE]...
5773 List all macros or macros with specified NAME(s) or VALUE(s)
5774 counter list [COUNTER]...
5775 List all or specified named COUNTER(s)
5776 iplist status [IPLIST]...
5777 List number of entries of all or specified IPLIST(s)
5778 iplist list [IPLIST]...
5779 List entries of all or specified IPLIST(s)
5780 iplist add IPLIST [TIMEOUT] IPADDRESS [IPADDRESS]...
5781 Add or refresh IPADDRESS(es) to IPLIST
5782 iplist del IPLIST IPADDRESS [IPADDRESS]...
5783 Delete IPADDRESS(es) from IPLIST
5784 iplist flush [IPLIST]...
5785 Delete all added IP addresses from all or specified IPLIST(s)
5786 iplist refresh [IPLIST]...
5787 Refresh all or specified IPLIST(s) now
5788 set interface INTERFACE zone {{ZONE | -}}
5789 Change INTERFACE to ZONE, or remove from all zones
5790
5791 Available OPTIONS:
5792
5793 --version Print version
5794 --verbose Verbose output
5795 --quiet Be quiet
5796 --force Force some operations, don\'t check anything
5797 --soft Don\'t force operations, check more
5798 --fork Fork as a background daemon process
5799 --syslog Enable syslog logging
5800 --set=OPTION=VALUE
5801 Set foomuuri{{}} config OPTION to VALUE
5802
5803 Internally used commands:
5804
5805 dbus Start D-Bus daemon
5806 monitor Start target monitor daemon""")
5807 if error:
5808 sys.exit(1)
5809 return 0
5810
5811
5812 def parse_command_line():
5813 """Parse command line to INTERNAL.command and INTERNAL.parameters."""
5814 # pylint: disable=too-many-branches
5815 for arg in sys.argv[1:]:
5816 if arg == '--help':
5817 INTERNAL.command = 'help'
5818 elif arg == '--version':
5819 INTERNAL.command = 'version'
5820 elif arg == '--verbose':
5821 option = arg[2:]
5822 CONFIG[option] += 1
5823 CONFIG_OVERRIDE[option] = CONFIG_OVERRIDE.get(option, 0) + 1
5824 elif arg == '--quiet':
5825 option = 'verbose'
5826 CONFIG[option] -= 1
5827 CONFIG_OVERRIDE[option] = CONFIG_OVERRIDE.get(option, 0) - 1
5828 elif arg in {'--fork', '--syslog'}:
5829 CONFIG[arg[2:]] = 1
5830 CONFIG_OVERRIDE[arg[2:]] = 1
5831 elif arg == '--force':
5832 INTERNAL.force += 1
5833 elif arg == '--soft':
5834 INTERNAL.force -= 1
5835 elif arg.startswith('--set='):
5836 if arg.count('=') == 1:
5837 fail(f'Invalid syntax for --set=OPTION=VALUE: {arg}')
5838 _, option, value = arg.split('=', 2)
5839 if option not in CONFIG:
5840 fail(f'Unknown foomuuri{{}} option: {option}')
5841 try:
5842 CONFIG.set_from_str(option, value)
5843 CONFIG_OVERRIDE[option] = value
5844 except ValueError:
5845 fail(f'Invalid foomuuri{{}} option {option} value: {value}')
5846 elif arg.startswith('--'):
5847 fail(f'Unknown option: {arg}')
5848 elif not INTERNAL.command:
5849 INTERNAL.command = arg
5850 else:
5851 INTERNAL.parameters.append(arg)
5852
5853
5854 def run_command() -> int:
5855 """Run CONFIG[_command]."""
5856 # Help/version doesn't need root
5857 if not INTERNAL.command or INTERNAL.command == 'help':
5858 return command_help(error=False)
5859
5860 if INTERNAL.command == 'version':
5861 print(VERSION)
5862 return 0
5863
5864 # Initialize syslogging
5865 syslog_open_or_close()
5866
5867 # Warning if not running as root
5868 INTERNAL.root_power = not os.getuid()
5869 if not INTERNAL.root_power:
5870 warning('Foomuuri should be run as "root"')
5871
5872 # Run command
5873 handler, allow_parameters = {
5874 'start': (command_start, False),
5875 'stop': (command_stop, False),
5876 'reload': (command_reload, False),
5877 'try-reload': (command_try_reload, False),
5878 'status': (command_status, False),
5879 'check': (command_start, False),
5880 'block': (command_block, False),
5881 'ruleset': (command_subcommand, True),
5882 'macro': (command_subcommand, True),
5883 'counter': (command_subcommand, True),
5884 'iplist': (command_subcommand, True),
5885 'list': (command_list, True),
5886 'set': (command_set, True),
5887 'dbus': (command_dbus, False),
5888 'monitor': (command_monitor, False),
5889 }.get(INTERNAL.command, (None, False))
5890 if not handler:
5891 return fail(f'Unknown command: {INTERNAL.command}')
5892 if INTERNAL.parameters and not allow_parameters:
5893 return command_help()
5894 return handler()
5895
5896
5897 def main():
5898 """Parse command line and run command."""
5899 parse_command_line()
5900 return run_command()
5901
5902
5903 if __name__ == '__main__':
5904 try:
5905 sys.exit(main())
5906 except BrokenPipeError:
5907 # Python flushes standard streams on exit; redirect remaining output
5908 # to devnull to avoid another BrokenPipeError at shutdown
5909 os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno())
5910 sys.exit(1)