2023-07-25 15:21:38 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from subprocess import *
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
####
|
|
|
|
# Define global variables dict, used in functions as default values.
|
|
|
|
# Its values can be changed.
|
|
|
|
#########################################################################################
|
|
|
|
|
2023-08-16 08:12:14 +02:00
|
|
|
_GLOBAL_VARIABLES = {
|
2023-07-27 15:53:10 +02:00
|
|
|
'transfer_mode' : 'tentacle',
|
|
|
|
'temporal' : '/tmp',
|
|
|
|
'data_dir' : '/var/spool/pandora/data_in/',
|
|
|
|
'tentacle_client' : 'tentacle_client',
|
|
|
|
'tentacle_ip' : '127.0.0.1',
|
|
|
|
'tentacle_port' : 41121,
|
2023-08-17 10:35:28 +02:00
|
|
|
'tentacle_extra_opts' : '',
|
|
|
|
'tentacle_retries' : 1
|
2023-07-25 15:21:38 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 14:07:57 +02:00
|
|
|
####
|
|
|
|
# Internal: Alias for output.print_debug function
|
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
def _print_debug(
|
|
|
|
var = "",
|
|
|
|
print_errors: bool = False
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Prints any list, dict, string, float or integer as a json
|
2023-08-14 15:44:56 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
var: The variable to be printed.
|
|
|
|
print_errors (bool): Whether to print errors.
|
2023-08-10 14:07:57 +02:00
|
|
|
"""
|
|
|
|
from .output import print_debug
|
|
|
|
print_debug(var, print_errors)
|
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
####
|
|
|
|
# Set a global variable with the specified name and assigns a value to it.
|
2023-07-27 15:53:10 +02:00
|
|
|
#########################################################################################
|
2023-07-25 15:21:38 +02:00
|
|
|
def set_global_variable(
|
2023-07-27 15:53:10 +02:00
|
|
|
variable_name: str = "",
|
2023-08-02 11:42:20 +02:00
|
|
|
value = None
|
2023-08-18 12:53:03 +02:00
|
|
|
)-> None:
|
2023-07-26 11:21:15 +02:00
|
|
|
"""
|
2023-08-16 08:12:14 +02:00
|
|
|
Sets the value of a global variable in the '_GLOBAL_VARIABLES' dictionary.
|
2023-07-26 11:21:15 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
variable_name (str): Name of the variable to set.
|
|
|
|
value (any): Value to assign to the variable.
|
2023-08-18 12:53:03 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
2023-07-26 11:21:15 +02:00
|
|
|
"""
|
2023-08-09 13:46:09 +02:00
|
|
|
from .general import set_dict_key_value
|
|
|
|
|
2023-08-16 08:12:14 +02:00
|
|
|
set_dict_key_value(_GLOBAL_VARIABLES, variable_name, value)
|
|
|
|
|
|
|
|
####
|
|
|
|
# Get a global variable with the specified name.
|
|
|
|
#########################################################################################
|
|
|
|
def get_global_variable(
|
|
|
|
variable_name: str = ""
|
2023-08-18 12:53:03 +02:00
|
|
|
)-> None:
|
2023-08-16 08:12:14 +02:00
|
|
|
"""
|
|
|
|
Gets the value of a global variable in the '_GLOBAL_VARIABLES' dictionary.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
variable_name (str): Name of the variable to set.
|
2023-08-18 12:53:03 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
2023-08-16 08:12:14 +02:00
|
|
|
"""
|
|
|
|
from .general import get_dict_key_value
|
|
|
|
|
|
|
|
get_dict_key_value(_GLOBAL_VARIABLES, variable_name)
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
####
|
|
|
|
# Sends file using tentacle protocol
|
2023-07-27 15:53:10 +02:00
|
|
|
#########################################################################################
|
2023-07-25 15:21:38 +02:00
|
|
|
def tentacle_xml(
|
2023-07-27 15:53:10 +02:00
|
|
|
data_file: str = "",
|
|
|
|
tentacle_ops: dict = {},
|
2023-08-16 08:12:14 +02:00
|
|
|
tentacle_path: str = _GLOBAL_VARIABLES['tentacle_client'],
|
2023-08-17 10:35:28 +02:00
|
|
|
retry: bool = False,
|
2023-07-27 15:53:10 +02:00
|
|
|
debug: int = 0,
|
|
|
|
print_errors: bool = True
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Sends file using tentacle protocol
|
2023-08-14 15:44:56 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
data_file (str): Path to the data file to be sent.
|
|
|
|
tentacle_ops (dict): Tentacle options as a dictionary (address [password] [port]).
|
|
|
|
tentacle_path (str): Custom path for the tentacle client executable.
|
2023-08-17 10:35:28 +02:00
|
|
|
retry (bool): Whether to retry sending the file if it fails.
|
2023-08-14 15:44:56 +02:00
|
|
|
debug (int): Debug mode flag. If enabled (1), the data file will not be removed after sending.
|
|
|
|
print_errors (bool): Whether to print error messages.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True for success, False for errors.
|
2023-07-25 15:21:38 +02:00
|
|
|
"""
|
2023-08-09 13:46:09 +02:00
|
|
|
from .output import print_stderr
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
if data_file is not None :
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
if not 'address' in tentacle_ops:
|
2023-08-16 08:12:14 +02:00
|
|
|
tentacle_ops['address'] = _GLOBAL_VARIABLES['tentacle_ip']
|
2023-07-27 15:53:10 +02:00
|
|
|
if not 'port' in tentacle_ops:
|
2023-08-16 08:12:14 +02:00
|
|
|
tentacle_ops['port'] = _GLOBAL_VARIABLES['tentacle_port']
|
2023-07-27 15:53:10 +02:00
|
|
|
if not 'extra_opts' in tentacle_ops:
|
2023-08-16 08:12:14 +02:00
|
|
|
tentacle_ops['extra_opts'] = _GLOBAL_VARIABLES['tentacle_extra_opts']
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
if tentacle_ops['address'] is None :
|
|
|
|
if print_errors:
|
2023-08-09 13:46:09 +02:00
|
|
|
print_stderr("Tentacle error: No address defined")
|
2023-07-27 15:53:10 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
try :
|
|
|
|
with open(data_file.strip(), 'r') as data:
|
|
|
|
data.read()
|
|
|
|
data.close()
|
|
|
|
except Exception as e :
|
|
|
|
if print_errors:
|
2023-08-09 13:46:09 +02:00
|
|
|
print_stderr(f"Tentacle error: {type(e).__name__} {e}")
|
2023-07-27 15:53:10 +02:00
|
|
|
return False
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
tentacle_cmd = f"{tentacle_path} -v -a {tentacle_ops['address']} -p {tentacle_ops['port']} {tentacle_ops['extra_opts']} {data_file.strip()}"
|
2023-08-17 11:23:36 +02:00
|
|
|
tentacle_exe=subprocess.Popen(tentacle_cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
|
|
|
|
rc=tentacle_exe.wait()
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-08-17 12:45:09 +02:00
|
|
|
result = True
|
|
|
|
|
2023-08-17 11:23:36 +02:00
|
|
|
if rc != 0 :
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-08-17 11:23:36 +02:00
|
|
|
if retry:
|
2023-08-17 10:35:28 +02:00
|
|
|
|
2023-08-17 12:45:09 +02:00
|
|
|
tentacle_retries = _GLOBAL_VARIABLES['tentacle_retries']
|
|
|
|
|
|
|
|
if tentacle_retries < 1:
|
|
|
|
tentacle_retries = 1
|
2023-08-17 10:35:28 +02:00
|
|
|
|
2023-08-17 11:23:36 +02:00
|
|
|
retry_count = 0
|
2023-08-17 10:35:28 +02:00
|
|
|
|
2023-08-17 12:45:09 +02:00
|
|
|
while retry_count < tentacle_retries :
|
2023-08-17 10:35:28 +02:00
|
|
|
|
2023-08-17 11:23:36 +02:00
|
|
|
tentacle_exe=subprocess.Popen(tentacle_cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
|
|
|
|
rc=tentacle_exe.wait()
|
2023-08-17 10:35:28 +02:00
|
|
|
|
2023-08-17 11:23:36 +02:00
|
|
|
if rc == 0:
|
|
|
|
break
|
2023-08-17 10:35:28 +02:00
|
|
|
|
2023-08-17 11:23:36 +02:00
|
|
|
if print_errors:
|
|
|
|
stderr = tentacle_exe.stderr.read().decode()
|
2023-08-17 12:45:09 +02:00
|
|
|
msg = f"Tentacle error (Retry {retry_count + 1}/{tentacle_retries}): {stderr}"
|
2023-08-17 11:23:36 +02:00
|
|
|
print_stderr(str(datetime.today().strftime('%Y-%m-%d %H:%M')) + msg)
|
|
|
|
|
|
|
|
retry_count += 1
|
2023-08-17 10:35:28 +02:00
|
|
|
|
2023-08-17 12:45:09 +02:00
|
|
|
if retry_count >= tentacle_retries:
|
|
|
|
result = False
|
2023-08-17 11:23:36 +02:00
|
|
|
else:
|
|
|
|
|
2023-08-17 10:35:28 +02:00
|
|
|
if print_errors:
|
|
|
|
stderr = tentacle_exe.stderr.read().decode()
|
|
|
|
msg="Tentacle error:" + str(stderr)
|
|
|
|
print_stderr(str(datetime.today().strftime('%Y-%m-%d %H:%M')) + msg)
|
2023-08-17 12:45:09 +02:00
|
|
|
result = False
|
2023-08-17 11:23:36 +02:00
|
|
|
|
2023-08-17 12:45:09 +02:00
|
|
|
if debug == 0 :
|
|
|
|
os.remove(data_file.strip())
|
|
|
|
|
|
|
|
return result
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
if print_errors:
|
2023-08-09 13:46:09 +02:00
|
|
|
print_stderr("Tentacle error: file path is required.")
|
2023-07-27 15:53:10 +02:00
|
|
|
return False
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
####
|
2023-07-27 15:53:10 +02:00
|
|
|
# Detect transfer mode and send XML.
|
|
|
|
#########################################################################################
|
2023-07-25 15:21:38 +02:00
|
|
|
def transfer_xml(
|
2023-07-27 15:53:10 +02:00
|
|
|
file: str = "",
|
2023-08-16 08:12:14 +02:00
|
|
|
transfer_mode: str = _GLOBAL_VARIABLES['transfer_mode'],
|
|
|
|
tentacle_ip: str = _GLOBAL_VARIABLES['tentacle_ip'],
|
|
|
|
tentacle_port: int = _GLOBAL_VARIABLES['tentacle_port'],
|
|
|
|
tentacle_extra_opts: str = _GLOBAL_VARIABLES['tentacle_extra_opts'],
|
|
|
|
data_dir: str = _GLOBAL_VARIABLES['data_dir']
|
2023-08-18 12:53:03 +02:00
|
|
|
)-> None:
|
2023-07-26 11:21:15 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Detects the transfer mode and calls the agentplugin() function to perform the transfer.
|
|
|
|
|
|
|
|
Args:
|
2023-07-27 15:53:10 +02:00
|
|
|
file (str): Path to file to send.
|
2023-08-16 08:12:14 +02:00
|
|
|
transfer_mode (str, optional): Transfer mode. Default is _GLOBAL_VARIABLES['transfer_mode'].
|
|
|
|
tentacle_ip (str, optional): IP address for Tentacle. Default is _GLOBAL_VARIABLES['tentacle_ip'].
|
|
|
|
tentacle_port (str, optional): Port for Tentacle. Default is _GLOBAL_VARIABLES['tentacle_port'].
|
|
|
|
data_dir (str, optional): Path to data dir with local transfer mode. Default is _GLOBAL_VARIABLES['data_dir'].
|
2023-08-18 12:53:03 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
2023-07-27 15:53:10 +02:00
|
|
|
"""
|
|
|
|
if file is not None:
|
|
|
|
if transfer_mode != "local":
|
|
|
|
tentacle_conf = {
|
|
|
|
'address' : tentacle_ip,
|
|
|
|
'port' : tentacle_port,
|
|
|
|
'extra_opts' : tentacle_extra_opts
|
|
|
|
}
|
|
|
|
tentacle_xml(file, tentacle_conf)
|
|
|
|
else:
|
|
|
|
shutil.move(file, data_dir)
|
|
|
|
|
|
|
|
####
|
|
|
|
# Creates a agent .data file in the specified data_dir folder
|
|
|
|
#########################################################################################
|
|
|
|
def write_xml(
|
|
|
|
xml: str = "",
|
|
|
|
agent_name: str = "",
|
2023-08-16 08:12:14 +02:00
|
|
|
data_dir: str = _GLOBAL_VARIABLES['temporal'],
|
2023-08-09 13:46:09 +02:00
|
|
|
print_errors: bool = False
|
2023-07-27 15:53:10 +02:00
|
|
|
) -> str:
|
|
|
|
"""
|
2023-08-14 15:44:56 +02:00
|
|
|
Creates an agent .data file in the specified data_dir folder
|
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
Args:
|
2023-08-14 15:44:56 +02:00
|
|
|
xml (str): XML string to be written in the file.
|
|
|
|
agent_name (str): Agent name for the XML and file name.
|
|
|
|
data_dir (str): Folder in which the file will be created.
|
|
|
|
print_errors (bool): Whether to print error messages.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: Path to the created .data file.
|
2023-07-26 11:21:15 +02:00
|
|
|
"""
|
2023-08-09 13:46:09 +02:00
|
|
|
from .general import generate_md5
|
|
|
|
from .output import print_stderr
|
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
Utime = datetime.now().strftime('%s')
|
|
|
|
agent_name_md5 = generate_md5(agent_name)
|
|
|
|
data_file = "%s/%s.%s.data" %(str(data_dir),agent_name_md5,str(Utime))
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
try:
|
|
|
|
with open(data_file, 'x') as data:
|
|
|
|
data.write(xml)
|
|
|
|
except OSError as o:
|
2023-08-09 13:46:09 +02:00
|
|
|
if print_errors:
|
|
|
|
print_stderr(f"ERROR - Could not write file: {o}, please check directory permissions")
|
2023-07-27 15:53:10 +02:00
|
|
|
except Exception as e:
|
2023-08-09 13:46:09 +02:00
|
|
|
if print_errors:
|
|
|
|
print_stderr(f"{type(e).__name__}: {e}")
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
return data_file
|