gpgv.cc 8.0 KB

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