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 json
|
2016-02-16 17:50:37 +02:00
|
|
|
|
2016-08-24 18:05:54 +03:00
|
|
|
from base.scope import Scope
|
2016-09-21 12:05:48 +03:00
|
|
|
from base.system.system import System
|
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-09-21 12:05:48 +03:00
|
|
|
scope = Scope().get_instance()
|
|
|
|
self.logger = scope.get_logger()
|
|
|
|
self.conf_manager = scope.get_configuration_manager()
|
|
|
|
self.db_service = scope.get_db_service()
|
|
|
|
self.event_manger = scope.get_event_manager()
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-05-06 16:42:21 +03:00
|
|
|
def missing_plugin_message(self, plugin):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
2016-05-06 16:42:21 +03:00
|
|
|
data['type'] = 'MISSING_PLUGIN'
|
|
|
|
data['pluginName'] = plugin.get_name()
|
|
|
|
data['pluginVersion'] = plugin.get_version()
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('Missing plugin message was created')
|
2016-05-06 16:42:21 +03:00
|
|
|
return str(json_data)
|
|
|
|
|
2016-04-03 00:58:33 +03:00
|
|
|
def task_status_msg(self, response):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
2016-04-03 00:58:33 +03:00
|
|
|
data['type'] = response.get_type()
|
|
|
|
data['taskId'] = response.get_id()
|
|
|
|
data['responseCode'] = response.get_code()
|
|
|
|
data['responseMessage'] = response.get_message()
|
2016-07-11 17:57:37 +03:00
|
|
|
response_data = None
|
2016-07-13 17:30:27 +03:00
|
|
|
if response.get_data() is not None:
|
2016-07-11 17:57:37 +03:00
|
|
|
response_data = json.loads(str(response.get_data()))
|
|
|
|
data['responseData'] = response_data
|
2016-04-03 00:58:33 +03:00
|
|
|
data['contentType'] = response.get_content_type()
|
|
|
|
data['timestamp'] = response.get_timestamp()
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('Task status message was created')
|
2016-04-03 00:58:33 +03:00
|
|
|
return str(json_data)
|
|
|
|
|
|
|
|
def policy_status_msg(self, response):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
2016-04-03 00:58:33 +03:00
|
|
|
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()
|
2016-07-11 17:57:37 +03:00
|
|
|
|
|
|
|
response_data = None
|
2016-07-13 17:30:27 +03:00
|
|
|
if response.get_data() is not None:
|
2016-07-11 17:57:37 +03:00
|
|
|
response_data = json.loads(str(response.get_data()))
|
|
|
|
|
|
|
|
data['responseData'] = response_data
|
2016-04-03 00:58:33 +03:00
|
|
|
data['contentType'] = response.get_content_type()
|
|
|
|
data['timestamp'] = response.get_timestamp()
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('Policy status message was created')
|
2016-04-03 00:58:33 +03:00
|
|
|
return str(json_data)
|
|
|
|
|
2017-08-14 11:17:18 +03:00
|
|
|
def login_msg(self, username,ip=None):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
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()
|
2017-08-14 11:17:18 +03:00
|
|
|
data['userIp'] = ip
|
2016-12-06 11:51:25 +03:00
|
|
|
|
2017-08-14 11:33:30 +03:00
|
|
|
self.logger.debug('USER IP : '+ str(ip)+ ' IPADDRESSES : '+ str(System.Hardware.Network.ip_addresses()).replace('[', '').replace(']', ''))
|
|
|
|
|
|
|
|
|
2016-12-06 11:51:25 +03:00
|
|
|
data['hardware.monitors'] = str(System.Hardware.monitors()),
|
|
|
|
data['hardware.screens'] = str(System.Hardware.screens()),
|
|
|
|
data['hardware.usbDevices'] = str(System.Hardware.usb_devices()),
|
|
|
|
data['hardware.printers'] = str(System.Hardware.printers()),
|
|
|
|
data['hardware.systemDefinitions'] = str(System.Hardware.system_definitions()),
|
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
json_data = json.dumps(data)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('Login message was created')
|
2016-03-07 17:59:02 +02:00
|
|
|
return json_data
|
|
|
|
|
2017-09-12 11:27:41 +03:00
|
|
|
def logout_msg(self, username,ip):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
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()
|
2017-09-12 11:27:41 +03:00
|
|
|
data['userIp'] = ip
|
2016-03-07 17:59:02 +02:00
|
|
|
json_data = json.dumps(data)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('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-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
2016-03-08 18:05:42 +02:00
|
|
|
data['type'] = 'GET_POLICIES'
|
2016-03-30 17:34:10 +03:00
|
|
|
|
2016-09-21 12:05:48 +03:00
|
|
|
user_policy_number = self.db_service.select_one_result('policy', 'version',
|
|
|
|
'type = \'U\' and name = \'' + username + '\'')
|
2016-03-30 17:34:10 +03:00
|
|
|
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)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('Get Policies message was created')
|
2016-03-11 17:39:32 +02:00
|
|
|
return json_data
|
|
|
|
|
|
|
|
def registration_msg(self):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
2016-03-11 17:39:32 +02:00
|
|
|
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)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('Registration message was created')
|
2016-03-11 17:39:32 +02:00
|
|
|
return json_data
|
|
|
|
|
|
|
|
def ldap_registration_msg(self):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
2016-03-11 17:39:32 +02:00
|
|
|
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-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('LDAP Registration message was created')
|
2016-03-07 17:59:02 +02:00
|
|
|
return json_data
|
|
|
|
|
|
|
|
def unregister_msg(self):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
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-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('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):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
2016-06-27 17:14:11 +03:00
|
|
|
data['type'] = 'REQUEST_AGREEMENT'
|
2016-06-30 17:04:44 +03:00
|
|
|
|
|
|
|
"""
|
2016-06-27 17:14:11 +03:00
|
|
|
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-30 17:04:44 +03:00
|
|
|
"""
|
|
|
|
|
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)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('Agreement request message was created')
|
2016-06-27 17:14:11 +03:00
|
|
|
return json_data
|
2016-06-28 12:24:02 +03:00
|
|
|
|
|
|
|
def agreement_answer_msg(self, username, answer):
|
2016-08-24 18:05:54 +03:00
|
|
|
data = dict()
|
2016-06-29 12:39:18 +03:00
|
|
|
data['type'] = 'AGREEMENT_STATUS'
|
2016-06-28 12:24:02 +03:00
|
|
|
data['username'] = username
|
2016-06-30 17:04:44 +03:00
|
|
|
data['accepted'] = answer
|
2016-06-28 12:24:02 +03:00
|
|
|
data['timestamp'] = Util.timestamp()
|
2016-09-21 12:05:48 +03:00
|
|
|
contract_content = self.db_service.select_one_result('contract', 'content',
|
|
|
|
'id =(select MAX(id) from contract)')
|
2016-06-28 12:24:02 +03:00
|
|
|
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)
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.debug('Agreement answer message was created')
|
2016-06-28 12:24:02 +03:00
|
|
|
return json_data
|