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-11 17:39:32 +02:00
|
|
|
import sys, pwd, os, datetime, json
|
2016-02-16 17:50:37 +02:00
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
sys.path.append('../..')
|
|
|
|
from base.Scope import Scope
|
2016-03-17 17:55:27 +02:00
|
|
|
import configparser
|
2016-02-16 17:50:37 +02:00
|
|
|
|
|
|
|
|
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-17 17:55: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-03-11 17:39:32 +02:00
|
|
|
# TODO can use sh commands or api for getting username and timestamp
|
2016-03-08 18:05:42 +02:00
|
|
|
|
2016-03-17 17:55:27 +02:00
|
|
|
def policy_request_msg(self):
|
|
|
|
#TODO volkan
|
|
|
|
|
|
|
|
self.logger.debug('[Messaging] Creating policy request message')
|
|
|
|
|
2016-03-18 18:42:36 +02:00
|
|
|
ahenk_version=self.db_service.select('policy',['version'],'type = \'A\'')
|
2016-03-17 17:55:27 +02:00
|
|
|
username='volkan'
|
2016-03-18 18:42:36 +02:00
|
|
|
user_version=self.db_service.select('policy',['version'],'type = \'U\' and name = \''+username+'\'')
|
|
|
|
|
2016-03-17 17:55:27 +02:00
|
|
|
if len(ahenk_version)==0:
|
|
|
|
ahenk_version.append(-1)
|
|
|
|
if len(user_version)==0:
|
|
|
|
user_version.append(-1)
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['type'] = 'POLICY_REQUEST'
|
|
|
|
data['username'] = username
|
|
|
|
data['ahenkPolicyVersion'] = str(''.join(ahenk_version[0]))
|
|
|
|
data['userPolicyVersion'] =str(''.join(user_version[0]))
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
self.logger.debug('[Messaging] Policy request message was created')
|
|
|
|
print(json_data)
|
|
|
|
return json_data
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
def login_msg(self):
|
|
|
|
data = {}
|
2016-03-08 18:05:42 +02:00
|
|
|
data['type'] = 'LOGIN'
|
2016-03-11 17:39:32 +02:00
|
|
|
data['username'] = str(pwd.getpwuid(os.getuid())[0])
|
2016-03-07 17:59:02 +02:00
|
|
|
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
|
|
|
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
|
|
|
|
|
|
|
|
def logout_msg(self):
|
|
|
|
data = {}
|
2016-03-08 18:05:42 +02:00
|
|
|
data['type'] = 'LOGOUT'
|
2016-03-11 17:39:32 +02:00
|
|
|
data['username'] = str(pwd.getpwuid(os.getuid())[0])
|
2016-03-07 17:59:02 +02:00
|
|
|
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
|
|
|
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-11 17:39:32 +02:00
|
|
|
def policies_msg(self):
|
2016-03-07 17:59:02 +02:00
|
|
|
data = {}
|
2016-03-08 18:05:42 +02:00
|
|
|
data['type'] = 'GET_POLICIES'
|
2016-03-11 17:39:32 +02:00
|
|
|
data['username'] = str(pwd.getpwuid(os.getuid())[0])
|
|
|
|
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
|
|
|
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'
|
|
|
|
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['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
|
|
|
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-03-07 17:59:02 +02:00
|
|
|
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
|
|
|
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-03-07 17:59:02 +02:00
|
|
|
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
|
|
|
|
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
|