"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "ospd/misc.py" between
ospd-2.0.0.tar.gz and ospd-2.0.1.tar.gz

About: OSPd is a base class for scanner wrappers which share the same communication protocol: OSP (OpenVAS Scanner Protocol).

misc.py  (ospd-2.0.0):misc.py  (ospd-2.0.1)
skipping to change at line 102 skipping to change at line 102
result['value'] = value result['value'] = value
result['host'] = host result['host'] = host
result['hostname'] = hostname result['hostname'] = hostname
result['port'] = port result['port'] = port
result['qod'] = qod result['qod'] = qod
results = self.scans_table[scan_id]['results'] results = self.scans_table[scan_id]['results']
results.append(result) results.append(result)
# Set scan_info's results to propagate results to parent process. # Set scan_info's results to propagate results to parent process.
self.scans_table[scan_id]['results'] = results self.scans_table[scan_id]['results'] = results
def remove_hosts_from_target_progress(self, scan_id, target, hosts):
"""Remove a list of hosts from the main scan progress table to avoid
the hosts to be included in the calculation of the scan progress"""
if not hosts:
return
targets = self.scans_table[scan_id]['target_progress']
for host in hosts:
if host in targets[target]:
del targets[target][host]
# Set scan_info's target_progress to propagate progresses
# to parent process.
self.scans_table[scan_id]['target_progress'] = targets
def set_progress(self, scan_id, progress): def set_progress(self, scan_id, progress):
""" Sets scan_id scan's progress. """ """ Sets scan_id scan's progress. """
if progress > 0 and progress <= 100: if progress > 0 and progress <= 100:
self.scans_table[scan_id]['progress'] = progress self.scans_table[scan_id]['progress'] = progress
if progress == 100: if progress == 100:
self.scans_table[scan_id]['end_time'] = int(time.time()) self.scans_table[scan_id]['end_time'] = int(time.time())
def set_host_progress(self, scan_id, target, host, progress): def set_host_progress(self, scan_id, target, host, progress):
""" Sets scan_id scan's progress. """ """ Sets scan_id scan's progress. """
if progress > 0 and progress <= 100: if progress > 0 and progress <= 100:
targets = self.scans_table[scan_id]['target_progress'] targets = self.scans_table[scan_id]['target_progress']
targets[target][host] = progress targets[target][host] = progress
# Set scan_info's target_progress to propagate progresses # Set scan_info's target_progress to propagate progresses
# to parent process. # to parent process.
self.scans_table[scan_id]['target_progress'] = targets self.scans_table[scan_id]['target_progress'] = targets
def set_host_finished(self, scan_id, target, host): def set_host_finished(self, scan_id, target, host):
""" Add the host in a list of finished hosts """ """ Add the host in a list of finished hosts """
finished_hosts = self.scans_table[scan_id]['finished_hosts'] finished_hosts = self.scans_table[scan_id]['finished_hosts']
finished_hosts[target].append(host)
if host not in finished_hosts[target]:
finished_hosts[target].append(host)
self.scans_table[scan_id]['finished_hosts'] = finished_hosts self.scans_table[scan_id]['finished_hosts'] = finished_hosts
def get_hosts_unfinished(self, scan_id): def get_hosts_unfinished(self, scan_id):
""" Get a list of unfinished hosts.""" """ Get a list of unfinished hosts."""
unfinished_hosts = list() unfinished_hosts = list()
for target in self.scans_table[scan_id]['finished_hosts']: for target in self.scans_table[scan_id]['finished_hosts']:
unfinished_hosts.extend(target_str_to_list(target)) unfinished_hosts.extend(target_str_to_list(target))
for target in self.scans_table[scan_id]['finished_hosts']: for target in self.scans_table[scan_id]['finished_hosts']:
for host in self.scans_table[scan_id]['finished_hosts'][target]: for host in self.scans_table[scan_id]['finished_hosts'][target]:
skipping to change at line 217 skipping to change at line 235
if self.data_manager is None: if self.data_manager is None:
self.data_manager = multiprocessing.Manager() self.data_manager = multiprocessing.Manager()
# Check if it is possible to resume task. To avoid to resume, the # Check if it is possible to resume task. To avoid to resume, the
# scan must be deleted from the scans_table. # scan must be deleted from the scans_table.
if ( if (
scan_id scan_id
and self.id_exists(scan_id) and self.id_exists(scan_id)
and (self.get_status(scan_id) == ScanStatus.STOPPED) and (self.get_status(scan_id) == ScanStatus.STOPPED)
): ):
self.scans_table[scan_id]['end_time'] = 0
return self.resume_scan(scan_id, options) return self.resume_scan(scan_id, options)
if not options: if not options:
options = dict() options = dict()
scan_info = self.data_manager.dict() scan_info = self.data_manager.dict()
scan_info['results'] = list() scan_info['results'] = list()
scan_info['finished_hosts'] = dict( scan_info['finished_hosts'] = dict(
[[target, []] for target, _, _, _ in targets] [[target, []] for target, _, _, _, _ in targets]
) )
scan_info['progress'] = 0 scan_info['progress'] = 0
scan_info['target_progress'] = dict( scan_info['target_progress'] = dict(
[[target, {}] for target, _, _, _ in targets] [[target, {}] for target, _, _, _, _ in targets]
) )
scan_info['targets'] = targets scan_info['targets'] = targets
scan_info['vts'] = vts scan_info['vts'] = vts
scan_info['options'] = options scan_info['options'] = options
scan_info['start_time'] = int(time.time()) scan_info['start_time'] = int(time.time())
scan_info['end_time'] = "0" scan_info['end_time'] = 0
scan_info['status'] = ScanStatus.INIT scan_info['status'] = ScanStatus.INIT
if scan_id is None or scan_id == '': if scan_id is None or scan_id == '':
scan_id = str(uuid.uuid4()) scan_id = str(uuid.uuid4())
scan_info['scan_id'] = scan_id scan_info['scan_id'] = scan_id
self.scans_table[scan_id] = scan_info self.scans_table[scan_id] = scan_info
return scan_id return scan_id
def set_status(self, scan_id, status): def set_status(self, scan_id, status):
""" Sets scan_id scan's status. """ """ Sets scan_id scan's status. """
self.scans_table[scan_id]['status'] = status self.scans_table[scan_id]['status'] = status
if status == ScanStatus.STOPPED:
self.scans_table[scan_id]['end_time'] = int(time.time())
def get_status(self, scan_id): def get_status(self, scan_id):
""" Get scan_id scans's status.""" """ Get scan_id scans's status."""
return self.scans_table[scan_id]['status'] return self.scans_table[scan_id]['status']
def get_options(self, scan_id): def get_options(self, scan_id):
""" Get scan_id scan's options list. """ """ Get scan_id scan's options list. """
return self.scans_table[scan_id]['options'] return self.scans_table[scan_id]['options']
skipping to change at line 266 skipping to change at line 288
def set_option(self, scan_id, name, value): def set_option(self, scan_id, name, value):
""" Set a scan_id scan's name option to value. """ """ Set a scan_id scan's name option to value. """
self.scans_table[scan_id]['options'][name] = value self.scans_table[scan_id]['options'][name] = value
def get_progress(self, scan_id): def get_progress(self, scan_id):
""" Get a scan's current progress value. """ """ Get a scan's current progress value. """
return self.scans_table[scan_id]['progress'] return self.scans_table[scan_id]['progress']
def simplify_exclude_host_list(self, scan_id, target):
""" Remove from exclude_hosts the received hosts in the finished_hosts
list sent by the client.
The finished hosts are sent also as exclude hosts for backward
compatibility purposses.
"""
exc_hosts_list = target_str_to_list(
self.get_exclude_hosts(scan_id, target)
)
finished_hosts_list = target_str_to_list(
self.get_finished_hosts(scan_id, target)
)
if finished_hosts_list and exc_hosts_list:
for finished in finished_hosts_list:
if finished in exc_hosts_list:
exc_hosts_list.remove(finished)
return exc_hosts_list
def get_target_progress(self, scan_id, target): def get_target_progress(self, scan_id, target):
""" Get a target's current progress value. """ Get a target's current progress value.
The value is calculated with the progress of each single host The value is calculated with the progress of each single host
in the target.""" in the target."""
total_hosts = len(target_str_to_list(target)) total_hosts = len(target_str_to_list(target))
exc_hosts_list = self.simplify_exclude_host_list(scan_id, target)
exc_hosts = len(exc_hosts_list) if exc_hosts_list else 0
host_progresses = self.scans_table[scan_id]['target_progress'].get( host_progresses = self.scans_table[scan_id]['target_progress'].get(
target target
) )
try: try:
t_prog = sum(host_progresses.values()) / total_hosts t_prog = sum(host_progresses.values()) / (total_hosts - exc_hosts)
except ZeroDivisionError: except ZeroDivisionError:
LOGGER.error( LOGGER.error(
"Zero division error in %s", self.get_target_progress.__name__ "Zero division error in %s", self.get_target_progress.__name__
) )
raise raise
return t_prog return t_prog
def get_start_time(self, scan_id): def get_start_time(self, scan_id):
""" Get a scan's start time. """ """ Get a scan's start time. """
skipping to change at line 298 skipping to change at line 343
def get_end_time(self, scan_id): def get_end_time(self, scan_id):
""" Get a scan's end time. """ """ Get a scan's end time. """
return self.scans_table[scan_id]['end_time'] return self.scans_table[scan_id]['end_time']
def get_target_list(self, scan_id): def get_target_list(self, scan_id):
""" Get a scan's target list. """ """ Get a scan's target list. """
target_list = [] target_list = []
for target, _, _, _ in self.scans_table[scan_id]['targets']: for target, _, _, _, _ in self.scans_table[scan_id]['targets']:
target_list.append(target) target_list.append(target)
return target_list return target_list
def get_ports(self, scan_id, target): def get_ports(self, scan_id, target):
""" Get a scan's ports list. If a target is specified """ Get a scan's ports list. If a target is specified
it will return the corresponding port for it. If not, it will return the corresponding port for it. If not,
it returns the port item of the first nested list in it returns the port item of the first nested list in
the target's list. the target's list.
""" """
if target: if target:
skipping to change at line 323 skipping to change at line 368
return self.scans_table[scan_id]['targets'][0][1] return self.scans_table[scan_id]['targets'][0][1]
def get_exclude_hosts(self, scan_id, target): def get_exclude_hosts(self, scan_id, target):
""" Get an exclude host list for a given target. """ Get an exclude host list for a given target.
""" """
if target: if target:
for item in self.scans_table[scan_id]['targets']: for item in self.scans_table[scan_id]['targets']:
if target == item[0]: if target == item[0]:
return item[3] return item[3]
def get_finished_hosts(self, scan_id, target):
""" Get the finished host list sent by the client for a given target.
"""
if target:
for item in self.scans_table[scan_id]['targets']:
if target == item[0]:
return item[4]
def get_credentials(self, scan_id, target): def get_credentials(self, scan_id, target):
""" Get a scan's credential list. It return dictionary with """ Get a scan's credential list. It return dictionary with
the corresponding credential for a given target. the corresponding credential for a given target.
""" """
if target: if target:
for item in self.scans_table[scan_id]['targets']: for item in self.scans_table[scan_id]['targets']:
if target == item[0]: if target == item[0]:
return item[2] return item[2]
def get_vts(self, scan_id): def get_vts(self, scan_id):
 End of changes. 12 change blocks. 
6 lines changed or deleted 59 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)