From f83e182c8c42d1e586aae72e4b305dfec681ec21 Mon Sep 17 00:00:00 2001 From: edip Date: Sat, 10 Nov 2018 22:28:32 +0300 Subject: [PATCH] register and unregister from user gui --- src/ahenkd.py | 2 +- src/base/agreement/agreement.py | 1 + src/base/agreement/ahenkmessage.py | 47 +++++++++++++++++ src/base/agreement/confirm.py | 3 +- src/base/execution/execution_manager.py | 2 +- src/base/messaging/anonymous_messenger.py | 39 +++++++++++--- src/base/registration/registration.py | 64 +++++------------------ src/base/util/util.py | 30 ++++++++++- 8 files changed, 123 insertions(+), 65 deletions(-) create mode 100644 src/base/agreement/ahenkmessage.py diff --git a/src/ahenkd.py b/src/ahenkd.py index 450477d..fb8d0a0 100644 --- a/src/ahenkd.py +++ b/src/ahenkd.py @@ -294,7 +294,7 @@ class AhenkDaemon(BaseDaemon): self.is_registered() - self.logger.info('Ahenk was registered') + #self.logger.info('Ahenk was registered') self.messenger = self.init_messenger() self.logger.info('Messenger was set') diff --git a/src/base/agreement/agreement.py b/src/base/agreement/agreement.py index 97c54f0..367ff80 100644 --- a/src/base/agreement/agreement.py +++ b/src/base/agreement/agreement.py @@ -68,6 +68,7 @@ class Agreement: agreement_path, title) result_code, p_out, p_err = Util.execute(command) + pout = str(p_out).replace('\n', '') if pout != 'Error': if pout == 'Y': diff --git a/src/base/agreement/ahenkmessage.py b/src/base/agreement/ahenkmessage.py new file mode 100644 index 0000000..8fd07df --- /dev/null +++ b/src/base/agreement/ahenkmessage.py @@ -0,0 +1,47 @@ +import sys +from easygui import multpasswordbox, msgbox + +def ask(message, title, host): + + field_names=[] + + if host =='': + field_names.append("Etki Alanı Sunucusu:") + + field_names.append("Yetkili Kullanıcı") + field_names.append("Parola") + + field_values = multpasswordbox( + msg=message, + title=title, fields=(field_names)) + + if field_values is None: + return print('N'); + + is_fieldvalue_empty = False; + + for value in field_values: + if value == '': + is_fieldvalue_empty = True; + + if is_fieldvalue_empty: + msgbox("Lütfen zorunlu alanları giriniz.", ok_button="Tamam") + return print('Z'); + + if host =='': + print(field_values[0],field_values[1],field_values[2]) + else: + print(field_values[0], field_values[1]) + +if __name__ == '__main__': + + if len(sys.argv) > 1: + try: + message=sys.argv[1] + title=sys.argv[2] + host=sys.argv[3] + ask(message,title, host) + except Exception as e: + print(str(e)) + else: + print('Argument fault. Check your parameters or content of parameters. Parameters: ' + str(sys.argv)) \ No newline at end of file diff --git a/src/base/agreement/confirm.py b/src/base/agreement/confirm.py index 942330d..12d8847 100644 --- a/src/base/agreement/confirm.py +++ b/src/base/agreement/confirm.py @@ -7,7 +7,8 @@ import easygui def confirm(message, title): - choice = easygui.ccbox(msg=message, title=title, choices=("Evet", "Hayır")) + choice = easygui.buttonbox(msg=message, title=title, choices=["Tamam"]) + if choice: print('Y') else: diff --git a/src/base/execution/execution_manager.py b/src/base/execution/execution_manager.py index 261351a..cd45407 100644 --- a/src/base/execution/execution_manager.py +++ b/src/base/execution/execution_manager.py @@ -405,7 +405,7 @@ class ExecutionManager(object): if 'not_authorized' == str(status): self.logger.info('Registration is failed. User not authorized') - msgbox('Ahenk etki alanından çıkarmak için yetkili kullanıcı haklarına sahip olmanız gerekmektedir.', + Util.show_message('Ahenk etki alanından çıkarmak için yetkili kullanıcı haklarına sahip olmanız gerekmektedir.', 'Kullanıcı Yetkilendirme Hatası') else : registration= Scope.get_instance().get_registration() diff --git a/src/base/messaging/anonymous_messenger.py b/src/base/messaging/anonymous_messenger.py index 104d654..0673cc4 100644 --- a/src/base/messaging/anonymous_messenger.py +++ b/src/base/messaging/anonymous_messenger.py @@ -9,11 +9,13 @@ from base.util.util import Util import time from base.system.system import System +import pwd + +from helper import system as sysx + from sleekxmpp import ClientXMPP from base.scope import Scope -from easygui import msgbox - sys.path.append('../..') @@ -102,7 +104,7 @@ class AnonymousMessenger(ClientXMPP): if 'not_authorized' == str(status): self.logger.info('Registration is failed. User not authorized') - msgbox('Ahenk etki alanına alınamadı !! Sadece yetkili kullanıcılar etki alanına kayıt yapabilir.', 'Kullanıcı Yetkilendirme Hatası') + Util.show_message('Ahenk etki alanına alınamadı !! Sadece yetkili kullanıcılar etki alanına kayıt yapabilir.', 'Kullanıcı Yetkilendirme Hatası') self.logger.debug('Disconnecting...') self.disconnect() @@ -111,14 +113,14 @@ class AnonymousMessenger(ClientXMPP): self.logger.info('Registred from server. Registration process starting.') self.event_manager.fireEvent('REGISTRATION_SUCCESS', j) msg = str(self.host) + " Etki Alanına hoş geldiniz." - msgbox(msg, "") + Util.show_message(msg, "") msg = "Değişikliklerin etkili olması için sistem yeniden başlayacaktır. Sistem yeniden başlatılıyor...." - msgbox(msg, "") + Util.show_message(msg, "") time.sleep(5) self.logger.info('Disconnecting...') self.disconnect() - self.logger.info('Rebooting...') + self.disable_local_users() Util.shutdown(); System.Process.kill_by_pid(int(System.Ahenk.get_pid_number())) sys.exit(2) @@ -126,7 +128,7 @@ class AnonymousMessenger(ClientXMPP): except Exception as e: self.logger.error('Error Message: {0}.'.format(str(e))) - msgbox(str(e)) + Util.show_message(str(e)) self.logger.debug('Disconnecting...') self.disconnect() @@ -134,7 +136,7 @@ class AnonymousMessenger(ClientXMPP): elif 'registration_error' == str(status): self.logger.info('Registration is failed. New registration request will send') #self.event_manager.fireEvent('REGISTRATION_ERROR', str(j)) - msgbox('Ahenk etki alanına alınamadı !! Kayıt esnasında hata oluştu. Lütfen sistem yöneticinize başvurunuz.', + Util.show_message('Ahenk etki alanına alınamadı !! Kayıt esnasında hata oluştu. Lütfen sistem yöneticinize başvurunuz.', 'Sistem Hatası') self.logger.debug('Disconnecting...') self.disconnect() @@ -145,3 +147,24 @@ class AnonymousMessenger(ClientXMPP): def send_direct_message(self, msg): self.logger.debug('<<--------Sending message: {0}'.format(msg)) self.send_message(mto=self.receiver, mbody=msg, mtype='normal') + + + def disable_local_users(self): + passwd_cmd = 'passwd -l {}' + change_home = 'usermod -m -d {0} {1}' + change_username = 'usermod -l {0} {1}' + content = Util.read_file('/etc/passwd') + kill_all_process = 'killall -KILL -u {}' + for p in pwd.getpwall(): + self.logger.info("User: '{0}' will be disabled and changed username and home directory of username".format(p.pw_name)) + if not sysx.shell_is_interactive(p.pw_shell): + continue + if p.pw_uid == 0: + continue + if p.pw_name in content: + new_home_dir = p.pw_dir.rstrip('/') + '-local/' + new_username = p.pw_name+'-local' + Util.execute(kill_all_process.format(p.pw_name)) + Util.execute(passwd_cmd.format(p.pw_name)) + Util.execute(change_username.format(new_username, p.pw_name)) + Util.execute(change_home.format(new_home_dir, new_username)) diff --git a/src/base/registration/registration.py b/src/base/registration/registration.py index 3690f97..af36c25 100644 --- a/src/base/registration/registration.py +++ b/src/base/registration/registration.py @@ -14,9 +14,9 @@ from helper import system as sysx import pwd from base.timer.setup_timer import SetupTimer from base.timer.timer import Timer -from easygui import multpasswordbox, msgbox import re import sys +import os class Registration: def __init__(self): @@ -47,37 +47,18 @@ class Registration: self.host = self.conf_manager.get("CONNECTION", "host") self.servicename = self.conf_manager.get("CONNECTION", "servicename") - self.user_name ='' - self.user_password='' + self.user_name = '' + self.user_password= '' - field_names = [] - if self.host == '': - field_names.append("Sunucu Adresi") + pout = Util.show_registration_message('Makineyi etki alanına almak için bilgileri ilgili alanlara giriniz. LÜTFEN DEVAM EDEN İŞLEMLERİ SONLANDIRDIĞINZA EMİN OLUNUZ !', + 'ETKI ALANINA KAYIT', self.host) - - field_names.append("Yetkili Kullanıcı") - field_names.append("Parola") - - field_values = multpasswordbox(msg='Makineyi etki alanına almak için bilgileri ilgili alanlara giriniz. LÜTFEN DEVAM EDEN İŞLEMLERİ SONLANDIRDIĞINZA EMİN OLUNUZ !', - title='ETKI ALANINA KAYIT', fields=(field_names)) - - if field_values is None: - return False; - - is_fieldvalue_empty= False; - - for value in field_values : - if value == '' : - is_fieldvalue_empty = True; - - if is_fieldvalue_empty: - msgbox("Lütfen zorunlu alanları giriniz.", ok_button="Tamam") - return False; + field_values = pout.split(' ') user_registration_info = list(field_values) if self.host == '' : - self.host = user_registration_info[0]; + self.host = user_registration_info[0] self.user_name = user_registration_info[1]; self.user_password = user_registration_info[2]; else: @@ -359,33 +340,14 @@ class Registration: 'and it is connected to XMPP server! Check your Ahenk configuration file (/etc/ahenk/ahenk.conf)') self.logger.error('Ahenk is shutting down...') print('Ahenk is shutting down...') - msgbox('Etki alanı sunucusuna ulaşılamadı. Lütfen sunucu adresini kontrol ediniz....','HATA') + + Util.show_message("Etki alanı sunucusuna ulaşılamadı. Lütfen sunucu adresini kontrol ediniz....","HATA") + System.Process.kill_by_pid(int(System.Ahenk.get_pid_number())) - def disable_local_users(self): - passwd_cmd = 'passwd -l {}' - change_home = 'usermod -m -d {0} {1}' - change_username = 'usermod -l {0} {1}' - content = self.util.read_file('/etc/passwd') - kill_all_process = 'killall -KILL -u {}' - for p in pwd.getpwall(): - if not sysx.shell_is_interactive(p.pw_shell): - continue - if p.pw_uid == 0: - continue - if p.pw_name in content: - - new_home_dir = p.pw_dir.rstrip('/') + '-local/' - new_username = p.pw_name+'-local' - self.util.execute(kill_all_process.format(p.pw_name)) - self.util.execute(passwd_cmd.format(p.pw_name)) - self.util.execute(change_username.format(new_username, p.pw_name)) - self.util.execute(change_home.format(new_home_dir, new_username)) - self.logger.debug("User: '{0}' will be disabled and changed username and home directory of username".format(p.pw_name)) def purge_and_unregister(self): - from easygui import msgbox,boolbox self.logger.info('Ahenk conf cleaned') self.logger.info('Ahenk conf cleaning from db') self.unregister() @@ -401,11 +363,9 @@ class Registration: self.logger.info('Enable Users') self.enable_local_users() + Util.show_message("Ahenk etki alanından çıkarılmıştır.", "") - - msgbox("Ahenk etki alanından çıkarılmıştır.") - - if boolbox("Değişikliklerin etkili olması için sistem yeniden başlatmanız gerekmektedir.","",["Yeniden Başlat", "Vazgeç"]): + if Util.show_message("Değişikliklerin etkili olması için sistem yeniden başlatmanız gerekmektedir.",""): Util.shutdown() System.Process.kill_by_pid(int(System.Ahenk.get_pid_number())) diff --git a/src/base/util/util.py b/src/base/util/util.py index c772093..294bd8b 100644 --- a/src/base/util/util.py +++ b/src/base/util/util.py @@ -332,8 +332,10 @@ class Util: Util.execute('export DISPLAY={0}; su - {1} -c \'{2}\''.format(display, user, inner_command)) @staticmethod - def ask_permission(display, username, message, title): - ask_path = '/usr/share/ahenk/base/agreement/confirm.py' + def show_message(message, title=""): + ask_path = '/usr/share/ahenk/src/base/agreement/confirm.py' + display= ":0" + username= os.getlogin() try: if username is not None: @@ -354,3 +356,27 @@ class Util: return None except Exception: return None + + @staticmethod + def show_registration_message(message,title,host=None): + login_user_name = os.getlogin() + + ask_path = '/usr/share/ahenk/src/base/agreement/ahenkmessage.py' + + display_number = ":0" + + if host is None: + command = 'export DISPLAY={0}; su - {1} -c \"python3 {2} \'{3}\' \'{4}\' \"'.format(display_number, login_user_name, + ask_path, message, title) + else: + command = 'export DISPLAY={0}; su - {1} -c \"python3 {2} \'{3}\' \'{4}\' \'{5}\' \"'.format(display_number, + login_user_name, + ask_path, + message, title, + host) + result_code, p_out, p_err = Util.execute(command) + + pout = str(p_out).replace('\n', '') + + return pout +