Change DEBUG, NOMAIL, TSTAMPCHECK mode flags to constants
authorJacob Bachmeyer <jcb@gnu.org>
Thu, 27 Jan 2022 05:13:50 +0000 (23:13 -0600)
committerJacob Bachmeyer <jcb@gnu.org>
Thu, 27 Jan 2022 05:13:50 +0000 (23:13 -0600)
This simplifies coverage analysis reports, since these flags are only set
while initializing the script and never changed during a run.

upload-ftp-v1.2.pl

index 51155162177a840701bc0f7eb53fd7f27cd65eed..f3d085d81e4e4b6cd39d336f9ad97fa4c23a3e03 100755 (executable)
@@ -149,12 +149,12 @@ BEGIN {
   our $help = '';
   our $version = '';
   # Set this to 1 or higher to get debug output in the log file.
-  our $DEBUG = 1;
+  my $DEBUG = 1;
 
-  our $NOMAIL = 0;
+  my $NOMAIL = 0;
 
   # Set this to 0 to disable the timestamp check on uploaded files in sub scan_incoming
-  our $TSTAMPCHECK = 1;
+  my $TSTAMPCHECK = 1;
 
   my $TestingMode = 0;
 
@@ -167,6 +167,10 @@ BEGIN {
             'testing-this-script' => \$TestingMode,
            );
 
+  constant->import(DEBUG => $DEBUG);
+  constant->import(NOMAIL => $NOMAIL);
+  constant->import(TSTAMPCHECK => $TSTAMPCHECK);
+
   constant->import(IN_TEST_MODE => $TestingMode);
 
   if ($TestingMode) {
@@ -188,9 +192,6 @@ BEGIN {
 our $style;
 our $help;
 our $version;
-our $DEBUG;
-our $NOMAIL;
-our $TSTAMPCHECK;
 
 &version_information () if ($version);
 &usage_information() if ($help);
@@ -507,7 +508,7 @@ sub scan_incoming {
 
     # Don't look at files with really long names, either.
     next if length ($ent) > 100;
-    ftp_syslog('debug', "($log_style) DEBUG: " . "uploaded file to check: $ent") if $DEBUG;
+    ftp_syslog('debug', "($log_style) DEBUG: " . "uploaded file to check: $ent") if DEBUG;
     $possible{$ent} = 1;
   }
   closedir (INCOMING) || ftp_die("FATAL: closedir($incoming_dir) failed: $!");
@@ -537,14 +538,14 @@ sub scan_incoming {
        # On modern (Debian) systems, condition a) is not met.
   my @lsof_args = (LSOF_BIN, "-Fn",
        map { "$incoming_dir/$_" } keys %possible);
-  ftp_syslog('debug', "($log_style) DEBUG: " . "lsof command line: " . join(' ',@lsof_args)) if $DEBUG;
+  ftp_syslog('debug', "($log_style) DEBUG: " . "lsof command line: " . join(' ',@lsof_args)) if DEBUG;
   my $pid = open (LSOF, "-|");
 
   if ($pid) {      # parent
     while (defined (my $line = <LSOF>)) {
-      ftp_syslog('debug', "($log_style) DEBUG: " . "lsof output: $line") if $DEBUG;
+      ftp_syslog('debug', "($log_style) DEBUG: " . "lsof output: $line") if DEBUG;
       next unless $line =~ /^n${incoming_dir}\/(.+)$/;  # only look at the name lines.
-      ftp_syslog('debug', "($log_style) DEBUG: " . "upload in progress for $1, ignoring during this run") if $DEBUG;
+      ftp_syslog('debug', "($log_style) DEBUG: " . "upload in progress for $1, ignoring during this run") if DEBUG;
       delete ($possible{$1}) || ftp_warn("WARNING: lsof found unrequested but open $1?!");
     }
     close (LSOF);
@@ -555,11 +556,11 @@ sub scan_incoming {
        # Do not consider files that have been modified in the last 2 minutes
        # This is an extra safety check to avoid trying to process files that are still
        # being uploaded.
-  if ($TSTAMPCHECK) {
+  if (TSTAMPCHECK) {
     for my $ent (keys %possible) {
                my @stat = stat($ent);
                if ($stat[9] >= time - 120) {
-       ftp_syslog('debug', "($log_style) DEBUG: " . "$ent has been modified in the last 2 minutes, skipping") if $DEBUG;
+       ftp_syslog('debug', "($log_style) DEBUG: " . "$ent has been modified in the last 2 minutes, skipping") if DEBUG;
                        delete ($possible{$ent});
                        next;
                }
@@ -573,7 +574,7 @@ sub scan_incoming {
     my $directive = "$base.directive.asc";
     my $bare_base = $base;
     $bare_base =~ s/\.directive\.asc$//g;
-    ftp_syslog('debug', "($log_style) DEBUG: " . "considering $ent for processing") if $DEBUG;
+    ftp_syslog('debug', "($log_style) DEBUG: " . "considering $ent for processing") if DEBUG;
 
     # work on this triple, if all three files exist, and the signature
     # and directive files aren't huge.  We want to exclude huge files
@@ -661,7 +662,7 @@ sub keyring_file {
   my $tmp = $directory;
   while (1) {
     if (-e "$package_config_base/$tmp/pubring.gpg") {
-      ftp_syslog('debug', "($log_style) DEBUG: " . "found keyring $package_config_base/$tmp/pubring.gpg") if $DEBUG;
+      ftp_syslog('debug', "($log_style) DEBUG: " . "found keyring $package_config_base/$tmp/pubring.gpg") if DEBUG;
       push(@pubrings,"$package_config_base/$tmp/pubring.gpg");
     }
     my $tmp2 = $tmp;
@@ -877,9 +878,9 @@ sub read_directive_file {
   # for debugging purposes. After this point, we don't need to pass the
   # $directive_file_contents to any subsequent &fatal calls.
   if (defined $info{'package'}) {
-    debug($directive_file_contents, $info{'package'}) if $DEBUG;
+    debug($directive_file_contents, $info{'package'}) if DEBUG;
   } else {
-    debug($directive_file_contents, '') if $DEBUG;
+    debug($directive_file_contents, '') if DEBUG;
   }
 
   # They have to specify a directory directive.
@@ -1053,7 +1054,7 @@ sub verify_keyring {
   my ($directive_file, $directive_file_contents, @keyrings) = @_;
 
   my $directive_file_size = -s $directive_file;
-  ftp_syslog('debug', "($log_style) DEBUG: $directive_file size is $directive_file_size") if $DEBUG;
+  ftp_syslog('debug', "($log_style) DEBUG: $directive_file size is $directive_file_size") if DEBUG;
 
   foreach (@keyrings) {
     # We need what gpgv writes to STDERR to determine the timestamp
@@ -1067,7 +1068,7 @@ sub verify_keyring {
     ($verify_str) = $verify_str =~ /^(.*)$/;
 
     ftp_syslog('debug',"($log_style) DEBUG: gpgv command line: $verify_str\n")
-      if ($DEBUG > 0);
+      if (DEBUG > 0);
     my $retval = '';
     open (GPGV, "$verify_str|")
       or &fatal("failed to run command: $verify_str",1);
@@ -1105,8 +1106,8 @@ sub check_files {
   my $sig_file_size = -s $sig_file;
   my $upload_file_size = -s $upload_file;
 
-  ftp_syslog('debug', "($log_style) DEBUG: $sig_file size is $sig_file_size") if $DEBUG;
-  ftp_syslog('debug', "($log_style) DEBUG: $upload_file size is $upload_file_size") if $DEBUG;
+  ftp_syslog('debug', "($log_style) DEBUG: $sig_file size is $sig_file_size") if DEBUG;
+  ftp_syslog('debug', "($log_style) DEBUG: $upload_file size is $upload_file_size") if DEBUG;
 
   my @keyrings = &keyring_file ($info{package},$info{directory});
   &fatal("no keyring for package $info{package}",0) if ($#keyrings < 0);
@@ -1124,10 +1125,10 @@ sub check_files {
   $valid
     or &fatal("gpg verify of upload file ($upload_file) failed",1);
 
-  my ($error_string, $error_log_ref) = check_vulnerabilities($upload_file,$log_style,$DEBUG);
+  my ($error_string, $error_log_ref) = check_vulnerabilities($upload_file,$log_style,DEBUG);
 
   my @error_log = @$error_log_ref;
-  if ($DEBUG and $#error_log > -1) {
+  if (DEBUG and $#error_log > -1) {
     foreach (@error_log) {
       ftp_syslog('debug', $_);
     }
@@ -1135,7 +1136,7 @@ sub check_files {
 
   &fatal($error_string,1,'',3) if ($error_string ne '');
 
-  ftp_syslog('debug', "($log_style) DEBUG: tested negative for CVE-2009-4029 and CVE-2012-3386") if $DEBUG;
+  ftp_syslog('debug', "($log_style) DEBUG: tested negative for CVE-2009-4029 and CVE-2012-3386") if DEBUG;
 }
 
 
@@ -1232,7 +1233,7 @@ sub cleanup_dir {
     $file =~ /^(.*)$/; $file = $1;
     my $mtime = $tmp[9];
     $mtime =~ /^(.*)$/; $mtime = $1;
-    ftp_syslog('debug',"($log_style) DEBUG: Removing $file, older than 24 hours (mtime: $tmp[9])\n") if ((time() > ($tmp[9]+24*3600)) && ($DEBUG > 0));
+    ftp_syslog('debug',"($log_style) DEBUG: Removing $file, older than 24 hours (mtime: $tmp[9])\n") if ((time() > ($tmp[9]+24*3600)) && (DEBUG > 0));
        unlink ("$dir/.$file");  # don't worry if it doesn't exist
        rename ("$dir/$file", "$dir/.$file") if (time() > ($mtime+24*3600));
   }
@@ -1241,7 +1242,7 @@ sub cleanup_dir {
 sub cleanup {
     for my $dir ($incoming_dir, $incoming_tmp, $desttmp) {
   for my $f (@_) {
-      ftp_syslog('debug',"($log_style) DEBUG: cleaning up $dir/$f\n") if ($DEBUG > 1);
+      ftp_syslog('debug',"($log_style) DEBUG: cleaning up $dir/$f\n") if (DEBUG > 1);
       # if we quit early enough, they might not be there.
       next unless defined $f && -e "$dir/$f";
 
@@ -1270,7 +1271,7 @@ sub fatal {
   my $exit_code = shift;
 
   $directive_file_contents ||= '';
-  if (($directive_file_contents ne '') && $DEBUG) {
+  if (($directive_file_contents ne '') && DEBUG) {
     &mail ($directive_file_contents,0,"debug: directive file contents");
   }
 
@@ -1339,7 +1340,7 @@ sub mail {
     ftp_syslog('info', "($log_style) No uploader e-mail address(es) to report this error to!");
     @email_list = ($email_always);
   }
-  if ($NOMAIL) {
+  if (NOMAIL) {
     ftp_syslog('info', "($log_style) NOMAIL is set - not sending email to @email_list");
   } else {
     ftp_syslog('info', "($log_style) Sending email to @email_list");
@@ -1353,7 +1354,7 @@ sub mail {
   #print STDERR "final emails: @email_list\n";
   # return @_;
 
-  if ($NOMAIL) {
+  if (NOMAIL) {
       if ($subject ne '') {
          ftp_syslog('info', "($log_style) Subject: '$subject'");
       } elsif (defined $info{package}) {
@@ -1411,7 +1412,7 @@ sub debug {
   my $msg = shift;
   my $package_name = shift;
 
-  if ($NOMAIL) {
+  if (NOMAIL) {
       ftp_syslog('info', "($log_style) Subject: [$m_style gnu-ftp-debug] new upload processed: $package_name\nBody: $msg");
   } else {
       my $smtp;