示例#1
0
    def process(self, event):

        if event.src_path != self.path[:-1]:
            if event.event_type == 'moved':
                plu_path = event.dest_path
                result = Commander().set_event([None, 'load', '-p', plu_path.replace(self.path, '')])
                if result is True and System.Ahenk.is_running() is True:
                    os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
            elif event.event_type == 'deleted':
                result = Commander().set_event([None, 'remove', '-p', event.src_path.replace(self.path, '')])
                if result is True and System.Ahenk.is_running() is True:
                    os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM)
示例#2
0
文件: ahenkd.py 项目: tncyclk/ahenk
                    print('Ahenk starting...')
                ahenk_daemon.run()
            elif sys.argv[1] == 'stop':
                if System.Ahenk.is_running() is True:
                    raise SystemExit
                else:
                    print('Ahenk not working!')
            elif sys.argv[1] == 'restart':
                if System.Ahenk.is_running() is True:
                    print('Ahenk restarting...')
                    ahenk_daemon.restart()
                else:
                    print('Ahenk starting...')
                    ahenk_daemon.run()
            elif sys.argv[1] == 'status':
                print(Commander().status())
            else:
                print(
                    'Unknown command. Usage : %s start|stop|restart|status|clean'
                    % sys.argv[0])
                sys.exit(2)
        elif len(sys.argv) > 2 and (sys.argv[1] in ('start')):
            params = sys.argv[1]
            hostName = sys.argv[2]
            userName = sys.argv[3]
            password = sys.argv[4]
            # directoryServer = sys.argv[5]
            # ahenk_daemon.set_register_user(hostName,userName,password,directoryServer)
            ahenk_daemon.set_register_user(hostName, userName, password)
            ahenk_daemon.run()
