qvm-sync-clock 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/python3
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Marek Marczykowski <marmarek@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. #
  22. import sys
  23. import os
  24. import re
  25. import subprocess
  26. from qubesadmin import Qubes
  27. def main():
  28. if os.geteuid() != 0:
  29. sys.stderr.write('This program must be run as root to set the date, aborting!\n')
  30. sys.exit(1)
  31. app = Qubes()
  32. clockvm = app.clockvm
  33. if not clockvm:
  34. sys.exit(0)
  35. if not clockvm.is_running():
  36. sys.stderr.write('ClockVM {} is not running, aborting.\n'.format(
  37. clockvm.name))
  38. sys.exit(0)
  39. p = clockvm.run_service('qubes.GetDate')
  40. untrusted_date_out = p.stdout.read(25).decode('ascii', errors='strict')
  41. untrusted_date_out = untrusted_date_out.strip()
  42. if not re.match(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+00:?00$', untrusted_date_out):
  43. sys.stderr.write('Invalid date received, aborting!\n')
  44. sys.exit(1)
  45. date_out = untrusted_date_out
  46. subprocess.check_call(['date', '-u', '-Iseconds', '-s', date_out],
  47. stdout=subprocess.DEVNULL)
  48. subprocess.check_call(['/sbin/hwclock', '--systohc'],
  49. stdout=subprocess.DEVNULL)
  50. if __name__ == '__main__':
  51. main()