gpgv.cc 8.2 KB

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