r3compatibility.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 vm: VM on which QubesDB entries were just created
  47. :type vm: qubes.vm.qubesvm.QubesVM
  48. """
  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. if vm.netvm:
  106. self.write_iptables_qubesdb_entry(vm.netvm)
  107. @qubes.ext.handler('firewall-changed')
  108. def on_firewall_changed(self, vm, event):
  109. if vm.is_running() and vm.netvm:
  110. self.write_iptables_qubesdb_entry(vm.netvm)
  111. def write_iptables_qubesdb_entry(self, firewallvm):
  112. firewallvm.qdb.rm("/qubes-iptables-domainrules/")
  113. iptables = "# Generated by Qubes Core on {0}\n".format(
  114. datetime.datetime.now().ctime())
  115. iptables += "*filter\n"
  116. iptables += ":INPUT DROP [0:0]\n"
  117. iptables += ":FORWARD DROP [0:0]\n"
  118. iptables += ":OUTPUT ACCEPT [0:0]\n"
  119. # Strict INPUT rules
  120. iptables += "-A INPUT -i vif+ -p udp -m udp --dport 68 -j DROP\n"
  121. iptables += "-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED " \
  122. "-j ACCEPT\n"
  123. iptables += "-A INPUT -p icmp -j ACCEPT\n"
  124. iptables += "-A INPUT -i lo -j ACCEPT\n"
  125. iptables += "-A INPUT -j REJECT --reject-with icmp-host-prohibited\n"
  126. iptables += "-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED " \
  127. "-j ACCEPT\n"
  128. # Deny inter-VMs networking
  129. iptables += "-A FORWARD -i vif+ -o vif+ -j DROP\n"
  130. iptables += "COMMIT\n"
  131. firewallvm.qdb.write("/qubes-iptables-header", iptables)
  132. for vm in firewallvm.connected_vms:
  133. iptables = "*filter\n"
  134. conf = vm.get_firewall_conf()
  135. xid = vm.xid
  136. if xid < 0: # VM not active ATM
  137. continue
  138. ip = vm.ip
  139. if ip is None:
  140. continue
  141. # Anti-spoof rules are added by vif-script (vif-route-qubes),
  142. # here we trust IP address
  143. accept_action = "ACCEPT"
  144. reject_action = "REJECT --reject-with icmp-host-prohibited"
  145. if conf["allow"]:
  146. default_action = accept_action
  147. rules_action = reject_action
  148. else:
  149. default_action = reject_action
  150. rules_action = accept_action
  151. for rule in conf["rules"]:
  152. iptables += "-A FORWARD -s {0} -d {1}".format(
  153. ip, rule["address"])
  154. if rule["netmask"] != 32:
  155. iptables += "/{0}".format(rule["netmask"])
  156. if rule["proto"] is not None and rule["proto"] != "any":
  157. iptables += " -p {0}".format(rule["proto"])
  158. if rule["portBegin"] is not None and rule["portBegin"] > 0:
  159. iptables += " --dport {0}".format(rule["portBegin"])
  160. if rule["portEnd"] is not None and \
  161. rule["portEnd"] > rule["portBegin"]:
  162. iptables += ":{0}".format(rule["portEnd"])
  163. iptables += " -j {0}\n".format(rules_action)
  164. if conf["allowDns"] and firewallvm.netvm is not None:
  165. # PREROUTING does DNAT to NetVM DNSes, so we need self.netvm.
  166. # properties
  167. iptables += "-A FORWARD -s {0} -p udp -d {1} --dport 53 -j " \
  168. "ACCEPT\n".format(ip, vm.dns[0])
  169. iptables += "-A FORWARD -s {0} -p udp -d {1} --dport 53 -j " \
  170. "ACCEPT\n".format(ip, vm.dns[1])
  171. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport 53 -j " \
  172. "ACCEPT\n".format(ip, vm.dns[0])
  173. iptables += "-A FORWARD -s {0} -p tcp -d {1} --dport 53 -j " \
  174. "ACCEPT\n".format(ip, vm.dns[1])
  175. if conf["allowIcmp"]:
  176. iptables += "-A FORWARD -s {0} -p icmp -j ACCEPT\n".format(ip)
  177. if conf["allowYumProxy"]:
  178. iptables += \
  179. "-A FORWARD -s {0} -p tcp -d {1} --dport {2} -j ACCEPT\n".\
  180. format(ip, yum_proxy_ip, yum_proxy_port)
  181. else:
  182. iptables += \
  183. "-A FORWARD -s {0} -p tcp -d {1} --dport {2} -j DROP\n".\
  184. format(ip, yum_proxy_ip, yum_proxy_port)
  185. iptables += "-A FORWARD -s {0} -j {1}\n".format(ip, default_action)
  186. iptables += "COMMIT\n"
  187. firewallvm.qdb.write("/qubes-iptables-domainrules/" + str(xid),
  188. iptables)
  189. # no need for ending -A FORWARD -j DROP, cause default action is DROP
  190. firewallvm.qdb.write("/qubes-iptables", 'reload')
  191. def write_services(self, vm):
  192. for feature, value in vm.features.items():
  193. service = self.features_to_services.get(feature, feature)
  194. # forcefully convert to '0' or '1'
  195. vm.qdb.write('/qubes-service/{}'.format(service),
  196. str(int(bool(value))))
  197. if 'updates-proxy-setup' in vm.features.keys():
  198. vm.qdb.write('/qubes-service/{}'.format('yum-proxy-setup'),
  199. str(int(bool(vm.features['updates-proxy-setup']))))