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. # for now used only to check for the tools presence
  23. untrusted_version = source_vm.qdb.read('/qubes-tools/version')
  24. # reserved for future use
  25. untrusted_os = source_vm.qdb.read('/qubes-tools/os')
  26. # qrexec agent presence (0 or 1)
  27. untrusted_qrexec = source_vm.qdb.read('/qubes-tools/qrexec')
  28. # gui agent presence (0 or 1)
  29. untrusted_gui = source_vm.qdb.read('/qubes-tools/gui')
  30. # default user for qvm-run etc
  31. untrusted_user = source_vm.qdb.read('/qubes-tools/default-user')
  32. if untrusted_version is None:
  33. # tools didn't advertised its features; it's strange that this
  34. # service is called, but ignore it
  35. return
  36. # any suspicious string will raise exception here
  37. version = int(untrusted_version)
  38. # untrusted_os - ignore for now
  39. if untrusted_qrexec is None:
  40. qrexec = 0
  41. else:
  42. qrexec = int(untrusted_qrexec)
  43. if untrusted_gui is None:
  44. gui = 0
  45. else:
  46. gui = int(untrusted_gui)
  47. if untrusted_user is not None and re.match(r'^[a-zA-Z0-9-]{1,255}$', untrusted_user):
  48. assert '@' not in untrusted_user
  49. assert '/' not in 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 is not None:
  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()