qubes-notify-tools 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/python2
  2. import os
  3. import re
  4. import sys
  5. import subprocess
  6. from qubes.qubes import QubesVmCollection,QubesException,QubesHVm
  7. from qubes.qubes import xs
  8. def main():
  9. source = os.getenv("QREXEC_REMOTE_DOMAIN")
  10. if source is None:
  11. print >> sys.stderr, 'This script must be called as qrexec service!'
  12. exit(1)
  13. qvm_collection = QubesVmCollection()
  14. qvm_collection.lock_db_for_writing()
  15. try:
  16. qvm_collection.load()
  17. source_vm = qvm_collection.get_vm_by_name(source)
  18. if source_vm is None:
  19. raise QubesException('Domain ' + source + ' does not exists (?!)')
  20. if not isinstance(source_vm, QubesHVm):
  21. raise QubesException('Service qubes.ToolsNotify is designed only for HVM domains')
  22. xs_path = "/local/domain/{0}/qubes-tools".format(source_vm.get_xid())
  23. # for now used only to check for the tools presence
  24. untrusted_version = xs.read('', '{0}/version'.format(xs_path))
  25. # reserved for future use
  26. untrusted_os = xs.read('', '{0}/os'.format(xs_path))
  27. # qrexec agent presence (0 or 1)
  28. untrusted_qrexec = xs.read('', '{0}/qrexec'.format(xs_path))
  29. # gui agent presence (0 or 1)
  30. untrusted_gui = xs.read('', '{0}/gui'.format(xs_path))
  31. # default user for qvm-run etc
  32. untrusted_user = xs.read('', '{0}/default-user'.format(xs_path))
  33. if untrusted_version is None:
  34. # tools didn't advertised its features; it's strange that this
  35. # service is called, but ignore it
  36. return
  37. # any suspicious string will raise exception here
  38. version = int(untrusted_version)
  39. # untrusted_os - ignore for now
  40. if untrusted_qrexec is None:
  41. qrexec = 0
  42. else:
  43. qrexec = int(untrusted_qrexec)
  44. if untrusted_gui is None:
  45. gui = 0
  46. else:
  47. gui = int(untrusted_gui)
  48. if untrusted_user is not None and re.match(r'^[a-zA-Z0-9-]{1,255}$', untrusted_user):
  49. assert '@' not in untrusted_user
  50. assert '/' not in untrusted_user
  51. user = untrusted_user
  52. else:
  53. user = None
  54. # Let the tools to be able to enable *or disable* each particular component
  55. source_vm.qrexec_installed = qrexec > 0
  56. source_vm.guiagent_installed = gui > 0
  57. if user is not None:
  58. source_vm.default_user = user
  59. qvm_collection.save()
  60. retcode = subprocess.call(['qvm-sync-appmenus', '--force-rpc'])
  61. if retcode == 0 and hasattr(source_vm, 'appmenus_recreate'):
  62. # TODO: call the same for child VMs? This isn't done for Linux VMs,
  63. # so probably should be ignored for Windows also
  64. source_vm.appmenus_recreate()
  65. except Exception as e:
  66. print >> sys.stderr, e.message
  67. exit(1)
  68. finally:
  69. qvm_collection.unlock_db()
  70. main()