From 775b7fcbfe5fed3243042e0dd523991bd2de7671 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 26 Jun 2024 01:59:33 -0400 Subject: [PATCH] Validate and normalize all command-line arguments --- ddclient.in | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/ddclient.in b/ddclient.in index 36cefc4..67279ba 100755 --- a/ddclient.in +++ b/ddclient.in @@ -1292,14 +1292,6 @@ sub main { init_config(); read_recap(opt('cache'), \%recap); 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'); update_nics(); @@ -1864,6 +1856,15 @@ sub _read_config { ###################################################################### sub init_config { %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'); @@ -1904,13 +1905,6 @@ sub init_config { $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{'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 if (defined($opt{'options'})) { # TODO: Perhaps the --options argument should be processed like the contents of the config @@ -2347,7 +2341,6 @@ sub sendmail { ###################################################################### ## split_by_comma ## default -## minimum ## opt ###################################################################### sub split_by_comma { @@ -2363,13 +2356,6 @@ sub default { # surprising behavior. 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 { my $v = shift; my $h = shift;