gpgv.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <apt-pkg/indexcopy.h>
  6. #include <apti18n.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. #include <sstream>
  14. #include <vector>
  15. #define GNUPGPREFIX "[GNUPG:]"
  16. #define GNUPGBADSIG "[GNUPG:] BADSIG"
  17. #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
  18. #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
  19. #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
  20. #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
  21. #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
  22. #define GNUPGNODATA "[GNUPG:] NODATA"
  23. class GPGVMethod : public pkgAcqMethod
  24. {
  25. private:
  26. string VerifyGetSigners(const char *file, const char *outfile,
  27. vector<string> &GoodSigners,
  28. vector<string> &BadSigners,
  29. vector<string> &WorthlessSigners,
  30. vector<string> &NoPubKeySigners);
  31. protected:
  32. virtual bool Fetch(FetchItem *Itm);
  33. public:
  34. GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
  35. };
  36. string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
  37. vector<string> &GoodSigners,
  38. vector<string> &BadSigners,
  39. vector<string> &WorthlessSigners,
  40. vector<string> &NoPubKeySigners)
  41. {
  42. bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
  43. // setup a (empty) stringstream for formating the return value
  44. std::stringstream ret;
  45. ret.str("");
  46. if (Debug == true)
  47. std::clog << "inside VerifyGetSigners" << std::endl;
  48. int fd[2];
  49. if (pipe(fd) < 0)
  50. return "Couldn't create pipe";
  51. pid_t pid = fork();
  52. if (pid < 0)
  53. return string("Couldn't spawn new process") + strerror(errno);
  54. else if (pid == 0)
  55. {
  56. if (SigVerify::RunGPGV(outfile, file, 3, fd) == false)
  57. {
  58. // TRANSLATOR: %s is the trusted keyring parts directory
  59. ioprintf(ret, _("No keyring installed in %s."),
  60. _config->FindDir("Dir::Etc::TrustedParts").c_str());
  61. return ret.str();
  62. }
  63. exit(111);
  64. }
  65. close(fd[1]);
  66. FILE *pipein = fdopen(fd[0], "r");
  67. // Loop over the output of gpgv, and check the signatures.
  68. size_t buffersize = 64;
  69. char *buffer = (char *) malloc(buffersize);
  70. size_t bufferoff = 0;
  71. while (1)
  72. {
  73. int c;
  74. // Read a line. Sigh.
  75. while ((c = getc(pipein)) != EOF && c != '\n')
  76. {
  77. if (bufferoff == buffersize)
  78. buffer = (char *) realloc(buffer, buffersize *= 2);
  79. *(buffer+bufferoff) = c;
  80. bufferoff++;
  81. }
  82. if (bufferoff == 0 && c == EOF)
  83. break;
  84. *(buffer+bufferoff) = '\0';
  85. bufferoff = 0;
  86. if (Debug == true)
  87. std::clog << "Read: " << buffer << std::endl;
  88. // Push the data into three separate vectors, which
  89. // we later concatenate. They're kept separate so
  90. // if we improve the apt method communication stuff later
  91. // it will be better.
  92. if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
  93. {
  94. if (Debug == true)
  95. std::clog << "Got BADSIG! " << std::endl;
  96. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  97. }
  98. if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
  99. {
  100. if (Debug == true)
  101. std::clog << "Got NO_PUBKEY " << std::endl;
  102. NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  103. }
  104. if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
  105. {
  106. if (Debug == true)
  107. std::clog << "Got NODATA! " << std::endl;
  108. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  109. }
  110. if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
  111. {
  112. if (Debug == true)
  113. std::clog << "Got KEYEXPIRED! " << std::endl;
  114. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  115. }
  116. if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
  117. {
  118. if (Debug == true)
  119. std::clog << "Got REVKEYSIG! " << std::endl;
  120. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  121. }
  122. if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
  123. {
  124. char *sig = buffer + sizeof(GNUPGPREFIX);
  125. char *p = sig + sizeof("GOODSIG");
  126. while (*p && isxdigit(*p))
  127. p++;
  128. *p = 0;
  129. if (Debug == true)
  130. std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
  131. GoodSigners.push_back(string(sig));
  132. }
  133. }
  134. fclose(pipein);
  135. int status;
  136. waitpid(pid, &status, 0);
  137. if (Debug == true)
  138. {
  139. std::clog << "gpgv exited\n";
  140. }
  141. if (WEXITSTATUS(status) == 0)
  142. {
  143. if (GoodSigners.empty())
  144. return _("Internal error: Good signature, but could not determine key fingerprint?!");
  145. return "";
  146. }
  147. else if (WEXITSTATUS(status) == 1)
  148. {
  149. return _("At least one invalid signature was encountered.");
  150. }
  151. else if (WEXITSTATUS(status) == 111)
  152. {
  153. ioprintf(ret, _("Could not execute 'gpgv' to verify signature (is gpgv installed?)"));
  154. return ret.str();
  155. }
  156. else
  157. {
  158. return _("Unknown error executing gpgv");
  159. }
  160. }
  161. bool GPGVMethod::Fetch(FetchItem *Itm)
  162. {
  163. URI Get = Itm->Uri;
  164. string Path = Get.Host + Get.Path; // To account for relative paths
  165. string keyID;
  166. vector<string> GoodSigners;
  167. vector<string> BadSigners;
  168. // a worthless signature is a expired or revoked one
  169. vector<string> WorthlessSigners;
  170. vector<string> NoPubKeySigners;
  171. FetchResult Res;
  172. Res.Filename = Itm->DestFile;
  173. URIStart(Res);
  174. // Run gpgv on file, extract contents and get the key ID of the signer
  175. string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
  176. GoodSigners, BadSigners, WorthlessSigners,
  177. NoPubKeySigners);
  178. if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
  179. {
  180. string errmsg;
  181. // In this case, something bad probably happened, so we just go
  182. // with what the other method gave us for an error message.
  183. if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
  184. errmsg = msg;
  185. else
  186. {
  187. if (!BadSigners.empty())
  188. {
  189. errmsg += _("The following signatures were invalid:\n");
  190. for (vector<string>::iterator I = BadSigners.begin();
  191. I != BadSigners.end(); I++)
  192. errmsg += (*I + "\n");
  193. }
  194. if (!WorthlessSigners.empty())
  195. {
  196. errmsg += _("The following signatures were invalid:\n");
  197. for (vector<string>::iterator I = WorthlessSigners.begin();
  198. I != WorthlessSigners.end(); I++)
  199. errmsg += (*I + "\n");
  200. }
  201. if (!NoPubKeySigners.empty())
  202. {
  203. errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
  204. for (vector<string>::iterator I = NoPubKeySigners.begin();
  205. I != NoPubKeySigners.end(); I++)
  206. errmsg += (*I + "\n");
  207. }
  208. }
  209. // this is only fatal if we have no good sigs or if we have at
  210. // least one bad signature. good signatures and NoPubKey signatures
  211. // happen easily when a file is signed with multiple signatures
  212. if(GoodSigners.empty() or !BadSigners.empty())
  213. return _error->Error("%s", errmsg.c_str());
  214. }
  215. // Just pass the raw output up, because passing it as a real data
  216. // structure is too difficult with the method stuff. We keep it
  217. // as three separate vectors for future extensibility.
  218. Res.GPGVOutput = GoodSigners;
  219. Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
  220. Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
  221. URIDone(Res);
  222. if (_config->FindB("Debug::Acquire::gpgv", false))
  223. {
  224. std::clog << "gpgv succeeded\n";
  225. }
  226. return true;
  227. }
  228. int main()
  229. {
  230. setlocale(LC_ALL, "");
  231. GPGVMethod Mth;
  232. return Mth.Run();
  233. }