gpgv.cc 8.0 KB

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