gpgv.cc 8.2 KB

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