generate-admin-policy 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python3
  2. # coding=utf-8
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <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. import argparse
  21. import os
  22. import sys
  23. import qubes.api.admin
  24. parser = argparse.ArgumentParser(
  25. description='Generate default Admin API policy')
  26. parser.add_argument('--include-base', action='store',
  27. default='include',
  28. help='Base path for included paths (default: %(default)s)')
  29. parser.add_argument('--destdir', action='store',
  30. default='/etc/qubes-rpc/policy',
  31. help='Directory where write output files to (default: %(default)s)')
  32. parser.add_argument('--verbose', action='store_true', default=False,
  33. help='Be verbose')
  34. parser.add_argument('--exclude', action='store', nargs='*',
  35. help='Exclude service')
  36. parser.add_argument('service', nargs='*', action='store',
  37. help='Generate policy for those services (default: all)')
  38. default_policy_header = '''\
  39. ## Note that policy parsing stops at the first match.
  40. ## Anything not specifically allowed here (or in included file) will be denied.
  41. ## Please use a single # to start your custom comments
  42. ## Add your entries here, make sure to append ",target=dom0" to all allow/ask actions
  43. ## Include a common file for all admin.* methods to ease setting up
  44. ## Management VM.
  45. ## To allow only specific actions, edit specific policy file, like this one. To
  46. ## allow all of them, edit appropriate /etc/qubes-rpc/include/admin-*.
  47. '''
  48. def write_default_policy(args, apiname, clasifiers):
  49. ''' Write single default policy for given API call '''
  50. assert 'scope' in clasifiers, \
  51. 'Method {} lack scope classifier'.format(apiname)
  52. assert any(attr in clasifiers for attr in ('read', 'write', 'execute')), \
  53. 'Method {} lack read/write/execute classifier'.format(apiname)
  54. assert clasifiers['scope'] in ('local', 'global'), \
  55. 'Method {} have invalid scope: {}'.format(apiname, clasifiers['scope'])
  56. file_to_include = 'admin-{scope}-{rwx}'.format(
  57. scope=clasifiers['scope'],
  58. rwx=('rwx' if clasifiers.get('write', False) or
  59. clasifiers.get('execute', False)
  60. else 'ro'))
  61. if args.verbose:
  62. print('Service {}: include {}'.format(apiname, file_to_include),
  63. file=sys.stderr)
  64. with open(os.path.join(args.destdir, apiname), 'w') as f:
  65. f.write(default_policy_header)
  66. f.write('$include:{}\n'.format(
  67. os.path.join(args.include_base, file_to_include)))
  68. def main(args=None):
  69. ''' Main function of default-admin-policy tool'''
  70. args = parser.parse_args(args)
  71. for func, apiname, _ in qubes.api.admin.QubesAdminAPI.list_methods():
  72. if args.service and apiname not in args.service:
  73. continue
  74. if args.exclude and apiname in args.exclude:
  75. continue
  76. write_default_policy(args, apiname, func.classifiers)
  77. if __name__ == '__main__':
  78. sys.exit(main())