gpgv.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include <apt-pkg/error.h>
  2. #include <apt-pkg/acquire-method.h>
  3. #include <apt-pkg/strutl.h>
  4. #include <apti18n.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <utime.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include <sys/wait.h>
  12. #include <iostream>
  13. #define GNUPGPREFIX "[GNUPG:]"
  14. #define GNUPGBADSIG "[GNUPG:] BADSIG"
  15. #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
  16. #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
  17. class GPGVMethod : public pkgAcqMethod
  18. {
  19. private:
  20. const char *VerifyGetSigners(const char *file, const char *outfile,
  21. vector<string> &GoodSigners, vector<string> &BadSigners,
  22. vector<string> &NoPubKeySigners);
  23. protected:
  24. virtual bool Fetch(FetchItem *Itm);
  25. public:
  26. GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
  27. };
  28. const char *GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
  29. vector<string> &GoodSigners,
  30. vector<string> &BadSigners,
  31. vector<string> &NoPubKeySigners)
  32. {
  33. if (_config->FindB("Debug::Acquire::gpgv", false))
  34. {
  35. std::cerr << "inside VerifyGetSigners" << std::endl;
  36. }
  37. pid_t pid;
  38. int fd[2];
  39. FILE *pipein;
  40. int status;
  41. struct stat buff;
  42. string gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
  43. string pubringpath = _config->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
  44. if (_config->FindB("Debug::Acquire::gpgv", false))
  45. {
  46. std::cerr << "gpgv path: " << gpgvpath << std::endl;
  47. std::cerr << "Keyring path: " << pubringpath << std::endl;
  48. }
  49. if (stat(pubringpath.c_str(), &buff) != 0)
  50. return (string("Couldn't access keyring: ") + strerror(errno)).c_str();
  51. if (pipe(fd) < 0)
  52. {
  53. return "Couldn't create pipe";
  54. }
  55. pid = fork();
  56. if (pid < 0)
  57. {
  58. return (string("Couldn't spawn new process") + strerror(errno)).c_str();
  59. }
  60. else if (pid == 0)
  61. {
  62. const char *Args[400];
  63. unsigned int i = 0;
  64. Args[i++] = gpgvpath.c_str();
  65. Args[i++] = "--status-fd";
  66. Args[i++] = "3";
  67. Args[i++] = "--keyring";
  68. Args[i++] = pubringpath.c_str();
  69. Configuration::Item const *Opts;
  70. Opts = _config->Tree("Acquire::gpgv::Options");
  71. if (Opts != 0)
  72. {
  73. Opts = Opts->Child;
  74. for (; Opts != 0; Opts = Opts->Next)
  75. {
  76. if (Opts->Value.empty() == true)
  77. continue;
  78. Args[i++] = Opts->Value.c_str();
  79. if(i >= 395) {
  80. std::cerr << _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl;
  81. exit(111);
  82. }
  83. }
  84. }
  85. Args[i++] = file;
  86. Args[i++] = outfile;
  87. Args[i++] = NULL;
  88. if (_config->FindB("Debug::Acquire::gpgv", false))
  89. {
  90. std::cerr << "Preparing to exec: " << gpgvpath;
  91. for(unsigned int j=0;Args[j] != NULL; j++)
  92. std::cerr << " " << Args[j];
  93. std::cerr << std::endl;
  94. }
  95. int nullfd = open("/dev/null", O_RDONLY);
  96. close(fd[0]);
  97. // Redirect output to /dev/null; we read from the status fd
  98. dup2(nullfd, STDOUT_FILENO);
  99. dup2(nullfd, STDERR_FILENO);
  100. // Redirect the pipe to the status fd (3)
  101. dup2(fd[1], 3);
  102. putenv("LANG=");
  103. putenv("LC_ALL=");
  104. putenv("LC_MESSAGES=");
  105. execvp(gpgvpath.c_str(), (char **)Args);
  106. exit(111);
  107. }
  108. close(fd[1]);
  109. pipein = fdopen(fd[0], "r");
  110. // Loop over the output of gpgv, and check the signatures.
  111. size_t buffersize = 64;
  112. char *buffer = (char *) malloc(buffersize);
  113. size_t bufferoff = 0;
  114. while (1)
  115. {
  116. int c;
  117. // Read a line. Sigh.
  118. while ((c = getc(pipein)) != EOF && c != '\n')
  119. {
  120. if (bufferoff == buffersize)
  121. buffer = (char *) realloc(buffer, buffersize *= 2);
  122. *(buffer+bufferoff) = c;
  123. bufferoff++;
  124. }
  125. if (bufferoff == 0 && c == EOF)
  126. break;
  127. *(buffer+bufferoff) = '\0';
  128. bufferoff = 0;
  129. if (_config->FindB("Debug::Acquire::gpgv", false))
  130. std::cerr << "Read: " << buffer << std::endl;
  131. // Push the data into three separate vectors, which
  132. // we later concatenate. They're kept separate so
  133. // if we improve the apt method communication stuff later
  134. // it will be better.
  135. if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
  136. {
  137. if (_config->FindB("Debug::Acquire::gpgv", false))
  138. std::cerr << "Got BADSIG! " << std::endl;
  139. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  140. }
  141. if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
  142. {
  143. if (_config->FindB("Debug::Acquire::gpgv", false))
  144. std::cerr << "Got NO_PUBKEY " << std::endl;
  145. NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  146. }
  147. if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0)
  148. {
  149. char *sig = buffer + sizeof(GNUPGPREFIX);
  150. char *p = sig + sizeof("VALIDSIG");
  151. while (*p && isxdigit(*p))
  152. p++;
  153. *p = 0;
  154. if (_config->FindB("Debug::Acquire::gpgv", false))
  155. std::cerr << "Got VALIDSIG, key ID:" << sig << std::endl;
  156. GoodSigners.push_back(string(sig));
  157. }
  158. }
  159. fclose(pipein);
  160. waitpid(pid, &status, 0);
  161. if (_config->FindB("Debug::Acquire::gpgv", false))
  162. {
  163. std::cerr << "gpgv exited\n";
  164. }
  165. if (WEXITSTATUS(status) == 0)
  166. {
  167. if (GoodSigners.empty())
  168. return _("Internal error: Good signature, but could not determine key fingerprint?!");
  169. return NULL;
  170. }
  171. else if (WEXITSTATUS(status) == 1)
  172. {
  173. return _("At least one invalid signature was encountered.");
  174. }
  175. else if (WEXITSTATUS(status) == 111)
  176. {
  177. // FIXME String concatenation considered harmful.
  178. return (string(_("Could not execute ")) + gpgvpath +
  179. string(_(" to verify signature (is gnupg installed?)"))).c_str();
  180. }
  181. else
  182. {
  183. return _("Unknown error executing gpgv");
  184. }
  185. }
  186. bool GPGVMethod::Fetch(FetchItem *Itm)
  187. {
  188. URI Get = Itm->Uri;
  189. string Path = Get.Host + Get.Path; // To account for relative paths
  190. string keyID;
  191. vector<string> GoodSigners;
  192. vector<string> BadSigners;
  193. vector<string> NoPubKeySigners;
  194. FetchResult Res;
  195. Res.Filename = Itm->DestFile;
  196. URIStart(Res);
  197. // Run gpgv on file, extract contents and get the key ID of the signer
  198. const char *msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
  199. GoodSigners, BadSigners, NoPubKeySigners);
  200. if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
  201. {
  202. string errmsg;
  203. // In this case, something bad probably happened, so we just go
  204. // with what the other method gave us for an error message.
  205. if (BadSigners.empty() && NoPubKeySigners.empty())
  206. errmsg = msg;
  207. else
  208. {
  209. if (!BadSigners.empty())
  210. {
  211. errmsg += _("The following signatures were invalid:\n");
  212. for (vector<string>::iterator I = BadSigners.begin();
  213. I != BadSigners.end(); I++)
  214. errmsg += (*I + "\n");
  215. }
  216. if (!NoPubKeySigners.empty())
  217. {
  218. errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
  219. for (vector<string>::iterator I = NoPubKeySigners.begin();
  220. I != NoPubKeySigners.end(); I++)
  221. errmsg += (*I + "\n");
  222. }
  223. }
  224. return _error->Error(errmsg.c_str());
  225. }
  226. // Transfer the modification times
  227. struct stat Buf;
  228. if (stat(Path.c_str(),&Buf) != 0)
  229. return _error->Errno("stat",_("Failed to stat %s"), Path.c_str());
  230. struct utimbuf TimeBuf;
  231. TimeBuf.actime = Buf.st_atime;
  232. TimeBuf.modtime = Buf.st_mtime;
  233. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  234. return _error->Errno("utime",_("Failed to set modification time"));
  235. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  236. return _error->Errno("stat",_("Failed to stat"));
  237. // Return a Done response
  238. Res.LastModified = Buf.st_mtime;
  239. Res.Size = Buf.st_size;
  240. // Just pass the raw output up, because passing it as a real data
  241. // structure is too difficult with the method stuff. We keep it
  242. // as three separate vectors for future extensibility.
  243. Res.GPGVOutput = GoodSigners;
  244. Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
  245. Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
  246. URIDone(Res);
  247. if (_config->FindB("Debug::Acquire::gpgv", false))
  248. {
  249. std::cerr << "gpgv succeeded\n";
  250. }
  251. return true;
  252. }
  253. int main()
  254. {
  255. setlocale(LC_ALL, "");
  256. GPGVMethod Mth;
  257. return Mth.Run();
  258. }