gpgv.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if (Debug == true)
  49. std::clog << "inside VerifyGetSigners" << std::endl;
  50. int fd[2];
  51. if (pipe(fd) < 0)
  52. return "Couldn't create pipe";
  53. pid_t pid = fork();
  54. if (pid < 0)
  55. return string("Couldn't spawn new process") + strerror(errno);
  56. else if (pid == 0)
  57. ExecGPGV(outfile, file, 3, fd);
  58. close(fd[1]);
  59. FILE *pipein = fdopen(fd[0], "r");
  60. // Loop over the output of gpgv, and check the signatures.
  61. size_t buffersize = 64;
  62. char *buffer = (char *) malloc(buffersize);
  63. size_t bufferoff = 0;
  64. while (1)
  65. {
  66. int c;
  67. // Read a line. Sigh.
  68. while ((c = getc(pipein)) != EOF && c != '\n')
  69. {
  70. if (bufferoff == buffersize)
  71. {
  72. char* newBuffer = (char *) realloc(buffer, buffersize *= 2);
  73. if (newBuffer == NULL)
  74. {
  75. free(buffer);
  76. return "Couldn't allocate a buffer big enough for reading";
  77. }
  78. buffer = newBuffer;
  79. }
  80. *(buffer+bufferoff) = c;
  81. bufferoff++;
  82. }
  83. if (bufferoff == 0 && c == EOF)
  84. break;
  85. *(buffer+bufferoff) = '\0';
  86. bufferoff = 0;
  87. if (Debug == true)
  88. std::clog << "Read: " << buffer << std::endl;
  89. // Push the data into three separate vectors, which
  90. // we later concatenate. They're kept separate so
  91. // if we improve the apt method communication stuff later
  92. // it will be better.
  93. if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
  94. {
  95. if (Debug == true)
  96. std::clog << "Got BADSIG! " << std::endl;
  97. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  98. }
  99. if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
  100. {
  101. if (Debug == true)
  102. std::clog << "Got NO_PUBKEY " << std::endl;
  103. NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  104. }
  105. if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
  106. {
  107. if (Debug == true)
  108. std::clog << "Got NODATA! " << std::endl;
  109. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  110. }
  111. if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
  112. {
  113. if (Debug == true)
  114. std::clog << "Got KEYEXPIRED! " << std::endl;
  115. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  116. }
  117. if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
  118. {
  119. if (Debug == true)
  120. std::clog << "Got REVKEYSIG! " << std::endl;
  121. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  122. }
  123. if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
  124. {
  125. char *sig = buffer + sizeof(GNUPGPREFIX);
  126. char *p = sig + sizeof("GOODSIG");
  127. while (*p && isxdigit(*p))
  128. p++;
  129. *p = 0;
  130. if (Debug == true)
  131. std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
  132. GoodSigners.push_back(string(sig));
  133. }
  134. }
  135. fclose(pipein);
  136. free(buffer);
  137. int status;
  138. waitpid(pid, &status, 0);
  139. if (Debug == true)
  140. {
  141. std::clog << "gpgv exited\n";
  142. }
  143. if (WEXITSTATUS(status) == 0)
  144. {
  145. if (GoodSigners.empty())
  146. return _("Internal error: Good signature, but could not determine key fingerprint?!");
  147. return "";
  148. }
  149. else if (WEXITSTATUS(status) == 1)
  150. return _("At least one invalid signature was encountered.");
  151. else if (WEXITSTATUS(status) == 111)
  152. return _("Could not execute 'gpgv' to verify signature (is gpgv installed?)");
  153. else if (WEXITSTATUS(status) == 112)
  154. {
  155. // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
  156. std::string errmsg;
  157. //TRANSLATORS: %s is a single techy word like 'NODATA'
  158. strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
  159. return errmsg;
  160. }
  161. else
  162. return _("Unknown error executing gpgv");
  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. }