示例#3
0
    def run_command_from_fifo(self, num, stack):
        """ docstring"""

        while True:
            try:
                event = Commander().get_event()
                if event is None:
                    break
                json_data = json.loads(event)
                self.logger.info(event)

            except Exception as e:
                self.logger.error(
                    'A problem occurred while loading json. Check json format! Error Message: {0}.'
                    ' Event = {1}'.format(str(e), str(event)))
                return

            if json_data is not None:
                self.logger.debug('Signal handled')
                self.logger.debug('Signal is :{0}'.format(
                    str(json_data['event'])))

                if str(json_data['event']
                       ) == 'login' and self.check_last_login():
                    username = json_data['username']
                    if username != "Debian-gdm" and username != "gdm":
                        display = json_data['display']
                        desktop = json_data['desktop']

                        ip = None
                        if 'ip' in json_data:
                            ip = json_data['ip']

                        domain = None
                        if 'domain' in json_data:
                            domain = json_data['domain']

                        self.logger.info(
                            'login event is handled for user: {0}'.format(
                                username))
                        self.logger.info(
                            'login parameters of login user, username: {0}, display: {1}, desktop: {2}, domain: {3}'
                            .format(username, display, desktop, domain))

                        Util.execute("systemctl restart sssd.service")
                        login_message = self.message_manager.login_msg(
                            username, ip)
                        self.messenger.send_direct_message(login_message)
                        agreement = Agreement()
                        agreement_choice = None

                        if agreement.check_agreement(
                                username
                        ) is not True and System.Ahenk.agreement() == '1':
                            self.logger.debug(
                                'User {0} has not accepted agreement.'.format(
                                    username))
                            thread_ask = Process(target=agreement.ask,
                                                 args=(
                                                     username,
                                                     display,
                                                 ))
                            thread_ask.start()
                            agreement_timeout = self.conf_manager.get(
                                'SESSION', 'agreement_timeout')
                            timeout = int(agreement_timeout)  # sec
                            timer = time.time()
                            while 1:
                                if thread_ask.is_alive() is False:
                                    self.logger.warning(
                                        '{0} was answered the question '.
                                        format(username))
                                    if Agreement().check_agreement(
                                            username) is True:
                                        self.logger.warning(
                                            'Choice of {0} is YES'.format(
                                                username))
                                        agreement_choice = True
                                        break
                                    elif Agreement().check_agreement(
                                            username) is False:
                                        self.logger.warning(
                                            'Choice of {0} is NO'.format(
                                                username))
                                        agreement_choice = False
                                        Util.close_session(username)
                                        break

                                if (time.time() - timer) > timeout:
                                    if thread_ask.is_alive():
                                        thread_ask.terminate()
                                    Util.close_session(username)
                                    self.logger.warning(
                                        'Session of {0} was ended because of timeout of contract agreement'
                                        .format(username))
                                    break
                                time.sleep(1)

                            if agreement_choice is not None:
                                self.messenger.send_direct_message(
                                    self.message_manager.agreement_answer_msg(
                                        username, agreement_choice))
                        else:
                            agreement_choice = True

                        if agreement_choice is True or System.Ahenk.agreement(
                        ) != '1':
                            self.db_service.delete('session', '1=1')
                            self.logger.info(
                                'Display is {0}, desktop env is {1} for {2}'.
                                format(display, desktop, username))
                            session_columns = self.db_service.get_cols(
                                'session')
                            # ['username', 'display', 'desktop', 'timestamp', 'ip', 'domain']
                            self.db_service.update(
                                'session', session_columns, [
                                    username, display, desktop,
                                    str(int(time.time())), ip, domain
                                ])

                            # Default policy for users --->> START
                            self.logger.info(
                                "Applying default policies for user {0}".
                                format(Util.get_username()))
                            self.default_policy.default_firefox_policy(
                                Util.get_username())
                            self.default_policy.disable_update_package_notify(
                                Util.get_username())
                            self.default_policy.create_pulseaudio_autostart()
                            # Default policy for users --->> STOP

                            get_policy_message = self.message_manager.policy_request_msg(
                                username)
                            self.plugin_manager.process_mode('safe', username)
                            self.plugin_manager.process_mode('login', username)

                            kward = dict()
                            kward['timeout_args'] = username
                            kward['checker_args'] = username

                            SetupTimer.start(
                                Timer(
                                    timeout=System.Ahenk.get_policy_timeout(),
                                    timeout_function=self.execute_manager.
                                    execute_default_policy,
                                    checker_func=self.execute_manager.
                                    is_policy_executed,
                                    kwargs=kward))

                            self.logger.info(
                                'Requesting updated policies from Lider. If Ahenk could not reach updated '
                                'policies in {0} sec, booked policies will be executed'
                                .format(System.Ahenk.get_policy_timeout()))
                            self.messenger.send_direct_message(
                                get_policy_message)

                elif str(json_data['event']) == 'logout':
                    username = json_data['username']
                    if username != "Debian-gdm" and username != "gdm":
                        self.execute_manager.remove_user_executed_policy_dict(
                            username)
                        self.plugin_manager.process_mode('logout', username)
                        self.plugin_manager.process_mode('safe', username)
                        if username != "Debian-gdm":
                            self.db_service.delete('session', '1=1')
                        # TODO delete all user records while initializing
                        self.logger.info(
                            'logout event is handled for user: {0}'.format(
                                username))
                        ip = None
                        if 'ip' in json_data:
                            ip = json_data['ip']

                        logout_message = self.message_manager.logout_msg(
                            username, ip)
                        self.messenger.send_direct_message(logout_message)
                        self.logger.info('Ahenk polkit file deleting..')
                        self.delete_polkit_user()
                        # self.db_service.delete('session', 'username=\'{0}\''.format(username))

                elif str(json_data['event']) == 'send':
                    self.logger.info(
                        'Sending message over ahenkd command. Response Message: {0}'
                        .format(json.dumps(json_data['message'])))
                    message = json.dumps(json_data['message'])
                    self.messenger.send_direct_message(message)

                elif str(json_data['event']) == 'unregister' and len(
                        json_data) == 1:
                    self.logger.info('Unregistering..')
                    self.execute_manager.set_unregister_credential_params(
                        None, None)
                    unregister_message = self.message_manager.unregister_msg(
                        None, None)
                    if unregister_message is not None:
                        self.messenger.send_direct_message(unregister_message)

                elif str(json_data['event']) == 'unregister' and len(
                        json_data) == 3:
                    self.logger.info('Unregistering..')
                    usernameForCheck = json_data['userName']
                    passwordForCheck = json_data['password']
                    ## send user credential info to execution manager for hide user notify when remote unregister
                    self.execute_manager.set_unregister_credential_params(
                        usernameForCheck, usernameForCheck)
                    self.logger.info('Unregistering by username+' +
                                     str(usernameForCheck))
                    unregister_message = self.message_manager.unregister_msg(
                        usernameForCheck, passwordForCheck)
                    if unregister_message is not None:
                        self.messenger.send_direct_message(unregister_message)

                elif str(json_data['event']) == 'load':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug('All plugins are loading to ahenk')
                        self.plugin_manager.load_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is loading to ahenk'.format(
                                    p_name))
                            self.plugin_manager.load_single_plugin(p_name)

                elif str(json_data['event']) == 'reload':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug('All plugins are reloading to ahenk')
                        self.plugin_manager.reload_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is reloading to ahenk'.format(
                                    p_name))
                            self.plugin_manager.reload_single_plugin(p_name)

                elif str(json_data['event']) == 'remove':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug(
                            'All plugins are removing from ahenk')
                        self.plugin_manager.remove_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is removing from ahenk'.format(
                                    p_name))
                            self.plugin_manager.remove_single_plugin(p_name)

                elif str(json_data['event']) == 'stop':
                    self.plugin_manager.process_mode('shutdown')
                    self.logger.info('Shutdown mode activated.')

                    # TODO timeout
                    while self.running_plugin() is False:
                        self.logger.debug('Waiting for progress of plugins...')
                        time.sleep(0.5)

                    Util.delete_file(System.Ahenk.fifo_file())
                    Scope().get_instance().get_custom_param(
                        'ahenk_daemon').stop()
                else:
                    self.logger.error('Unknown command error. Command:' +
                                      json_data['event'])
                self.logger.debug('Processing of handled event is completed')
