This commit is contained in:
emrekgn 2016-07-29 19:07:33 +03:00
commit cb18b1b7a6
6 changed files with 55 additions and 59 deletions

2
debian/control vendored
View file

@ -8,5 +8,5 @@ Homepage: http://www.liderahenk.org.tr
Package: ahenk Package: ahenk
Architecture: any Architecture: any
Depends:python3 (>= 3), cython, libidn11, libidn11-dev, python3-dev, python3-pip, libffi-dev, libssl-dev, python3-paramiko, python3-cpuinfo, python3-psutil (>= 4), libpam-script, python3-sleekxmpp, python3-easygui Depends:python3 (>= 3), cython, libidn11, libidn11-dev, python3-dev, python3-pip, python3-watchdog, libffi-dev, libssl-dev, python3-paramiko, python3-cpuinfo, python3-psutil (>= 4), libpam-script, python3-sleekxmpp, python3-easygui
Description: Lider Ahenk is an open source project which provides solutions to manage, monitor and audit unlimited number of different systems and users on a network. Description: Lider Ahenk is an open source project which provides solutions to manage, monitor and audit unlimited number of different systems and users on a network.

View file

@ -124,13 +124,9 @@ class AhenkDeamon(BaseDaemon):
try: try:
while registration.is_registered() is False: while registration.is_registered() is False:
# while Scope.getInstance().getRegistration().is_registered() is False:
max_attemp_number -= 1 max_attemp_number -= 1
logger.debug('[AhenkDeamon] Ahenk is not registered. Attempting for registration') logger.debug('[AhenkDeamon] Ahenk is not registered. Attempting for registration')
# TODO 'Could not reach Registration response from Lider. Be sure Lider is running and it is connected to XMPP server!'
registration.registration_request() registration.registration_request()
# Scope.getInstance().getRegistration().registration_request()
if max_attemp_number < 0: if max_attemp_number < 0:
logger.warning('[AhenkDeamon] Number of Attempting for registration is over') logger.warning('[AhenkDeamon] Number of Attempting for registration is over')
self.registration_failed() self.registration_failed()
@ -258,16 +254,21 @@ class AhenkDeamon(BaseDaemon):
def run_command_from_fifo(self, num, stack): def run_command_from_fifo(self, num, stack):
json_data = json.loads(Commander().get_event()) scope = Scope().getInstance()
plugin_manager = scope.getPluginManager()
message_manager = scope.getMessageManager()
messenger = scope.getMessenger()
conf_manager = scope.getConfigurationManager()
db_service = scope.getDbService()
execute_manager = scope.getExecutionManager()
try:
json_data = json.loads(Commander().get_event())
except Exception as e:
self.logger.error('[AhenkDeamon] A problem occurred while loading json. Check json format! Error Message: {0}'.format(str(e)))
return
if json_data is not None: if json_data is not None:
scope = Scope().getInstance()
plugin_manager = scope.getPluginManager()
message_manager = scope.getMessageManager()
messenger = scope.getMessenger()
conf_manager = scope.getConfigurationManager()
db_service = scope.getDbService()
execute_manager = scope.getExecutionManager()
self.logger.debug('[AhenkDeamon] Signal handled') self.logger.debug('[AhenkDeamon] Signal handled')
self.logger.debug('[AhenkDeamon] Signal is :{}'.format(str(json_data['event']))) self.logger.debug('[AhenkDeamon] Signal is :{}'.format(str(json_data['event'])))
@ -351,8 +352,8 @@ class AhenkDeamon(BaseDaemon):
plugin_manager.process_mode('safe', username) plugin_manager.process_mode('safe', username)
elif 'send' == str(json_data['event']): elif 'send' == str(json_data['event']):
self.logger.info('[AhenkDeamon] Sending message over ahenkd command. Response Message: {}'.format(str(json_data['message']))) self.logger.info('[AhenkDeamon] Sending message over ahenkd command. Response Message: {}'.format(json.dumps(json_data['message'])))
message = str(json.dumps(json_data['message'])) message = json.dumps(json_data['message'])
messenger.send_direct_message(message) messenger.send_direct_message(message)
elif 'load' == str(json_data['event']): elif 'load' == str(json_data['event']):

View file

