2016-03-02 18:31:29 +02:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
2016-03-23 16:41:54 +02:00
import datetime
import json
import uuid
2016-03-23 17:15:27 +02:00
from uuid import getnode as get_mac
from base . Scope import Scope
2016-06-20 17:35:59 +03:00
from base . messaging . AnonymousMessenger import AnonymousMessager
2016-05-18 15:18:14 +03:00
from base . system . system import System
2016-07-22 10:37:27 +03:00
from base . timer . setup_timer import SetupTimer
from base . timer . timer import Timer
2016-07-25 16:28:23 +03:00
from base . system . system import System
2016-03-02 18:31:29 +02:00
2016-06-27 17:21:10 +03:00
class Registration :
2016-03-02 18:31:29 +02:00
def __init__ ( self ) :
2016-03-04 19:23:21 +02:00
scope = Scope ( ) . getInstance ( )
2016-03-23 16:41:54 +02:00
self . logger = scope . getLogger ( )
self . message_manager = scope . getMessageManager ( )
2016-03-07 17:59:02 +02:00
self . event_manager = scope . getEventManager ( )
2016-06-20 17:35:59 +03:00
self . messenger = scope . getMessenger ( )
2016-04-08 15:54:26 +03:00
self . conf_manager = scope . getConfigurationManager ( )
2016-03-23 16:41:54 +02:00
self . db_service = scope . getDbService ( )
2016-03-07 17:59:02 +02:00
2016-04-08 15:54:26 +03:00
self . event_manager . register_event ( ' REGISTRATION_RESPONSE ' , self . registration_process )
2016-03-23 16:41:54 +02:00
if self . is_registered ( ) :
self . logger . debug ( ' [Registration] Ahenk already registered ' )
2016-03-04 19:23:21 +02:00
else :
2016-03-07 12:55:14 +02:00
self . register ( True )
2016-03-04 19:23:21 +02:00
2016-03-07 12:55:14 +02:00
def registration_request ( self ) :
2016-03-11 17:37:38 +02:00
self . logger . debug ( ' [Registration] Requesting registration ' )
2016-07-22 10:37:27 +03:00
SetupTimer . start ( Timer ( System . Ahenk . registration_timeout ( ) , timeout_function = self . registration_timeout , checker_func = self . is_registered , kwargs = None ) )
2016-07-25 16:28:23 +03:00
anon_messenger = AnonymousMessager ( self . message_manager . registration_msg ( ) )
anon_messenger . connect_to_server ( )
2016-03-07 17:59:02 +02:00
2016-03-10 09:53:51 +02:00
def ldap_registration_request ( self ) :
2016-03-11 17:37:38 +02:00
self . logger . debug ( ' [Registration] Requesting LDAP registration ' )
2016-06-20 17:35:59 +03:00
self . messenger . send_Direct_message ( self . message_manager . ldap_registration_msg ( ) )
2016-03-10 09:53:51 +02:00
2016-04-08 15:54:26 +03:00
def registration_process ( self , reg_reply ) :
2016-03-11 17:37:38 +02:00
self . logger . debug ( ' [Registration] Reading registration reply ' )
2016-03-07 17:59:02 +02:00
j = json . loads ( reg_reply )
2016-03-23 16:41:54 +02:00
self . logger . debug ( ' [Registration] ' + j [ ' message ' ] )
status = str ( j [ ' status ' ] ) . lower ( )
2016-07-29 19:09:22 +03:00
dn = str ( j [ ' agentDn ' ] )
2016-04-08 15:54:26 +03:00
2016-03-23 16:41:54 +02:00
self . logger . debug ( ' [Registration] Registration status: ' + str ( status ) )
2016-03-07 17:59:02 +02:00
2016-04-08 15:54:26 +03:00
if ' already_exists ' == str ( status ) or ' registered ' == str ( status ) or ' registered_without_ldap ' == str ( status ) :
2016-03-23 16:41:54 +02:00
self . logger . debug ( ' dn: ' + dn )
2016-04-08 15:54:26 +03:00
self . update_registration_attrs ( dn )
2016-03-23 16:41:54 +02:00
elif ' registration_error ' == str ( status ) :
2016-03-11 17:37:38 +02:00
self . logger . info ( ' [Registration] Registration is failed. New registration request will send ' )
2016-03-07 17:59:02 +02:00
self . re_register ( )
2016-04-08 15:54:26 +03:00
else :
self . logger . error ( ' [Registration] Bad message type of registration response ' )
2016-03-11 17:37:38 +02:00
2016-04-08 15:54:26 +03:00
def update_registration_attrs ( self , dn = None ) :
2016-03-11 17:37:38 +02:00
self . logger . debug ( ' [Registration] Registration configuration is updating... ' )
2016-03-23 16:41:54 +02:00
self . db_service . update ( ' registration ' , [ ' dn ' , ' registered ' ] , [ dn , 1 ] , ' registered = 0 ' )
2016-03-10 09:53:51 +02:00
2016-03-23 17:34:12 +02:00
if self . conf_manager . has_section ( ' CONNECTION ' ) :
2016-04-08 15:54:26 +03:00
self . conf_manager . set ( ' CONNECTION ' , ' uid ' , self . db_service . select_one_result ( ' registration ' , ' jid ' , ' registered=1 ' ) )
self . conf_manager . set ( ' CONNECTION ' , ' password ' , self . db_service . select_one_result ( ' registration ' , ' password ' , ' registered=1 ' ) )
# TODO get file path?
2016-03-23 17:34:12 +02:00
with open ( ' /etc/ahenk/ahenk.conf ' , ' w ' ) as configfile :
self . conf_manager . write ( configfile )
self . logger . debug ( ' [Registration] Registration configuration file is updated ' )
2016-03-04 19:23:21 +02:00
def is_registered ( self ) :
2016-07-25 16:28:23 +03:00
try :
if str ( System . Ahenk . uid ( ) ) :
return True
else :
return False
except :
2016-03-04 19:23:21 +02:00
return False
2016-03-10 09:53:51 +02:00
def is_ldap_registered ( self ) :
2016-03-23 16:41:54 +02:00
dn = self . db_service . select_one_result ( ' registration ' , ' dn ' , ' registered = 1 ' )
if dn is not None and dn != ' ' :
2016-03-10 09:53:51 +02:00
return True
else :
return False
2016-03-23 16:41:54 +02:00
def register ( self , uuid_depend_mac = False ) :
cols = [ ' jid ' , ' password ' , ' registered ' , ' params ' , ' timestamp ' ]
vals = [ str ( self . generate_uuid ( uuid_depend_mac ) ) , str ( self . generate_password ( ) ) , 0 , str ( self . get_registration_params ( ) ) , str ( datetime . datetime . now ( ) . strftime ( " %d - % m- % Y % I: % M " ) ) ]
self . db_service . delete ( ' registration ' , ' 1==1 ' )
self . db_service . update ( ' registration ' , cols , vals )
self . logger . debug ( ' [Registration] Registration parameters were created ' )
def get_registration_params ( self ) :
2016-05-16 17:28:58 +03:00
2016-03-23 16:41:54 +02:00
params = {
2016-05-19 16:14:55 +03:00
' ipAddresses ' : str ( System . Hardware . Network . ip_addresses ( ) ) . replace ( ' [ ' , ' ' ) . replace ( ' ] ' , ' ' ) ,
' macAddresses ' : str ( System . Hardware . Network . mac_addresses ( ) ) . replace ( ' [ ' , ' ' ) . replace ( ' ] ' , ' ' ) ,
2016-05-17 12:28:17 +03:00
' hostname ' : System . Os . hostname ( ) ,
' os.name ' : System . Os . name ( ) ,
' os.version ' : System . Os . version ( ) ,
' os.kernel ' : System . Os . kernel_release ( ) ,
' os.distributionName ' : System . Os . distribution_name ( ) ,
' os.distributionId ' : System . Os . distribution_id ( ) ,
' os.distributionVersion ' : System . Os . distribution_version ( ) ,
' os.architecture ' : System . Os . architecture ( ) ,
' hardware.cpu.architecture ' : System . Hardware . Cpu . architecture ( ) ,
2016-06-13 11:57:58 +03:00
' hardware.cpu.logicalCoreCount ' : System . Hardware . Cpu . logical_core_count ( ) ,
' hardware.cpu.physicalCoreCount ' : System . Hardware . Cpu . physical_core_count ( ) ,
2016-05-17 12:28:17 +03:00
' hardware.disk.total ' : System . Hardware . Disk . total ( ) ,
' hardware.disk.used ' : System . Hardware . Disk . used ( ) ,
' hardware.disk.free ' : System . Hardware . Disk . free ( ) ,
' hardware.disk.partitions ' : System . Hardware . Disk . partitions ( ) ,
' hardware.memory.total ' : System . Hardware . Memory . total ( ) ,
' hardware.network.ipAddresses ' : System . Hardware . Network . ip_addresses ( ) ,
' sessions.userNames ' : System . Sessions . user_name ( ) ,
2016-03-23 16:41:54 +02:00
}
return json . dumps ( params )
2016-03-02 18:31:29 +02:00
2016-03-04 19:23:21 +02:00
def unregister ( self ) :
2016-03-11 17:37:38 +02:00
self . logger . debug ( ' [Registration] Ahenk is unregistering... ' )
2016-03-23 16:41:54 +02:00
self . db_service . delete ( ' registration ' , ' 1==1 ' )
self . logger . debug ( ' [Registration] Ahenk is unregistered ' )
2016-03-07 17:59:02 +02:00
2016-03-07 12:55:14 +02:00
def re_register ( self ) :
2016-03-11 17:37:38 +02:00
self . logger . debug ( ' [Registration] Reregistrating... ' )
2016-03-07 12:55:14 +02:00
self . unregister ( )
self . register ( False )
2016-03-04 19:23:21 +02:00
2016-03-23 16:41:54 +02:00
def generate_uuid ( self , depend_mac = True ) :
2016-03-02 18:31:29 +02:00
if depend_mac is False :
self . logger . debug ( ' [Registration] uuid creating randomly ' )
2016-03-23 16:41:54 +02:00
return uuid . uuid4 ( ) # make a random UUID
2016-03-02 18:31:29 +02:00
else :
2016-03-11 17:37:38 +02:00
self . logger . debug ( ' [Registration] uuid creating according to mac address ' )
2016-03-23 16:41:54 +02:00
return uuid . uuid3 ( uuid . NAMESPACE_DNS , str ( get_mac ( ) ) ) # make a UUID using an MD5 hash of a namespace UUID and a mac address
2016-03-02 18:31:29 +02:00
2016-03-04 19:23:21 +02:00
def generate_password ( self ) :
return uuid . uuid4 ( )
2016-07-22 10:37:27 +03:00
def registration_timeout ( self ) :
self . logger . error ( ' [Registration] Could not reach registration response from Lider. Be sure XMPP server is reachable and it supports anonymous message, Lider is running properly '
' and it is connected to XMPP server! Check your Ahenk configuration file (/etc/ahenk/ahenk.conf) ' )
self . logger . error ( ' [Registration] Ahenk is shutting down... ' )
print ( ' Ahenk is shutting down... ' )
System . Process . kill_by_pid ( int ( System . Ahenk . get_pid_number ( ) ) )