qubes-notify-tools 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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:
  49. if re.match(r'^[a-zA-Z0-9-]+$', untrusted_user):
  50. user = untrusted_user
  51. else:
  52. user = None
  53. # Let the tools to be able to enable *or disable* each particular component
  54. source_vm.qrexec_installed = qrexec > 0
  55. source_vm.guiagent_installed = gui > 0
  56. if user:
  57. source_vm.default_user = user
  58. qvm_collection.save()
  59. retcode = subprocess.call(['qvm-sync-appmenus', '--force-rpc'])
  60. if retcode == 0 and hasattr(source_vm, 'appmenus_recreate'):
  61. # TODO: call the same for child VMs? This isn't done for Linux VMs,
  62. # so probably should be ignored for Windows also
  63. source_vm.appmenus_recreate()
  64. except Exception as e:
  65. print >> sys.stderr, e.message
  66. exit(1)
  67. finally:
  68. qvm_collection.unlock_db()
  69. main()