2017-05-01 02:21:17 +02:00
|
|
|
# encoding=utf-8
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
2017-06-26 13:57:11 +02:00
|
|
|
# Copyright (C) 2016 Marek Marczykowski-Górecki
|
2017-05-01 02:21:17 +02:00
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
2017-05-25 12:52:48 +02:00
|
|
|
# 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
|
2017-05-01 02:21:17 +02:00
|
|
|
# (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
|
2017-05-25 12:52:48 +02:00
|
|
|
# GNU Lesser General Public License for more details.
|
2017-05-01 02:21:17 +02:00
|
|
|
#
|
2017-05-25 12:52:48 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
2017-05-01 02:21:17 +02:00
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
2018-04-30 04:13:45 +02:00
|
|
|
import datetime
|
2018-04-30 16:26:54 +02:00
|
|
|
import re
|
|
|
|
import time
|
2017-05-01 02:21:17 +02:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.firewall
|
|
|
|
import qubesadmin.tests
|
|
|
|
import qubesadmin.tests.tools
|
|
|
|
import qubesadmin.tools.qvm_firewall
|
2017-05-01 02:21:17 +02:00
|
|
|
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
class TC_00_RuleAction(qubesadmin.tests.QubesTestCase):
|
2017-05-01 02:21:17 +02:00
|
|
|
def setUp(self):
|
|
|
|
super(TC_00_RuleAction, self).setUp()
|
2017-05-11 23:21:04 +02:00
|
|
|
self.action = qubesadmin.tools.qvm_firewall.RuleAction(
|
2017-05-01 02:21:17 +02:00
|
|
|
None, dest='rule')
|
|
|
|
|
|
|
|
def test_000_named_opts(self):
|
|
|
|
ns = argparse.Namespace()
|
|
|
|
self.action(None, ns, ['dsthost=127.0.0.1', 'action=accept'])
|
|
|
|
self.assertEqual(ns.rule,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.firewall.Rule(
|
2017-05-01 02:21:17 +02:00
|
|
|
None, action='accept', dsthost='127.0.0.1/32'))
|
|
|
|
|
|
|
|
def test_001_unnamed_opts(self):
|
|
|
|
ns = argparse.Namespace()
|
|
|
|
self.action(None, ns, ['accept', '127.0.0.1', 'tcp', '80'])
|
|
|
|
self.assertEqual(ns.rule,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.firewall.Rule(
|
2017-05-01 02:21:17 +02:00
|
|
|
None, action='accept', dsthost='127.0.0.1/32',
|
|
|
|
proto='tcp', dstports=80))
|
|
|
|
|
|
|
|
def test_002_unnamed_opts(self):
|
|
|
|
ns = argparse.Namespace()
|
|
|
|
self.action(None, ns, ['accept', '127.0.0.1', 'icmp', '8'])
|
|
|
|
self.assertEqual(ns.rule,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.firewall.Rule(
|
2017-05-01 02:21:17 +02:00
|
|
|
None, action='accept', dsthost='127.0.0.1/32',
|
|
|
|
proto='icmp', icmptype=8))
|
|
|
|
|
|
|
|
def test_003_mixed_opts(self):
|
|
|
|
ns = argparse.Namespace()
|
|
|
|
self.action(None, ns, ['dsthost=127.0.0.1', 'accept',
|
|
|
|
'dstports=443', 'tcp'])
|
|
|
|
self.assertEqual(ns.rule,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.firewall.Rule(
|
2017-05-01 02:21:17 +02:00
|
|
|
None, action='accept', dsthost='127.0.0.1/32',
|
|
|
|
proto='tcp', dstports=443))
|
|
|
|
|
2018-04-30 04:13:45 +02:00
|
|
|
def test_004_expire_absolute(self):
|
|
|
|
ns = argparse.Namespace()
|
|
|
|
self.action(None, ns, ['dsthost=127.0.0.1', 'action=accept',
|
|
|
|
'expire=1525054180'])
|
|
|
|
self.assertEqual(ns.rule,
|
|
|
|
qubesadmin.firewall.Rule(
|
|
|
|
None, action='accept', dsthost='127.0.0.1/32',
|
|
|
|
expire=1525054180))
|
|
|
|
|
|
|
|
def test_005_expire_relative(self):
|
|
|
|
ns = argparse.Namespace()
|
|
|
|
now = int(datetime.datetime.now().strftime('%s'))
|
|
|
|
self.action(None, ns, ['dsthost=127.0.0.1', 'action=accept',
|
|
|
|
'expire=+100'])
|
|
|
|
self.assertEqual(ns.rule,
|
|
|
|
qubesadmin.firewall.Rule(
|
|
|
|
None, action='accept', dsthost='127.0.0.1/32',
|
|
|
|
expire=now+100))
|
|
|
|
|
2018-05-02 23:54:46 +02:00
|
|
|
def test_006_dsthost_aliases(self):
|
|
|
|
ns = argparse.Namespace()
|
|
|
|
for name in ['dsthost', 'dst4', 'dst6']:
|
|
|
|
self.action(None, ns, [name + '=127.0.0.1', 'accept'])
|
|
|
|
self.assertEqual(ns.rule,
|
|
|
|
qubesadmin.firewall.Rule(
|
|
|
|
None, action='accept', dsthost='127.0.0.1/32'))
|
|
|
|
|
2017-05-01 02:21:17 +02:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
class TC_10_qvm_firewall(qubesadmin.tests.QubesTestCase):
|
2017-05-01 02:21:17 +02:00
|
|
|
def setUp(self):
|
|
|
|
super(TC_10_qvm_firewall, self).setUp()
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
|
2017-05-01 02:21:17 +02:00
|
|
|
b'0\0test-vm class=AppVM state=Halted\n'
|
|
|
|
|
|
|
|
def test_000_list(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = \
|
|
|
|
b'0\0action=accept dsthost=qubes-os.org\n' \
|
|
|
|
b'action=drop proto=icmp\n'
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
|
|
|
qubesadmin.tools.qvm_firewall.main(['test-vm', 'list'], app=self.app)
|
2017-05-01 02:21:17 +02:00
|
|
|
self.assertEqual(
|
|
|
|
[l.strip() for l in stdout.getvalue().splitlines()],
|
|
|
|
['NO ACTION HOST PROTOCOL PORT(S) SPECIAL '
|
2018-04-30 16:26:54 +02:00
|
|
|
'TARGET ICMP TYPE EXPIRE COMMENT',
|
2017-05-01 02:21:17 +02:00
|
|
|
'0 accept qubes-os.org - - - '
|
2018-04-30 16:26:54 +02:00
|
|
|
' - - -',
|
2017-05-01 02:21:17 +02:00
|
|
|
'1 drop - icmp - - '
|
2018-04-30 16:26:54 +02:00
|
|
|
' - - -',
|
2017-05-01 02:21:17 +02:00
|
|
|
])
|
|
|
|
|
2018-04-30 04:16:23 +02:00
|
|
|
def test_001_list_default(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = \
|
|
|
|
b'0\0action=accept dsthost=qubes-os.org proto=tcp ' \
|
|
|
|
b'dstports=443-443\n' \
|
|
|
|
b'action=drop proto=icmp icmptype=8\n' \
|
|
|
|
b'action=accept specialtarget=dns comment=Allow DNS\n'
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
2018-04-30 04:16:23 +02:00
|
|
|
qubesadmin.tools.qvm_firewall.main(['test-vm'], app=self.app)
|
2017-05-01 02:21:17 +02:00
|
|
|
self.assertEqual(
|
|
|
|
[l.strip() for l in stdout.getvalue().splitlines()],
|
|
|
|
['NO ACTION HOST PROTOCOL PORT(S) SPECIAL '
|
2018-04-30 16:26:54 +02:00
|
|
|
'TARGET ICMP TYPE EXPIRE COMMENT',
|
2017-05-01 02:21:17 +02:00
|
|
|
'0 accept qubes-os.org tcp 443 - '
|
2018-04-30 16:26:54 +02:00
|
|
|
' - - -',
|
2017-05-01 02:21:17 +02:00
|
|
|
'1 drop - icmp - - '
|
2018-04-30 16:26:54 +02:00
|
|
|
' 8 - -',
|
2017-05-01 02:21:17 +02:00
|
|
|
'2 accept - - - dns '
|
2018-04-30 16:26:54 +02:00
|
|
|
' - - Allow DNS',
|
2017-05-01 02:21:17 +02:00
|
|
|
])
|
|
|
|
|
2018-04-30 16:26:54 +02:00
|
|
|
|
|
|
|
def test_002_list_expire(self):
|
|
|
|
in_1h = int(time.time()) + 3600
|
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
|
|
|
None, None)] = \
|
|
|
|
'0\0action=accept dsthost=qubes-os.org proto=tcp ' \
|
|
|
|
'dstports=443-443 expire={}\n'.format(in_1h).encode()
|
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
|
|
|
qubesadmin.tools.qvm_firewall.main(['test-vm'], app=self.app)
|
|
|
|
line = stdout.getvalue().splitlines()[-1]
|
|
|
|
match = re.match(
|
|
|
|
'0 accept qubes-os.org tcp 443 - '
|
|
|
|
' - \+(.{4})s -',
|
|
|
|
line)
|
|
|
|
self.assertTrue(match, "no match for: {!r}".format(line))
|
|
|
|
duration = int(match.group(1))
|
|
|
|
self.assertTrue(3590 < duration <= 3600)
|
|
|
|
|
2017-05-01 02:21:17 +02:00
|
|
|
def test_002_list_raw(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = \
|
|
|
|
b'0\0action=accept dsthost=qubes-os.org\n' \
|
|
|
|
b'action=drop proto=icmp\n'
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
|
|
|
qubesadmin.tools.qvm_firewall.main(['test-vm', '--raw', 'list'],
|
2017-05-01 02:21:17 +02:00
|
|
|
app=self.app)
|
|
|
|
self.assertEqual(
|
|
|
|
[l.strip() for l in stdout.getvalue().splitlines()],
|
|
|
|
['action=accept dsthost=qubes-os.org',
|
|
|
|
'action=drop proto=icmp',
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_003_list_raw_reload(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = \
|
|
|
|
b'0\0action=accept dsthost=qubes-os.org\n' \
|
|
|
|
b'action=drop proto=icmp\n'
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Reload',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = b'0\0'
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
|
|
|
qubesadmin.tools.qvm_firewall.main(
|
2017-05-01 02:21:17 +02:00
|
|
|
['test-vm', '--raw', '--reload', 'list'],
|
|
|
|
app=self.app)
|
|
|
|
self.assertEqual(
|
|
|
|
[l.strip() for l in stdout.getvalue().splitlines()],
|
|
|
|
['action=accept dsthost=qubes-os.org',
|
|
|
|
'action=drop proto=icmp',
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_010_add_after(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = \
|
|
|
|
b'0\0action=accept dsthost=qubes-os.org\n' \
|
|
|
|
b'action=drop proto=icmp\n'
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
|
2017-05-01 02:21:17 +02:00
|
|
|
b'action=accept dsthost=qubes-os.org\n'
|
|
|
|
b'action=drop proto=icmp\n'
|
|
|
|
b'action=accept dst4=192.168.0.0/24 comment=Allow LAN\n')] = \
|
|
|
|
b'0\0'
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_firewall.main(
|
2017-05-01 02:21:17 +02:00
|
|
|
['test-vm', 'add', 'accept', '192.168.0.0/24', 'comment=Allow LAN'],
|
|
|
|
app=self.app
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_011_add_before(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = \
|
|
|
|
b'0\0action=accept dsthost=qubes-os.org\n' \
|
|
|
|
b'action=drop proto=icmp\n'
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
|
2017-05-01 02:21:17 +02:00
|
|
|
b'action=accept dsthost=qubes-os.org\n'
|
|
|
|
b'action=accept dst4=192.168.0.0/24 comment=Allow LAN\n'
|
|
|
|
b'action=drop proto=icmp\n')] = b'0\0'
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_firewall.main(
|
2017-05-01 02:21:17 +02:00
|
|
|
['test-vm', 'add', '--before', '1', 'accept', '192.168.0.0/24',
|
|
|
|
'comment=Allow LAN'],
|
|
|
|
app=self.app
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_020_del_number(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = \
|
|
|
|
b'0\0action=accept dsthost=qubes-os.org\n' \
|
|
|
|
b'action=drop proto=icmp\n'
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
|
2017-05-01 02:21:17 +02:00
|
|
|
b'action=accept dsthost=qubes-os.org\n')] = b'0\0'
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_firewall.main(
|
2017-05-01 02:21:17 +02:00
|
|
|
['test-vm', 'del', '--rule-no', '1'],
|
|
|
|
app=self.app
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_021_del_rule(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Get',
|
2017-05-01 02:21:17 +02:00
|
|
|
None, None)] = \
|
|
|
|
b'0\0action=accept dsthost=qubes-os.org\n' \
|
|
|
|
b'action=drop proto=icmp\n'
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('test-vm', 'admin.vm.firewall.Set', None,
|
2017-05-01 02:21:17 +02:00
|
|
|
b'action=accept dsthost=qubes-os.org\n')] = b'0\0'
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_firewall.main(
|
2017-05-01 02:21:17 +02:00
|
|
|
['test-vm', 'del', 'drop', 'proto=icmp'],
|
|
|
|
app=self.app
|
|
|
|
)
|