2016-04-01 14:58:24 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: >
|
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
2016-04-04 18:12:19 +03:00
|
|
|
|
2016-04-01 14:58:24 +03:00
|
|
|
from base.plugin.AbstractCommand import AbstractCommand
|
2016-04-04 18:12:19 +03:00
|
|
|
from base.model.MessageType import MessageType
|
|
|
|
from base.model.MessageCode import MessageCode
|
2016-04-01 14:58:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Browser(AbstractCommand):
|
|
|
|
"""docstring for Browser"""
|
|
|
|
|
|
|
|
def __init__(self, data, context):
|
|
|
|
super(Browser, self).__init__()
|
|
|
|
self.data = data
|
|
|
|
self.context = context
|
|
|
|
self.mozilla_config_file = 'mozilla.cfg'
|
|
|
|
self.local_settings_JS_file = 'local-settings.js'
|
|
|
|
self.local_settings_JS_path = 'defaults/pref/'
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger = self.scope.getLogger()
|
2016-04-01 14:58:24 +03:00
|
|
|
|
|
|
|
def handle_policy(self):
|
|
|
|
|
2016-04-04 18:12:19 +03:00
|
|
|
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')
|
2016-04-03 01:41:10 +03:00
|
|
|
|
2016-04-04 18:12:19 +03:00
|
|
|
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)))
|
2016-04-03 01:41:10 +03:00
|
|
|
|
|
|
|
def write_to_user_profile(self, username):
|
|
|
|
|
|
|
|
try:
|
|
|
|
username = str(username).strip()
|
|
|
|
profile_paths = self.find_user_preference_paths(username)
|
|
|
|
|
|
|
|
# User might have multiple firefox profile directories
|
|
|
|
for path in profile_paths:
|
|
|
|
path = str(path) + '/user.js'
|
2016-04-04 18:12:19 +03:00
|
|
|
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')
|
|
|
|
|
2016-04-03 01:41:10 +03:00
|
|
|
except Exception as e:
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger.error('[Browser] A problem occured while writing user profile: {0}'.format(str(e)))
|
2016-04-03 01:41:10 +03:00
|
|
|
# Remove global lock files to tell Firefox to load the user file
|
2016-04-04 18:12:19 +03:00
|
|
|
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')
|
2016-04-01 14:58:24 +03:00
|
|
|
|
|
|
|
def write_to_global_profile(self):
|
|
|
|
firefox_installation_path = self.find_firefox_installation_path()
|
|
|
|
preferences = json.loads(self.data['preferences'])
|
|
|
|
|
|
|
|
mozilla_cfg = open(str(firefox_installation_path) + self.mozilla_config_file, 'w')
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger.debug('[Browser] Mozilla configuration file is created')
|
2016-04-01 14:58:24 +03:00
|
|
|
for pref in preferences:
|
2016-04-04 18:12:19 +03:00
|
|
|
if pref['value'].isdigit() or str(pref['value']) == 'false' or str(pref['value']) == 'true':
|
2016-04-01 14:58:24 +03:00
|
|
|
value = pref['value']
|
|
|
|
else:
|
2016-04-04 18:12:19 +03:00
|
|
|
value = '\"' + pref['value'] + '\"'
|
2016-04-01 14:58:24 +03:00
|
|
|
line = 'lockPref("' + str(pref['preferenceName']) + '",' + value + ');\n'
|
|
|
|
mozilla_cfg.write(line)
|
|
|
|
mozilla_cfg.close()
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger.debug('[Browser] Preferences were wrote to Mozilla configuration file')
|
2016-04-01 14:58:24 +03:00
|
|
|
|
|
|
|
local_settings_path = str(firefox_installation_path) + self.local_settings_JS_path
|
|
|
|
if not os.path.exists(local_settings_path):
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger.debug('[Browser] Firefox local setting path not found, it will be created')
|
2016-04-01 14:58:24 +03:00
|
|
|
os.makedirs(local_settings_path)
|
|
|
|
local_settings_js = open(local_settings_path + self.local_settings_JS_file, 'w')
|
2016-04-03 01:41:10 +03:00
|
|
|
local_settings_js.write(
|
|
|
|
'pref("general.config.obscure_value", 0);\npref("general.config.filename", "mozilla.cfg");\n')
|
2016-04-01 14:58:24 +03:00
|
|
|
local_settings_js.close()
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger.debug('[Browser] Firefox local settings were configured')
|
2016-04-01 14:58:24 +03:00
|
|
|
|
2016-04-03 01:41:10 +03:00
|
|
|
def silent_remove(self, filename):
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger.debug('[Browser] {0} removed successfully'.format(filename))
|
2016-04-03 01:41:10 +03:00
|
|
|
except OSError as e:
|
2016-04-04 18:12:19 +03:00
|
|
|
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):
|
2016-04-03 01:41:10 +03:00
|
|
|
|
|
|
|
paths = []
|
2016-04-04 18:12:19 +03:00
|
|
|
firefox_path = '/home/' + user_name + '/.mozilla/firefox/'
|
2016-04-03 01:41:10 +03:00
|
|
|
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())
|
2016-04-04 18:12:19 +03:00
|
|
|
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')
|
2016-04-03 01:41:10 +03:00
|
|
|
|
2016-04-01 14:58:24 +03:00
|
|
|
def find_firefox_installation_path(self):
|
|
|
|
installation_path = '/usr/lib/firefox/'
|
|
|
|
if not os.path.exists(installation_path):
|
|
|
|
installation_path = '/opt/firefox/'
|
|
|
|
if not os.path.exists(installation_path):
|
|
|
|
installation_path = '/usr/lib/iceweasel/'
|
|
|
|
if not os.path.exists(installation_path):
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger.error('[Browser] Firefox installation path not found')
|
2016-04-01 14:58:24 +03:00
|
|
|
return None
|
2016-04-04 18:12:19 +03:00
|
|
|
self.logger.debug('[Browser] Firefox installation path found successfully')
|
2016-04-01 14:58:24 +03:00
|
|
|
return installation_path
|
|
|
|
|
2016-04-04 18:12:19 +03:00
|
|
|
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')
|
|
|
|
|
2016-04-01 14:58:24 +03:00
|
|
|
|
|
|
|
def handle_policy(profile_data, context):
|
|
|
|
browser = Browser(profile_data, context)
|
|
|
|
browser.handle_policy()
|
|
|
|
print("This is policy file - BROWSER")
|