Files
cacti/scripts/ping.pl
T
Thomas Vincent eb2fa88a40 hardening: validate input and fail closed in data input scripts (#8447)
* fix(scripts): fail closed in data input scripts and keep cactigetOIDs secrets private

Rejected arguments now return U instead of an unfiltered count, the Motorola script is executable and reports errors on stderr, and cactigetOIDs reads credentials from a private defaults file.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(service): make cactid's environment file optional and widen the fail2ban dates

The unit failed on systems without /etc/sysconfig/cactid, and the fail2ban filter only parsed the default Y-m-d log date.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: guard log/.htaccess file rules for Apache 2.4 and trim the release archive

Bare Order/Deny blocks returned 500 without mod_access_compat, and git archive shipped dev-only configuration.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* hardening(scripts): validate arguments and bound remote input in data input scripts

The Motorola script accepts only a bare host and port, refuses redirects, caps response size and time and passes through only numeric values; diskfree.sh and the PO validator end option parsing before their arguments.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* hardening(contrib): skip invalid agent hostnames in cactigetOIDs

Hosts whose name starts with '-' or contains whitespace are skipped with a warning, and snmpget now receives '--' before the agent and OID.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(changelog): add fragment for #8447

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(scripts): fail closed on an invalid loadavg.pl selector

An argument that is not a number left the selector undefined, so the
script printed all three averages with exit 0 and warned three times on
stderr. Print U and exit 1 instead, as the other data input scripts do.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: enforce the modem fetch deadline during blocked reads

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-11-17 13:39:42 -05:00

89 lines
2.1 KiB
Prolog

#!/usr/bin/env perl
use strict;
use warnings;
use IPC::Open3;
use Symbol 'gensym';
delete @ENV{qw(PATH)};
$ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin';
# take care for tcp:hostname or TCP:ip@
my $host = $ARGV[0] // '';
$host =~ s/^(?:tcp|udp)6?://gis;
# leave IPv6 in tact
my $host_copy = $host;
if (($host_copy =~ tr/://) == 0) {
$host =~ s/:[0-9]{1,5}$//gis;
}
# Addition2
# A leading '-' would be parsed by ping as an option rather than a host.
($host) = $host =~ /^([\w.:][\w.:-]*)$/;
if (!defined $host) {
print 'U';
exit 1;
}
# always have the language in english; LC_ALL overrides any inherited LANG
$ENV{LC_ALL}='C';
#
# Get the OS name
#
my $osname = "$^O";
sub run_ping {
my (@cmd) = @_;
my $err = gensym;
my $pid = open3(undef, my $out, $err, @cmd);
my @lines = (<$out>, <$err>);
waitpid($pid, 0);
return join('', @lines);
}
my @ping_cmd;
if ($osname =~ /solaris/i) {
# Solaris needs a different ping command
@ping_cmd = ('ping', '-s', $host, '64', '1');
} else {
@ping_cmd = ('ping', '-c', '1', $host);
}
my $output = run_ping(@ping_cmd);
my $ping = '';
for my $line (split /\n/, $output) {
if ($line =~ /((?:icmp_)?[sr]eq=.*t(ime|emps)|unknown host|Unknown host|not supported|Name or service not known|No address associated with name)/) {
$ping = $line;
last;
}
}
chomp($ping);
if (($ping =~ 'unknown host') || ($ping =~ 'Unknown host') || ($ping =~ 'not supported') || ($ping =~ 'Name or service not known') || ($ping =~ 'No address associated with name')) {
if ((-f '/bin/ping6') || (-f '/sbin/ping6')) {
my $output6 = run_ping('ping6', '-c', '1', $host);
for my $line (split /\n/, $output6) {
if ($line =~ /((?:icmp_)?[sr]eq=.*t(ime|emps))/) {
$ping = $line;
last;
}
}
chomp($ping);
}
}
if ($ping =~ m/(.*t(?:ime|emps)=)([\w\.]*).*(ms|usec)/) {
my ($value, $unit) = ($2, $3);
if ($value eq '') {
print 'U'; # avoid cacti errors, but do not fake rrdtool stats
} elsif ($unit eq 'usec') {
print $value/1000; # re-calculate in units of 'ms'
} else {
print $value;
}
} else {
print 'U';
}