示例#4
0
    def run_command_from_fifo(self, num, stack):
        """ docstring"""

        while True:
            try:
                event = Commander().get_event()
                if event is None:
                    break
                json_data = json.loads(event)
            except Exception as e:
                self.logger.error(
                    'A problem occurred while loading json. Check json format! Error Message: {0}.'
                    ' Event = {1}'.format(str(e), str(event)))
                return

            if json_data is not None:

                self.logger.debug('Signal handled')
                self.logger.debug('Signal is :{0}'.format(
                    str(json_data['event'])))

                if str(json_data['event']
                       ) == 'login' and self.check_last_login():
                    username = json_data['username']
                    display = json_data['display']
                    desktop = json_data['desktop']

                    ip = None
                    if 'ip' in json_data:
                        ip = json_data['ip']

                    self.logger.info(
                        'login event is handled for user: {0}'.format(
                            username))
                    Util.execute("systemctl restart sssd.service")
                    login_message = self.message_manager.login_msg(
                        username, ip)
                    self.messenger.send_direct_message(login_message)

                elif str(json_data['event']) == 'logout':
                    username = json_data['username']
                    self.db_service.delete('session',
                                           'username=\'{0}\''.format(username))
                    self.execute_manager.remove_user_executed_policy_dict(
                        username)
                    # TODO delete all user records while initializing
                    self.logger.info(
                        'logout event is handled for user: {0}'.format(
                            username))
                    ip = None
                    if 'ip' in json_data:
                        ip = json_data['ip']
                    logout_message = self.message_manager.logout_msg(
                        username, ip)
                    self.messenger.send_direct_message(logout_message)

                    self.logger.info('Ahenk polkit file deleting..')
                    self.delete_polkit_user()

                    self.plugin_manager.process_mode('logout', username)
                    self.plugin_manager.process_mode('safe', username)

                elif str(json_data['event']) == 'send':
                    self.logger.info(
                        'Sending message over ahenkd command. Response Message: {0}'
                        .format(json.dumps(json_data['message'])))
                    message = json.dumps(json_data['message'])
                    self.messenger.send_direct_message(message)

                elif str(json_data['event']) == 'unregister':
                    self.logger.info('Unregistering..')
                    unregister_message = self.message_manager.unregister_msg()
                    if unregister_message is not None:
                        self.messenger.send_direct_message(unregister_message)

                elif str(json_data['event']) == 'load':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug('All plugins are loading to ahenk')
                        self.plugin_manager.load_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is loading to ahenk'.format(
                                    p_name))
                            self.plugin_manager.load_single_plugin(p_name)

                elif str(json_data['event']) == 'reload':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug('All plugins are reloading to ahenk')
                        self.plugin_manager.reload_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is reloading to ahenk'.format(
                                    p_name))
                            self.plugin_manager.reload_single_plugin(p_name)

                elif str(json_data['event']) == 'remove':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug(
                            'All plugins are removing from ahenk')
                        self.plugin_manager.remove_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is removing from ahenk'.format(
                                    p_name))
                            self.plugin_manager.remove_single_plugin(p_name)

                elif str(json_data['event']) == 'stop':
                    self.plugin_manager.process_mode('shutdown')
                    self.logger.info('Shutdown mode activated.')

                    # TODO timeout
                    while self.running_plugin() is False:
                        self.logger.debug('Waiting for progress of plugins...')
                        time.sleep(0.5)

                    Util.delete_file(System.Ahenk.fifo_file())
                    Scope().get_instance().get_custom_param(
                        'ahenk_daemon').stop()
                else:
                    self.logger.error('Unknown command error. Command:' +
                                      json_data['event'])
                self.logger.debug('Processing of handled event is completed')
