gpgv.cc 8.0 KB

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