2017-05-01 02:21:17 +02:00
|
|
|
# encoding=utf-8
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
'''qvm-firewall tool'''
|
|
|
|
|
|
|
|
import argparse
|
2018-04-30 04:13:45 +02:00
|
|
|
import datetime
|
2017-05-01 02:21:17 +02:00
|
|
|
import sys
|
|
|
|
import itertools
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.exc
|
|
|
|
import qubesadmin.firewall
|
|
|
|
import qubesadmin.tools
|
2017-05-01 02:21:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RuleAction(argparse.Action):
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
'''Parser action for a single firewall rule. It accept syntax:
|
|
|
|
- <action> [<dsthost> [<proto> [<dstports>|<icmptype>]]]
|
|
|
|
- action=<action> [specialtarget=dns] [dsthost=<dsthost>]
|
|
|
|
[proto=<proto>] [dstports=<dstports>] [icmptype=<icmptype>]
|
|
|
|
|
|
|
|
Or a mix of them.
|
|
|
|
'''
|
|
|
|
def __call__(self, _parser, namespace, values, option_string=None):
|
|
|
|
if not values:
|
|
|
|
setattr(namespace, self.dest, None)
|
|
|
|
return
|
2021-06-25 10:50:02 +02:00
|
|
|
assumed_order = ['action', 'forwardtype', 'dsthost', 'proto', 'srcports', 'dstports', 'icmptype']
|
2018-04-30 04:13:45 +02:00
|
|
|
allowed_opts = assumed_order + ['specialtarget', 'comment', 'expire']
|
2017-05-01 02:21:17 +02:00
|
|
|
kwargs = {}
|
|
|
|
for opt in values:
|
2020-05-15 16:01:18 +02:00
|
|
|
if opt[-1] == '=':
|
|
|
|
raise argparse.ArgumentError(
|
|
|
|
None, 'invalid rule description: {}'.format(opt))
|
2017-05-01 02:21:17 +02:00
|
|
|
opt_elements = opt.split('=')
|
|
|
|
if len(opt_elements) == 2:
|
|
|
|
key, value = opt_elements
|
|
|
|
elif len(opt_elements) == 1:
|
|
|
|
key, value = assumed_order[0], opt
|
|
|
|
else:
|
|
|
|
raise argparse.ArgumentError(None,
|
|
|
|
'invalid rule description: {}'.format(opt))
|
2018-05-02 23:54:46 +02:00
|
|
|
if key in ['dst4', 'dst6']:
|
|
|
|
key = 'dsthost'
|
2017-05-01 02:21:17 +02:00
|
|
|
if key not in allowed_opts:
|
|
|
|
raise argparse.ArgumentError(None,
|
|
|
|
'Invalid rule element: {}'.format(opt))
|
2018-04-30 04:13:45 +02:00
|
|
|
if key == 'expire' and value.startswith('+'):
|
|
|
|
value = (datetime.datetime.now() +
|
|
|
|
datetime.timedelta(seconds=int(value[1:]))).\
|
|
|
|
strftime('%s')
|
2017-05-01 02:21:17 +02:00
|
|
|
kwargs[key] = value
|
|
|
|
if key in assumed_order:
|
|
|
|
assumed_order.remove(key)
|
|
|
|
if key == 'proto' and value in ['tcp', 'udp']:
|
|
|
|
assumed_order.remove('icmptype')
|
|
|
|
elif key == 'proto' and value in ['icmp']:
|
|
|
|
assumed_order.remove('dstports')
|
2017-05-11 23:21:04 +02:00
|
|
|
rule = qubesadmin.firewall.Rule(None, **kwargs)
|
2017-05-01 02:21:17 +02:00
|
|
|
setattr(namespace, self.dest, rule)
|
|
|
|
|
2018-04-29 13:17:19 +02:00
|
|
|
epilog = """
|
|
|
|
Rules can be given as positional arguments:
|
|
|
|
<action> [<dsthost> [<proto> [<dstports>|<icmptype>]]]
|
|
|
|
|
|
|
|
And as keyword arguments:
|
|
|
|
action=<action> [specialtarget=dns] [dsthost=<dsthost>]
|
|
|
|
[proto=<proto>] [dstports=<dstports>] [icmptype=<icmptype>]
|
2018-04-30 04:13:45 +02:00
|
|
|
[expire=<expire>]
|
2018-04-29 13:17:19 +02:00
|
|
|
|
|
|
|
Both formats, positional and keyword arguments, can be used
|
|
|
|
interchangeably.
|
|
|
|
|
2018-05-03 00:57:21 +02:00
|
|
|
Available matches:
|
2021-06-22 18:51:48 +02:00
|
|
|
action accept, drop or forward
|
|
|
|
forwardtype internal or external (only with action=forward)
|
2018-05-02 23:54:46 +02:00
|
|
|
dst4 synonym for dsthost
|
|
|
|
dst6 synonym for dsthost
|
2018-04-29 13:17:19 +02:00
|
|
|
dsthost IP, network or hostname
|
|
|
|
(e.g. 10.5.3.2, 192.168.0.0/16,
|
|
|
|
www.example.com, fd00::/8)
|
|
|
|
dstports port or port range
|
|
|
|
(e.g. 443 or 1200-1400)
|
2021-06-22 18:51:48 +02:00
|
|
|
srcports port in input (only with action=forward)
|
2018-04-29 13:17:19 +02:00
|
|
|
icmptype icmp type number (e.g. 8 for echo requests)
|
|
|
|
proto icmp, tcp or udp
|
|
|
|
specialtarget only the value dns is currently supported,
|
|
|
|
it matches the configured dns servers of
|
|
|
|
a VM
|
2018-05-03 00:57:21 +02:00
|
|
|
expire the rule is automatically removed at the time given as
|
|
|
|
seconds since 1/1/1970, or +seconds (e.g. +300 for a rule
|
|
|
|
to expire in 5 minutes)
|
2018-04-29 13:17:19 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
parser = qubesadmin.tools.QubesArgumentParser(vmname_nargs=1, epilog=epilog,
|
|
|
|
formatter_class=argparse.RawTextHelpFormatter)
|
2017-05-01 02:21:17 +02:00
|
|
|
|
|
|
|
action = parser.add_subparsers(dest='command', help='action to perform')
|
|
|
|
|
|
|
|
action_add = action.add_parser('add', help='add rule')
|
|
|
|
action_add.add_argument('--before', type=int, default=None,
|
2018-05-03 00:57:21 +02:00
|
|
|
help='Add rule before rule with given number instead at the end')
|
|
|
|
action_add.add_argument('rule', metavar='match', nargs='+', action=RuleAction,
|
2017-05-01 02:21:17 +02:00
|
|
|
help='rule description')
|
|
|
|
|
|
|
|
action_del = action.add_parser('del', help='remove rule')
|
|
|
|
action_del.add_argument('--rule-no', dest='rule_no', type=int,
|
|
|
|
action='store', help='rule number')
|
2018-05-03 00:57:21 +02:00
|
|
|
action_del.add_argument('rule', metavar='match', nargs='*', action=RuleAction,
|
2017-05-01 02:21:17 +02:00
|
|
|
help='rule to be removed')
|
|
|
|
|
|
|
|
action_list = action.add_parser('list', help='list rules')
|
|
|
|
|
2020-02-24 14:20:55 +01:00
|
|
|
action_reset = action.add_parser(
|
|
|
|
'reset',
|
|
|
|
help='remove all firewall rules and reset to default '
|
|
|
|
'(accept all connections)')
|
|
|
|
|
2017-05-01 02:21:17 +02:00
|
|
|
parser.add_argument('--reload', '-r', action='store_true',
|
2018-05-03 00:57:21 +02:00
|
|
|
help='force reload of rules even when unchanged')
|
2017-05-01 02:21:17 +02:00
|
|
|
|
|
|
|
parser.add_argument('--raw', action='store_true',
|
|
|
|
help='output rules as raw strings, instead of nice table')
|
|
|
|
|
|
|
|
|
|
|
|
def rules_list_table(vm):
|
|
|
|
'''Print rules to stdout in human-readable form (table)
|
|
|
|
|
|
|
|
:param vm: VM object
|
|
|
|
:return: None
|
|
|
|
'''
|
2021-06-25 10:50:02 +02:00
|
|
|
header = ['NO', 'ACTION', 'FORWARD TYPE', 'HOST', 'PROTOCOL', 'SRCPORT(S)',
|
|
|
|
'DSTPORT(S)', 'SPECIAL TARGET', 'ICMP TYPE', 'EXPIRE', 'COMMENT']
|
2017-05-01 02:21:17 +02:00
|
|
|
rows = []
|
|
|
|
for (rule, rule_no) in zip(vm.firewall.rules, itertools.count()):
|
2018-04-30 16:26:54 +02:00
|
|
|
row = [x.pretty_value if x is not None else '-' for x in [
|
2017-05-01 02:21:17 +02:00
|
|
|
rule.action,
|
2021-06-22 18:51:48 +02:00
|
|
|
rule.forwardtype,
|
2017-05-01 02:21:17 +02:00
|
|
|
rule.dsthost,
|
|
|
|
rule.proto,
|
2021-06-22 18:51:48 +02:00
|
|
|
rule.srcports,
|
2021-06-25 02:05:54 +02:00
|
|
|
rule.dstports,
|
2017-05-01 02:21:17 +02:00
|
|
|
rule.specialtarget,
|
|
|
|
rule.icmptype,
|
2018-04-30 16:26:54 +02:00
|
|
|
rule.expire,
|
2021-06-25 10:50:02 +02:00
|
|
|
rule.comment,
|
2017-05-01 02:21:17 +02:00
|
|
|
]]
|
2018-04-30 16:26:54 +02:00
|
|
|
rows.append([str(rule_no)] + row)
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.print_table([header] + rows)
|
2017-05-01 02:21:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def rules_list_raw(vm):
|
|
|
|
'''Print rules in machine-readable form (as specified in Admin API)
|
|
|
|
|
|
|
|
:param vm: VM object
|
|
|
|
:return: None
|
|
|
|
'''
|
|
|
|
for rule in vm.firewall.rules:
|
|
|
|
sys.stdout.write(rule.rule + '\n')
|
|
|
|
|
|
|
|
|
|
|
|
def rules_add(vm, args):
|
|
|
|
'''Add a rule defined by args.rule'''
|
|
|
|
if args.before is not None:
|
|
|
|
vm.firewall.rules.insert(args.before, args.rule)
|
|
|
|
else:
|
|
|
|
vm.firewall.rules.append(args.rule)
|
|
|
|
vm.firewall.save_rules()
|
|
|
|
|
|
|
|
|
|
|
|
def rules_del(vm, args):
|
|
|
|
'''Delete a rule according to args.rule/args.rule_no'''
|
|
|
|
if args.rule_no is not None:
|
|
|
|
vm.firewall.rules.pop(args.rule_no)
|
|
|
|
else:
|
|
|
|
vm.firewall.rules.remove(args.rule)
|
|
|
|
vm.firewall.save_rules()
|
|
|
|
|
|
|
|
|
|
|
|
def main(args=None, app=None):
|
|
|
|
'''Main routine of :program:`qvm-firewall`.'''
|
|
|
|
try:
|
|
|
|
args = parser.parse_args(args, app=app)
|
|
|
|
vm = args.domains[0]
|
|
|
|
if args.command == 'add':
|
|
|
|
rules_add(vm, args)
|
|
|
|
elif args.command == 'del':
|
|
|
|
rules_del(vm, args)
|
2020-02-24 14:20:55 +01:00
|
|
|
elif args.command == 'reset':
|
|
|
|
vm.firewall.rules.clear()
|
|
|
|
vm.firewall.rules.append(qubesadmin.firewall.Rule('action=accept'))
|
|
|
|
vm.firewall.save_rules()
|
2018-04-30 04:16:23 +02:00
|
|
|
else:
|
2017-05-01 02:21:17 +02:00
|
|
|
if args.raw:
|
|
|
|
rules_list_raw(vm)
|
|
|
|
else:
|
|
|
|
rules_list_table(vm)
|
|
|
|
if args.reload:
|
|
|
|
vm.firewall.reload()
|
2021-06-25 02:05:54 +02:00
|
|
|
except qubesadmin.exc.QubesException as e:
|
2017-05-01 02:21:17 +02:00
|
|
|
parser.print_error(str(e))
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|