gpgv.cc 8.2 KB

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