2011-02-21 18:15:35 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Tomasz Sterna <tomek@xiaoka.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
2017-11-06 21:06:30 +01:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
2011-02-21 18:15:35 +01:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2017-07-12 14:10:43 +02:00
|
|
|
import datetime
|
2011-03-03 22:42:10 +01:00
|
|
|
import re
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
from PyQt5 import QtCore, QtGui, QtWidgets # pylint: disable=import-error
|
2017-07-12 14:10:43 +02:00
|
|
|
import qubesadmin.firewall
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-14 15:29:57 +01:00
|
|
|
from . import ui_newfwruledlg # pylint: disable=no-name-in-module
|
2011-02-21 18:15:35 +01:00
|
|
|
|
|
|
|
|
2017-07-12 14:10:43 +02:00
|
|
|
class FirewallModifiedOutsideError(ValueError):
|
|
|
|
pass
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2017-11-06 22:46:35 +01:00
|
|
|
class QIPAddressValidator(QtGui.QValidator):
|
2017-11-14 15:29:57 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2017-11-06 23:54:33 +01:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super(QIPAddressValidator, self).__init__(parent)
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-09 17:57:21 +01:00
|
|
|
def validate(self, input_string, pos):
|
2017-11-09 16:34:09 +01:00
|
|
|
# pylint: disable=too-many-return-statements,no-self-use
|
2017-11-09 17:57:21 +01:00
|
|
|
hostname = str(input_string)
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-14 15:29:57 +01:00
|
|
|
if len(hostname) > 255 or not hostname:
|
2019-05-22 23:10:09 +02:00
|
|
|
return QtGui.QValidator.Intermediate, input_string, pos
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2011-03-21 22:08:51 +01:00
|
|
|
if hostname == "*":
|
2019-05-22 23:10:09 +02:00
|
|
|
return QtGui.QValidator.Acceptable, input_string, pos
|
2011-03-21 22:08:51 +01:00
|
|
|
|
|
|
|
unmask = hostname.split("/", 1)
|
|
|
|
if len(unmask) == 2:
|
|
|
|
hostname = unmask[0]
|
|
|
|
mask = unmask[1]
|
|
|
|
if mask.isdigit() or mask == "":
|
2017-11-09 17:13:56 +01:00
|
|
|
if re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", hostname) is None:
|
2019-05-22 23:10:09 +02:00
|
|
|
return QtGui.QValidator.Invalid, input_string, pos
|
2011-03-21 22:08:51 +01:00
|
|
|
if mask != "":
|
|
|
|
mask = int(unmask[1])
|
|
|
|
if mask < 0 or mask > 32:
|
2019-05-22 23:10:09 +02:00
|
|
|
return QtGui.QValidator.Invalid, input_string, pos
|
2011-03-21 22:08:51 +01:00
|
|
|
else:
|
2019-05-22 23:10:09 +02:00
|
|
|
return QtGui.QValidator.Invalid, input_string, pos
|
2011-03-21 22:08:51 +01:00
|
|
|
|
2011-03-03 22:42:10 +01:00
|
|
|
if hostname[-1:] == ".":
|
|
|
|
hostname = hostname[:-1]
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2011-03-03 22:42:10 +01:00
|
|
|
if hostname[-1:] == "-":
|
2019-05-22 23:10:09 +02:00
|
|
|
return QtGui.QValidator.Intermediate, input_string, pos
|
2011-03-03 22:42:10 +01:00
|
|
|
|
2017-11-09 17:13:56 +01:00
|
|
|
allowed = re.compile(r"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
|
2011-03-03 22:42:10 +01:00
|
|
|
if all(allowed.match(x) for x in hostname.split(".")):
|
2019-05-22 23:10:09 +02:00
|
|
|
return QtGui.QValidator.Acceptable, input_string, pos
|
|
|
|
|
|
|
|
return QtGui.QValidator.Invalid, input_string, pos
|
2011-03-03 22:42:10 +01:00
|
|
|
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
class NewFwRuleDlg(QtWidgets.QDialog, ui_newfwruledlg.Ui_NewFwRuleDlg):
|
2017-11-06 23:54:33 +01:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super(NewFwRuleDlg, self).__init__(parent)
|
2011-02-21 18:15:35 +01:00
|
|
|
self.setupUi(self)
|
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
self.set_ok_state(False)
|
2011-03-21 22:08:51 +01:00
|
|
|
self.addressComboBox.setValidator(QIPAddressValidator())
|
2017-11-06 23:18:18 +01:00
|
|
|
self.addressComboBox.editTextChanged.connect(
|
|
|
|
self.address_editing_finished)
|
|
|
|
self.serviceComboBox.setValidator(QtGui.QRegExpValidator(
|
|
|
|
QtCore.QRegExp("[a-z][a-z0-9-]+|[0-9]+(-[0-9]+)?",
|
|
|
|
QtCore.Qt.CaseInsensitive), None))
|
2012-03-04 17:36:25 +01:00
|
|
|
self.serviceComboBox.setEnabled(False)
|
2019-05-22 23:10:09 +02:00
|
|
|
self.serviceComboBox.setInsertPolicy(QtWidgets.QComboBox.InsertAtBottom)
|
2011-03-21 22:08:51 +01:00
|
|
|
self.populate_combos()
|
2019-05-22 23:10:09 +02:00
|
|
|
self.serviceComboBox.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
|
2011-03-03 22:42:10 +01:00
|
|
|
|
2019-09-12 22:05:56 +02:00
|
|
|
self.model = None
|
|
|
|
|
|
|
|
def try_to_create_rule(self):
|
|
|
|
# return True if successful, False otherwise
|
|
|
|
address = str(self.addressComboBox.currentText())
|
|
|
|
service = str(self.serviceComboBox.currentText())
|
|
|
|
|
|
|
|
rule = qubesadmin.firewall.Rule(None, action='accept')
|
|
|
|
|
|
|
|
if address is not None and address != "*":
|
|
|
|
try:
|
|
|
|
rule.dsthost = address
|
|
|
|
except ValueError:
|
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self, self.tr("Invalid address"),
|
|
|
|
self.tr("Address '{0}' is invalid.").format(address))
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.tcp_radio.isChecked():
|
|
|
|
rule.proto = 'tcp'
|
|
|
|
elif self.udp_radio.isChecked():
|
|
|
|
rule.proto = 'udp'
|
|
|
|
|
|
|
|
if self.model.port_range_pattern.fullmatch(service):
|
|
|
|
try:
|
|
|
|
rule.dstports = service
|
|
|
|
except ValueError:
|
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
|
|
|
self.tr("Invalid port or service"),
|
|
|
|
self.tr("Port number or service '{0}' is "
|
|
|
|
"invalid.").format(service))
|
|
|
|
return False
|
|
|
|
elif service:
|
|
|
|
if self.model.service_port_pattern.fullmatch(service):
|
|
|
|
parsed_service = self.model.service_port_pattern.match(
|
|
|
|
service).groups()[0]
|
|
|
|
else:
|
|
|
|
parsed_service = service
|
|
|
|
|
|
|
|
try:
|
|
|
|
rule.dstports = parsed_service
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
if self.model.get_service_port(parsed_service) is not None:
|
|
|
|
rule.dstports = self.model.get_service_port(parsed_service)
|
|
|
|
else:
|
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
|
|
|
self.tr("Invalid port or service"),
|
|
|
|
self.tr(
|
|
|
|
"Port number or service '{0}' is "
|
|
|
|
"invalid.".format(parsed_service)))
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.model.current_row is not None:
|
|
|
|
self.model.set_child(self.model.current_row, rule)
|
|
|
|
else:
|
|
|
|
self.model.append_child(rule)
|
|
|
|
return True
|
|
|
|
|
2016-06-21 02:05:21 +02:00
|
|
|
def accept(self):
|
|
|
|
if self.tcp_radio.isChecked() or self.udp_radio.isChecked():
|
2017-11-14 15:29:57 +01:00
|
|
|
if not self.serviceComboBox.currentText():
|
2019-05-22 23:10:09 +02:00
|
|
|
msg = QtWidgets.QMessageBox()
|
|
|
|
msg.warning(
|
|
|
|
self, self.tr("Firewall rule"),
|
2017-11-06 23:18:18 +01:00
|
|
|
self.tr("You need to fill service "
|
|
|
|
"name/port for TCP/UDP rule"))
|
2016-06-21 02:05:21 +02:00
|
|
|
return
|
2019-09-12 22:05:56 +02:00
|
|
|
if self.try_to_create_rule():
|
|
|
|
super().accept()
|
2012-03-04 17:36:25 +01:00
|
|
|
|
2011-03-21 22:08:51 +01:00
|
|
|
def populate_combos(self):
|
|
|
|
example_addresses = [
|
|
|
|
"", "www.example.com",
|
|
|
|
"192.168.1.100", "192.168.0.0/16",
|
|
|
|
"*"
|
|
|
|
]
|
2019-08-02 16:34:55 +02:00
|
|
|
example_services = [
|
|
|
|
'', '22', '80', '1024-1234',
|
2015-04-25 19:26:42 +02:00
|
|
|
'http', 'https', 'ftp', 'ftps', 'smtp',
|
2019-08-02 16:34:55 +02:00
|
|
|
'pop3', 'pop3s', 'imap', 'imaps', 'odmr',
|
2017-11-06 23:54:33 +01:00
|
|
|
'nntp', 'nntps', 'ssh', 'telnet', 'telnets', 'ntp',
|
2019-08-02 16:58:30 +02:00
|
|
|
'snmp', 'ldap', 'ldaps', 'irc', 'ircs-u', 'xmpp-client',
|
2019-08-02 16:34:55 +02:00
|
|
|
'syslog', 'printer', 'nfs', 'x11'
|
2011-03-03 22:42:10 +01:00
|
|
|
]
|
2011-03-21 22:08:51 +01:00
|
|
|
for address in example_addresses:
|
|
|
|
self.addressComboBox.addItem(address)
|
2019-08-02 16:34:55 +02:00
|
|
|
for service in example_services:
|
2011-03-03 22:42:10 +01:00
|
|
|
self.serviceComboBox.addItem(service)
|
|
|
|
|
|
|
|
def address_editing_finished(self):
|
2017-11-08 16:53:49 +01:00
|
|
|
self.set_ok_state(True)
|
2011-03-03 22:42:10 +01:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
def set_ok_state(self, ok_state):
|
2019-05-22 23:10:09 +02:00
|
|
|
ok_button = self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok)
|
2011-03-03 22:42:10 +01:00
|
|
|
if ok_button is not None:
|
2017-11-08 16:53:49 +01:00
|
|
|
ok_button.setEnabled(ok_state)
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2012-02-29 10:42:57 +01:00
|
|
|
def on_tcp_radio_toggled(self, checked):
|
2012-03-04 17:36:25 +01:00
|
|
|
if checked:
|
|
|
|
self.serviceComboBox.setEnabled(True)
|
2012-02-29 10:42:57 +01:00
|
|
|
|
|
|
|
def on_udp_radio_toggled(self, checked):
|
2012-03-04 17:36:25 +01:00
|
|
|
if checked:
|
|
|
|
self.serviceComboBox.setEnabled(True)
|
2012-02-29 10:42:57 +01:00
|
|
|
|
|
|
|
def on_any_radio_toggled(self, checked):
|
2012-03-04 17:36:25 +01:00
|
|
|
if checked:
|
|
|
|
self.serviceComboBox.setEnabled(False)
|
2012-02-29 10:42:57 +01:00
|
|
|
|
2018-12-28 17:40:35 +01:00
|
|
|
|
2017-11-06 22:46:35 +01:00
|
|
|
class QubesFirewallRulesModel(QtCore.QAbstractItemModel):
|
2011-02-21 18:15:35 +01:00
|
|
|
def __init__(self, parent=None):
|
2017-11-06 22:46:35 +01:00
|
|
|
QtCore.QAbstractItemModel.__init__(self, parent)
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2019-09-12 22:05:56 +02:00
|
|
|
self.current_row = None
|
|
|
|
|
2019-08-02 16:34:55 +02:00
|
|
|
self.__column_names = {0: "Address", 1: "Port/Service", 2: "Protocol", }
|
2011-03-03 22:42:10 +01:00
|
|
|
self.__services = list()
|
2019-08-02 16:58:30 +02:00
|
|
|
|
|
|
|
self.port_range_pattern = re.compile(r'\d+-\d+')
|
2019-08-10 03:54:35 +02:00
|
|
|
self.service_port_pattern = re.compile(r'(\d*) \([a-zA-Z0-9-]*\)')
|
2019-08-02 16:58:30 +02:00
|
|
|
|
2017-11-06 23:18:18 +01:00
|
|
|
pattern = re.compile(
|
2017-11-09 17:13:56 +01:00
|
|
|
r"(?P<name>[a-z][a-z0-9-]+)\s+(?P<port>[0-9]+)/"
|
|
|
|
r"(?P<protocol>[a-z]+)",
|
2017-11-06 23:18:18 +01:00
|
|
|
re.IGNORECASE)
|
2018-12-28 17:40:35 +01:00
|
|
|
with open('/etc/services', 'r', encoding='utf-8') as file:
|
2017-11-17 17:34:38 +01:00
|
|
|
for line in file:
|
|
|
|
match = pattern.match(line)
|
|
|
|
if match is not None:
|
|
|
|
service = match.groupdict()
|
|
|
|
self.__services.append(
|
|
|
|
(service["name"], int(service["port"]),))
|
2011-03-03 22:42:10 +01:00
|
|
|
|
2012-03-12 15:54:16 +01:00
|
|
|
self.fw_changed = False
|
2019-05-22 23:10:09 +02:00
|
|
|
self.allow = None # is the default policy allow or deny
|
|
|
|
self.temp_full_access_expire_time = None # temporary full access time
|
|
|
|
self.__vm = None # VM that the model applies to
|
|
|
|
self.__children = None # list of rules in the FW
|
2012-03-12 15:54:16 +01:00
|
|
|
|
2012-02-29 10:42:57 +01:00
|
|
|
def sort(self, idx, order):
|
2017-11-06 22:46:35 +01:00
|
|
|
rev = (order == QtCore.Qt.AscendingOrder)
|
2017-11-17 17:34:38 +01:00
|
|
|
self.children.sort(key=lambda x: self.get_column_string(idx, x),
|
|
|
|
reverse=rev)
|
2017-10-13 18:04:49 +02:00
|
|
|
|
2012-08-07 15:03:04 +02:00
|
|
|
index1 = self.createIndex(0, 0)
|
2017-11-08 16:53:49 +01:00
|
|
|
index2 = self.createIndex(len(self) - 1, len(self.__column_names) - 1)
|
2012-08-07 15:03:04 +02:00
|
|
|
self.dataChanged.emit(index1, index2)
|
2012-02-29 10:42:57 +01:00
|
|
|
|
2011-03-03 22:42:10 +01:00
|
|
|
def get_service_name(self, port):
|
|
|
|
for service in self.__services:
|
2017-10-13 18:04:49 +02:00
|
|
|
if str(service[1]) == str(port):
|
2019-08-02 16:34:55 +02:00
|
|
|
return "{0} ({1})".format(str(port), service[0])
|
2011-03-03 22:42:10 +01:00
|
|
|
return str(port)
|
|
|
|
|
|
|
|
def get_service_port(self, name):
|
|
|
|
for service in self.__services:
|
|
|
|
if service[0] == name:
|
|
|
|
return service[1]
|
|
|
|
return None
|
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
def get_column_string(self, col, rule):
|
2017-11-09 17:19:17 +01:00
|
|
|
# pylint: disable=too-many-return-statements
|
2017-10-13 18:04:49 +02:00
|
|
|
# Address
|
|
|
|
if col == 0:
|
|
|
|
if rule.dsthost is None:
|
|
|
|
return "*"
|
2017-11-14 15:29:57 +01:00
|
|
|
if rule.dsthost.type == 'dst4' and rule.dsthost.prefixlen == '32':
|
|
|
|
return str(rule.dsthost)[:-3]
|
|
|
|
if rule.dsthost.type == 'dst6' and rule.dsthost.prefixlen == '128':
|
|
|
|
return str(rule.dsthost)[:-4]
|
|
|
|
return str(rule.dsthost)
|
2017-10-13 18:04:49 +02:00
|
|
|
|
|
|
|
# Service
|
|
|
|
if col == 1:
|
2017-07-12 14:10:43 +02:00
|
|
|
if rule.dstports is None:
|
2017-10-13 18:04:49 +02:00
|
|
|
return "any"
|
2017-11-14 15:29:57 +01:00
|
|
|
if rule.dstports.range[0] != rule.dstports.range[1]:
|
2017-10-13 18:04:49 +02:00
|
|
|
return str(rule.dstports)
|
2017-11-14 15:29:57 +01:00
|
|
|
return self.get_service_name(rule.dstports)
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
# Protocol
|
|
|
|
if col == 2:
|
|
|
|
if rule.proto is None:
|
|
|
|
return "any"
|
2017-11-14 15:29:57 +01:00
|
|
|
return str(rule.proto)
|
2017-10-13 18:04:49 +02:00
|
|
|
return "unknown"
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2019-10-23 18:18:39 +02:00
|
|
|
def get_firewall_conf(self, vm):
|
2017-07-12 14:10:43 +02:00
|
|
|
conf = {
|
|
|
|
'allow': None,
|
2017-10-13 18:04:49 +02:00
|
|
|
'expire': 0,
|
2017-07-12 14:10:43 +02:00
|
|
|
'rules': [],
|
|
|
|
}
|
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
allow_dns = False
|
|
|
|
allow_icmp = False
|
2017-07-12 14:10:43 +02:00
|
|
|
common_action = None
|
|
|
|
|
2018-05-11 02:30:20 +02:00
|
|
|
reversed_rules = reversed(vm.firewall.rules)
|
|
|
|
last_rule = next(reversed_rules, None)
|
|
|
|
|
|
|
|
if last_rule is None:
|
2019-10-23 18:18:39 +02:00
|
|
|
raise FirewallModifiedOutsideError(
|
|
|
|
self.tr('At least one rule must exist.'))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
if last_rule == qubesadmin.firewall.Rule('action=accept') \
|
|
|
|
or last_rule == qubesadmin.firewall.Rule('action=drop'):
|
|
|
|
common_action = last_rule.action
|
|
|
|
else:
|
2019-10-23 18:18:39 +02:00
|
|
|
raise FirewallModifiedOutsideError(
|
|
|
|
self.tr('Last rule must be either drop all or accept all.'))
|
2017-10-13 18:04:49 +02:00
|
|
|
|
|
|
|
dns_rule = qubesadmin.firewall.Rule(None,
|
|
|
|
action='accept', specialtarget='dns')
|
|
|
|
icmp_rule = qubesadmin.firewall.Rule(None,
|
|
|
|
action='accept', proto='icmp')
|
2018-05-11 02:30:20 +02:00
|
|
|
for rule in reversed_rules:
|
2017-10-13 18:04:49 +02:00
|
|
|
if rule == dns_rule:
|
2017-11-08 16:53:49 +01:00
|
|
|
allow_dns = True
|
2017-07-12 14:10:43 +02:00
|
|
|
continue
|
|
|
|
|
2017-11-17 19:12:06 +01:00
|
|
|
if rule == icmp_rule:
|
2017-11-08 16:53:49 +01:00
|
|
|
allow_icmp = True
|
2017-07-12 14:10:43 +02:00
|
|
|
continue
|
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
if rule.specialtarget is not None or rule.icmptype is not None:
|
2019-10-23 18:18:39 +02:00
|
|
|
raise FirewallModifiedOutsideError(
|
|
|
|
self.tr("Rule type unknown!"))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
if (rule.dsthost is not None or rule.proto is not None) \
|
|
|
|
and rule.expire is None:
|
|
|
|
if rule.action == 'accept':
|
|
|
|
conf['rules'].insert(0, rule)
|
|
|
|
continue
|
2019-10-23 18:18:39 +02:00
|
|
|
raise FirewallModifiedOutsideError(
|
|
|
|
self.tr('No blacklist support.'))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
if rule.expire is not None and rule.dsthost is None \
|
|
|
|
and rule.proto is None:
|
|
|
|
conf['expire'] = int(str(rule.expire))
|
|
|
|
continue
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2019-10-23 18:18:39 +02:00
|
|
|
raise FirewallModifiedOutsideError(self.tr('it does not add up.'))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
conf['allow'] = (common_action == 'accept')
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
if not allow_icmp and not conf['allow']:
|
2019-10-23 18:18:39 +02:00
|
|
|
raise FirewallModifiedOutsideError(self.tr('ICMP must be allowed.'))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
if not allow_dns and not conf['allow']:
|
2019-10-23 18:18:39 +02:00
|
|
|
raise FirewallModifiedOutsideError(self.tr('DNS must be allowed'))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
return conf
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-11-09 16:34:09 +01:00
|
|
|
@staticmethod
|
|
|
|
def write_firewall_conf(vm, conf):
|
2017-10-13 18:04:49 +02:00
|
|
|
rules = []
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
for rule in conf['rules']:
|
|
|
|
rules.append(rule)
|
|
|
|
|
|
|
|
if not conf['allow']:
|
2019-05-22 23:10:09 +02:00
|
|
|
rules.append(qubesadmin.firewall.Rule(
|
|
|
|
None, action='accept', specialtarget='dns'))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
if not conf['allow']:
|
2019-05-22 23:10:09 +02:00
|
|
|
rules.append(qubesadmin.firewall.Rule(
|
|
|
|
None, action='accept', proto='icmp'))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
if conf['allow']:
|
2019-05-22 23:10:09 +02:00
|
|
|
rules.append(qubesadmin.firewall.Rule(
|
|
|
|
None, action='accept'))
|
2017-10-13 18:04:49 +02:00
|
|
|
else:
|
2019-05-22 23:10:09 +02:00
|
|
|
rules.append(qubesadmin.firewall.Rule(
|
|
|
|
None, action='drop'))
|
2017-07-12 14:10:43 +02:00
|
|
|
|
|
|
|
vm.firewall.rules = rules
|
|
|
|
|
2011-02-21 18:15:35 +01:00
|
|
|
def set_vm(self, vm):
|
|
|
|
self.__vm = vm
|
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
self.clear_children()
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-07-12 14:10:43 +02:00
|
|
|
conf = self.get_firewall_conf(vm)
|
2011-03-03 22:42:10 +01:00
|
|
|
|
|
|
|
self.allow = conf["allow"]
|
2017-10-13 18:04:49 +02:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
self.temp_full_access_expire_time = conf['expire']
|
2011-03-03 22:42:10 +01:00
|
|
|
|
2011-03-02 15:06:39 +01:00
|
|
|
for rule in conf["rules"]:
|
2017-11-08 16:53:49 +01:00
|
|
|
self.append_child(rule)
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2011-03-03 22:42:10 +01:00
|
|
|
def get_vm_name(self):
|
|
|
|
return self.__vm.name
|
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
def apply_rules(self, allow, temp_full_access=False,
|
|
|
|
temp_full_access_time=None):
|
2011-02-21 18:15:35 +01:00
|
|
|
assert self.__vm is not None
|
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
if self.allow != allow or \
|
2017-11-08 16:53:49 +01:00
|
|
|
(self.temp_full_access_expire_time != 0) != temp_full_access:
|
2012-03-12 15:54:16 +01:00
|
|
|
self.fw_changed = True
|
|
|
|
|
2017-11-06 23:54:33 +01:00
|
|
|
conf = {"allow": allow,
|
2011-03-21 22:08:39 +01:00
|
|
|
"rules": list()
|
2019-05-22 23:10:09 +02:00
|
|
|
}
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
conf['rules'].extend(self.children)
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
if temp_full_access and not allow:
|
|
|
|
conf["rules"].append(qubesadmin.firewall.Rule(
|
|
|
|
None,
|
|
|
|
action='accept',
|
|
|
|
expire=int(datetime.datetime.now().strftime("%s")) +
|
|
|
|
temp_full_access_time * 60))
|
2014-03-28 05:19:07 +01:00
|
|
|
|
2012-03-12 15:54:16 +01:00
|
|
|
if self.fw_changed:
|
2017-07-12 14:10:43 +02:00
|
|
|
self.write_firewall_conf(self.__vm, conf)
|
2012-03-12 15:54:16 +01:00
|
|
|
|
2017-10-13 18:04:49 +02:00
|
|
|
def populate_edit_dialog(self, dialog, row):
|
|
|
|
address = self.get_column_string(0, self.children[row])
|
|
|
|
dialog.addressComboBox.setItemText(0, address)
|
|
|
|
dialog.addressComboBox.setCurrentIndex(0)
|
|
|
|
service = self.get_column_string(1, self.children[row])
|
|
|
|
if service == "any":
|
|
|
|
service = ""
|
|
|
|
dialog.serviceComboBox.setItemText(0, service)
|
|
|
|
dialog.serviceComboBox.setCurrentIndex(0)
|
|
|
|
protocol = self.get_column_string(2, self.children[row])
|
|
|
|
if protocol == "tcp":
|
|
|
|
dialog.tcp_radio.setChecked(True)
|
|
|
|
elif protocol == "udp":
|
|
|
|
dialog.udp_radio.setChecked(True)
|
|
|
|
else:
|
|
|
|
dialog.any_radio.setChecked(True)
|
|
|
|
|
2017-11-06 23:54:33 +01:00
|
|
|
def run_rule_dialog(self, dialog, row=None):
|
2019-09-12 22:05:56 +02:00
|
|
|
self.current_row = row
|
|
|
|
dialog.model = self
|
|
|
|
dialog.exec()
|
2017-10-13 18:04:49 +02:00
|
|
|
|
2017-11-06 22:46:35 +01:00
|
|
|
def index(self, row, column, parent=QtCore.QModelIndex()):
|
2011-02-21 18:15:35 +01:00
|
|
|
if not self.hasIndex(row, column, parent):
|
2017-11-06 22:46:35 +01:00
|
|
|
return QtCore.QModelIndex()
|
2011-02-21 18:15:35 +01:00
|
|
|
|
|
|
|
return self.createIndex(row, column, self.children[row])
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
def parent(self, child): # pylint: disable=unused-argument,no-self-use
|
2017-11-06 22:46:35 +01:00
|
|
|
return QtCore.QModelIndex()
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-09 16:08:16 +01:00
|
|
|
# pylint: disable=invalid-name,unused-argument
|
2017-11-06 22:46:35 +01:00
|
|
|
def rowCount(self, parent=QtCore.QModelIndex()):
|
2011-02-21 18:15:35 +01:00
|
|
|
return len(self)
|
|
|
|
|
2017-11-09 16:08:16 +01:00
|
|
|
# pylint: disable=invalid-name,unused-argument
|
2017-11-06 22:46:35 +01:00
|
|
|
def columnCount(self, parent=QtCore.QModelIndex()):
|
2017-11-08 16:53:49 +01:00
|
|
|
return len(self.__column_names)
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-09 16:34:09 +01:00
|
|
|
# pylint: disable=invalid-name,no-self-use
|
2017-11-06 22:46:35 +01:00
|
|
|
def hasChildren(self, index=QtCore.QModelIndex()):
|
2017-11-08 16:53:49 +01:00
|
|
|
parent_item = index.internalPointer()
|
2017-11-17 17:34:38 +01:00
|
|
|
return parent_item is None
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-06 22:46:35 +01:00
|
|
|
def data(self, index, role=QtCore.Qt.DisplayRole):
|
|
|
|
if index.isValid() and role == QtCore.Qt.DisplayRole:
|
2017-11-06 23:54:33 +01:00
|
|
|
return self.get_column_string(index.column(),
|
|
|
|
self.children[index.row()])
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
# pylint: disable=invalid-name
|
2017-11-06 22:46:35 +01:00
|
|
|
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
|
2017-11-08 16:53:49 +01:00
|
|
|
if section < len(self.__column_names) \
|
2017-11-06 23:18:18 +01:00
|
|
|
and orientation == QtCore.Qt.Horizontal \
|
|
|
|
and role == QtCore.Qt.DisplayRole:
|
2017-11-09 17:30:22 +01:00
|
|
|
return self.__column_names[section]
|
2011-02-21 18:15:35 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def children(self):
|
|
|
|
return self.__children
|
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
def append_child(self, child):
|
2011-02-21 18:15:35 +01:00
|
|
|
row = len(self)
|
2017-11-06 22:46:35 +01:00
|
|
|
self.beginInsertRows(QtCore.QModelIndex(), row, row)
|
2011-02-21 18:15:35 +01:00
|
|
|
self.children.append(child)
|
|
|
|
self.endInsertRows()
|
|
|
|
index = self.createIndex(row, 0, child)
|
|
|
|
self.dataChanged.emit(index, index)
|
2014-03-28 05:15:52 +01:00
|
|
|
self.fw_changed = True
|
2011-02-21 18:15:35 +01:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
def remove_child(self, i):
|
2011-02-21 18:15:35 +01:00
|
|
|
if i >= len(self):
|
|
|
|
return
|
|
|
|
|
2017-11-06 22:46:35 +01:00
|
|
|
self.beginRemoveRows(QtCore.QModelIndex(), i, i)
|
2011-02-21 18:15:35 +01:00
|
|
|
del self.children[i]
|
|
|
|
self.endRemoveRows()
|
|
|
|
index = self.createIndex(i, 0)
|
|
|
|
self.dataChanged.emit(index, index)
|
2014-03-28 05:15:52 +01:00
|
|
|
self.fw_changed = True
|
2011-03-02 15:06:39 +01:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
def set_child(self, i, child):
|
2011-03-03 22:42:10 +01:00
|
|
|
self.children[i] = child
|
|
|
|
index = self.createIndex(i, 0, child)
|
|
|
|
self.dataChanged.emit(index, index)
|
2014-03-28 05:15:52 +01:00
|
|
|
self.fw_changed = True
|
2011-03-03 22:42:10 +01:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
def clear_children(self):
|
2011-02-21 18:15:35 +01:00
|
|
|
self.__children = list()
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.children)
|