From 550cad1804be4e8bfaad4fa259007a68917c2a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Mon, 4 Apr 2016 18:12:19 +0300 Subject: [PATCH] browser plugin implemented user-ahenk --- opt/ahenk/plugins/browser/policy.py | 106 ++++++++++++++++++---------- 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/opt/ahenk/plugins/browser/policy.py b/opt/ahenk/plugins/browser/policy.py index a002abd..9946e1f 100644 --- a/opt/ahenk/plugins/browser/policy.py +++ b/opt/ahenk/plugins/browser/policy.py @@ -5,7 +5,10 @@ import json import os + from base.plugin.AbstractCommand import AbstractCommand +from base.model.MessageType import MessageType +from base.model.MessageCode import MessageCode class Browser(AbstractCommand): @@ -18,18 +21,26 @@ class Browser(AbstractCommand): self.mozilla_config_file = 'mozilla.cfg' self.local_settings_JS_file = 'local-settings.js' self.local_settings_JS_path = 'defaults/pref/' + self.logger = self.scope.getLogger() def handle_policy(self): - print("Browser handling") + self.logger.info('[Browser] Browser plugin handling...') + try: + username = self.context.get('username') + if username is not None: + self.logger.debug('[Browser] Writing preferences to user profile') + self.write_to_user_profile(username) + self.set_result(MessageType.POLICY_STATUS, MessageCode.POLICY_PROCESSED, 'User browser profile processed successfully') - username = None - if username is not None: - self.write_to_user_profile(username) - else: - self.write_to_global_profile() - - #prefsText -lockPrefsText + else: + self.logger.debug('[Browser] Writing preferences to global profile') + self.write_to_global_profile() + self.set_result(MessageType.POLICY_STATUS, MessageCode.POLICY_PROCESSED, 'Agent browser profile processed successfully') + self.logger.info('[Browser] Browser profile is handled successfully') + except Exception as e: + self.logger.error('[Browser] A problem occured while handling browser profile: {0}'.format(str(e))) + self.set_result(MessageType.POLICY_STATUS, MessageCode.POLICY_ERROR, 'A problem occured while handling browser profile: {0}'.format(str(e))) def write_to_user_profile(self, username): @@ -40,69 +51,81 @@ class Browser(AbstractCommand): # User might have multiple firefox profile directories for path in profile_paths: path = str(path) + '/user.js' - userjss = open(path, 'w') - userjss.write("prefsText") - userjss.close() - changeOwner = 'chown ' + username + ':' + username + ' ' + path - #TODO(result_code, p_out, p_err) = self.execute_command(changeOwner, shell=True) + user_jss = open(path, 'w') + preferences = json.loads(self.data['preferences']) + self.logger.debug('[Browser] Writing preferences to user.js file ...') + for pref in preferences: + if pref['value'].isdigit() or str(pref['value']) == 'false' or str(pref['value']) == 'true': + value = pref['value'] + else: + value = '\"' + pref['value'] + '\"' + line = 'user_pref("' + str(pref['preferenceName']) + '",' + value + ');\n' + user_jss.write(line) + + self.logger.debug('[Browser] User preferences were wrote successfully') + user_jss.close() + change_owner = 'chown ' + username + ':' + username + ' ' + path + self.context.execute(change_owner) + self.logger.debug('[Browser] Preferences file owner is changed') + except Exception as e: - print('ERR1') + self.logger.error('[Browser] A problem occured while writing user profile: {0}'.format(str(e))) # Remove global lock files to tell Firefox to load the user file - installation_path = self.find_firefox_installation_path() - if installation_path is None: - return - self.silent_remove(str(installation_path) + self.mozilla_config_file) - self.silent_remove(str(installation_path) + self.local_settings_JS_path + self.local_settings_JS_file) + installation_path = self.find_firefox_installation_path() + if installation_path is None: + self.logger.error('[Browser] Firefox installation directory could not be found! Finishing task...') + return + self.silent_remove(str(installation_path) + self.mozilla_config_file) + self.silent_remove(str(installation_path) + self.local_settings_JS_path + self.local_settings_JS_file) + self.logger.debug('[Browser] User profiles have been set successfully') def write_to_global_profile(self): - # TODO NEED DEBUG firefox_installation_path = self.find_firefox_installation_path() preferences = json.loads(self.data['preferences']) - print(str(firefox_installation_path)) - print("W1111" + str(firefox_installation_path) + self.mozilla_config_file) - mozilla_cfg = open(str(firefox_installation_path) + self.mozilla_config_file, 'w') - print(str(firefox_installation_path) + self.mozilla_config_file) - + self.logger.debug('[Browser] Mozilla configuration file is created') for pref in preferences: - if isinstance(pref['value'], int) is True or isinstance(pref['value'], bool) is True or str( - pref['value']) is 'false' or str(pref['value']) is 'true': + if pref['value'].isdigit() or str(pref['value']) == 'false' or str(pref['value']) == 'true': value = pref['value'] else: - value = '"' + pref['value'] + '"' + value = '\"' + pref['value'] + '\"' line = 'lockPref("' + str(pref['preferenceName']) + '",' + value + ');\n' mozilla_cfg.write(line) mozilla_cfg.close() - - print("close") + self.logger.debug('[Browser] Preferences were wrote to Mozilla configuration file') local_settings_path = str(firefox_installation_path) + self.local_settings_JS_path - - print("W2222" + local_settings_path) if not os.path.exists(local_settings_path): + self.logger.debug('[Browser] Firefox local setting path not found, it will be created') os.makedirs(local_settings_path) - local_settings_js = open(local_settings_path + self.local_settings_JS_file, 'w') local_settings_js.write( 'pref("general.config.obscure_value", 0);\npref("general.config.filename", "mozilla.cfg");\n') local_settings_js.close() + self.logger.debug('[Browser] Firefox local settings were configured') def silent_remove(self, filename): try: os.remove(filename) + self.logger.debug('[Browser] {0} removed successfully'.format(filename)) except OSError as e: - print('ERR2') + self.logger.error('[Browser] Problem occured while removing file: {0}. Exception is: {1}'.format(filename, str(e))) + + def find_user_preference_paths(self, user_name): - def find_user_preference_paths(self, userName): paths = [] - firefox_path = '/home/' + userName + '/.mozilla/firefox/' + firefox_path = '/home/' + user_name + '/.mozilla/firefox/' profile_ini_file = open(firefox_path + 'profiles.ini', 'r') profile_ini_file_lines = profile_ini_file.readlines() for line in profile_ini_file_lines: if 'Path' in line: paths.append(firefox_path + str(line.split('=')[1]).strip()) - return paths + if len(paths) > 0: + self.logger.debug('[Browser] User preferences path found successfully') + return paths + else: + self.logger.error('[Browser] User preferences path not found') def find_firefox_installation_path(self): installation_path = '/usr/lib/firefox/' @@ -111,9 +134,18 @@ class Browser(AbstractCommand): if not os.path.exists(installation_path): installation_path = '/usr/lib/iceweasel/' if not os.path.exists(installation_path): + self.logger.error('[Browser] Firefox installation path not found') return None + self.logger.debug('[Browser] Firefox installation path found successfully') return installation_path + def set_result(self, type=None, code=None, message=None, data=None, content_type=None): + self.context.put('message_type', type) + self.context.put('message_code', code) + self.context.put('message', message) + # self.context.put('data') + # self.context.put('content_type') + def handle_policy(profile_data, context): browser = Browser(profile_data, context)