gpgv.cc 8.3 KB

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