def get_configs_handler(self, message, command_id, user_data, command_connection, closed=False): req_id = user_data try: data_connection = self.get_configs_map[command_id] except: err_msg = 'AgentListener: Failed sending agent GET_CONFIGS response.' err_msg += '\ndata_connection object not found for command_id: %s' %\ command_id err_msg += '\nDumping get_configs_map dict:\n' err_msg += '%s' % str(self.get_configs_map) logging.error(err_msg, exc_info=True) return if closed: err_msg = 'AgentListener: Failed GET_CONFIGS from agent.\n' err_msg += 'Connection closed by agent.' logging.warning(err_msg) response = ServerInterface.build_internal_error_response(req_id) data_connection.send_message(json.dumps(response), True) return if command_id not in self.get_configs_map.keys(): err_msg = 'AgentListener: GET_CONFIGS agent request with invalid id' logging.warning(err_msg) response = ServerInterface.build_internal_error_response(req_id) data_connection.send_message(json.dumps(response), True) return configs = message response = ServerInterface.build_accepted_response(req_id) body = dict() body[AgentGetConfigsResponseBody.configs] = configs response[ResponseFields.body] = body data_connection.send_message(json.dumps(response), True)
def evaluate_restart_request(self, agent_request, req_id, data_connection): # Check the format is correct restart_request = AgentRestartRequest(agent_request) if not restart_request.sanity_check(): response = ServerInterface.build_invalid_response(req_id) data_connection.send_message(json.dumps(response), True) return # Make sure the agent tracker is up hostname = restart_request.get_hostname() if self.agent_tracker is None or self.command_tracker is None: response = ServerInterface.build_internal_error_response(req_id) data_connection.send_message(json.dumps(response), True) return # Check the host is tracked if not self.agent_tracker.have_host(hostname): response = ServerInterface.build_invalid_response(req_id) data_connection.send_message(json.dumps(response), True) return # Try sending the command command_id = self.command_tracker.send_command(hostname,\ 'GENERAL', 'RESTART') if command_id is -1: response = ServerInterface.build_internal_error_response(req_id) data_connection.send_message(json.dumps(response), True) else: response = ServerInterface.build_accepted_response(req_id) data_connection.send_message(json.dumps(response), True)
def evaluate_set_configs_request(self, agent_request, req_id,\ data_connection): # Check the format is correct set_configs_request = AgentSetConfigsRequest(agent_request) if not set_configs_request.sanity_check(): response = ServerInterface.build_invalid_response(req_id) data_connection.send_message(json.dumps(response), True) return # Make sure the agent tracker is up hostname = set_configs_request.get_hostname() if self.agent_tracker is None or self.command_tracker is None: response = ServerInterface.build_internal_error_response(req_id) data_connection.send_message(json.dumps(response), True) return # Check the host is tracked if not self.agent_tracker.have_host(hostname): response = ServerInterface.build_invalid_response(req_id) data_connection.send_message(json.dumps(response), True) return # Try sending the command configs = set_configs_request.get_configs() command_sent = self.command_tracker.send_command(hostname,\ 'GENERAL', 'SET_CONFIGS', command_body=configs) if not command_sent: response = ServerInterface.build_internal_error_response(req_id) data_connection.send_message(json.dumps(response), True) else: response = ServerInterface.build_accepted_response(req_id) data_connection.send_message(json.dumps(response), True)
def evaluate_get_users_request(self, agent_request, req_id, data_connection): usernames = self.users.keys() response = ServerInterface.build_accepted_response(req_id) body = dict() body[AgentGetUsersResponseBody.usernames] = usernames response[ResponseFields.body] = body data_connection.send_message(json.dumps(response), True)
def evaluate_del_user_request(self, agent_request, req_id, data_connection): del_user_request = AgentDelUserRequest(agent_request) if not del_user_request.sanity_check(): response = ServerInterface.build_invalid_response(req_id) data_connection.send_message(json.dumps(response), True) return username = del_user_request.get_username() self.del_user(username) response = ServerInterface.build_accepted_response(req_id) data_connection.send_message(json.dumps(response), True)
def evaluate_add_user_request(self, agent_request, req_id, data_connection): add_user_request = AgentAddUserRequest(agent_request) if not add_user_request.sanity_check(): response = ServerInterface.build_invalid_response(req_id) data_connection.send_message(json.dumps(response), True) return username = add_user_request.get_username() password = add_user_request.get_password() logging.info('########## %s %s', username, password) self.add_user(username, password) response = ServerInterface.build_accepted_response(req_id) data_connection.send_message(json.dumps(response), True)
def handle_real_time_close(self, device_sensor_request, req_id, data_connection): close_request = DeviceSensorRealTimeClose(device_sensor_request) if not close_request.sanity_check(): response = ServerInterface.build_invalid_response(req_id) data_connection.send_message(json.dumps(response), True) return original_req_id = close_request.get_request_id() gui_hostname = data_connection.peer_host try: command_id = self.active_command_connections[(gui_hostname, original_req_id)] agent_hostname = device_sensor_request.get_agent_hostname() self.command_tracker.close_command_connection(agent_hostname, command_id) except: pass response = ServerInterface.build_accepted_response(req_id) data_connection.send_message(json.dumps(response), True)
def real_time_command_callback(self, message, command_id, handler_user_data, command_connection, closed=False): if closed: logging.debug('DeviceSensor: Command Connection %d closed', command_id) message = dict() message['state'] = 'DOWN' else: message['state'] = 'UP' response = ServerInterface.build_accepted_response(-1) response[ResponseFields.response_type] = 'DEVICE_SENSOR_REAL_TIME_REQUEST' response[ResponseFields.body] = message data_connection = handler_user_data sent_ok = data_connection.send_message(json.dumps(response), True) # Check the requesting application didn't went down if not sent_ok: return False return True