Merge remote-tracking branch 'donkult/feature/generalize-gzipindex' into debian/sid
[ntk/apt.git] / methods / gpgv.cc
index c58e6cc..ae521a2 100644 (file)
@@ -1,16 +1,27 @@
-#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 <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"
@@ -45,114 +56,24 @@ 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;
 
-   pid_t pid;
    int fd[2];
-   FILE *pipein;
-   int status;
-   string const gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
-   // FIXME: remove support for deprecated APT::GPGV setting
-   string const trustedFile = _config->FindFile("Dir::Etc::Trusted",
-                       _config->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg").c_str());
-   string const trustedPath = _config->FindDir("Dir::Etc::TrustedParts", "/etc/apt/trusted.gpg.d");
-   if (Debug == true)
-   {
-      std::clog << "gpgv path: " << gpgvpath << std::endl;
-      std::clog << "Keyring file: " << trustedFile << std::endl;
-      std::clog << "Keyring path: " << trustedPath << std::endl;
-   }
-
-   vector<string> keyrings = GetListOfFilesInDir(trustedPath, "gpg", false);
-   if (FileExists(trustedFile) == true)
-      keyrings.push_back(trustedFile);
-
-   if (keyrings.empty() == true)
-   {
-      // TRANSLATOR: %s is the trusted keyring parts directory
-      ioprintf(ret, _("No keyring installed in %s."), trustedPath.c_str());
-      return ret.str();
-   }
 
    if (pipe(fd) < 0)
       return "Couldn't create pipe";
 
-   pid = fork();
+   pid_t pid = fork();
    if (pid < 0)
       return string("Couldn't spawn new process") + strerror(errno);
    else if (pid == 0)
-   {
-      const char *Args[400];
-      unsigned int i = 0;
-
-      Args[i++] = gpgvpath.c_str();
-      Args[i++] = "--status-fd";
-      Args[i++] = "3";
-      Args[i++] = "--ignore-time-conflict";
-      for (vector<string>::const_iterator K = keyrings.begin();
-          K != keyrings.end(); ++K)
-      {
-        Args[i++] = "--keyring";
-        Args[i++] = K->c_str();
-        // check overflow (minus a bit of extra space at the end)
-        if(i >= sizeof(Args)/sizeof(char*)-5) {
-           std::clog << _("E: Too many keyrings should be passed to gpgv. Exiting.") << std::endl;
-           exit(111);
-        }
-      }
-
-      Configuration::Item const *Opts;
-      Opts = _config->Tree("Acquire::gpgv::Options");
-      if (Opts != 0)
-      {
-         Opts = Opts->Child;
-        for (; Opts != 0; Opts = Opts->Next)
-         {
-            if (Opts->Value.empty() == true)
-               continue;
-            Args[i++] = Opts->Value.c_str();
-           // check overflow (minus a bit of extra space at the end)
-           if(i >= sizeof(Args)/sizeof(char*)-5) { 
-              std::clog << _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl;
-              exit(111);
-           }
-         }
-      }
-      Args[i++] = file;
-      Args[i++] = outfile;
-      Args[i++] = NULL;
-
-      if (Debug == true)
-      {
-         std::clog << "Preparing to exec: " << gpgvpath;
-        for(unsigned int j=0;Args[j] != NULL; j++)
-           std::clog << " " << Args[j];
-        std::clog << std::endl;
-      }
-      int const nullfd = open("/dev/null", O_RDONLY);
-      close(fd[0]);
-      // Redirect output to /dev/null; we read from the status fd
-      dup2(nullfd, STDOUT_FILENO); 
-      dup2(nullfd, STDERR_FILENO); 
-      // Redirect the pipe to the status fd (3)
-      dup2(fd[1], 3);
-
-      putenv((char *)"LANG=");
-      putenv((char *)"LC_ALL=");
-      putenv((char *)"LC_MESSAGES=");
-      execvp(gpgvpath.c_str(), (char **)Args);
-             
-      exit(111);
-   }
+      ExecGPGV(outfile, file, 3, fd);
    close(fd[1]);
 
-   pipein = fdopen(fd[0], "r"); 
-   
+   FILE *pipein = fdopen(fd[0], "r");
+
    // Loop over the output of gpgv, and check the signatures.
    size_t buffersize = 64;
    char *buffer = (char *) malloc(buffersize);
@@ -164,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++;
       }
@@ -224,7 +153,9 @@ string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
       }
    }
    fclose(pipein);
+   free(buffer);
 
+   int status;
    waitpid(pid, &status, 0);
    if (Debug == true)
    {
@@ -238,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 '%s' to verify signature (is gpgv installed?)"), gpgvpath.c_str());
-      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)
@@ -284,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");
          }
       }