mirror of
https://github.com/altlinux/gpupdate.git
synced 2025-03-21 18:50:38 +03:00
NOT TESTED: prepare to refactoring scripts_applier
This commit is contained in:
parent
1486084594
commit
4701847d1b
@ -1,7 +1,7 @@
|
||||
#
|
||||
# GPOA - GPO Applier for Linux
|
||||
#
|
||||
# Copyright (C) 2019-2020 BaseALT Ltd.
|
||||
# Copyright (C) 2019-2022 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
|
||||
@ -38,7 +38,8 @@ class scripts_applier(applier_frontend):
|
||||
def __init__(self, storage, sid):
|
||||
self.storage = storage
|
||||
self.sid = sid
|
||||
self.scripts = self.storage.get_scripts(self.sid)
|
||||
self.startup_scripts = self.storage.get_scripts(self.sid, 'STARTUP')
|
||||
self.shutdown_scripts = self.storage.get_scripts(self.sid, 'SHUTDOWN')
|
||||
self.folder_path = Path(self.__cache_scripts)
|
||||
self.__module_enabled = check_enabled(self.storage
|
||||
, self.__module_name
|
||||
@ -61,12 +62,14 @@ class scripts_applier(applier_frontend):
|
||||
Creating and updating folder directories for scripts and copying them
|
||||
'''
|
||||
self.folder_path.mkdir(parents=True, exist_ok=True)
|
||||
for ts in self.scripts:
|
||||
if ts.path.split('/')[-4] == 'MACHINE':
|
||||
script_path = (self.__cache_scripts +
|
||||
ts.policy_num + '/' +
|
||||
'/'.join(ts.path.split('/')[ts.path.split('/').index('POLICIES')+4:-1]))
|
||||
install_script(ts, script_path, '700')
|
||||
for ts in self.startup_scripts:
|
||||
# FIXME
|
||||
script_path = os.path.join(self.__cache_scripts)
|
||||
install_script(ts, script_path, '700')
|
||||
for ts in self.shutdown_scripts:
|
||||
# FIXME
|
||||
script_path = os.path.join(self.__cache_scripts)
|
||||
install_script(ts, script_path, '700')
|
||||
|
||||
def run(self):
|
||||
self.filling_cache()
|
||||
@ -88,8 +91,9 @@ class scripts_applier_user(applier_frontend):
|
||||
def __init__(self, storage, sid, username):
|
||||
self.storage = storage
|
||||
self.sid = sid
|
||||
self.logon_scripts = self.storage.get_scripts(self.sid, 'LOGON')
|
||||
self.logoff_scripts = self.storage.get_scripts(self.sid, 'LOGOFF')
|
||||
self.username = username
|
||||
self.scripts = self.storage.get_scripts(self.sid)
|
||||
self.folder_path = Path(self.__cache_scripts + self.username)
|
||||
self.__module_enabled = check_enabled(self.storage
|
||||
, self.__module_name
|
||||
@ -114,13 +118,14 @@ class scripts_applier_user(applier_frontend):
|
||||
'''
|
||||
self.folder_path.mkdir(parents=True, exist_ok=True)
|
||||
if self.__module_enabled:
|
||||
for ts in self.scripts:
|
||||
if ts.path.split('/')[-4] == 'USER':
|
||||
script_path = (self.__cache_scripts +
|
||||
self.username + '/' +
|
||||
ts.policy_num + '/' +
|
||||
'/'.join(ts.path.split('/')[ts.path.split('/').index('POLICIES')+4:-1]))
|
||||
install_script(ts, script_path, '755')
|
||||
for ts in self.logon_scripts:
|
||||
# FIXME
|
||||
script_path = os.path.join(self.__cache_scripts, self.username)
|
||||
install_script(ts, script_path, '755')
|
||||
for ts in self.logoff_scripts:
|
||||
# FIXME
|
||||
script_path = os.path.join(self.__cache_scripts, self.username)
|
||||
install_script(ts, script_path, '755')
|
||||
|
||||
def user_context_apply(self):
|
||||
pass
|
||||
@ -136,7 +141,7 @@ class scripts_applier_user(applier_frontend):
|
||||
else:
|
||||
log('D159')
|
||||
|
||||
def install_script(storage_script_entry, script_path, access_permissions):
|
||||
def install_script(storage_script_entry, script_dir, access_permissions):
|
||||
'''
|
||||
Copy scripts to specific directories and
|
||||
if given arguments
|
||||
@ -144,13 +149,14 @@ def install_script(storage_script_entry, script_path, access_permissions):
|
||||
'''
|
||||
dir_cr = Path(script_path)
|
||||
dir_cr.mkdir(parents=True, exist_ok=True)
|
||||
script_file = (script_path + '/' +
|
||||
str(int(storage_script_entry.queue)).zfill(5) +
|
||||
'_' + storage_script_entry.path.split('/')[-1])
|
||||
script_name = str(int(storage_script_entry.number)).zfill(5) +
|
||||
'_' + os.path.basename(storage_script_entry.path)
|
||||
script_file = os.path.join(script_dir, script_name)
|
||||
shutil.copyfile(storage_script_entry.path, script_file)
|
||||
|
||||
os.chmod(script_file, int(access_permissions, base = 8))
|
||||
if storage_script_entry.arg:
|
||||
dir_path = script_path + '/' + script_file.split('/')[-1] + '.arg'
|
||||
dir_path = script_path + '/' + script_name + '.arg'
|
||||
dir_arg = Path(dir_path)
|
||||
dir_arg.mkdir(parents=True, exist_ok=True)
|
||||
file_arg = open(dir_path + '/arg', 'w')
|
||||
|
@ -146,23 +146,11 @@ def get_merger(preference_type):
|
||||
return mergers[preference_type]
|
||||
|
||||
class gpt:
|
||||
__policy_num = 0
|
||||
__sid_gpt = str()
|
||||
|
||||
def __init__(self, gpt_path, sid):
|
||||
self.path = gpt_path
|
||||
self.sid = sid
|
||||
self.storage = registry_factory('registry')
|
||||
self.name = ''
|
||||
if not gpt.__sid_gpt:
|
||||
gpt.__sid_gpt = self.sid
|
||||
else:
|
||||
if gpt.__sid_gpt == self.sid:
|
||||
gpt.__policy_num += 1
|
||||
else:
|
||||
gpt.__sid_gpt = self.sid
|
||||
gpt.__policy_num = 0
|
||||
self.policy_num = gpt.__policy_num
|
||||
self.guid = self.path.rpartition('/')[2]
|
||||
if 'default' == self.guid:
|
||||
self.guid = 'Local Policy'
|
||||
@ -221,7 +209,7 @@ class gpt:
|
||||
util.preg.merge_polfile(self.settings['machine']['regpol'], policy_name=self.name)
|
||||
# Merge machine preferences to registry if possible
|
||||
for preference_name, preference_path in self.settings['machine'].items():
|
||||
if preference_path and preference_name != 'scripts':
|
||||
if preference_path:
|
||||
preference_type = get_preftype(preference_path)
|
||||
logdata = dict({'pref': preference_type.value, 'sid': self.sid})
|
||||
log('D28', logdata)
|
||||
@ -229,11 +217,6 @@ class gpt:
|
||||
preference_merger = get_merger(preference_type)
|
||||
preference_objects = preference_parser(preference_path)
|
||||
preference_merger(self.storage, self.sid, preference_objects, self.name)
|
||||
if preference_path and preference_name == 'scripts':
|
||||
logdata = dict({'pref': preference_path, 'sid': self.sid})
|
||||
log('D28', logdata)
|
||||
preference_objects = read_scripts(preference_path)
|
||||
merge_scripts(self.storage, self.sid, preference_objects, self.name, str(self.policy_num).zfill(5))
|
||||
except Exception as exc:
|
||||
logdata = dict()
|
||||
logdata['gpt'] = self.name
|
||||
@ -252,7 +235,7 @@ class gpt:
|
||||
util.preg.merge_polfile(self.settings['user']['regpol'], sid=self.sid, policy_name=self.name)
|
||||
# Merge user preferences to registry if possible
|
||||
for preference_name, preference_path in self.settings['user'].items():
|
||||
if preference_path and preference_name != 'scripts':
|
||||
if preference_path:
|
||||
preference_type = get_preftype(preference_path)
|
||||
logdata = dict({'pref': preference_type.value, 'sid': self.sid})
|
||||
log('D29', logdata)
|
||||
@ -260,11 +243,6 @@ class gpt:
|
||||
preference_merger = get_merger(preference_type)
|
||||
preference_objects = preference_parser(preference_path)
|
||||
preference_merger(self.storage, self.sid, preference_objects, self.name)
|
||||
if preference_path and preference_name == 'scripts':
|
||||
logdata = dict({'pref': preference_path, 'sid': self.sid})
|
||||
log('D29', logdata)
|
||||
preference_objects = read_scripts(preference_path)
|
||||
merge_scripts(self.storage, self.sid, preference_objects, self.name, str(self.policy_num).zfill(5))
|
||||
except Exception as exc:
|
||||
logdata = dict()
|
||||
logdata['gpt'] = self.name
|
||||
|
@ -17,57 +17,120 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import configparser
|
||||
import re
|
||||
import os
|
||||
|
||||
|
||||
def read_scripts(scripts_file):
|
||||
scripts = set()
|
||||
scripts = scripts_lists()
|
||||
|
||||
logon_scripts = dict()
|
||||
logoff_scripts = dict()
|
||||
startup_scripts = dict()
|
||||
shutdown_scripts = dict()
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read(scripts_file, encoding = 'utf-16')
|
||||
scripts_file_dir = os.path.dirname(scripts_file)
|
||||
|
||||
actions = config.sections()
|
||||
for act in actions:
|
||||
count_qu = list()
|
||||
act_upper = act.upper()
|
||||
if act_upper == 'LOGON':
|
||||
section_scripts = logon_scripts
|
||||
elif act_upper == 'LOGOFF':
|
||||
section_scripts = logoff_scripts
|
||||
elif act_upper == 'STARTUP':
|
||||
section_scripts = startup_scripts
|
||||
elif act_upper == 'SHUTDOWN':
|
||||
section_scripts = shutdown_scripts
|
||||
else:
|
||||
continue
|
||||
|
||||
for key in config[act]:
|
||||
qu = re.sub(r'[a-zA-Z]', '', key)
|
||||
if qu not in count_qu:
|
||||
obj_scr = script(act)
|
||||
obj_scr.queue = qu
|
||||
count_qu.append(qu)
|
||||
key_lower = key.lower()
|
||||
key_split = key_lower.split('cmdline')
|
||||
if len(key_split) > 1 and not key_split[1]:
|
||||
if key_split[0].isdigit():
|
||||
key_index = int(key_split[0])
|
||||
section_script[key_index] = script(act, scripts_file_dir, config[act][key])
|
||||
key_split = key_lower.split('parameters')
|
||||
if len(key_split) > 1 and not key_split[1]:
|
||||
if key_split[0].isdigit():
|
||||
key_index = int(key_split[0])
|
||||
script = section_script.get(key_index)
|
||||
if script: script.set_args(config[act][key])
|
||||
|
||||
if key.lower().find('cmdline') != -1 and count_qu:
|
||||
obj_scr.path = '{}{}/{}'.format(
|
||||
scripts_file.removesuffix(scripts_file.split('/')[-1]),
|
||||
act.upper(),
|
||||
config[act][key].upper())
|
||||
if key.lower().find('parameters') != -1 and count_qu:
|
||||
obj_scr.arg = config[act][key]
|
||||
scripts.add(obj_scr)
|
||||
for i in sorted(logon_scripts.keys()):
|
||||
scripts_lists.add_script(logon_scripts[i])
|
||||
for i in sorted(logoff_scripts.keys()):
|
||||
scriptslists.add_script(logoff_scripts[i])
|
||||
for i in sorted(startup_scripts.keys()):
|
||||
scriptslists.add_script(startup_scripts[i])
|
||||
for i in sorted(shutdown_scripts.keys()):
|
||||
scriptslists.add_script(shutdown_scripts[i])
|
||||
|
||||
return list(scripts)
|
||||
return scripts
|
||||
|
||||
def merge_scripts(storage, sid, scripts_objects, policy_name, policy_num):
|
||||
for script in scripts_objects:
|
||||
script.policy_num = policy_num
|
||||
storage.add_script(sid, script, policy_name)
|
||||
def merge_scripts(storage, sid, scripts_objects, policy_name):
|
||||
for script in scripts_objects.get_logon_scripts()
|
||||
storage.add_logon_script(sid, script)
|
||||
for script in scripts_objects.get_logoff_scripts()
|
||||
storage.add_logoff_script(sid, script)
|
||||
for script in scripts_objects.get_startup_scripts()
|
||||
storage.add_startup_script(sid, script)
|
||||
for script in scripts_objects.get_shutdown_scripts()
|
||||
storage.add_shutdown_script(sid, script)
|
||||
|
||||
class scripts_lists:
|
||||
def __init__ (self):
|
||||
self.__logon_scripts = list()
|
||||
self.__logoff_scripts = list()
|
||||
self.__startup_scripts = list()
|
||||
self.__shutdown_scripts = list()
|
||||
|
||||
def get_logon_scripts(self, action):
|
||||
return self.__logon_scripts
|
||||
def get_logoff_scripts(self, action):
|
||||
return self.__logoff_scripts
|
||||
def get_startup_scripts(self, action):
|
||||
return self.__startup_scripts
|
||||
def get_shutdown_scripts(self, action):
|
||||
return self.__shutdown_scripts
|
||||
|
||||
def add_script(self, action, script):
|
||||
self.get_action_list(action).append(script)
|
||||
def add_script(self, action, script):
|
||||
self.get_action_list(action).append(script)
|
||||
def add_script(self, action, script):
|
||||
self.get_action_list(action).append(script)
|
||||
def add_script(self, action, script):
|
||||
self.get_action_list(action).append(script)
|
||||
|
||||
class script:
|
||||
def __init__(self, action):
|
||||
self.action = action
|
||||
self.queue = str()
|
||||
self.policy_num = str()
|
||||
self.path = str()
|
||||
self.arg = str()
|
||||
__logon_counter = 0
|
||||
__logoff_counter = 0
|
||||
__startup_counter = 0
|
||||
__shutdown_counter = 0
|
||||
|
||||
def set_obj(self, qu):
|
||||
if qu is not self.queue:
|
||||
self.queue = qu
|
||||
def __init__(self, action, script_dir, script_filename):
|
||||
action_upper = action.upper()
|
||||
self.action = action_upper
|
||||
self.path = os.path.join(script_dir, action_upper, script_filename.upper())
|
||||
self.args = None
|
||||
|
||||
def add_item(self,qu, data):
|
||||
self.qu[qu].append(data)
|
||||
if action_upper == 'LOGON':
|
||||
self.number = script.__logon_counter
|
||||
script.__logon_counter += 1
|
||||
elif action_upper == 'LOGOFF':
|
||||
self.number = script.__logoff_counter
|
||||
script.__logoff_counter += 1
|
||||
elif action_upper == 'STARTUP':
|
||||
self.number = script.__startup_counter
|
||||
script.__startup_counter += 1
|
||||
elif action_upper == 'SHUTDOWN':
|
||||
self.number = script.__shutdown_counter
|
||||
script.__shutdown_counter += 1
|
||||
|
||||
def set_args(self, args):
|
||||
self.args = args
|
||||
|
||||
def checke_qu(self, qu):
|
||||
if qu in self.qu.keys():
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
@ -182,12 +182,10 @@ class script_entry(object):
|
||||
self.sid = sid
|
||||
self.policy_name = policy_name
|
||||
self.action = scrobj.action
|
||||
self.queue = scrobj.queue
|
||||
self.policy_num = scrobj.policy_num
|
||||
self.number = scrobj.number
|
||||
self.path = scrobj.path
|
||||
self.arg = scrobj.arg
|
||||
|
||||
|
||||
def update_fields(self):
|
||||
'''
|
||||
Return list of fields to update
|
||||
@ -195,8 +193,7 @@ class script_entry(object):
|
||||
fields = dict()
|
||||
fields['policy_name'] = self.policy_name
|
||||
fields['action'] = self.action
|
||||
fields['queue'] = self.queue
|
||||
fields['policy_num'] = self.policy_num
|
||||
fields['number'] = self.number
|
||||
fields['path'] = self.path
|
||||
fields['arg'] = self.arg
|
||||
|
||||
|
@ -150,8 +150,7 @@ class sqlite_registry(registry):
|
||||
, Column('id', Integer, primary_key=True)
|
||||
, Column('sid', String)
|
||||
, Column('policy_name', String)
|
||||
, Column('queue', String)
|
||||
, Column('policy_num', String)
|
||||
, Column('number', String)
|
||||
, Column('action', String)
|
||||
, Column('path', String)
|
||||
, Column('arg', String)
|
||||
@ -427,9 +426,18 @@ class sqlite_registry(registry):
|
||||
def get_envvars(self, sid):
|
||||
return self._filter_sid_list(envvar_entry, sid)
|
||||
|
||||
def get_scripts(self, sid):
|
||||
return self._filter_sid_list(script_entry, sid)
|
||||
def _filter_scripts_list(self, row_object, sid, action):
|
||||
res = (self
|
||||
.db_session
|
||||
.query(row_object)
|
||||
.filter(row_object.sid == sid)
|
||||
.filter(row_object.action == action)
|
||||
.order_by(row_object.id)
|
||||
.all())
|
||||
return res
|
||||
|
||||
def get_scripts(self, sid, action):
|
||||
return self._filter_scripts_list(script_entry, sid, action)
|
||||
|
||||
def get_hkcu_entry(self, sid, hive_key):
|
||||
res = (self
|
||||
|
Loading…
x
Reference in New Issue
Block a user