generate-admin-policy 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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('--dest', action='store',
  30. default='/etc/qubes/policy.d/90-admin-default.policy',
  31. help='Path where write output file to (default: %(default)s)')
  32. parser.add_argument('--header', action='store',
  33. default='90-admin-default.policy.header',
  34. help='File to prepend to the policy (default: %(default)s)')
  35. parser.add_argument('--verbose', action='store_true', default=False,
  36. help='Be verbose')
  37. parser.add_argument('--exclude', action='store', nargs='*',
  38. help='Exclude service')
  39. parser.add_argument('service', nargs='*', action='store',
  40. help='Generate policy for those services (default: all)')
  41. def write_default_policy(args, apiname, clasifiers, f):
  42. ''' Write single default policy for given API call '''
  43. assert 'scope' in clasifiers, \
  44. 'Method {} lack scope classifier'.format(apiname)
  45. assert any(attr in clasifiers for attr in ('read', 'write', 'execute')), \
  46. 'Method {} lack read/write/execute classifier'.format(apiname)
  47. assert clasifiers['scope'] in ('local', 'global'), \
  48. 'Method {} have invalid scope: {}'.format(apiname, clasifiers['scope'])
  49. file_to_include = 'admin-{scope}-{rwx}'.format(
  50. scope=clasifiers['scope'],
  51. rwx=('rwx' if clasifiers.get('write', False) or
  52. clasifiers.get('execute', False)
  53. else 'ro'))
  54. if args.verbose:
  55. print('Service {}: include {}'.format(apiname, file_to_include),
  56. file=sys.stderr)
  57. f.write('!include-service {} * {}\n'.format(
  58. apiname,
  59. os.path.join(args.include_base, file_to_include)))
  60. def main(args=None):
  61. ''' Main function of default-admin-policy tool'''
  62. args = parser.parse_args(args)
  63. with open(os.path.join(args.dest), 'w') as f:
  64. with open(args.header) as header_f:
  65. f.write(header_f.read())
  66. for func, apiname, _ in qubes.api.admin.QubesAdminAPI.list_methods():
  67. if args.service and apiname not in args.service:
  68. continue
  69. if args.exclude and apiname in args.exclude:
  70. continue
  71. write_default_policy(args, apiname, func.classifiers, f)
  72. if __name__ == '__main__':
  73. sys.exit(main())