gpgv.cc 10 KB

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