qvm-sync-clock 1.8 KB

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