2020-10-29 17:03:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2020 Nico Schottelius (nico-cdist at schottelius.org)
|
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2020-10-29 17:49:20 +00:00
|
|
|
from multiprocessing import Process
|
|
|
|
import os
|
2020-10-29 22:16:08 +00:00
|
|
|
import logging
|
2020-10-29 17:03:27 +00:00
|
|
|
from scapy.all import *
|
|
|
|
|
2020-10-29 17:49:20 +00:00
|
|
|
# Datetime overwrites scapy.all.datetime - needs to be imported AFTER
|
|
|
|
import datetime
|
|
|
|
|
2020-12-11 18:37:53 +00:00
|
|
|
import cdist.config
|
|
|
|
|
2021-04-22 07:29:53 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2020-10-29 22:16:08 +00:00
|
|
|
log = logging.getLogger("scan")
|
2021-04-25 10:45:34 +00:00
|
|
|
datetime_format = '%Y-%m-%d %H:%M:%S'
|
|
|
|
|
2021-05-26 08:17:48 +00:00
|
|
|
|
2021-04-25 10:45:34 +00:00
|
|
|
class Host(object):
|
|
|
|
def __init__(self, addr, outdir, name_mapper=None):
|
|
|
|
self.addr = addr
|
|
|
|
self.workdir = os.path.join(outdir, addr)
|
|
|
|
self.name_mapper = name_mapper
|
|
|
|
|
|
|
|
os.makedirs(self.workdir, exist_ok=True)
|
|
|
|
|
|
|
|
def __get(self, key, default=None):
|
|
|
|
fname = os.path.join(self.workdir, key)
|
2021-05-26 08:17:48 +00:00
|
|
|
value = default
|
2021-04-25 10:45:34 +00:00
|
|
|
if os.path.isfile(fname):
|
|
|
|
with open(fname, "r") as fd:
|
|
|
|
value = fd.readline()
|
|
|
|
return value
|
|
|
|
|
|
|
|
def __set(self, key, value):
|
|
|
|
fname = os.path.join(self.workdir, key)
|
|
|
|
with open(fname, "w") as fd:
|
|
|
|
fd.write(f"{value}")
|
|
|
|
|
|
|
|
def name(self, default=None):
|
2021-05-26 08:17:48 +00:00
|
|
|
if self.name_mapper is None:
|
2021-04-25 10:45:34 +00:00
|
|
|
return default
|
|
|
|
|
|
|
|
fpath = os.path.join(os.getcwd(), self.name_mapper)
|
|
|
|
if os.path.isfile(fpath) and os.access(fpath, os.X_OK):
|
2021-05-26 08:17:48 +00:00
|
|
|
out = subprocess.run([fpath, self.addr], capture_output=True)
|
|
|
|
if out.returncode != 0:
|
|
|
|
return default
|
|
|
|
else:
|
2021-04-25 10:45:34 +00:00
|
|
|
value = out.stdout.decode()
|
2021-04-26 10:09:20 +00:00
|
|
|
return (default if len(value) == 0 else value)
|
2021-04-25 10:45:34 +00:00
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
def address(self):
|
|
|
|
return self.addr
|
|
|
|
|
|
|
|
def last_seen(self, default=None):
|
|
|
|
raw = self.__get('last_seen')
|
|
|
|
if raw:
|
|
|
|
return datetime.datetime.strptime(raw, datetime_format)
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
def last_configured(self, default=None):
|
|
|
|
raw = self.__get('last_configured')
|
|
|
|
if raw:
|
|
|
|
return datetime.datetime.strptime(raw, datetime_format)
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
def seen(self):
|
|
|
|
now = datetime.datetime.now().strftime(datetime_format)
|
|
|
|
self.__set('last_seen', now)
|
|
|
|
|
2021-04-26 10:09:55 +00:00
|
|
|
# XXX: There's no easy way to use the config module without feeding it with
|
|
|
|
# CLI args. Might as well call everything from scratch!
|
2021-04-25 10:45:34 +00:00
|
|
|
def configure(self):
|
2021-04-26 10:09:55 +00:00
|
|
|
target = self.name() or self.address()
|
2021-05-26 08:17:48 +00:00
|
|
|
cmd = ['cdist', 'config', '-v', target]
|
2021-04-26 10:09:55 +00:00
|
|
|
|
|
|
|
fname = os.path.join(self.workdir, 'last_configuration_log')
|
|
|
|
with open(fname, "w") as fd:
|
|
|
|
log.debug("Executing: %s", cmd)
|
|
|
|
completed_process = subprocess.run(cmd, stdout=fd, stderr=fd)
|
|
|
|
if completed_process.returncode != 0:
|
2021-05-26 08:17:48 +00:00
|
|
|
log.error("%s return with non-zero code %i - see %s for \
|
|
|
|
details.", cmd, completed_process.returncode, fname)
|
2021-04-26 10:09:55 +00:00
|
|
|
|
2021-04-25 10:45:34 +00:00
|
|
|
now = datetime.datetime.now().strftime(datetime_format)
|
|
|
|
self.__set('last_configured', now)
|
2020-10-29 22:16:08 +00:00
|
|
|
|
2021-05-26 08:17:48 +00:00
|
|
|
|
2020-10-29 17:49:20 +00:00
|
|
|
class Trigger(object):
|
|
|
|
"""
|
|
|
|
Trigger an ICMPv6EchoReply from all hosts that are alive
|
|
|
|
"""
|
|
|
|
|
2021-04-22 07:29:53 +00:00
|
|
|
def __init__(self, interfaces, sleeptime, verbose=False):
|
2020-10-29 17:49:20 +00:00
|
|
|
self.interfaces = interfaces
|
2021-04-22 07:29:53 +00:00
|
|
|
|
|
|
|
# Used by scapy / send in trigger/2.
|
2020-10-29 17:49:20 +00:00
|
|
|
self.verbose = verbose
|
|
|
|
|
2021-04-22 07:29:53 +00:00
|
|
|
# Delay in seconds between sent ICMPv6EchoRequests.
|
|
|
|
self.sleeptime = sleeptime
|
2020-10-29 17:49:20 +00:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.processes = []
|
|
|
|
for interface in self.interfaces:
|
|
|
|
p = Process(target=self.run_interface, args=(interface,))
|
|
|
|
self.processes.append(p)
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
def join(self):
|
|
|
|
for process in self.processes:
|
|
|
|
process.join()
|
|
|
|
|
|
|
|
def run_interface(self, interface):
|
|
|
|
while True:
|
|
|
|
self.trigger(interface)
|
|
|
|
time.sleep(self.sleeptime)
|
|
|
|
|
|
|
|
def trigger(self, interface):
|
2021-04-22 07:29:53 +00:00
|
|
|
try:
|
|
|
|
log.debug("Sending ICMPv6EchoRequest on %s", interface)
|
2021-05-26 08:17:48 +00:00
|
|
|
packet = IPv6(
|
|
|
|
dst="ff02::1%{}".format(interface)
|
|
|
|
) / ICMPv6EchoRequest()
|
2021-04-22 07:29:53 +00:00
|
|
|
send(packet, verbose=self.verbose)
|
|
|
|
except Exception as e:
|
2021-05-26 08:17:48 +00:00
|
|
|
log.error("Could not send ICMPv6EchoRequest: %s", e)
|
2020-10-29 17:03:27 +00:00
|
|
|
|
2020-11-03 05:43:57 +00:00
|
|
|
|
2020-10-29 17:03:27 +00:00
|
|
|
class Scanner(object):
|
2020-10-29 17:49:20 +00:00
|
|
|
"""
|
|
|
|
Scan for replies of hosts, maintain the up-to-date database
|
|
|
|
"""
|
|
|
|
|
2021-05-26 08:17:48 +00:00
|
|
|
def __init__(self, interfaces, autoconfigure=False, outdir=None,
|
|
|
|
name_mapper=None):
|
2020-10-29 17:49:20 +00:00
|
|
|
self.interfaces = interfaces
|
2021-05-26 08:17:48 +00:00
|
|
|
self.autoconfigure = autoconfigure
|
2021-04-25 10:45:34 +00:00
|
|
|
self.name_mapper = name_mapper
|
|
|
|
self.config_delay = datetime.timedelta(seconds=3600)
|
2020-10-29 17:49:20 +00:00
|
|
|
|
|
|
|
if outdir:
|
|
|
|
self.outdir = outdir
|
|
|
|
else:
|
2020-10-29 22:16:08 +00:00
|
|
|
self.outdir = os.path.join(os.environ['HOME'], '.cdist', 'scan')
|
2021-04-25 10:45:34 +00:00
|
|
|
os.makedirs(self.outdir, exist_ok=True)
|
|
|
|
|
|
|
|
self.running_configs = {}
|
2020-10-29 17:49:20 +00:00
|
|
|
|
|
|
|
def handle_pkg(self, pkg):
|
2020-10-29 17:03:27 +00:00
|
|
|
if ICMPv6EchoReply in pkg:
|
2021-04-25 10:45:34 +00:00
|
|
|
host = Host(pkg['IPv6'].src, self.outdir, self.name_mapper)
|
|
|
|
if host.name():
|
2021-05-26 08:17:48 +00:00
|
|
|
log.verbose("Host %s (%s) is alive", host.name(),
|
|
|
|
host.address())
|
2021-04-25 10:45:34 +00:00
|
|
|
else:
|
|
|
|
log.verbose("Host %s is alive", host.address())
|
2021-05-26 08:17:48 +00:00
|
|
|
|
2021-04-25 10:45:34 +00:00
|
|
|
host.seen()
|
|
|
|
|
2021-04-26 10:09:55 +00:00
|
|
|
# Configure if needed.
|
2021-04-25 10:45:34 +00:00
|
|
|
if self.autoconfigure and \
|
2021-05-26 08:17:48 +00:00
|
|
|
host.last_configured(default=datetime.datetime.min) + \
|
|
|
|
self.config_delay < datetime.datetime.now():
|
2021-04-25 10:45:34 +00:00
|
|
|
self.config(host)
|
2020-10-29 17:49:20 +00:00
|
|
|
|
2021-04-22 08:20:49 +00:00
|
|
|
def list(self):
|
2021-04-25 10:45:34 +00:00
|
|
|
hosts = []
|
|
|
|
for addr in os.listdir(self.outdir):
|
|
|
|
hosts.append(Host(addr, self.outdir, self.name_mapper))
|
2021-04-22 08:20:49 +00:00
|
|
|
|
|
|
|
return hosts
|
|
|
|
|
2021-04-25 10:45:34 +00:00
|
|
|
def config(self, host):
|
2021-05-26 08:17:48 +00:00
|
|
|
if host.name() is None:
|
|
|
|
log.debug("config - could not resolve name for %s, aborting.",
|
|
|
|
host.address())
|
2021-04-25 10:45:34 +00:00
|
|
|
return
|
|
|
|
|
2021-04-26 10:09:55 +00:00
|
|
|
previous_config_process = self.running_configs.get(host.name())
|
2021-05-26 08:17:48 +00:00
|
|
|
if previous_config_process is not None and \
|
|
|
|
previous_config_process.is_alive():
|
|
|
|
log.debug("config - is already running for %s, aborting.",
|
|
|
|
host.name())
|
2021-04-25 10:45:34 +00:00
|
|
|
|
2021-05-26 08:17:48 +00:00
|
|
|
log.info("config - running against host %s (%s).", host.name(),
|
|
|
|
host.address())
|
2021-04-26 10:09:55 +00:00
|
|
|
p = Process(target=host.configure())
|
|
|
|
p.start()
|
2021-04-25 10:45:34 +00:00
|
|
|
self.running_configs[host.name()] = p
|
|
|
|
|
2020-10-29 22:16:08 +00:00
|
|
|
def start(self):
|
|
|
|
self.process = Process(target=self.scan)
|
|
|
|
self.process.start()
|
|
|
|
|
|
|
|
def join(self):
|
|
|
|
self.process.join()
|
2020-10-29 17:03:27 +00:00
|
|
|
|
|
|
|
def scan(self):
|
2020-10-29 22:16:08 +00:00
|
|
|
log.debug("Scanning - zzzzz")
|
2021-04-22 07:29:53 +00:00
|
|
|
try:
|
|
|
|
sniff(iface=self.interfaces,
|
|
|
|
filter="icmp6",
|
|
|
|
prn=self.handle_pkg)
|
|
|
|
except Exception as e:
|
2021-05-26 08:17:48 +00:00
|
|
|
log.error("Could not start listener: %s", e)
|