qvm-sync-clock 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. p = clockvm.run_service('qubes.GetDate')
  34. untrusted_date_out = p.stdout.read(25).decode('ascii', errors='strict')
  35. untrusted_date_out = untrusted_date_out.strip()
  36. if not re.match(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+00:?00$', untrusted_date_out):
  37. sys.stderr.write('Invalid date received, aborting!\n')
  38. sys.exit(1)
  39. date_out = untrusted_date_out
  40. subprocess.check_call(['date', '-u', '-Iseconds', '-s', date_out],
  41. stdout=subprocess.DEVNULL)
  42. subprocess.check_call(['hwclock', '--systohc'],
  43. stdout=subprocess.DEVNULL)
  44. if __name__ == '__main__':
  45. main()