gpgv.cc 8.1 KB

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