@ -4,6 +4,7 @@ import json
import os import os
import queue as Queue import queue as Queue
import threading import threading
import ast
from base.command.fifo import Fifo from base.command.fifo import Fifo
from base.model.enum.ContentType import ContentType from base.model.enum.ContentType import ContentType
@ -71,7 +72,7 @@ class Commander(object):
response['type'] = MessageType.TASK_STATUS.value response['type'] = MessageType.TASK_STATUS.value
response['taskId'] = params[3] response['taskId'] = params[3]
if params[4] == '-m': if params[4] == '-m':
response['contentType'] = ContentType.TEXT_PLAIN.value response['contentType'] = ContentType.APPLICATION_JSON.value
response['responseData'] = params[5] response['responseData'] = params[5]
elif params[4] == '-f': elif params[4] == '-f':
if os.path.exists(str(params[5])): if os.path.exists(str(params[5])):
@ -101,7 +102,7 @@ class Commander(object):
response['commandExecutionId'] = params[5] response['commandExecutionId'] = params[5]
if params[6] == '-m': if params[6] == '-m':
response['contentType'] = ContentType.TEXT_PLAIN.value response['contentType'] = ContentType.APPLICATION_JSON.value
response['responseData'] = params[7] response['responseData'] = params[7]
elif params[6] == '-f': elif params[6] == '-f':
if os.path.exists(str(params[7])): if os.path.exists(str(params[7])):
@ -125,13 +126,12 @@ class Commander(object):
else: else:
print('Wrong or missing parameter. Usage: send -p <policy_version> -c <command_execution_id> -m|-f <message_content>|<file_path> -e|-s|-w') print('Wrong or missing parameter. Usage: send -p <policy_version> -c <command_execution_id> -m|-f <message_content>|<file_path> -e|-s|-w')
return None return None
print('RESPONSE=' + str(response).replace("'", '"'))
data['message'] = json.loads(str(response).replace("'", '"')) data['message'] = ast.literal_eval(str(response))
else: else:
print('Wrong or missing parameter. Usage : %s start|stop|restart|status|clean|send') print('Wrong or missing parameter. Usage : %s start|stop|restart|status|clean|send')
return None return None
else: else:
if params[1] == 'clean': if params[1] == 'clean':
@ -196,7 +196,7 @@ class Commander(object):
if os.path.exists(db_path): if os.path.exists(db_path):
os.remove(db_path) os.remove(db_path)
#TODO remove pid file # TODO remove pid file
config.set('CONNECTION', 'uid', '') config.set('CONNECTION', 'uid', '')

View file

@ -72,7 +72,7 @@ class Plugin(threading.Thread):
if obj_name == "TASK": if obj_name == "TASK":
self.logger.debug('[Plugin] Executing task') self.logger.debug('[Plugin] Executing task')
command = Scope.getInstance().getPluginManager().find_command(self.getName(), item_obj.get_command_cls_id().lower()) command = Scope.getInstance().getPluginManager().find_command(self.getName(), item_obj.get_command_cls_id().lower())
self.context.put('task_id', item_obj.get_command_cls_id().lower()) self.context.put('task_id', item_obj.get_id())
task_data = item_obj.get_parameter_map() task_data = item_obj.get_parameter_map()
self.logger.debug('[Plugin] Handling task') self.logger.debug('[Plugin] Handling task')

View file

@ -2,44 +2,39 @@ import os
import signal import signal
import time import time
# from watchdog.events import FileSystemEventHandler from watchdog.events import FileSystemEventHandler
# from watchdog.observers import Observer from watchdog.observers import Observer
from base.command.commander import Commander from base.command.commander import Commander
from base.system.system import System from base.system.system import System
"""
class FileEventHandler(FileSystemEventHandler): class FileEventHandler(FileSystemEventHandler):
def __init__(self, plugin_path): def __init__(self, plugin_path):
self.path = plugin_path self.path = plugin_path
def process(self, event): def process(self, event):
if event.event_type == 'created':
result = Commander().set_event([None, 'load', '-p', event.src_path.replace(self.path, '')]) if event.src_path != self.path[:-1]:
if result is True: if event.event_type in ('created', 'modified', 'moved'):
if System.Ahenk.is_running() is True: plu_path = event.src_path
if event.event_type == 'moved':
plu_path = event.dest_path
result = Commander().set_event([None, 'load', '-p', plu_path.replace(self.path, '')])
if result is True and System.Ahenk.is_running() is True:
os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
elif event.event_type == 'deleted':
result = Commander().set_event([None, 'remove', '-p', event.src_path.replace(self.path, '')])
if result is True and System.Ahenk.is_running() is True:
os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM) os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
elif event.event_type == 'deleted':
# TODO
print('plugin removed')
def on_created(self, event): def on_any_event(self, event):
if event.is_directory: if event.is_directory:
self.process(event) self.process(event)
def on_deleted(self, event):
if event.is_directory:
self.process(event)
def on_modified(self,event):
print("MODIFIED-"+str(event.src_path))
"""
class PluginInstallListener: class PluginInstallListener:
def listen(self, path): def listen(self, path):
pass
"""
observer = Observer() observer = Observer()
event_handler = FileEventHandler(path) event_handler = FileEventHandler(path)
observer.schedule(event_handler, path, recursive=False) observer.schedule(event_handler, path, recursive=False)
@ -50,4 +45,3 @@ class PluginInstallListener:
except KeyboardInterrupt: except KeyboardInterrupt:
observer.stop() observer.stop()
observer.join() observer.join()
"""

