Ahenk/opt/ahenk/plugins/browser/policy.py

122 lines
4.5 KiB
Python
Raw Normal View History

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
from base.plugin.AbstractCommand import AbstractCommand
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/'
def handle_policy(self):
print("Browser handling")
2016-04-03 01:41:10 +03:00
username = None
if username is not None:
self.write_to_user_profile(username)
else:
self.write_to_global_profile()
#prefsText -lockPrefsText
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'
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)
except Exception as e:
print('ERR1')
# 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)
2016-04-01 14:58:24 +03:00
def write_to_global_profile(self):
2016-04-03 01:41:10 +03:00
# TODO NEED DEBUG
2016-04-01 14:58:24 +03:00
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)
for pref in preferences:
2016-04-03 01:41:10 +03:00
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':
2016-04-01 14:58:24 +03:00
value = pref['value']
else:
value = '"' + pref['value'] + '"'
line = 'lockPref("' + str(pref['preferenceName']) + '",' + value + ');\n'
mozilla_cfg.write(line)
mozilla_cfg.close()
print("close")
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):
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-03 01:41:10 +03:00
def silent_remove(self, filename):
try:
os.remove(filename)
except OSError as e:
print('ERR2')
def find_user_preference_paths(self, userName):
paths = []
firefox_path = '/home/' + userName + '/.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
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):
return None
return installation_path
def handle_policy(profile_data, context):
browser = Browser(profile_data, context)
browser.handle_policy()
print("This is policy file - BROWSER")