2016-02-16 17:50:37 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2016-03-07 17:59:02 +02:00
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
2016-03-23 17:15:27 +02:00
|
|
|
import datetime
|
|
|
|
import json
|
2016-02-16 17:50:37 +02:00
|
|
|
|
2016-06-27 17:36:12 +03:00
|
|
|
from base.system.system import System
|
2016-03-07 17:59:02 +02:00
|
|
|
from base.Scope import Scope
|
2016-06-27 17:14:11 +03:00
|
|
|
from base.util.util import Util
|
2016-02-16 17:50:37 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
# TODO Message Factory
|
2016-03-11 17:39:32 +02:00
|
|
|
class Messaging(object):
|
2016-02-21 03:24:54 +02:00
|
|
|
def __init__(self):
|
2016-03-07 17:59:02 +02:00
|
|
|
scope = Scope().getInstance()
|
|
|
|
self.logger = scope.getLogger()
|
2016-03-11 17:39:32 +02:00
|
|
|
self.conf_manager = scope.getConfigurationManager()
|
2016-03-23 17:15:27 +02:00
|
|
|
self.db_service = scope.getDbService()
|
2016-03-11 17:39:32 +02:00
|
|
|
self.event_manger = scope.getEventManager()
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-05-06 16:42:21 +03:00
|
|
|
def missing_plugin_message(self, plugin):
|
|
|
|
data = {}
|
|
|
|
data['type'] = 'MISSING_PLUGIN'
|
|
|
|
data['pluginName'] = plugin.get_name()
|
|
|
|
data['pluginVersion'] = plugin.get_version()
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
self.logger.debug('[Messaging]Missing plugin message was created')
|
|
|
|
return str(json_data)
|
|
|
|
|
2016-04-03 00:58:33 +03:00
|
|
|
def task_status_msg(self, response):
|
|
|
|
data = {}
|
|
|
|
data['type'] = response.get_type()
|
|
|
|
data['taskId'] = response.get_id()
|
|
|
|
data['responseCode'] = response.get_code()
|
|
|
|
data['responseMessage'] = response.get_message()
|
|
|
|
data['responseData'] = response.get_data()
|
|
|
|
data['contentType'] = response.get_content_type()
|
|
|
|
data['timestamp'] = response.get_timestamp()
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
self.logger.debug('[Messaging] Task status message was created')
|
|
|
|
return str(json_data)
|
|
|
|
|
|
|
|
def policy_status_msg(self, response):
|
|
|
|
data = {}
|
|
|
|
data['type'] = response.get_type()
|
2016-04-12 17:31:38 +03:00
|
|
|
data['policyVersion'] = response.get_policy_version()
|
|
|
|
data['commandExecutionId'] = response.get_execution_id()
|
2016-04-03 00:58:33 +03:00
|
|
|
data['responseCode'] = response.get_code()
|
|
|
|
data['responseMessage'] = response.get_message()
|
|
|
|
data['responseData'] = response.get_data()
|
|
|
|
data['contentType'] = response.get_content_type()
|
|
|
|
data['timestamp'] = response.get_timestamp()
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
self.logger.debug('[Messaging] Policy status message was created')
|
|
|
|
return str(json_data)
|
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def login_msg(self, username):
|
2016-03-07 17:59:02 +02:00
|
|
|
data = {}
|
2016-03-08 18:05:42 +02:00
|
|
|
data['type'] = 'LOGIN'
|
2016-03-22 16:43:47 +02:00
|
|
|
data['username'] = username
|
2016-06-27 17:36:12 +03:00
|
|
|
data['ipAddresses'] = str(System.Hardware.Network.ip_addresses()).replace('[', '').replace(']', '')
|
|
|
|
data['timestamp'] = Util.timestamp()
|
2016-03-07 17:59:02 +02:00
|
|
|
json_data = json.dumps(data)
|
2016-03-11 17:39:32 +02:00
|
|
|
self.logger.debug('[Messaging] Login message was created')
|
2016-03-07 17:59:02 +02:00
|
|
|
return json_data
|
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def logout_msg(self, username):
|
2016-03-07 17:59:02 +02:00
|
|
|
data = {}
|
2016-03-08 18:05:42 +02:00
|
|
|
data['type'] = 'LOGOUT'
|
2016-03-22 16:43:47 +02:00
|
|
|
data['username'] = str(username)
|
2016-06-27 17:36:12 +03:00
|
|
|
data['timestamp'] = Util.timestamp()
|
2016-03-07 17:59:02 +02:00
|
|
|
json_data = json.dumps(data)
|
2016-03-11 17:39:32 +02:00
|
|
|
self.logger.debug('[Messaging] Logout message was created')
|
2016-03-07 17:59:02 +02:00
|
|
|
return json_data
|
|
|
|
|
2016-03-30 17:34:10 +03:00
|
|
|
def policy_request_msg(self, username):
|
2016-03-07 17:59:02 +02:00
|
|
|
data = {}
|
2016-03-08 18:05:42 +02:00
|
|
|
data['type'] = 'GET_POLICIES'
|
2016-03-30 17:34:10 +03:00
|
|
|
|
|
|
|
user_policy_number = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \'' + username + '\'')
|
|
|
|
machine_policy_number = self.db_service.select_one_result('policy', 'version', 'type = \'A\'')
|
|
|
|
|
|
|
|
data['userPolicyVersion'] = user_policy_number
|
2016-04-03 00:58:33 +03:00
|
|
|
data['agentPolicyVersion'] = machine_policy_number
|
2016-03-30 17:34:10 +03:00
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
data['username'] = str(username)
|
2016-06-27 17:36:12 +03:00
|
|
|
data['timestamp'] = Util.timestamp()
|
2016-03-11 17:39:32 +02:00
|
|
|
json_data = json.dumps(data)
|
|
|
|
self.logger.debug('[Messaging] Get Policies message was created')
|
|
|
|
return json_data
|
|
|
|
|
|
|
|
def registration_msg(self):
|
|
|
|
data = {}
|
|
|
|
data['type'] = 'REGISTER'
|
2016-05-12 17:33:33 +03:00
|
|
|
data['from'] = self.db_service.select_one_result('registration', 'jid', ' 1=1')
|
2016-05-06 16:42:21 +03:00
|
|
|
data['password'] = self.db_service.select_one_result('registration', 'password', ' 1=1')
|
2016-05-12 17:33:33 +03:00
|
|
|
|
2016-05-06 16:42:21 +03:00
|
|
|
params = self.db_service.select_one_result('registration', 'params', ' 1=1')
|
2016-05-18 14:55:52 +03:00
|
|
|
data['data'] = json.loads(str(params))
|
2016-04-08 15:54:26 +03:00
|
|
|
json_params = json.loads(str(params))
|
|
|
|
data['macAddresses'] = json_params['macAddresses']
|
|
|
|
data['ipAddresses'] = json_params['ipAddresses']
|
|
|
|
data['hostname'] = json_params['hostname']
|
2016-05-12 17:33:33 +03:00
|
|
|
|
2016-05-06 16:42:21 +03:00
|
|
|
data['timestamp'] = self.db_service.select_one_result('registration', 'timestamp', ' 1=1')
|
2016-03-11 17:39:32 +02:00
|
|
|
json_data = json.dumps(data)
|
|
|
|
self.logger.debug('[Messaging] Registration message was created')
|
|
|
|
return json_data
|
|
|
|
|
|
|
|
def ldap_registration_msg(self):
|
|
|
|
data = {}
|
|
|
|
data['type'] = 'REGISTER_LDAP'
|
|
|
|
data['from'] = str(self.conf_manager.get('REGISTRATION', 'from'))
|
|
|
|
data['password'] = str(self.conf_manager.get('REGISTRATION', 'password'))
|
|
|
|
data['macAddresses'] = str(self.conf_manager.get('REGISTRATION', 'macAddresses'))
|
|
|
|
data['ipAddresses'] = str(self.conf_manager.get('REGISTRATION', 'ipAddresses'))
|
|
|
|
data['hostname'] = str(self.conf_manager.get('REGISTRATION', 'hostname'))
|
2016-06-27 17:36:12 +03:00
|
|
|
data['timestamp'] = Util.timestamp()
|
2016-03-07 17:59:02 +02:00
|
|
|
json_data = json.dumps(data)
|
2016-03-11 17:39:32 +02:00
|
|
|
self.logger.debug('[Messaging] LDAP Registration message was created')
|
2016-03-07 17:59:02 +02:00
|
|
|
return json_data
|
|
|
|
|
|
|
|
def unregister_msg(self):
|
|
|
|
data = {}
|
2016-03-08 18:05:42 +02:00
|
|
|
data['type'] = 'UNREGISTER'
|
2016-03-11 17:39:32 +02:00
|
|
|
data['from'] = str(self.conf_manager.get('REGISTRATION', 'from'))
|
|
|
|
data['password'] = str(self.conf_manager.get('REGISTRATION', 'password'))
|
|
|
|
data['macAddresses'] = str(self.conf_manager.get('REGISTRATION', 'macAddresses'))
|
|
|
|
data['ipAddresses'] = str(self.conf_manager.get('REGISTRATION', 'ipAddresses'))
|
|
|
|
data['hostname'] = str(self.conf_manager.get('REGISTRATION', 'hostname'))
|
|
|
|
# data['username'] = str(pwd.getpwuid( os.getuid() )[ 0 ])
|
2016-06-27 17:36:12 +03:00
|
|
|
data['timestamp'] = Util.timestamp()
|
2016-03-07 17:59:02 +02:00
|
|
|
json_data = json.dumps(data)
|
2016-03-11 17:39:32 +02:00
|
|
|
self.logger.debug('[Messaging] Unregister message was created')
|
2016-03-07 17:59:02 +02:00
|
|
|
return json_data
|
2016-06-27 17:14:11 +03:00
|
|
|
|
|
|
|
def agreement_request_msg(self):
|
|
|
|
data = {}
|
|
|
|
data['type'] = 'REQUEST_AGREEMENT'
|
|
|
|
contract_content = self.db_service.select_one_result('contract', 'content', 'id =(select MAX(id) from contract)')
|
|
|
|
if contract_content is not None and contract_content != '':
|
|
|
|
data['md5'] = Util.get_md5_text(contract_content)
|
|
|
|
else:
|
|
|
|
data['md5'] = ''
|
2016-06-27 17:36:12 +03:00
|
|
|
data['timestamp'] = Util.timestamp()
|
2016-06-27 17:14:11 +03:00
|
|
|
json_data = json.dumps(data)
|
|
|
|
self.logger.debug('[Messaging] Agreement request message was created')
|
|
|
|
return json_data
|
2016-06-28 12:24:02 +03:00
|
|
|
|
|
|
|
def agreement_answer_msg(self, username, answer):
|
|
|
|
data = {}
|
|
|
|
data['type'] = 'AGREEMENT_ANSWER'
|
|
|
|
data['username'] = username
|
|
|
|
data['answer'] = str(answer).upper()
|
|
|
|
data['timestamp'] = Util.timestamp()
|
|
|
|
contract_content = self.db_service.select_one_result('contract', 'content', 'id =(select MAX(id) from contract)')
|
|
|
|
if contract_content is not None and contract_content != '':
|
|
|
|
data['md5'] = Util.get_md5_text(contract_content)
|
|
|
|
else:
|
|
|
|
data['md5'] = ''
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
self.logger.debug('[Messaging] Agreement answer message was created')
|
|
|
|
return json_data
|