示例#5
0
    def run_command_from_fifo(self, num, stack):
        """ docstring"""

        while True:
            try:
                event = Commander().get_event()
                if event is None:
                    break
                json_data = json.loads(event)
            except Exception as e:
                self.logger.error(
                    'A problem occurred while loading json. Check json format! Error Message: {0}.'
                    ' Event = {1}'.format(str(e), str(event)))
                return

            if json_data is not None:

                self.logger.debug('Signal handled')
                self.logger.debug('Signal is :{0}'.format(
                    str(json_data['event'])))

                if str(json_data['event']) == 'login':
                    username = json_data['username']
                    display = json_data['display']
                    desktop = json_data['desktop']
                    self.logger.info(
                        'login event is handled for user: {0}'.format(
                            username))
                    login_message = self.message_manager.login_msg(username)
                    self.messenger.send_direct_message(login_message)

                    agreement = Agreement()
                    agreement_choice = None

                    if agreement.check_agreement(username) is not True:
                        self.logger.debug(
                            'User {0} has not accepted agreement.'.format(
                                username))
                        thread_ask = Process(target=agreement.ask,
                                             args=(
                                                 username,
                                                 display,
                                             ))
                        thread_ask.start()

                        agreement_timeout = self.conf_manager.get(
                            'SESSION', 'agreement_timeout')

                        timeout = int(agreement_timeout)  # sec
                        timer = time.time()
                        while 1:
                            if thread_ask.is_alive() is False:
                                self.logger.warning(
                                    '{0} was answered the question '.format(
                                        username))
                                if Agreement().check_agreement(
                                        username) is True:
                                    self.logger.warning(
                                        'Choice of {0} is YES'.format(
                                            username))
                                    agreement_choice = True
                                    break
                                elif Agreement().check_agreement(
                                        username) is False:
                                    self.logger.warning(
                                        'Choice of {0} is NO'.format(username))
                                    agreement_choice = False
                                    Util.close_session(username)
                                    break

                            if (time.time() - timer) > timeout:
                                if thread_ask.is_alive():
                                    thread_ask.terminate()
                                Util.close_session(username)
                                self.logger.warning(
                                    'Session of {0} was ended because of timeout of contract agreement'
                                    .format(username))
                                break
                            time.sleep(1)

                        if agreement_choice is not None:
                            self.messenger.send_direct_message(
                                self.message_manager.agreement_answer_msg(
                                    username, agreement_choice))
                    else:
                        agreement_choice = True

                    if agreement_choice is True:
                        self.db_service.delete(
                            'session', 'username=\'{0}\''.format(username))

                        self.logger.info(
                            'Display is {0}, desktop env is {1} for {2}'.
                            format(display, desktop, username))
                        session_columns = self.db_service.get_cols('session')
                        self.db_service.update(
                            'session', session_columns,
                            [username, display, desktop,
                             Util.timestamp()])
                        get_policy_message = self.message_manager.policy_request_msg(
                            username)

                        self.plugin_manager.process_mode('safe', username)
                        self.plugin_manager.process_mode('login', username)

                        kward = dict()
                        kward['timeout_args'] = username
                        kward['checker_args'] = username

                        SetupTimer.start(
                            Timer(timeout=System.Ahenk.get_policy_timeout(),
                                  timeout_function=self.execute_manager.
                                  execute_default_policy,
                                  checker_func=self.execute_manager.
                                  is_policy_executed,
                                  kwargs=kward))

                        self.logger.info(
                            'Requesting updated policies from Lider. If Ahenk could not reach updated '
                            'policies in {0} sec, booked policies will be executed'
                            .format(System.Ahenk.get_policy_timeout()))
                        self.messenger.send_direct_message(get_policy_message)

                elif str(json_data['event']) == 'logout':
                    username = json_data['username']
                    self.db_service.delete('session',
                                           'username=\'{0}\''.format(username))
                    self.execute_manager.remove_user_executed_policy_dict(
                        username)
                    # TODO delete all user records while initializing
                    self.logger.info(
                        'logout event is handled for user: {0}'.format(
                            username))
                    logout_message = self.message_manager.logout_msg(username)
                    self.messenger.send_direct_message(logout_message)

                    self.plugin_manager.process_mode('logout', username)
                    self.plugin_manager.process_mode('safe', username)

                elif str(json_data['event']) == 'send':
                    self.logger.info(
                        'Sending message over ahenkd command. Response Message: {0}'
                        .format(json.dumps(json_data['message'])))
                    message = json.dumps(json_data['message'])
                    self.messenger.send_direct_message(message)

                elif str(json_data['event']) == 'load':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug('All plugins are loading to ahenk')
                        self.plugin_manager.load_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is loading to ahenk'.format(
                                    p_name))
                            self.plugin_manager.load_single_plugin(p_name)

                elif str(json_data['event']) == 'reload':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug('All plugins are reloading to ahenk')
                        self.plugin_manager.reload_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is reloading to ahenk'.format(
                                    p_name))
                            self.plugin_manager.reload_single_plugin(p_name)

                elif str(json_data['event']) == 'remove':
                    plugin_name = str(json_data['plugins'])

                    if plugin_name == 'all':
                        self.logger.debug(
                            'All plugins are removing from ahenk')
                        self.plugin_manager.remove_plugins()
                    else:
                        for p_name in plugin_name.split(','):
                            self.logger.debug(
                                '{0} plugin is removing from ahenk'.format(
                                    p_name))
                            self.plugin_manager.remove_single_plugin(p_name)

                elif str(json_data['event']) == 'stop':
                    self.plugin_manager.process_mode('shutdown')
                    self.logger.info('Shutdown mode activated.')

                    # TODO timeout
                    while self.running_plugin() is False:
                        self.logger.debug('Waiting for progress of plugins...')
                        time.sleep(0.5)

                    Util.delete_file(System.Ahenk.fifo_file())
                    Scope().get_instance().get_custom_param(
                        'ahenk_daemon').stop()
                else:
                    self.logger.error('Unknown command error. Command:' +
                                      json_data['event'])
                self.logger.debug('Processing of handled event is completed')