Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions deadman
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,10 @@ class Deadman:

for line in configfile:

line = re.sub('\t', ' ', line)
line = re.sub('\s+', ' ', line)
line = re.sub('^#.*', '', line)
line = re.sub(';\s*#', '', line)
line = re.sub(r'\t', ' ', line)
line = re.sub(r'\s+', ' ', line)
line = re.sub(r'^#.*', '', line)
line = re.sub(r';\s*#', '', line)
line = line.strip()

if line == "":
Expand Down Expand Up @@ -801,6 +801,27 @@ def pingcmd(osname, ipv):
return None


def parse_and_validate_config(configfile):

try:
with open(configfile, 'r') as file:
for line in file:
stripped_line = line.strip()
# Only check without '#'
if not stripped_line.startswith('#'):
# Check if hping3 is installed and if the user has root privileges
if 'tcp=dstport:' in stripped_line:
if not which('hping3'):
print("hping3 is not installed. Please install it to use tcp based ping.")
sys.exit(-1)
if os.geteuid() != 0:
print("Root privileges are required to use tcp based ping. Please run as root.")
sys.exit(-1)
except FileNotFoundError:
print(f"Configuration file {configfile} not found.")
sys.exit(-1)

return None

def main(stdscr, configfile, async_mode):

Expand All @@ -810,9 +831,6 @@ def main(stdscr, configfile, async_mode):
curses.init_pair(UP_COLOR, curses.COLOR_GREEN, -1)
curses.init_pair(DOWN_COLOR, curses.COLOR_RED, -1)

"""
XXX: parse and validating config file shoud be done before curses.wrapper.
"""

deadman = Deadman(stdscr, configfile)
if async_mode:
Expand Down Expand Up @@ -847,6 +865,9 @@ if __name__ == '__main__':
BLINK_ARROW = args.blink_arrow
LOGDIR = args.logdir

# Parse and validate config file
parse_and_validate_config(args.configfile.name)

try:
curses.wrapper(main, args.configfile, args.async_mode)

Expand Down