Validate and normalize all command-line arguments

This commit is contained in:
Richard Hansen 2024-06-26 01:59:33 -04:00
parent bbf98dd031
commit 775b7fcbfe

View file

@ -1292,14 +1292,6 @@ sub main {
init_config(); init_config();
read_recap(opt('cache'), \%recap); read_recap(opt('cache'), \%recap);
print_info() if opt('debug') && opt('verbose'); print_info() if opt('debug') && opt('verbose');
fatal("invalid argument '--use=%s'; possible values are:\n%s",
opt('use'), join("\n", ip_strategies_usage()))
if defined(opt('use')) && !$ip_strategies{lc(opt('use'))};
fatal("invalid argument '--usev6=%s'; possible values are:\n%s",
opt('usev6'), join("\n", ipv6_strategies_usage()))
if defined(opt('usev6')) && !$ipv6_strategies{lc(opt('usev6'))};
$daemon = opt('daemon'); $daemon = opt('daemon');
update_nics(); update_nics();
@ -1864,6 +1856,15 @@ sub _read_config {
###################################################################### ######################################################################
sub init_config { sub init_config {
%opt = %saved_opt; %opt = %saved_opt;
# TODO: This might grab an arbitrary protocol-specific variable definition, which could cause
# surprising behavior.
for my $var (keys(%{$variables{'merged'}})) {
# TODO: Also validate $opt{'options'}.
next if !defined($opt{$var}) || ref($opt{$var});
if (!eval { $opt{$var} = check_value($opt{$var}, $variables{'merged'}{$var}); 1; }) {
fatal("invalid argument '--$var=$opt{$var}': $@");
}
}
## ##
$opt{'quiet'} = 0 if opt('verbose'); $opt{'quiet'} = 0 if opt('verbose');
@ -1904,13 +1905,6 @@ sub init_config {
$opt{'min-interval'} = max(interval(opt('min-interval')), interval(default('min-interval'))); $opt{'min-interval'} = max(interval(opt('min-interval')), interval(default('min-interval')));
$opt{'min-error-interval'} = max(interval(opt('min-error-interval')), interval(default('min-error-interval'))); $opt{'min-error-interval'} = max(interval(opt('min-error-interval')), interval(default('min-error-interval')));
$opt{'timeout'} = 0 if opt('timeout') < 0;
## parse an interval expression (such as '5m') into number of seconds
$opt{'daemon'} = interval(opt('daemon')) if defined($opt{'daemon'});
## make sure the interval isn't too short
$opt{'daemon'} = minimum('daemon') if opt('daemon') && opt('daemon') < minimum('daemon');
## define or modify host options specified on the command-line ## define or modify host options specified on the command-line
if (defined($opt{'options'})) { if (defined($opt{'options'})) {
# TODO: Perhaps the --options argument should be processed like the contents of the config # TODO: Perhaps the --options argument should be processed like the contents of the config
@ -2347,7 +2341,6 @@ sub sendmail {
###################################################################### ######################################################################
## split_by_comma ## split_by_comma
## default ## default
## minimum
## opt ## opt
###################################################################### ######################################################################
sub split_by_comma { sub split_by_comma {
@ -2363,13 +2356,6 @@ sub default {
# surprising behavior. # surprising behavior.
return $variables{'merged'}{$v}{'default'}; return $variables{'merged'}{$v}{'default'};
} }
sub minimum {
my $v = shift;
return undef if !defined($variables{'merged'}{$v});
# TODO: This might grab an arbitrary protocol-specific variable definition, which could cause
# surprising behavior.
return $variables{'merged'}{$v}{'minimum'};
}
sub opt { sub opt {
my $v = shift; my $v = shift;
my $h = shift; my $h = shift;