Merge remote-tracking branch 'donkult/feature/generalize-gzipindex' into debian/sid
[ntk/apt.git] / methods / gpgv.cc
index 018e4f6..ae521a2 100644 (file)
@@ -1,20 +1,28 @@
-#include <apt-pkg/error.h>
+#include <config.h>
+
 #include <apt-pkg/acquire-method.h>
+#include <apt-pkg/configuration.h>
+#include <apt-pkg/error.h>
+#include <apt-pkg/gpgv.h>
 #include <apt-pkg/strutl.h>
-#include <apt-pkg/fileutl.h>
-#include <apt-pkg/indexcopy.h>
-#include <apti18n.h>
 
-#include <utime.h>
-#include <stdio.h>
-#include <fcntl.h>
+#include <ctype.h>
 #include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/wait.h>
+#include <unistd.h>
 #include <iostream>
-#include <sstream>
-
+#include <string>
 #include <vector>
 
+#include <apti18n.h>
+
+using std::string;
+using std::vector;
+
 #define GNUPGPREFIX "[GNUPG:]"
 #define GNUPGBADSIG "[GNUPG:] BADSIG"
 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
@@ -48,9 +56,6 @@ string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
                                         vector<string> &NoPubKeySigners)
 {
    bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
-   // setup a (empty) stringstream for formating the return value
-   std::stringstream ret;
-   ret.str("");
 
    if (Debug == true)
       std::clog << "inside VerifyGetSigners" << std::endl;
@@ -64,16 +69,7 @@ string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
    if (pid < 0)
       return string("Couldn't spawn new process") + strerror(errno);
    else if (pid == 0)
-   {
-      if (SigVerify::RunGPGV(outfile, file, 3, fd) == false)
-      {
-        // TRANSLATOR: %s is the trusted keyring parts directory
-        ioprintf(ret, _("No keyring installed in %s."),
-                 _config->FindDir("Dir::Etc::TrustedParts", "/etc/apt/trusted.gpg.d").c_str());
-        return ret.str();
-      }
-      exit(111);
-   }
+      ExecGPGV(outfile, file, 3, fd);
    close(fd[1]);
 
    FILE *pipein = fdopen(fd[0], "r");
@@ -89,8 +85,16 @@ string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
       // Read a line.  Sigh.
       while ((c = getc(pipein)) != EOF && c != '\n')
       {
-         if (bufferoff == buffersize)
-            buffer = (char *) realloc(buffer, buffersize *= 2);
+        if (bufferoff == buffersize)
+        {
+           char* newBuffer = (char *) realloc(buffer, buffersize *= 2);
+           if (newBuffer == NULL)
+           {
+              free(buffer);
+              return "Couldn't allocate a buffer big enough for reading";
+           }
+           buffer = newBuffer;
+        }
          *(buffer+bufferoff) = c;
          bufferoff++;
       }
@@ -149,6 +153,7 @@ string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
       }
    }
    fclose(pipein);
+   free(buffer);
 
    int status;
    waitpid(pid, &status, 0);
@@ -164,18 +169,19 @@ string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
       return "";
    }
    else if (WEXITSTATUS(status) == 1)
-   {
       return _("At least one invalid signature was encountered.");
-   }
    else if (WEXITSTATUS(status) == 111)
+      return _("Could not execute 'gpgv' to verify signature (is gpgv installed?)");
+   else if (WEXITSTATUS(status) == 112)
    {
-      ioprintf(ret, _("Could not execute 'gpgv' to verify signature (is gpgv installed?)"));
-      return ret.str();
+      // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
+      std::string errmsg;
+      //TRANSLATORS: %s is a single techy word like 'NODATA'
+      strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
+      return errmsg;
    }
    else
-   {
       return _("Unknown error executing gpgv");
-   }
 }
 
 bool GPGVMethod::Fetch(FetchItem *Itm)
@@ -210,21 +216,21 @@ bool GPGVMethod::Fetch(FetchItem *Itm)
          {
             errmsg += _("The following signatures were invalid:\n");
             for (vector<string>::iterator I = BadSigners.begin();
-                I != BadSigners.end(); I++)
+                I != BadSigners.end(); ++I)
                errmsg += (*I + "\n");
          }
          if (!WorthlessSigners.empty())
          {
             errmsg += _("The following signatures were invalid:\n");
             for (vector<string>::iterator I = WorthlessSigners.begin();
-                I != WorthlessSigners.end(); I++)
+                I != WorthlessSigners.end(); ++I)
                errmsg += (*I + "\n");
          }
          if (!NoPubKeySigners.empty())
          {
              errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
             for (vector<string>::iterator I = NoPubKeySigners.begin();
-                I != NoPubKeySigners.end(); I++)
+                I != NoPubKeySigners.end(); ++I)
                errmsg += (*I + "\n");
          }
       }