r3compatibility.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 base64
  25. import datetime
  26. import qubes.ext
  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. # /qubes-vm-updateable
  60. vm.qdb.write('/qubes-vm-updateable', str(vm.updateable))
  61. # /qubes-base-template
  62. try:
  63. if vm.template:
  64. vm.qdb.write('/qubes-base-template', str(vm.template))
  65. else:
  66. vm.qdb.write('/qubes-base-template', '')
  67. except AttributeError:
  68. vm.qdb.write('/qubes-base-template', '')
  69. # /qubes-debug-mode: 0, 1
  70. vm.qdb.write('/qubes-debug-mode', str(int(vm.debug)))
  71. # /qubes-timezone
  72. timezone = vm.qdb.read('/timezone')
  73. if timezone:
  74. vm.qdb.write('/qubes-timezone', timezone)
  75. # /qubes-vm-persistence
  76. persistence = vm.qdb.read('/persistence')
  77. if persistence:
  78. vm.qdb.write('/qubes-vm-persistence', persistence)
  79. # /qubes-random-seed
  80. # write a new one, to make sure it wouldn't be reused/leaked
  81. vm.qdb.write('/qubes-random-seed',
  82. base64.b64encode(qubes.utils.urandom(64)))
  83. # /qubes-keyboard
  84. # not needed for now - the old one is still present
  85. # Networking
  86. if vm.provides_network:
  87. # '/qubes-netvm-network' value is only checked for being non empty
  88. vm.qdb.write('/qubes-netvm-network', vm.gateway)
  89. vm.qdb.write('/qubes-netvm-netmask', vm.netmask)
  90. vm.qdb.write('/qubes-netvm-gateway', vm.gateway)
  91. vm.qdb.write('/qubes-netvm-primary-dns', vm.dns[0])
  92. vm.qdb.write('/qubes-netvm-secondary-dns', vm.dns[1])
  93. if vm.netvm is not None:
  94. vm.qdb.write('/qubes-ip', vm.ip)
  95. vm.qdb.write('/qubes-netmask', vm.netvm.netmask)
  96. vm.qdb.write('/qubes-gateway', vm.netvm.gateway)
  97. vm.qdb.write('/qubes-primary-dns', vm.dns[0])
  98. vm.qdb.write('/qubes-secondary-dns', vm.dns[1])
  99. vm.qdb.write("/qubes-iptables-error", '')
  100. self.write_iptables_qubesdb_entry(vm)
  101. self.write_services(vm)
  102. # FIXME use event after creating Xen domain object, but before "resume"
  103. @qubes.ext.handler('domain-start')
  104. def on_domain_started(self, vm, event, **kwargs):
  105. # pylint: disable=unused-argument
  106. if vm.netvm:
  107. self.write_iptables_qubesdb_entry(vm.netvm)
  108. @qubes.ext.handler('firewall-changed')
  109. def on_firewall_changed(self, vm, event):
  110. # pylint: disable=unused-argument
  111. if vm.is_running() and vm.netvm:
  112. self.write_iptables_qubesdb_entry(vm.netvm)
  113. def write_iptables_qubesdb_entry(self, firewallvm):
  114. # pylint: disable=no-self-use
  115. firewallvm.qdb.rm("/qubes-iptables-domainrules/")
  116. iptables = "# Generated by Qubes Core on {0}\n".format(
  117. datetime.datetime.now().ctime())
  118. iptables += "*filter\n"
  119. iptables += ":INPUT DROP [0:0]\n"
  120. iptables += ":FORWARD DROP [0:0]\n"
  121. iptables += ":OUTPUT ACCEPT [0:0]\n"
  122. # Strict INPUT rules
  123. iptables += "-A INPUT -i vif+ -p udp -m udp --dport 68 -j DROP\n"
  124. iptables += "-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED " \
  125. "-j ACCEPT\n"
  126. iptables += "-A INPUT -p icmp -j ACCEPT\n"
  127. iptables += "-A INPUT -i lo -j ACCEPT\n"
  128. iptables += "-A INPUT -j REJECT --reject-with icmp-host-prohibited\n"
  129. iptables += "-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED " \
  130. "-j ACCEPT\n"
  131. # Deny inter-VMs networking
  132. iptables += "-A FORWARD -i vif+ -o vif+ -j DROP\n"
  133. iptables += "COMMIT\n"
  134. firewallvm.qdb.write("/qubes-iptables-header", iptables)
  135. for vm in firewallvm.connected_vms:
  136. iptables = "*filter\n"
  137. conf = vm.get_firewall_conf()
  138. xid = vm.xid
  139. if xid < 0: # VM not active ATM
  140. continue
  141. ip = vm.ip
  142. if ip is None:
  143. continue
  144. # Anti-spoof rules are added by vif-script (vif-route-qubes),
  145. # here we trust IP address
  146. accept_action = "ACCEPT"
  147. reject_action = "REJECT --reject-with icmp-host-prohibited"
  148. if conf["allow"]:
  149. default_action = accept_action
  150. rules_action = reject_action
  151. else:
  152. default_action = reject_action
  153. rules_action = accept_action
  154. for rule in conf["rules"]:
  155. iptables += "-A FORWARD -s {0} -d {1}".format(
  156. ip, rule["address"])
  157. if rule["netmask"] != 32:
  158. iptables += "/{0}".format(rule["netmask"])
  159. if rule["proto"] is not None and rule["proto"] != "any":
  160. iptables += " -p {0}".format(rule["proto"])
  161. if rule["portBegin"] is not None and rule["portBegin"] > 0:
  162. iptables += " --dport {0}".format(rule["portBegin"])
  163. if rule["portEnd"] is not None and \
  164. rule["portEnd"] > rule["portBegin"]:
  165. iptables += ":{0}".format(rule["portEnd"])
  166. iptables += " -j {0}\n".format(rules_action)
  167. if conf["allowDns"] and firewallvm.netvm is not None:
  168. # PREROUTING does DNAT to NetVM DNSes, so we need self.netvm.
  169. # properties
  170. iptables += "-A FORWARD -s {0} -p udp -d {1} --dport 53 -j " \
  171. "ACCEPT\n".format(ip, vm.dns[0])
  172. iptables += "-A FORWARD -s {0} -p udp -d {1} --dport 53 -j " \
  173. "ACCEPT\n".format(ip, vm.dns[1])
  174. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport 53 -j " \
  175. "ACCEPT\n".format(ip, vm.dns[0])
  176. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport 53 -j " \
  177. "ACCEPT\n".format(ip, vm.dns[1])
  178. if conf["allowIcmp"]:
  179. iptables += "-A FORWARD -s {0} -p icmp -j ACCEPT\n".format(ip)
  180. if conf["allowYumProxy"]:
  181. iptables += \
  182. "-A FORWARD -s {0} -p tcp -d {1} --dport {2} -j ACCEPT\n".\
  183. format(ip, yum_proxy_ip, yum_proxy_port)
  184. else:
  185. iptables += \
  186. "-A FORWARD -s {0} -p tcp -d {1} --dport {2} -j DROP\n".\
  187. format(ip, yum_proxy_ip, yum_proxy_port)
  188. iptables += "-A FORWARD -s {0} -j {1}\n".format(ip, default_action)
  189. iptables += "COMMIT\n"
  190. firewallvm.qdb.write("/qubes-iptables-domainrules/" + str(xid),
  191. iptables)
  192. # no need for ending -A FORWARD -j DROP, cause default action is DROP
  193. firewallvm.qdb.write("/qubes-iptables", 'reload')
  194. def write_services(self, vm):
  195. for feature, value in vm.features.items():
  196. service = self.features_to_services.get(feature, feature)
  197. # forcefully convert to '0' or '1'
  198. vm.qdb.write('/qubes-service/{}'.format(service),
  199. str(int(bool(value))))
  200. if 'updates-proxy-setup' in vm.features.keys():
  201. vm.qdb.write('/qubes-service/{}'.format('yum-proxy-setup'),
  202. str(int(bool(vm.features['updates-proxy-setup']))))