r3compatibility.py 9.2 KB

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