From 4493fb34d3f1e8f95de03b39c683da682998682d Mon Sep 17 00:00:00 2001 From: edip Date: Wed, 2 Jan 2019 16:38:07 +0300 Subject: [PATCH] registration add util methods --- src/base/registration/registration.py | 156 +++++++++++--------------- 1 file changed, 68 insertions(+), 88 deletions(-) diff --git a/src/base/registration/registration.py b/src/base/registration/registration.py index df524cb..54be040 100644 --- a/src/base/registration/registration.py +++ b/src/base/registration/registration.py @@ -15,7 +15,6 @@ import pwd from base.timer.setup_timer import SetupTimer from base.timer.timer import Timer import re -import sys import os class Registration: @@ -46,30 +45,22 @@ class Registration: self.host = self.conf_manager.get("CONNECTION", "host") self.servicename = self.conf_manager.get("CONNECTION", "servicename") - self.user_name = '' self.user_password= '' - user_name= os.getlogin() - self.logger.debug('User : '+ str(user_name)) - pout = Util.show_registration_message(user_name,'Makineyi Lider MYS sistemine kaydetmek için bilgileri ilgili alanlara giriniz. LÜTFEN DEVAM EDEN İŞLEMLERİ SONLANDIRDIĞINZA EMİN OLUNUZ !', 'LIDER MYS SISTEMINE KAYIT', self.host) - self.logger.debug('pout : ' + str(pout)) - field_values = pout.split(' ') - user_registration_info = list(field_values) - if self.host == '' : self.host = user_registration_info[0] - self.user_name = user_registration_info[1]; - self.user_password = user_registration_info[2]; + self.user_name = user_registration_info[1] + self.user_password = user_registration_info[2] else: - self.user_name = user_registration_info[0]; - self.user_password = user_registration_info[1]; + self.user_name = user_registration_info[0] + self.user_password = user_registration_info[1] #anon_messenger = AnonymousMessenger(self.message_manager.registration_msg(user_name,user_password), self.host,self.servicename) #anon_messenger.connect_to_server() @@ -97,7 +88,6 @@ class Registration: print(e) raise - def update_registration_attrs(self, dn=None): self.logger.debug('Registration configuration is updating...') self.db_service.update('registration', ['dn', 'registered'], [dn, 1], ' registered = 0') @@ -125,7 +115,6 @@ class Registration: admin_dn = str(reg_reply['ldapUserDn']) # get user full dn from server.. password same #admin_password = self.user_password # same user get from server admin_password = self.db_service.select_one_result('registration', 'password', ' registered=1') - if server_address != '' and dn != '' and version != '' and admin_dn != '' and admin_password != '': (result_code, p_out, p_err) = self.util.execute("/bin/bash /usr/share/ahenk/plugins/ldap-login/scripts/ldap-login.sh {0} {1} {2} {3} {4}".format( server_address, "\'" + dn + "\'", "\'" + admin_dn + "\'", "\'" + admin_password + "\'", version)) @@ -140,13 +129,15 @@ class Registration: raise Exception( 'LDAP Ayarları yapılırken hata oluştu. Lütfen ağ bağlantınızı kontrol ediniz. Deponuzun güncel olduğundan emin olunuz.') - - def registration_error(self, reg_reply): self.re_register() - def change_pam_ldap_configs(self): + def change_pam_ldap_configs(self,reg_reply): + + server_address = str(reg_reply['ldapServer']) + dn = str(reg_reply['ldapBaseDn']) + # pattern for clearing file data from spaces, tabs and newlines pattern = re.compile(r'\s+') @@ -161,32 +152,32 @@ class Registration: pam_script_configured_file_path = "/usr/share/ahenk/plugins/ldap-login/config-files/pam_script" # create pam_scripts_original directory if not exists - if not self.is_exist(pam_scripts_original_directory_path): + if not self.util.is_exist(pam_scripts_original_directory_path): self.logger.info("Creating {0} directory.".format(pam_scripts_original_directory_path)) - self.create_directory(pam_scripts_original_directory_path) + self.util.create_directory(pam_scripts_original_directory_path) - if self.is_exist(ldap_back_up_file_path): + if self.util.is_exist(ldap_back_up_file_path): self.logger.info("Changing {0} with {1}.".format(ldap_original_file_path, ldap_configured_file_path)) - self.copy_file(ldap_configured_file_path, ldap_original_file_path) + self.util.copy_file(ldap_configured_file_path, ldap_original_file_path) else: self.logger.info("Backing up {0}".format(ldap_original_file_path)) - self.copy_file(ldap_original_file_path, ldap_back_up_file_path) + self.util.copy_file(ldap_original_file_path, ldap_back_up_file_path) self.logger.info( "{0} file is replaced with {1}.".format(ldap_original_file_path, ldap_configured_file_path)) - self.copy_file(ldap_configured_file_path, ldap_original_file_path) + self.util.copy_file(ldap_configured_file_path, ldap_original_file_path) - if self.is_exist(pam_script_back_up_file_path): - self.copy_file(pam_script_configured_file_path, pam_script_original_file_path) + if self.util.is_exist(pam_script_back_up_file_path): + self.util.copy_file(pam_script_configured_file_path, pam_script_original_file_path) self.logger.info( "{0} is replaced with {1}.".format(pam_script_original_file_path, pam_script_configured_file_path)) else: self.logger.info("Backing up {0}".format(pam_script_original_file_path)) - self.copy_file(pam_script_original_file_path, pam_script_back_up_file_path) + self.util.copy_file(pam_script_original_file_path, pam_script_back_up_file_path) self.logger.info( "{0} file is replaced with {1}".format(pam_script_original_file_path, pam_script_configured_file_path)) - self.copy_file(pam_script_configured_file_path, pam_script_original_file_path) + self.util.copy_file(pam_script_configured_file_path, pam_script_original_file_path) - (result_code, p_out, p_err) = self.execute("DEBIAN_FRONTEND=noninteractive pam-auth-update --package") + (result_code, p_out, p_err) = self.util.execute("DEBIAN_FRONTEND=noninteractive pam-auth-update --package") if result_code == 0: self.logger.info("'DEBIAN_FRONTEND=noninteractive pam-auth-update --package' has run successfully") else: @@ -201,15 +192,15 @@ class Registration: text = pattern.sub('', file_data) is_configuration_done_before = False - if ("passwd:compatldap" not in text): + if "passwd:compatldap" not in text: file_data = file_data.replace("passwd: compat", "passwd: compat ldap") is_configuration_done_before = True - if ("group:compatldap" not in text): + if "group:compatldap" not in text: file_data = file_data.replace("group: compat", "group: compat ldap") is_configuration_done_before = True - if ("shadow:compatldap" not in text): + if "shadow:compatldap" not in text: file_data = file_data.replace("shadow: compat", "shadow: compat ldap") is_configuration_done_before = True @@ -225,7 +216,7 @@ class Registration: # configure ldap-cache self.logger.info("Starting to ldap-cache configurations.") - result_code, p_out, p_err = self.execute("apt-get install nss-updatedb -y") + result_code, p_out, p_err = self.util.execute("apt-get install nss-updatedb -y") if result_code != 0: self.logger.error("Error occured while downloading nss-updatedb.") else: @@ -260,26 +251,26 @@ class Registration: file_ns_switch = open("/etc/nsswitch.conf", 'w') file_ns_switch.write(file_data) file_ns_switch.close() - self.execute("nss_updatedb ldap") + self.util.execute("nss_updatedb ldap") # create cron job for ldap cache content = "#!/bin/bash\n" \ "nss-updatedb ldap" nss_update_cron_job_file_path = "/etc/cron.daily/nss-updatedb" - if self.is_exist(nss_update_cron_job_file_path): + if self.util.is_exist(nss_update_cron_job_file_path): self.logger.info( "{0} exists. File will be deleted and creating new one.".format(nss_update_cron_job_file_path)) - self.delete_file(nss_update_cron_job_file_path) - self.create_file(nss_update_cron_job_file_path) - self.write_file(nss_update_cron_job_file_path, content, 'w+') - self.execute("chmod +x " + nss_update_cron_job_file_path) + self.util.delete_file(nss_update_cron_job_file_path) + self.util.create_file(nss_update_cron_job_file_path) + self.util.write_file(nss_update_cron_job_file_path, content, 'w+') + self.util.execute("chmod +x " + nss_update_cron_job_file_path) else: self.logger.info( "{0} doesnt exist. File will be created and content will be written.".format( nss_update_cron_job_file_path)) - self.create_file(nss_update_cron_job_file_path) - self.write_file(nss_update_cron_job_file_path, content, 'w+') - self.execute("chmod +x " + nss_update_cron_job_file_path) + self.util.create_file(nss_update_cron_job_file_path) + self.util.write_file(nss_update_cron_job_file_path, content, 'w+') + self.util.execute("chmod +x " + nss_update_cron_job_file_path) # configure /etc/libnss-ldap.conf libnss_ldap_file_path = "/etc/libnss-ldap.conf" @@ -288,17 +279,17 @@ class Registration: "\nnss_reconnect_sleeptime 1" \ "\nnss_reconnect_maxsleeptime 8" \ "\nnss_reconnect_maxconntries 2" - if self.is_exist(libnss_ldap_file_path): + if self.util.is_exist(libnss_ldap_file_path): self.logger.info("{0} exists.".format(libnss_ldap_file_path)) - self.execute("sed -i '/bind_policy hard/c\\' " + libnss_ldap_file_path) - self.execute("sed -i '/nss_reconnect_tries 1/c\\' " + libnss_ldap_file_path) - self.execute("sed -i '/nss_reconnect_sleeptime 1/c\\' " + libnss_ldap_file_path) - self.execute("sed -i '/nss_reconnect_maxsleeptime 8/c\\' " + libnss_ldap_file_path) - self.execute("sed -i '/nss_reconnect_maxconntries 2/c\\' " + libnss_ldap_file_path) - self.write_file(libnss_ldap_file_path, content, 'a+') + self.util.execute("sed -i '/bind_policy hard/c\\' " + libnss_ldap_file_path) + self.util.execute("sed -i '/nss_reconnect_tries 1/c\\' " + libnss_ldap_file_path) + self.util.execute("sed -i '/nss_reconnect_sleeptime 1/c\\' " + libnss_ldap_file_path) + self.util.execute("sed -i '/nss_reconnect_maxsleeptime 8/c\\' " + libnss_ldap_file_path) + self.util.execute("sed -i '/nss_reconnect_maxconntries 2/c\\' " + libnss_ldap_file_path) + self.util.write_file(libnss_ldap_file_path, content, 'a+') self.logger.info("Configuration has been made to {0}.".format(libnss_ldap_file_path)) - result_code, p_out, p_err = self.execute("apt-get install libnss-db libpam-ccreds -y") + result_code, p_out, p_err = self.util.execute("apt-get install libnss-db libpam-ccreds -y") if result_code != 0: self.logger.error("Error occured while downloading libnss-db libpam-ccreds.") else: @@ -306,25 +297,25 @@ class Registration: # configure sudo-ldap sudo_ldap_conf_file_path = "/etc/sudo-ldap.conf" - content = "sudoers_base ou=Roles," + self.data['dn'] \ - + "\nBASE " + self.data['dn'] \ - + "\nURI ldap://" + self.data['server-address'] + content = "sudoers_base ou=Roles," + dn \ + + "\nBASE " + dn \ + + "\nURI ldap://" + server_address # clean if config is already written - self.execute("sed -i '/BASE /c\\' " + sudo_ldap_conf_file_path) - self.execute("sed -i '/sudoers_base /c\\' " + sudo_ldap_conf_file_path) - self.execute("sed -i '/URI /c\\' " + sudo_ldap_conf_file_path) + self.util.execute("sed -i '/BASE /c\\' " + sudo_ldap_conf_file_path) + self.util.execute("sed -i '/sudoers_base /c\\' " + sudo_ldap_conf_file_path) + self.util.execute("sed -i '/URI /c\\' " + sudo_ldap_conf_file_path) - if self.is_exist(sudo_ldap_conf_file_path): + if self.util.is_exist(sudo_ldap_conf_file_path): self.logger.info("{0} exists.".format(sudo_ldap_conf_file_path)) - self.write_file(sudo_ldap_conf_file_path, content, 'a+') + self.util.write_file(sudo_ldap_conf_file_path, content, 'a+') self.logger.info("Content is written to {0} successfully.".format(sudo_ldap_conf_file_path)) # Configure lightdm.service # check if 99-pardus-xfce.conf exists if not create pardus_xfce_path = "/usr/share/lightdm/lightdm.conf.d/99-pardus-xfce.conf" - if not self.is_exist(pardus_xfce_path): + if not self.util.is_exist(pardus_xfce_path): self.logger.info("99-pardus-xfce.conf does not exist.") - self.create_file(pardus_xfce_path) + self.util.create_file(pardus_xfce_path) file_lightdm = open(pardus_xfce_path, 'a') file_lightdm.write("[Seat:*]\n") @@ -333,20 +324,18 @@ class Registration: self.logger.info("lightdm has been configured.") else: self.logger.info("99-pardus-xfce.conf exists. Delete file and create new one.") - self.delete_file(pardus_xfce_path) - self.create_file(pardus_xfce_path) + self.util.delete_file(pardus_xfce_path) + self.util.create_file(pardus_xfce_path) file_lightdm = open(pardus_xfce_path, 'a') file_lightdm.write("[Seat:*]") file_lightdm.write("greeter-hide-users=true") file_lightdm.close() self.logger.info("lightdm.conf has been configured.") - self.execute("systemctl restart nscd.service") - self.execute("pam-auth-update --force") + self.util.execute("systemctl restart nscd.service") + self.util.execute("pam-auth-update --force") self.logger.info("LDAP Login operation has been completed.") - - def is_registered(self): try: if str(System.Ahenk.uid()): @@ -450,17 +439,11 @@ 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...') - Util.show_message(os.getlogin(),':0',"Lider MYS sistemine ulaşılamadı. Lütfen sunucu adresini kontrol ediniz....","HATA") - System.Process.kill_by_pid(int(System.Ahenk.get_pid_number())) - - def purge_and_unregister(self): try: - - self.logger.info('Ahenk conf cleaned') self.logger.info('Ahenk conf cleaning from db') self.unregister() @@ -477,11 +460,8 @@ class Registration: self.logger.info('Enable Users') self.enable_local_users() Util.shutdown() - except Exception as e: self.logger.error("Error while running purge_and_unregister process.. Error Message " + str(e)) - - #System.Process.kill_by_pid(int(System.Ahenk.get_pid_number())) #sys.exit(2) @@ -495,20 +475,20 @@ class Registration: pam_script_back_up_file_path = "/usr/share/ahenk/pam_scripts_original/pam_script" pam_script_original_file_path = "/usr/share/pam-configs/pam_script" - if self.is_exist(ldap_back_up_file_path): + if self.util.is_exist(ldap_back_up_file_path): self.logger.info("Replacing {0} with {1}".format(ldap_original_file_path, ldap_back_up_file_path)) - self.copy_file(ldap_back_up_file_path, ldap_original_file_path) + self.util.copy_file(ldap_back_up_file_path, ldap_original_file_path) self.logger.info("Deleting {0}".format(ldap_back_up_file_path)) - self.delete_file(ldap_back_up_file_path) + self.util.delete_file(ldap_back_up_file_path) - if self.is_exist(pam_script_back_up_file_path): + if self.util.is_exist(pam_script_back_up_file_path): self.logger.info( "Replacing {0} with {1}".format(pam_script_original_file_path, pam_script_back_up_file_path)) - self.copy_file(pam_script_back_up_file_path, pam_script_original_file_path) + self.util.copy_file(pam_script_back_up_file_path, pam_script_original_file_path) self.logger.info("Deleting {0}".format(pam_script_back_up_file_path)) - self.delete_file(pam_script_back_up_file_path) + self.util.delete_file(pam_script_back_up_file_path) - (result_code, p_out, p_err) = self.execute("DEBIAN_FRONTEND=noninteractive pam-auth-update --package") + (result_code, p_out, p_err) = self.util.execute("DEBIAN_FRONTEND=noninteractive pam-auth-update --package") if result_code == 0: self.logger.info("'DEBIAN_FRONTEND=noninteractive pam-auth-update --package' has run successfully") else: @@ -551,17 +531,17 @@ class Registration: # Configure ldap-cache nss_update_cron_job_file_path = "/etc/cron.daily/nss-updatedb" - if self.is_exist(nss_update_cron_job_file_path): - self.delete_file(nss_update_cron_job_file_path) + if self.util.is_exist(nss_update_cron_job_file_path): + self.util.delete_file(nss_update_cron_job_file_path) self.logger.info("{0} is deleted.".format(nss_update_cron_job_file_path)) # Configure lightdm.service pardus_xfce_path = "/usr/share/lightdm/lightdm.conf.d/99-pardus-xfce.conf" - if self.is_exist(pardus_xfce_path): + if self.util.is_exist(pardus_xfce_path): self.logger.info("99-pardus-xfce.conf exists. Deleting file.") - self.delete_file(pardus_xfce_path) + self.util.delete_file(pardus_xfce_path) - self.execute("systemctl restart nscd.service") + self.util.execute("systemctl restart nscd.service") self.logger.info("Operation finished") def clean(self): @@ -633,4 +613,4 @@ class Registration: 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)) - Util.execute(change_permisson.format(new_home_dir)) + Util.execute(change_permisson.format(new_home_dir)) \ No newline at end of file