qubes-receive-updates 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. import os
  23. import os.path
  24. import re
  25. import sys
  26. import subprocess
  27. import shutil
  28. import glob
  29. import grp
  30. from qubes.qubes import QubesVmCollection
  31. updates_dir = "/var/lib/qubes/updates"
  32. updates_rpm_dir = updates_dir + "/rpm"
  33. updates_repodata_dir = updates_dir + "/repodata"
  34. updates_error_file = updates_dir + "/errors"
  35. updates_error_file_handle = None
  36. comps_file = None
  37. if os.path.exists('/usr/share/qubes/Qubes-comps.xml'):
  38. comps_file = '/usr/share/qubes/Qubes-comps.xml'
  39. package_regex = re.compile(r"^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._+-]{1,128}.rpm$")
  40. gpg_ok_regex = re.compile(r"pgp md5 OK$")
  41. def dom0updates_fatal(pkg, msg):
  42. global updates_error_file_handle
  43. print >> sys.stderr, msg
  44. if updates_error_file_handle is None:
  45. updates_error_file_handle = open(updates_error_file, "a")
  46. updates_error_file_handle.write(msg + "\n")
  47. os.remove(pkg)
  48. def handle_dom0updates(updatevm):
  49. global updates_error_file_handle
  50. source=os.getenv("QREXEC_REMOTE_DOMAIN")
  51. if source != updatevm.name:
  52. print >> sys.stderr, 'Domain ' + str(source) + ' not allowed to send dom0 updates'
  53. exit(1)
  54. # Clean old packages
  55. if os.path.exists(updates_rpm_dir):
  56. shutil.rmtree(updates_rpm_dir)
  57. if os.path.exists(updates_repodata_dir):
  58. shutil.rmtree(updates_repodata_dir)
  59. if os.path.exists(updates_error_file):
  60. os.remove(updates_error_file)
  61. qubes_gid = grp.getgrnam('qubes').gr_gid
  62. os.mkdir(updates_rpm_dir)
  63. os.chown(updates_rpm_dir, -1, qubes_gid)
  64. os.chmod(updates_rpm_dir, 0775)
  65. subprocess.check_call(["/usr/lib/qubes/qfile-dom0-unpacker", str(os.getuid()), updates_rpm_dir])
  66. # Verify received files
  67. for untrusted_f in os.listdir(updates_rpm_dir):
  68. if not package_regex.match(untrusted_f):
  69. dom0updates_fatal(updates_rpm_dir + '/' + untrusted_f, 'Domain ' + source + ' sent unexpected file: ' + untrusted_f)
  70. else:
  71. f = untrusted_f
  72. full_path = updates_rpm_dir + "/" + f
  73. if os.path.islink(full_path) or not os.path.isfile(full_path):
  74. dom0updates_fatal(full_path, 'Domain ' + source + ' sent not regular file')
  75. p = subprocess.Popen (["/bin/rpm", "-K", full_path],
  76. stdout=subprocess.PIPE)
  77. output = p.communicate()[0]
  78. if p.returncode != 0:
  79. dom0updates_fatal(full_path, 'Error while verifing %s signature: %s' % (f, output))
  80. if not gpg_ok_regex.search(output.strip()):
  81. dom0updates_fatal(full_path, 'Domain ' + source + ' sent not signed rpm: ' + f)
  82. if updates_error_file_handle is not None:
  83. updates_error_file_handle.close()
  84. # After updates received - create repo metadata
  85. createrepo_cmd = ["/usr/bin/createrepo"]
  86. if comps_file:
  87. createrepo_cmd += ["-g", comps_file]
  88. createrepo_cmd += ["-q", updates_dir]
  89. subprocess.check_call(createrepo_cmd)
  90. os.chown(updates_repodata_dir, -1, qubes_gid)
  91. os.chmod(updates_repodata_dir, 0775)
  92. # Clean old cache
  93. subprocess.call(["sudo", "/usr/bin/yum", "-q", "clean", "all"], stdout=sys.stderr)
  94. # This will fail because of "smart" detection of no-network, but it will invalidate the cache
  95. try:
  96. null = open('/dev/null','w')
  97. subprocess.call(["/usr/bin/pkcon", "refresh"], stdout=null)
  98. null.close()
  99. except:
  100. pass
  101. exit(0)
  102. def main():
  103. qvm_collection = QubesVmCollection()
  104. qvm_collection.lock_db_for_reading()
  105. qvm_collection.load()
  106. qvm_collection.unlock_db()
  107. updatevm = qvm_collection.get_updatevm_vm()
  108. handle_dom0updates(updatevm)
  109. main()