gpgv.cc 8.2 KB

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