From e7fdfdca74f119ac1f68f3d44a990b41eaffbf7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tuncay=20=C3=87OLAK?= Date: Fri, 1 Oct 2021 00:57:30 +0300 Subject: [PATCH] changed command for service enable and disable and other bugfix --- src/base/messaging/anonymous_messenger.py | 23 +++++++++- src/base/messaging/messaging.py | 12 +++++- src/base/messaging/messenger.py | 43 ++++++++++++++++--- .../execute_sssd_ad_authentication.py | 18 +++++++- src/plugins/local-user/add_user.py | 20 ++++++++- src/plugins/local-user/edit_user.py | 20 ++++++++- src/plugins/manage-root/set_root_password.py | 7 +-- src/plugins/service/get_services.py | 15 +++---- src/plugins/service/service_list.py | 30 +++++++------ 9 files changed, 146 insertions(+), 42 deletions(-) diff --git a/src/base/messaging/anonymous_messenger.py b/src/base/messaging/anonymous_messenger.py index 729f24e..6af5105 100644 --- a/src/base/messaging/anonymous_messenger.py +++ b/src/base/messaging/anonymous_messenger.py @@ -95,13 +95,22 @@ class AnonymousMessenger(ClientXMPP): def recv_direct_message(self, msg): if msg['type'] in ['normal']: - self.logger.info('---------->Received message: {0}'.format(str(msg['body']))) + self.logger.info('Reading registration reply') j = json.loads(str(msg['body'])) message_type = j['type'] status = str(j['status']).lower() dn = str(j['agentDn']) self.logger.debug('Registration status: ' + str(status)) + is_password = False + for key, value in j.items(): + if "password" in key.lower(): + j[key] = "********" + is_password = True + if is_password: + self.logger.info('---------->Received message: {0}'.format(str(j))) + else: + self.logger.info('---------->Received message: {0}'.format(str(msg['body']))) if 'not_authorized' == str(status): self.logger.debug('[REGISTRATION IS FAILED]. User not authorized') @@ -154,5 +163,15 @@ class AnonymousMessenger(ClientXMPP): self.logger.debug('Fired event is: {0}'.format(message_type)) def send_direct_message(self, msg): - self.logger.debug('<<--------Sending message: {0}'.format(msg)) + body = json.loads(str(msg)) + if body['type'] == "REGISTER" or body['type'] == "UNREGISTER": + is_password = False + for key, value in body.items(): + if "password" in key.lower(): + body[key] = "********" + is_password = True + if is_password: + self.logger.info('<<--------Sending message: {0}'.format(body)) + else: + self.logger.info('<<--------Sending message: {0}'.format(msg)) self.send_message(mto=self.receiver, mbody=msg, mtype='normal') diff --git a/src/base/messaging/messaging.py b/src/base/messaging/messaging.py index caa239d..7146451 100644 --- a/src/base/messaging/messaging.py +++ b/src/base/messaging/messaging.py @@ -154,7 +154,17 @@ class Messaging(object): data['timestamp'] = self.db_service.select_one_result('registration', 'timestamp', ' 1=1') json_data = json.dumps(data) self.logger.debug('Registration message was created') - self.logger.info('Registration message was created. Data content: '+ json_data) + + body = json.loads(str(json_data)) + is_password = False + for key, value in body.items(): + if "password" in key.lower(): + body[key] = "********" + is_password = True + if is_password: + self.logger.info('Registration message was created. Data content: {0}'.format(body)) + + #self.logger.info('Registration message was created. Data content: ' + json_data) return json_data def ldap_registration_msg(self): diff --git a/src/base/messaging/messenger.py b/src/base/messaging/messenger.py index be49d52..8709c84 100644 --- a/src/base/messaging/messenger.py +++ b/src/base/messaging/messenger.py @@ -92,7 +92,17 @@ class Messenger(ClientXMPP): def send_direct_message(self, msg): try: - self.logger.info('<<--------Sending message: {0}'.format(msg)) + body = json.loads(str(msg)) + if body['type'] == "REGISTER" or body['type'] == "UNREGISTER": + is_password = False + for key, value in body.items(): + if "password" in key.lower(): + body[key] = "********" + is_password = True + if is_password: + self.logger.info('<<--------Sending message: {0}'.format(body)) + else: + self.logger.info('<<--------Sending message: {0}'.format(msg)) self.send_message(mto=self.receiver, mbody=msg, mtype='normal') except Exception as e: self.logger.error( @@ -109,12 +119,31 @@ class Messenger(ClientXMPP): self.logger.info('---------->Received message: {0}'.format(str(msg['body']))) if j['type'] == "EXECUTE_TASK": - i = json.loads(str(j['task'])) - plugin_name = i['plugin']['name'] - if plugin_name == "manage-root": - parameter_map = i['parameterMap'] - parameter_map.pop("RootPassword") - self.logger.info("---------->Received message: {}".format(str(parameter_map))) + message = json.loads(str(msg['body'])) + task = json.loads(str(message['task'])) + #plugin_name = task['plugin']['name'] + parameter_map = task['parameterMap'] + use_file_transfer = message['fileServerConf'] + is_password = False + for key, value in parameter_map.items(): + if "password" in key.lower(): + parameter_map[key] = "********" + task['parameterMap'] = parameter_map + message['task'] = task + is_password = True + if use_file_transfer != None: + #message['fileServerConf'] = "*******" + file_server_conf = message['fileServerConf'] + file_server_param = file_server_conf['parameterMap'] + for key, value in file_server_param.items(): + if "password" in key.lower(): + file_server_param[key] = "********" + file_server_conf['parameterMap'] = file_server_param + #message['fileServerConf']['parameterMap'] = file_server_param + message['fileServerConf'] = file_server_conf + is_password = True + if is_password: + self.logger.info('---------->Received message: {0}'.format(str(message))) else: self.logger.info('---------->Received message: {0}'.format(str(msg['body']))) self.event_manger.fireEvent(message_type, str(msg['body'])) diff --git a/src/base/registration/execute_sssd_ad_authentication.py b/src/base/registration/execute_sssd_ad_authentication.py index 9d696b2..e95fd8d 100644 --- a/src/base/registration/execute_sssd_ad_authentication.py +++ b/src/base/registration/execute_sssd_ad_authentication.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: Agah Hulusi ÖZ +import subprocess from base.scope import Scope from base.util.util import Util @@ -183,7 +184,7 @@ class ExecuteSSSDAdAuthentication: if (self.join_try_counter == 5): break else: - (result_code, p_out, p_err) = self.util.execute( + (result_code, p_out, p_err) = self.execute_command( "echo \"{0}\" | realm join --user={1} {2}".format(password, ad_username, domain_name.upper())) if (result_code == 0): @@ -356,3 +357,18 @@ class ExecuteSSSDAdAuthentication: self.logger.info("AD Login işlemi esnasında hata oluştu.") return False + def execute_command(self, command, stdin=None, env=None, cwd=None, shell=True, result=True): + + try: + process = subprocess.Popen(command, stdin=stdin, env=env, cwd=cwd, stderr=subprocess.PIPE, + stdout=subprocess.PIPE, shell=shell) + if result is True: + result_code = process.wait() + p_out = process.stdout.read().decode("unicode_escape") + p_err = process.stderr.read().decode("unicode_escape") + return result_code, p_out, p_err + else: + return None, None, None + except Exception as e: + return 1, 'Could not execute command' + diff --git a/src/plugins/local-user/add_user.py b/src/plugins/local-user/add_user.py index db0fd75..22bac12 100644 --- a/src/plugins/local-user/add_user.py +++ b/src/plugins/local-user/add_user.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- # Author:Mine DOGAN # Author:Tuncay ÇOLAK +import subprocess from base.plugin.abstract_plugin import AbstractPlugin from pathlib import Path @@ -60,10 +61,10 @@ class AddUser(AbstractPlugin): self.logger.debug('Added user to these groups: {}'.format(self.groups)) if str(self.password).strip() != "": - result_code, p_out, p_err = self.execute(self.create_shadow_password.format(self.password)) + result_code, p_out, p_err = self.execute_command(self.create_shadow_password.format(self.password)) shadow_password = p_out.strip() # shadow_password = crypt.crypt(self.password) - self.execute(self.change_password.format('\'{}\''.format(shadow_password), self.username)) + self.execute_command(self.change_password.format('\'{}\''.format(shadow_password), self.username)) self.logger.debug('Changed password.') self.execute(self.change_shell.format(self.username)) @@ -143,6 +144,21 @@ class AddUser(AbstractPlugin): self.context.create_response(code=self.message_code.TASK_ERROR.value, message='Local-User görevi çalıştırılırken bir hata oluştu.') + ## this methode is only for local-user password plugin + def execute_command(self, command, stdin=None, env=None, cwd=None, shell=True, result=True): + try: + process = subprocess.Popen(command, stdin=stdin, env=env, cwd=cwd, stderr=subprocess.PIPE, + stdout=subprocess.PIPE, shell=shell) + if result is True: + result_code = process.wait() + p_out = process.stdout.read().decode("unicode_escape") + p_err = process.stderr.read().decode("unicode_escape") + return result_code, p_out, p_err + else: + return None, None, None + except Exception as e: + return 1, 'Could not execute command: {0}. Error Message: {1}'.format(command, str(e)), '' + def handle_task(task, context): add_user = AddUser(task, context) diff --git a/src/plugins/local-user/edit_user.py b/src/plugins/local-user/edit_user.py index a5aef24..756286f 100644 --- a/src/plugins/local-user/edit_user.py +++ b/src/plugins/local-user/edit_user.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- # Author:Mine DOGAN # Author:Tuncay ÇOLAK +import subprocess from base.plugin.abstract_plugin import AbstractPlugin from pathlib import Path @@ -64,9 +65,9 @@ class EditUser(AbstractPlugin): self.username = self.new_username if str(self.password).strip() != "": - result_code, p_out, p_err = self.execute(self.create_shadow_password.format(self.password)) + result_code, p_out, p_err = self.execute_command(self.create_shadow_password.format(self.password)) shadow_password = p_out.strip() - self.execute(self.change_password.format('\'{}\''.format(shadow_password), self.username)) + self.execute_command(self.change_password.format('\'{}\''.format(shadow_password), self.username)) self.logger.debug('Changed password.') if self.current_home != self.home: @@ -155,6 +156,21 @@ class EditUser(AbstractPlugin): self.context.create_response(code=self.message_code.TASK_ERROR.value, message='Local-User görevi çalıştırılırken bir hata oluştu.') + ## this methode is only for local-user password plugin + def execute_command(self, command, stdin=None, env=None, cwd=None, shell=True, result=True): + try: + process = subprocess.Popen(command, stdin=stdin, env=env, cwd=cwd, stderr=subprocess.PIPE, + stdout=subprocess.PIPE, shell=shell) + if result is True: + result_code = process.wait() + p_out = process.stdout.read().decode("unicode_escape") + p_err = process.stderr.read().decode("unicode_escape") + return result_code, p_out, p_err + else: + return None, None, None + except Exception as e: + return 1, 'Could not execute command: {0}. Error Message: {1}'.format(command, str(e)), '' + def handle_task(task, context): edit_user = EditUser(task, context) edit_user.handle_task() diff --git a/src/plugins/manage-root/set_root_password.py b/src/plugins/manage-root/set_root_password.py index 3d5c9e1..757ee07 100644 --- a/src/plugins/manage-root/set_root_password.py +++ b/src/plugins/manage-root/set_root_password.py @@ -98,21 +98,16 @@ class RootPassword(AbstractPlugin): try: process = subprocess.Popen(command, stdin=stdin, env=env, cwd=cwd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=shell) - self.logger.debug('Executing command for manage-root') - if result is True: result_code = process.wait() p_out = process.stdout.read().decode("unicode_escape") p_err = process.stderr.read().decode("unicode_escape") - return result_code, p_out, p_err else: return None, None, None except Exception as e: - return 1, 'Could not execute command: {0}. Error Message: {1}'.format(command, str(e)), '' - - + return 1, 'Could not execute command' def handle_task(task, context): diff --git a/src/plugins/service/get_services.py b/src/plugins/service/get_services.py index 3b15730..0b58495 100644 --- a/src/plugins/service/get_services.py +++ b/src/plugins/service/get_services.py @@ -97,17 +97,16 @@ class GetServices(AbstractPlugin): del service[0] if len(service)>0 and '.service' in service[0]: # service[0] = service name, service[1] is loaded, service[2] active or not, - result, out, err = self.execute(self.service_status.format(service[0])) # check service is enable or not on auto start - auto='INACTIVE' - if 'disabled' in out: - auto='INACTIVE' - elif 'enabled' in out: - auto='ACTIVE' + # result, out, err = self.execute(self.service_status.format(service[0])) # check service is enable or not on auto start + result, out, err = self.execute("systemctl is-enabled {0}".format(service[0])) + auto = 'disabled' + if 'enabled' in out: + auto = 'enabled' if service[2] == 'active': - self.add_file(service[0], "ACTIVE", auto) + self.add_file(service[0], "active", auto) else: - self.add_file(service[0], 'INACTIVE',auto) + self.add_file(service[0], 'inactive', auto) print(service) diff --git a/src/plugins/service/service_list.py b/src/plugins/service/service_list.py index b93bda9..2a1ef78 100644 --- a/src/plugins/service/service_list.py +++ b/src/plugins/service/service_list.py @@ -28,7 +28,7 @@ class ServiceList(AbstractPlugin): return result_code, message, item def set_startup_service(self, service_name, action): - (result_code, p_out, p_err) = self.execute('update-rc.d {0} {1}'.format(service_name, action)) + (result_code, p_out, p_err) = self.execute('systemctl {0} {1}'.format(action, service_name)) if result_code == 0: message = 'Service startup action was successful: {}'.format(service_name) else: @@ -39,17 +39,22 @@ class ServiceList(AbstractPlugin): def get_service_status(self, service_item): - service_name=str(service_item['serviceName']) + service_name = str(service_item['serviceName']) result, p_out, err = self.execute('systemctl status {0}'.format(service_name)) if 'not-found' in p_out: service_item["serviceStatus"] = 'Service Not Found' elif 'running' in p_out: - service_item["serviceStatus"] = 'Running' + service_item["serviceStatus"] = 'active' elif ('inactive' in p_out) or ('failed' in p_out): - service_item["serviceStatus"] = 'Stopped' + service_item["serviceStatus"] = 'inactive' + result, out, err = self.execute("systemctl is-enabled {0}".format(service_name)) + auto = 'disabled' + if 'enabled' in out: + auto = 'enabled' + service_item["startAuto"] = auto return service_item @@ -61,30 +66,29 @@ class ServiceList(AbstractPlugin): for item in items: try: if item['serviceStatus'] is not None and ( - str(item['serviceStatus']) == 'Başlat' or str(item['serviceStatus']) == 'Start' or str(item['serviceStatus']) == 'START' ): + str(item['serviceStatus']) == 'start' or str(item['serviceStatus']) == 'active' or str(item['serviceStatus']) == 'START'): resultcode, message, item = self.start_stop_service(item, "start") resultMessage += message if item['serviceStatus'] is not None and ( - str(item['serviceStatus']) == 'Durdur' or str(item['serviceStatus']) == 'Stop' or str(item['serviceStatus']) == 'STOP' ): + str(item['serviceStatus']) == 'stop' or str(item['serviceStatus']) == 'inactive' or str(item['serviceStatus']) == 'STOP'): resultcode, message, item= self.start_stop_service(item, "stop") resultMessage += message if item['startAuto'] is not None and ( - str(item['startAuto']) == 'Başlat' or str(item['startAuto']) == 'Start' or str(item['startAuto']) == 'START'): - resultcode, message = self.set_startup_service(item, "defaults") + str(item['startAuto']) == 'enabled' or str(item['startAuto']) == 'Start' or str(item['startAuto']) == 'START'): + resultcode, message = self.set_startup_service(item['serviceName'], "enable") resultMessage += message if item['startAuto'] is not None and ( - str(item['startAuto']) == 'Durdur' or str(item['startAuto']) == 'Stop' or str(item['startAuto']) == 'STOP' ): - resultcode, message = self.set_startup_service(item, "remove") + str(item['startAuto']) == 'disabled' or str(item['startAuto']) == 'Stop' or str(item['startAuto']) == 'STOP'): + resultcode, message = self.set_startup_service(item['serviceName'], "disable") resultMessage += message - - item=self.get_service_status(item) + item = self.get_service_status(item) except Exception as e: resultMessage += '{0} servisinin isteklerini gerçekleştirirken hata ile karşılaşıldı. Hdata : {1}\r\n'.format( str(item['serviceName']), str(e)) self.logger.debug(resultMessage) - data = {'ResultMessage': resultMessage, 'service_list': items } + data = {'ResultMessage': resultMessage, 'service_list': items} self.context.create_response(code=self.message_code.TASK_PROCESSED.value, message='Servis istekleri gerçekleştirildi',