View file

@ -4,7 +4,8 @@
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com> # Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import imp import imp
import os import os
import threading from multiprocessing import Process
from base.Scope import Scope from base.Scope import Scope
from base.model.PluginBean import PluginBean from base.model.PluginBean import PluginBean
from base.model.modes.init_mode import InitMode from base.model.modes.init_mode import InitMode
@ -14,6 +15,8 @@ from base.model.modes.safe_mode import SafeMode
from base.model.modes.shutdown_mode import ShutdownMode from base.model.modes.shutdown_mode import ShutdownMode
from base.plugin.Plugin import Plugin from base.plugin.Plugin import Plugin
from base.plugin.PluginQueue import PluginQueue from base.plugin.PluginQueue import PluginQueue
from base.plugin.plugin_install_listener import PluginInstallListener
from base.system.system import System
# TODO create base abstract class # TODO create base abstract class
@ -31,7 +34,7 @@ class PluginManager(object):
self.plugins = [] self.plugins = []
self.pluginQueueDict = dict() self.pluginQueueDict = dict()
# self.listener = self.install_listener() self.listener = self.install_listener()
self.delayed_profiles = {} self.delayed_profiles = {}
self.delayed_tasks = {} self.delayed_tasks = {}
@ -177,18 +180,18 @@ class PluginManager(object):
if ahenk_profiles is not None: if ahenk_profiles is not None:
self.logger.info('[PluginManager] Working on Ahenk profiles...') self.logger.info('[PluginManager] Working on Ahenk profiles...')
for agent_profile in ahenk_profiles: for agent_profile in ahenk_profiles:
same_plugin_profile = None
for usr_profile in user_profiles:
if usr_profile.plugin.name == agent_profile.plugin.name:
same_plugin_profile = usr_profile
if agent_profile.overridable.lower() != 'true': if same_plugin_profile is not None:
temp_list = [] if agent_profile.overridable.lower() == 'true':
for usr_profile in user_profiles: self.logger.debug('[PluginManager] Agent profile of {0} plugin will not executed because of profile override rules.'.format(agent_profile.plugin.name))
if usr_profile.plugin.name != agent_profile.plugin.name: continue
temp_list.append(usr_profile) else:
else: self.logger.warning('[PluginManager] User profile of {0} plugin will not executed because of profile override rules.'.format(agent_profile.plugin.name))
self.logger.debug('[PluginManager] User profile of {0} plugin will not executed because of profile override rules.'.format(agent_profile.plugin.name)) user_profiles.remove(same_plugin_profile)
user_profiles = temp_list
else:
self.logger.debug('[PluginManager] Agent profile of {0} plugin will not executed because of profile override rules.'.format(agent_profile.plugin.name))
continue
agent_profile.set_username(None) agent_profile.set_username(None)
self.process_profile(agent_profile) self.process_profile(agent_profile)
@ -261,13 +264,11 @@ class PluginManager(object):
self.logger.warning('[PluginManager] {0} not found in {1} plugin'.format((mode + '.py'), plugin_name)) self.logger.warning('[PluginManager] {0} not found in {1} plugin'.format((mode + '.py'), plugin_name))
return None return None
"""
def install_listener(self): def install_listener(self):
listener = PluginInstallListener() listener = PluginInstallListener()
thread = Process(target=listener.listen, args=(System.Ahenk.plugins_path(),)) thread = Process(target=listener.listen, args=(System.Ahenk.plugins_path(),))
thread.start() thread.start()
return thread return thread
"""
def is_plugin_loaded(self, plugin_name): def is_plugin_loaded(self, plugin_name):
try: try: