1
0
mirror of https://github.com/altlinux/gpupdate.git synced 2025-02-09 09:57:50 +03:00
gpupdate/gpoa/gpupdate-setup

306 lines
8.9 KiB
Plaintext
Raw Normal View History

2020-03-27 21:30:55 +04:00
#! /usr/bin/env python3
#
# GPOA - GPO Applier for Linux
#
# Copyright (C) 2019-2020 BaseALT Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2020-03-27 21:30:55 +04:00
import os
import sys
import argparse
import subprocess
import re
from util.util import (
runcmd
, get_backends
, get_default_policy_name
, get_policy_entries
, get_policy_variants
)
from util.config import GPConfig
class Runner:
__control_path = '/usr/sbin/control'
__systemctl_path = '/bin/systemctl'
__etc_policy_dir = '/etc/local-policy'
__usr_policy_dir = '/usr/share/local-policy'
def __init__(self):
self.etc_policies = get_policy_entries(self.__etc_policy_dir)
self.usr_policies = get_policy_entries(self.__usr_policy_dir)
self.arguments = parse_arguments()
2020-08-13 08:05:46 +04:00
2020-03-27 21:30:55 +04:00
def parse_arguments():
'''
Parse CLI arguments.
'''
parser = argparse.ArgumentParser(prog='gpupdate-setup')
subparsers = parser.add_subparsers(dest='action',
metavar='action',
help='Group Policy management actions (default action is status)')
2020-03-27 21:30:55 +04:00
parser_list = subparsers.add_parser('list',
help='List avalable types of local policy')
2020-08-13 08:05:46 +04:00
parser_list = subparsers.add_parser('list-backends',
help='Show list of available backends')
2020-03-27 21:30:55 +04:00
parser_status = subparsers.add_parser('status',
help='Show current Group Policy status')
parser_enable = subparsers.add_parser('enable',
help='Enable Group Policy subsystem')
parser_disable = subparsers.add_parser('disable',
help='Disable Group Policy subsystem')
2020-03-27 21:30:55 +04:00
parser_write = subparsers.add_parser('write',
help='Operate on Group Policies (enable or disable)')
parser_default = subparsers.add_parser('default-policy',
help='Show name of default policy')
2020-04-14 16:08:06 +04:00
parser_active = subparsers.add_parser('active-policy',
help='Show name of policy enabled')
2020-03-27 21:30:55 +04:00
parser_write.add_argument('status',
choices=['enable', 'disable'],
help='Enable or disable Group Policies')
parser_write.add_argument('localpolicy',
default=None,
nargs='?',
help='Name of local policy to enable')
parser_write.add_argument('backend',
default='samba',
type=str,
nargs='?',
const='backend',
choices=['local', 'samba'],
help='Backend (source of settings) name')
parser_enable.add_argument('localpolicy',
default=None,
2020-03-27 21:30:55 +04:00
nargs='?',
help='Name of local policy to enable')
parser_enable.add_argument('backend',
default='samba',
type=str,
nargs='?',
const='backend',
choices=['local', 'samba'],
help='Backend (source of settings) name')
2020-03-27 21:30:55 +04:00
return parser.parse_args()
def validate_policy_name(policy_name):
return policy_name in [os.path.basename(d) for d in get_policy_variants()]
def is_unit_enabled(unit_name, unit_global=False):
'''
Check that designated systemd unit is enabled
'''
2020-08-18 16:12:45 +04:00
command = ['/bin/systemctl', 'is-enabled', unit_name]
if unit_global:
command = ['/bin/systemctl', '--global', 'is-enabled', unit_name]
2020-08-18 16:12:45 +04:00
value = runcmd(command)
# If first line of stdout is equal to "enabled" and return code
# is zero then unit is considered enabled.
rc = value[0]
result = []
try:
result = value[1][0]
except IndexError as exc:
return False
if result == 'enabled' and rc == 0:
2020-08-18 16:12:45 +04:00
return True
return False
2020-03-27 21:30:55 +04:00
def get_status():
'''
Check that gpupdate.service and gpupdate-user.service are enabled.
'''
2020-08-18 16:12:45 +04:00
is_gpupdate = is_unit_enabled('gpupdate.service')
is_gpupdate_user = is_unit_enabled('gpupdate-user.service')
2020-03-27 21:30:55 +04:00
2020-08-18 16:12:45 +04:00
if is_gpupdate and is_gpupdate_user:
return True
return False
2020-03-27 21:30:55 +04:00
def get_active_policy_name():
'''
Show the name of an active Local Policy template
'''
config = GPConfig()
return config.get_local_policy_template()
2020-03-27 21:30:55 +04:00
def rollback_on_error(command_name):
'''
Disable group policy services in case command returns error code
'''
if 0 != runcmd(command_name)[0]:
disable_gp()
return False
return True
2020-03-27 21:30:55 +04:00
def disable_gp():
'''
Consistently disable group policy services
'''
2020-08-17 18:47:25 +04:00
cmd_set_global_policy = ['/usr/sbin/control', 'system-policy', 'global']
cmd_set_local_policy = ['/usr/sbin/control', 'system-policy', 'local']
cmd_disable_gpupdate_service = ['/bin/systemctl', 'disable', 'gpupdate.service']
cmd_disable_gpupdate_user_service = ['/bin/systemctl', '--global', 'disable', 'gpupdate-user.service']
cmd_control_system_auth = ['/usr/sbin/control', 'system-auth']
config = GPConfig()
auth_result = 'local'
try:
auth_result = runcmd(cmd_control_system_auth)[1][0]
except Exception as exc:
print(str(exc))
2020-08-17 18:47:25 +04:00
if auth_result != 'local':
2020-08-17 18:47:25 +04:00
runcmd(cmd_set_global_policy)
else:
2020-08-17 18:47:25 +04:00
runcmd(cmd_set_local_policy)
runcmd(cmd_disable_gpupdate_service)
runcmd(cmd_disable_gpupdate_user_service)
2020-03-27 21:30:55 +04:00
def enable_gp(policy_name, backend_type):
'''
Consistently enable group policy services
'''
2020-03-27 21:30:55 +04:00
policy_dir = '/usr/share/local-policy'
etc_policy_dir = '/etc/local-policy'
2020-08-17 18:47:25 +04:00
cmd_set_gpupdate_policy = ['/usr/sbin/control', 'system-policy', 'gpupdate']
cmd_gpoa_nodomain = ['/usr/sbin/gpoa', '--nodomain', '--loglevel 5']
cmd_enable_gpupdate_service = ['/bin/systemctl', 'enable', 'gpupdate.service']
cmd_enable_gpupdate_user_service = ['/bin/systemctl', '--global', 'enable', 'gpupdate-user.service']
config = GPConfig()
target_policy_name = get_default_policy_name()
2020-03-27 21:30:55 +04:00
if policy_name:
if validate_policy_name(policy_name):
target_policy_name = policy_name
2020-04-14 16:08:06 +04:00
print (target_policy_name)
default_policy_name = os.path.join(policy_dir, target_policy_name)
2020-03-27 21:30:55 +04:00
if not os.path.isdir(etc_policy_dir):
os.makedirs(etc_policy_dir)
config.set_local_policy_template(default_policy_name)
2020-03-27 21:30:55 +04:00
# Enable oddjobd_gpupdate in PAM config
if not rollback_on_error(cmd_set_gpupdate_policy):
return
2020-03-27 21:30:55 +04:00
# Bootstrap the Group Policy engine
if not rollback_on_error(cmd_gpoa_nodomain):
return
2020-08-17 18:47:25 +04:00
# Enable gpupdate.service
if not rollback_on_error(cmd_enable_gpupdate_service):
return
2020-08-18 16:12:45 +04:00
if not is_unit_enabled('gpupdate.service'):
disable_gp()
return
2020-03-27 21:30:55 +04:00
# Enable gpupdate-setup.service for all users
if not rollback_on_error(cmd_enable_gpupdate_user_service):
return
2020-08-18 16:12:45 +04:00
if not is_unit_enabled('gpupdate-user.service'):
disable_gp()
return
2020-03-27 21:30:55 +04:00
def act_list():
'''
Show list of available templates of Local Policy
'''
for entry in get_policy_variants():
print(entry.rpartition('/')[2])
def act_list_backends():
'''
List backends supported by GPOA
'''
backends = get_backends()
for backend in backends:
print(backend)
def act_status():
'''
Check that group policy services are enabled
'''
if get_status():
print('enabled')
else:
print('disabled')
2020-03-27 21:30:55 +04:00
def act_write(localpolicy, backend):
'''
Enable or disable group policy services
'''
if arguments.status == 'enable' or arguments.status == '#t':
enable_gp(localpolicy, backend)
if arguments.status == 'disable' or arguments.status == '#f':
disable_gp()
def act_enable(localpolicy, backend):
'''
Enable group policy services
'''
enable_gp(localpolicy, backend)
def act_active_policy():
'''
Print active Local Policy template name to stdout
'''
print(get_active_policy_name())
def act_default_policy():
'''
Print default Local Policy template name to stdout
'''
print(get_default_policy_name())
def main():
arguments = parse_arguments()
action = dict()
action['list'] = act_list
action['list-backends'] = act_list_backends
action['status'] = act_status
action['write'] = act_write
action['enable'] = act_enable
action['disable'] = disable_gp
action['active-policy'] = act_active_policy
action['default-policy'] = act_default_policy
if arguments.action == None:
action['status']()
elif arguments.action == 'enable':
action[arguments.action](arguments.localpolicy, arguments.backend)
elif arguments.action == 'write':
action[arguments.action](arguments.localpolicy)
else:
action[arguments.action]()
2020-04-14 16:08:06 +04:00
2020-03-27 21:30:55 +04:00
if __name__ == '__main__':
main()