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-02 11:32:34 +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,
|
|
|
|
'tentacle_extra_opts' : ''
|
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
|
|
|
|
"""
|
|
|
|
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-07-25 15:21:38 +02:00
|
|
|
):
|
2023-07-26 11:21:15 +02:00
|
|
|
"""
|
2023-08-02 11:32:34 +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-09 13:46:09 +02:00
|
|
|
from .general import set_dict_key_value
|
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
set_dict_key_value(GLOBAL_VARIABLES, variable_name, value)
|
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-02 11:32:34 +02:00
|
|
|
tentacle_path: str = GLOBAL_VARIABLES['tentacle_client'],
|
2023-07-27 15:53:10 +02:00
|
|
|
debug: int = 0,
|
|
|
|
print_errors: bool = True
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Sends file using tentacle protocol
|
2023-07-25 15:21:38 +02:00
|
|
|
- Only works with one file at time.
|
|
|
|
- file variable needs full file path.
|
|
|
|
- tentacle_opts should be a dict with tentacle options (address [password] [port]).
|
|
|
|
- tentacle_path allows to define a custom path for tentacle client in case is not in sys path).
|
|
|
|
- if debug is enabled, the data file will not be removed after being sent.
|
2023-07-27 15:53:10 +02:00
|
|
|
- if print_errors is enabled, function will print all error messages
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
Returns True for OK and 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-02 11:32:34 +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-02 11:32:34 +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-02 11:32:34 +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-07-25 15:21:38 +02:00
|
|
|
|
2023-08-09 13:46:09 +02:00
|
|
|
tentacle_exe=subprocess.Popen(tentacle_cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
|
2023-07-27 15:53:10 +02:00
|
|
|
rc=tentacle_exe.wait()
|
|
|
|
|
|
|
|
if debug == 0 :
|
|
|
|
os.remove(data_file.strip())
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
if rc != 0 :
|
|
|
|
if print_errors:
|
|
|
|
stderr = tentacle_exe.stderr.read().decode()
|
|
|
|
msg="Tentacle error:" + str(stderr)
|
2023-08-09 13:46:09 +02:00
|
|
|
print_stderr(str(datetime.today().strftime('%Y-%m-%d %H:%M')) + msg)
|
2023-07-27 15:53:10 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
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-02 11:32:34 +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-07-25 15:21:38 +02:00
|
|
|
):
|
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-02 11:32:34 +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-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-09 13:46:09 +02:00
|
|
|
data_dir: str = GLOBAL_VARIABLES['temporal'],
|
|
|
|
print_errors: bool = False
|
2023-07-27 15:53:10 +02:00
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Creates a agent .data file in the specified data_dir folder
|
|
|
|
Args:
|
|
|
|
- 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.
|
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
|