2020-04-06 18:13:10 +03:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Tuncay ÇOLAK <tuncay.colak@tubitak.gov.tr>
import json
import time
from base64 import b64encode
from os import urandom
from base . plugin . abstract_plugin import AbstractPlugin
class SetupVnc ( AbstractPlugin ) :
""" docstring for SetupVnc """
def __init__ ( self , data , context ) :
super ( AbstractPlugin , self ) . __init__ ( )
self . data = data
self . context = context
self . logger = self . get_logger ( )
self . password = self . create_password ( 10 )
self . port = self . get_port_number ( )
self . logger . debug ( ' Parameters were initialized ' )
def handle_task ( self ) :
self . logger . debug ( ' Handling task ' )
try :
self . run_vnc_server ( )
self . logger . info ( ' VNC Server running ' )
ip_addresses = str ( self . Hardware . Network . ip_addresses ( ) ) . replace ( ' [ ' , ' ' ) . replace ( ' ] ' , ' ' ) . replace ( " ' " , ' ' )
self . data [ ' port ' ] = self . port
self . data [ ' password ' ] = self . password
self . data [ ' host ' ] = ip_addresses
self . logger . debug ( ' Response data created ' )
if self . data [ ' permission ' ] == " yes " :
message = " VNC başarı lı bir şekilde yapı landı rı ldı ! \n {0} ip ' li bilgisayara uzak erişim sağlanacaktı r. \n Kullanı cı sı nı n izni için lütfen bekleyiniz... ' " . format ( self . data [ ' host ' ] )
elif self . data [ ' permission ' ] == " no " :
message = " VNC başarı lı bir şekilde yapı landı rı ldı ! \n {0} ip ' li bilgisayara kullanı cı izni gerektirmeksizin uzak erişim sağlanmı ştı r... ' " . format ( self . data [ ' host ' ] )
else :
message = " VNC başarı lı bir şekilde yapı landı rı ldı ! \n {0} ip ' li bilgisayara kullanı cı izni ve bildirim gerektirmeksizin uzak erişim sağlanmı ştı r... ' " . format ( self . data [ ' host ' ] )
self . context . create_response ( code = self . get_message_code ( ) . TASK_PROCESSED . value ,
message = message ,
data = json . dumps ( self . data ) ,
content_type = self . get_content_type ( ) . APPLICATION_JSON . value )
except Exception as e :
self . logger . error ( ' A problem occurred while running VNC server. Error Message: {} ' . format ( str ( e ) ) )
self . context . create_response ( code = self . get_message_code ( ) . TASK_ERROR . value ,
message = ' VNC sunucusu çalı şı rken bir hata oluştu. ' )
def run_vnc_server ( self ) :
2020-10-06 14:00:55 +03:00
# user_name = self.db_service.select_one_result('session', 'username', " 1=1 order by id desc ")
user_name = self . get_username ( )
2020-09-16 09:59:52 +03:00
self . logger . info ( ' get logon username is {0} ' . format ( user_name ) )
self . logger . debug ( ' Is VNC server installed? ' )
if self . is_installed ( ' x11vnc ' ) is False :
self . logger . debug ( ' VNC server not found, it is installing ' )
self . install_with_apt_get ( ' x11vnc ' )
self . logger . debug ( ' VNC server was installed ' )
self . logger . debug ( ' Killing running VNC proceses ' )
self . execute ( " ps aux | grep x11vnc | grep ' port " + self . port + " ' | awk ' { print $2} ' | xargs kill -9 " ,
result = False )
self . logger . debug ( ' Running VNC proceses were killed ' )
self . logger . debug ( ' Getting display and username... ' )
2020-10-06 14:00:55 +03:00
# display_number = self.get_username_display(user_name)
display_number = self . Sessions . display ( user_name )
desktop_env = self . get_desktop_env ( )
if desktop_env == " gnome " :
2020-10-08 12:09:57 +03:00
display_number = self . get_username_display_gnome ( user_name )
2020-10-06 14:00:55 +03:00
self . logger . info ( " Get display of {0} is {1} " . format ( user_name , display_number ) )
2020-10-27 14:40:52 +03:00
#homedir = self.get_homedir(user_name)
#self.logger.info("Get home directory of {0} is {1}".format(user_name, homedir))
2020-10-06 14:00:55 +03:00
# this user_name for execute method
user_name = self . get_as_user ( )
2020-09-16 09:59:52 +03:00
self . logger . debug ( ' Username: {0} Display: {1} ' . format ( user_name , display_number ) )
2020-10-27 14:40:52 +03:00
#if self.is_exist('{0}/.vncahenk{1}'.format(homedir, user_name)) is True:
# self.delete_folder('{0}/.vncahenk{1}'.format(homedir, user_name))
# self.logger.debug('Cleaning previous configurations.')
#self.logger.debug('Creating user VNC conf file as user')
#self.execute('su - {0} -c "mkdir -p {1}/.vncahenk{0}"'.format(user_name, homedir), result=False)
#self.logger.debug('Creating password as user')
#self.execute('su - {0} -c "x11vnc -storepasswd {1} {2}/.vncahenk{3}/x11vncpasswd"'.format(user_name, self.password, homedir,user_name), result=False)
2020-09-16 09:59:52 +03:00
self . logger . debug ( ' Running VNC server as user. ' )
if self . data [ ' permission ' ] == " yes " :
self . send_notify ( " Liderahenk " ,
" Lider Ahenk Sistem Yoneticisi tarafindan \n 5 sn sonra bilgisayarı nı za uzak erişim sağlanacaktı r. \n Bağlantı kapatı ldı ktan sonra ayrı ca bilgilendirilecektir. " ,
2020-10-06 14:00:55 +03:00
display_number , user_name , timeout = 50000 )
2020-09-16 09:59:52 +03:00
time . sleep ( 5 )
2020-10-27 14:40:52 +03:00
# self.execute('su - {0} -c "x11vnc -accept \'popup\' -gone \'popup\' -rfbport {1} -passwd {2} -o {2}/.vncahenk{3}/vnc.log -display {4}"'.format(
# user_name, self.port, user_name, display_number), result=False)
self . execute (
' su - {0} -c " x11vnc -accept \' popup \' -gone \' popup \' -rfbport {1} -passwd {2} -display {3} " ' . format (
user_name , self . port , self . password , display_number ) , result = False )
2020-09-16 09:59:52 +03:00
elif self . data [ " permission " ] == " no " :
self . logger . info ( " Lider Ahenk sistem yöneticisi 5 sn sonra bilgisayarı nı za uzak erişim sağlayacaktı r. " )
self . send_notify ( " Liderahenk " ,
" Lider Ahenk Sistem Yoneticisi tarafindan \n 5 sn sonra bilgisayarı nı za uzak erişim sağlanacaktı r. \n Bağlantı kapatı ldı ktan sonra ayrı ca bilgilendirilecektir. " ,
2020-10-06 14:00:55 +03:00
display_number , user_name , timeout = 50000 )
2020-10-27 14:40:52 +03:00
time . sleep ( 2 )
self . execute ( ' su - {0} -c " x11vnc -gone \' popup \' -rfbport {1} -passwd {2} -display {3} " ' . format (
user_name , self . port , self . password , display_number ) , result = False )
2020-09-16 09:59:52 +03:00
else :
2020-10-27 14:40:52 +03:00
self . execute ( ' su - {0} -c " x11vnc -rfbport {1} -passwd {2} -display {3} " ' . format (
user_name , self . port , self . password , display_number ) , result = False )
2020-09-16 09:59:52 +03:00
self . logger . info ( " Lider Ahenk sistem yöneticisi tarafı ndan kullanı cı izni ve bildirim gerektirmeksizin uzak erişim sağlanmı ştı r " )
2020-04-06 18:13:10 +03:00
def create_password ( self , pass_range ) :
self . logger . debug ( ' Password created ' )
random_bytes = urandom ( pass_range )
return b64encode ( random_bytes ) . decode ( ' utf-8 ' )
def get_port_number ( self ) :
self . logger . debug ( ' Target port is 5999 ' )
return ' 5999 '
def handle_task ( task , context ) :
vnc = SetupVnc ( task , context )
vnc . handle_task ( )