gpgv.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <apt-pkg/error.h>
  2. #include <apt-pkg/acquire-method.h>
  3. #include <apt-pkg/strutl.h>
  4. #include <apt-pkg/fileutl.h>
  5. #include <apt-pkg/indexcopy.h>
  6. #include <apti18n.h>
  7. #include <utime.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include <sys/wait.h>
  12. #include <iostream>
  13. #include <sstream>
  14. #include <vector>
  15. #define GNUPGPREFIX "[GNUPG:]"
  16. #define GNUPGBADSIG "[GNUPG:] BADSIG"
  17. #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
  18. #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
  19. #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
  20. #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
  21. #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
  22. #define GNUPGNODATA "[GNUPG:] NODATA"
  23. class GPGVMethod : public pkgAcqMethod
  24. {
  25. private:
  26. string VerifyGetSigners(const char *file, const char *outfile,
  27. vector<string> &GoodSigners,
  28. vector<string> &BadSigners,
  29. vector<string> &WorthlessSigners,
  30. vector<string> &NoPubKeySigners);
  31. protected:
  32. virtual bool Fetch(FetchItem *Itm);
  33. public:
  34. GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
  35. };
  36. string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
  37. vector<string> &GoodSigners,
  38. vector<string> &BadSigners,
  39. vector<string> &WorthlessSigners,
  40. vector<string> &NoPubKeySigners)
  41. {
  42. bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
  43. // setup a (empty) stringstream for formating the return value
  44. std::stringstream ret;
  45. ret.str("");
  46. if (Debug == true)
  47. std::clog << "inside VerifyGetSigners" << std::endl;
  48. pid_t pid;
  49. int fd[2];
  50. FILE *pipein;
  51. int status;
  52. string const gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
  53. std::vector<const char*> Args = SigVerify::GetGPGVCommandLine();
  54. if (Args.empty() == true)
  55. {
  56. // TRANSLATOR: %s is the trusted keyring parts directory
  57. ioprintf(ret, _("No keyring installed in %s."),
  58. _config->FindDir("Dir::Etc::TrustedParts", "/etc/apt/trusted.gpg.d").c_str());
  59. return ret.str();
  60. }
  61. if (pipe(fd) < 0)
  62. return "Couldn't create pipe";
  63. pid = fork();
  64. if (pid < 0)
  65. return string("Couldn't spawn new process") + strerror(errno);
  66. else if (pid == 0)
  67. {
  68. Args.push_back("--status-fd");
  69. Args.push_back("3");
  70. Args.push_back(file);
  71. Args.push_back(outfile);
  72. Args.push_back(NULL);
  73. if (Debug == true)
  74. {
  75. std::clog << "Preparing to exec: " << gpgvpath;
  76. for(std::vector<const char *>::const_iterator a = Args.begin();*a != NULL; ++a)
  77. std::clog << " " << *a;
  78. std::clog << std::endl;
  79. }
  80. int const nullfd = open("/dev/null", O_RDONLY);
  81. close(fd[0]);
  82. // Redirect output to /dev/null; we read from the status fd
  83. dup2(nullfd, STDOUT_FILENO);
  84. dup2(nullfd, STDERR_FILENO);
  85. // Redirect the pipe to the status fd (3)
  86. dup2(fd[1], 3);
  87. putenv((char *)"LANG=");
  88. putenv((char *)"LC_ALL=");
  89. putenv((char *)"LC_MESSAGES=");
  90. execvp(gpgvpath.c_str(), (char **) &Args[0]);
  91. exit(111);
  92. }
  93. close(fd[1]);
  94. pipein = fdopen(fd[0], "r");
  95. // Loop over the output of gpgv, and check the signatures.
  96. size_t buffersize = 64;
  97. char *buffer = (char *) malloc(buffersize);
  98. size_t bufferoff = 0;
  99. while (1)
  100. {
  101. int c;
  102. // Read a line. Sigh.
  103. while ((c = getc(pipein)) != EOF && c != '\n')
  104. {
  105. if (bufferoff == buffersize)
  106. buffer = (char *) realloc(buffer, buffersize *= 2);
  107. *(buffer+bufferoff) = c;
  108. bufferoff++;
  109. }
  110. if (bufferoff == 0 && c == EOF)
  111. break;
  112. *(buffer+bufferoff) = '\0';
  113. bufferoff = 0;
  114. if (Debug == true)
  115. std::clog << "Read: " << buffer << std::endl;
  116. // Push the data into three separate vectors, which
  117. // we later concatenate. They're kept separate so
  118. // if we improve the apt method communication stuff later
  119. // it will be better.
  120. if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
  121. {
  122. if (Debug == true)
  123. std::clog << "Got BADSIG! " << std::endl;
  124. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  125. }
  126. if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
  127. {
  128. if (Debug == true)
  129. std::clog << "Got NO_PUBKEY " << std::endl;
  130. NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  131. }
  132. if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
  133. {
  134. if (Debug == true)
  135. std::clog << "Got NODATA! " << std::endl;
  136. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  137. }
  138. if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
  139. {
  140. if (Debug == true)
  141. std::clog << "Got KEYEXPIRED! " << std::endl;
  142. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  143. }
  144. if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
  145. {
  146. if (Debug == true)
  147. std::clog << "Got REVKEYSIG! " << std::endl;
  148. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  149. }
  150. if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
  151. {
  152. char *sig = buffer + sizeof(GNUPGPREFIX);
  153. char *p = sig + sizeof("GOODSIG");
  154. while (*p && isxdigit(*p))
  155. p++;
  156. *p = 0;
  157. if (Debug == true)
  158. std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
  159. GoodSigners.push_back(string(sig));
  160. }
  161. }
  162. fclose(pipein);
  163. waitpid(pid, &status, 0);
  164. if (Debug == true)
  165. {
  166. std::clog << "gpgv exited\n";
  167. }
  168. if (WEXITSTATUS(status) == 0)
  169. {
  170. if (GoodSigners.empty())
  171. return _("Internal error: Good signature, but could not determine key fingerprint?!");
  172. return "";
  173. }
  174. else if (WEXITSTATUS(status) == 1)
  175. {
  176. return _("At least one invalid signature was encountered.");
  177. }
  178. else if (WEXITSTATUS(status) == 111)
  179. {
  180. ioprintf(ret, _("Could not execute '%s' to verify signature (is gpgv installed?)"), gpgvpath.c_str());
  181. return ret.str();
  182. }
  183. else
  184. {
  185. return _("Unknown error executing gpgv");
  186. }
  187. }
  188. bool GPGVMethod::Fetch(FetchItem *Itm)
  189. {
  190. URI Get = Itm->Uri;
  191. string Path = Get.Host + Get.Path; // To account for relative paths
  192. string keyID;
  193. vector<string> GoodSigners;
  194. vector<string> BadSigners;
  195. // a worthless signature is a expired or revoked one
  196. vector<string> WorthlessSigners;
  197. vector<string> NoPubKeySigners;
  198. FetchResult Res;
  199. Res.Filename = Itm->DestFile;
  200. URIStart(Res);
  201. // Run gpgv on file, extract contents and get the key ID of the signer
  202. string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
  203. GoodSigners, BadSigners, WorthlessSigners,
  204. NoPubKeySigners);
  205. if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
  206. {
  207. string errmsg;
  208. // In this case, something bad probably happened, so we just go
  209. // with what the other method gave us for an error message.
  210. if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
  211. errmsg = msg;
  212. else
  213. {
  214. if (!BadSigners.empty())
  215. {
  216. errmsg += _("The following signatures were invalid:\n");
  217. for (vector<string>::iterator I = BadSigners.begin();
  218. I != BadSigners.end(); I++)
  219. errmsg += (*I + "\n");
  220. }
  221. if (!WorthlessSigners.empty())
  222. {
  223. errmsg += _("The following signatures were invalid:\n");
  224. for (vector<string>::iterator I = WorthlessSigners.begin();
  225. I != WorthlessSigners.end(); I++)
  226. errmsg += (*I + "\n");
  227. }
  228. if (!NoPubKeySigners.empty())
  229. {
  230. errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
  231. for (vector<string>::iterator I = NoPubKeySigners.begin();
  232. I != NoPubKeySigners.end(); I++)
  233. errmsg += (*I + "\n");
  234. }
  235. }
  236. // this is only fatal if we have no good sigs or if we have at
  237. // least one bad signature. good signatures and NoPubKey signatures
  238. // happen easily when a file is signed with multiple signatures
  239. if(GoodSigners.empty() or !BadSigners.empty())
  240. return _error->Error("%s", errmsg.c_str());
  241. }
  242. // Just pass the raw output up, because passing it as a real data
  243. // structure is too difficult with the method stuff. We keep it
  244. // as three separate vectors for future extensibility.
  245. Res.GPGVOutput = GoodSigners;
  246. Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
  247. Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
  248. URIDone(Res);
  249. if (_config->FindB("Debug::Acquire::gpgv", false))
  250. {
  251. std::clog << "gpgv succeeded\n";
  252. }
  253. return true;
  254. }
  255. int main()
  256. {
  257. setlocale(LC_ALL, "");
  258. GPGVMethod Mth;
  259. return Mth.Run();
  260. }