gpgv.cc 10 KB

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