qvm-features-request 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/python3
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import argparse
  24. import os
  25. import subprocess
  26. import sys
  27. import qubesdb
  28. def is_active(service):
  29. status = subprocess.call(["systemctl", "is-active", "--quiet", service])
  30. return status == 0
  31. class FeatureRequestAction(argparse.Action):
  32. '''Action for argument parser that stores a property.'''
  33. # pylint: disable=redefined-builtin,too-few-public-methods
  34. def __init__(self,
  35. option_strings,
  36. dest='features',
  37. metavar='NAME=VALUE',
  38. required=False,
  39. help='request a feature with the value'):
  40. super(FeatureRequestAction, self).__init__(option_strings, dest=dest,
  41. metavar=metavar, nargs='*', required=required, default={},
  42. help=help)
  43. def __call__(self, parser, namespace, values, option_string=None):
  44. for request in values:
  45. try:
  46. feature, value = request.split('=', 1)
  47. except ValueError:
  48. parser.error(
  49. 'invalid feature request token: {!r}'.format(request))
  50. getattr(namespace, self.dest)[feature] = value
  51. parser = argparse.ArgumentParser(
  52. description='submit a feature request to the dom0')
  53. parser.add_argument('--commit',
  54. action='store_true', default=False,
  55. help='actually send the request (without it, only make entries in qubesdb)')
  56. parser.add_argument('features',
  57. action=FeatureRequestAction)
  58. def main(args=None):
  59. args = parser.parse_args(args)
  60. if not is_active("qubes-qrexec-agent"):
  61. return
  62. qdb = qubesdb.QubesDB()
  63. for feature, value in args.features.items():
  64. qdb.write('/features-request/' + feature, value)
  65. if args.commit:
  66. devnull = os.open(os.devnull, os.O_RDWR)
  67. subprocess.check_call(
  68. ['qrexec-client-vm', 'dom0', 'qubes.FeaturesRequest'],
  69. stdin=devnull, stdout=devnull)
  70. if __name__ == '__main__':
  71. sys.exit(main())