r3compatibility.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013-2016 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. import datetime
  25. import qubes.ext
  26. import qubes.firewall
  27. import qubes.vm.qubesvm
  28. import qubes.vm.appvm
  29. import qubes.vm.templatevm
  30. import qubes.utils
  31. yum_proxy_ip = '10.137.255.254'
  32. yum_proxy_port = '8082'
  33. class R3Compatibility(qubes.ext.Extension):
  34. '''Maintain VM interface compatibility with R3.0 and R3.1.
  35. At lease where possible.
  36. '''
  37. features_to_services = {
  38. 'services/ntpd': 'ntpd',
  39. 'check-updates': 'qubes-update-check',
  40. 'dvm': 'qubes-dvm',
  41. }
  42. # noinspection PyUnusedLocal
  43. @qubes.ext.handler('domain-qdb-create')
  44. def on_domain_qdb_create(self, vm, event):
  45. '''
  46. :param qubes.vm.qubesvm.QubesVM vm: \
  47. VM on which QubesDB entries were just created
  48. ''' # pylint: disable=unused-argument
  49. # /qubes-vm-type: AppVM, NetVM, ProxyVM, TemplateVM
  50. if isinstance(vm, qubes.vm.templatevm.TemplateVM):
  51. vmtype = 'TemplateVM'
  52. elif vm.netvm is not None and vm.provides_network:
  53. vmtype = 'ProxyVM'
  54. elif vm.netvm is None and vm.provides_network:
  55. vmtype = 'NetVM'
  56. else:
  57. vmtype = 'AppVM'
  58. vm.qdb.write('/qubes-vm-type', vmtype)
  59. vm.qdb.write("/qubes-iptables-error", '')
  60. self.write_iptables_qubesdb_entry(vm)
  61. self.write_services(vm)
  62. @qubes.ext.handler('domain-spawn')
  63. def on_domain_started(self, vm, event, **kwargs):
  64. # pylint: disable=unused-argument
  65. if vm.netvm:
  66. self.write_iptables_qubesdb_entry(vm.netvm)
  67. @qubes.ext.handler('firewall-changed')
  68. def on_firewall_changed(self, vm, event):
  69. # pylint: disable=unused-argument
  70. if vm.is_running() and vm.netvm:
  71. self.write_iptables_qubesdb_entry(vm.netvm)
  72. def write_iptables_qubesdb_entry(self, firewallvm):
  73. # pylint: disable=no-self-use
  74. firewallvm.qdb.rm("/qubes-iptables-domainrules/")
  75. iptables = "# Generated by Qubes Core on {0}\n".format(
  76. datetime.datetime.now().ctime())
  77. iptables += "*filter\n"
  78. iptables += ":INPUT DROP [0:0]\n"
  79. iptables += ":FORWARD DROP [0:0]\n"
  80. iptables += ":OUTPUT ACCEPT [0:0]\n"
  81. # Strict INPUT rules
  82. iptables += "-A INPUT -i vif+ -p udp -m udp --dport 68 -j DROP\n"
  83. iptables += "-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED " \
  84. "-j ACCEPT\n"
  85. iptables += "-A INPUT -p icmp -j ACCEPT\n"
  86. iptables += "-A INPUT -i lo -j ACCEPT\n"
  87. iptables += "-A INPUT -j REJECT --reject-with icmp-host-prohibited\n"
  88. iptables += "-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED " \
  89. "-j ACCEPT\n"
  90. # Deny inter-VMs networking
  91. iptables += "-A FORWARD -i vif+ -o vif+ -j DROP\n"
  92. iptables += "COMMIT\n"
  93. firewallvm.qdb.write("/qubes-iptables-header", iptables)
  94. for vm in firewallvm.connected_vms:
  95. iptables = "*filter\n"
  96. conf = vm.firewall
  97. xid = vm.xid
  98. if xid < 0: # VM not active ATM
  99. continue
  100. ip = vm.ip
  101. if ip is None:
  102. continue
  103. # Anti-spoof rules are added by vif-script (vif-route-qubes),
  104. # here we trust IP address
  105. for rule in conf.rules:
  106. if rule.specialtarget == 'dns':
  107. if rule.dstports not in ('53', None):
  108. continue
  109. if rule.proto:
  110. protos = {'tcp', 'udp'}.intersection(str(rule.proto))
  111. else:
  112. protos = {'tcp', 'udp'}
  113. for proto in protos:
  114. if rule.dsthost:
  115. dsthosts = set(vm.dns).intersection(
  116. [str(rule.dsthost).replace('/24', '')])
  117. else:
  118. dsthosts = vm.dns
  119. for dsthost in dsthosts:
  120. iptables += '-A FORWARD -s {}'.format(ip)
  121. iptables += ' -d {!s}'.format(dsthost)
  122. iptables += ' -p {!s}'.format(proto)
  123. iptables += ' --dport 53'
  124. iptables += ' -j {}\n'.format(
  125. str(rule.action).upper())
  126. else:
  127. iptables += '-A FORWARD -s {}'.format(ip)
  128. if rule.dsthost:
  129. iptables += ' -d {!s}'.format(rule.dsthost)
  130. if rule.proto:
  131. iptables += ' -p {!s}'.format(rule.proto)
  132. if rule.dstports:
  133. iptables += ' --dport {}'.format(
  134. str(rule.dstports).replace('-', ':'))
  135. iptables += ' -j {0}\n'.format(str(rule.action).upper())
  136. iptables += '-A FORWARD -s {0} -j {1}\n'.format(ip,
  137. str(conf.policy).upper())
  138. iptables += 'COMMIT\n'
  139. firewallvm.qdb.write('/qubes-iptables-domainrules/' + str(xid),
  140. iptables)
  141. # no need for ending -A FORWARD -j DROP, cause default action is DROP
  142. firewallvm.qdb.write('/qubes-iptables', 'reload')
  143. def write_services(self, vm):
  144. for feature, value in vm.features.items():
  145. service = self.features_to_services.get(feature, feature)
  146. # forcefully convert to '0' or '1'
  147. vm.qdb.write('/qubes-service/{}'.format(service),
  148. str(int(bool(value))))
  149. if 'updates-proxy-setup' in vm.features.keys():
  150. vm.qdb.write('/qubes-service/{}'.format('yum-proxy-setup'),
  151. str(int(bool(vm.features['updates-proxy-setup']))))