gpgv.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. // check overflow (minus a bit of extra space at the end)
  89. if(i >= sizeof(Args)/sizeof(char*)-5) {
  90. std::clog << _("E: Too many keyrings should be passed to gpgv. Exiting.") << std::endl;
  91. exit(111);
  92. }
  93. }
  94. Configuration::Item const *Opts;
  95. Opts = _config->Tree("Acquire::gpgv::Options");
  96. if (Opts != 0)
  97. {
  98. Opts = Opts->Child;
  99. for (; Opts != 0; Opts = Opts->Next)
  100. {
  101. if (Opts->Value.empty() == true)
  102. continue;
  103. Args[i++] = Opts->Value.c_str();
  104. // check overflow (minus a bit of extra space at the end)
  105. if(i >= sizeof(Args)/sizeof(char*)-5) {
  106. std::clog << _("E: Argument list from Acquire::gpgv::Options too long. Exiting.") << std::endl;
  107. exit(111);
  108. }
  109. }
  110. }
  111. Args[i++] = file;
  112. Args[i++] = outfile;
  113. Args[i++] = NULL;
  114. if (Debug == true)
  115. {
  116. std::clog << "Preparing to exec: " << gpgvpath;
  117. for(unsigned int j=0;Args[j] != NULL; j++)
  118. std::clog << " " << Args[j];
  119. std::clog << std::endl;
  120. }
  121. int const nullfd = open("/dev/null", O_RDONLY);
  122. close(fd[0]);
  123. // Redirect output to /dev/null; we read from the status fd
  124. dup2(nullfd, STDOUT_FILENO);
  125. dup2(nullfd, STDERR_FILENO);
  126. // Redirect the pipe to the status fd (3)
  127. dup2(fd[1], 3);
  128. putenv((char *)"LANG=");
  129. putenv((char *)"LC_ALL=");
  130. putenv((char *)"LC_MESSAGES=");
  131. execvp(gpgvpath.c_str(), (char **)Args);
  132. exit(111);
  133. }
  134. close(fd[1]);
  135. pipein = fdopen(fd[0], "r");
  136. // Loop over the output of gpgv, and check the signatures.
  137. size_t buffersize = 64;
  138. char *buffer = (char *) malloc(buffersize);
  139. size_t bufferoff = 0;
  140. while (1)
  141. {
  142. int c;
  143. // Read a line. Sigh.
  144. while ((c = getc(pipein)) != EOF && c != '\n')
  145. {
  146. if (bufferoff == buffersize)
  147. buffer = (char *) realloc(buffer, buffersize *= 2);
  148. *(buffer+bufferoff) = c;
  149. bufferoff++;
  150. }
  151. if (bufferoff == 0 && c == EOF)
  152. break;
  153. *(buffer+bufferoff) = '\0';
  154. bufferoff = 0;
  155. if (Debug == true)
  156. std::clog << "Read: " << buffer << std::endl;
  157. // Push the data into three separate vectors, which
  158. // we later concatenate. They're kept separate so
  159. // if we improve the apt method communication stuff later
  160. // it will be better.
  161. if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
  162. {
  163. if (Debug == true)
  164. std::clog << "Got BADSIG! " << std::endl;
  165. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  166. }
  167. if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
  168. {
  169. if (Debug == true)
  170. std::clog << "Got NO_PUBKEY " << std::endl;
  171. NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  172. }
  173. if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
  174. {
  175. if (Debug == true)
  176. std::clog << "Got NODATA! " << std::endl;
  177. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  178. }
  179. if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
  180. {
  181. if (Debug == true)
  182. std::clog << "Got KEYEXPIRED! " << std::endl;
  183. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  184. }
  185. if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
  186. {
  187. if (Debug == true)
  188. std::clog << "Got REVKEYSIG! " << std::endl;
  189. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  190. }
  191. if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
  192. {
  193. char *sig = buffer + sizeof(GNUPGPREFIX);
  194. char *p = sig + sizeof("GOODSIG");
  195. while (*p && isxdigit(*p))
  196. p++;
  197. *p = 0;
  198. if (Debug == true)
  199. std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
  200. GoodSigners.push_back(string(sig));
  201. }
  202. }
  203. fclose(pipein);
  204. waitpid(pid, &status, 0);
  205. if (Debug == true)
  206. {
  207. std::clog << "gpgv exited\n";
  208. }
  209. if (WEXITSTATUS(status) == 0)
  210. {
  211. if (GoodSigners.empty())
  212. return _("Internal error: Good signature, but could not determine key fingerprint?!");
  213. return "";
  214. }
  215. else if (WEXITSTATUS(status) == 1)
  216. {
  217. return _("At least one invalid signature was encountered.");
  218. }
  219. else if (WEXITSTATUS(status) == 111)
  220. {
  221. ioprintf(ret, _("Could not execute '%s' to verify signature (is gpgv installed?)"), gpgvpath.c_str());
  222. return ret.str();
  223. }
  224. else
  225. {
  226. return _("Unknown error executing gpgv");
  227. }
  228. }
  229. bool GPGVMethod::Fetch(FetchItem *Itm)
  230. {
  231. URI Get = Itm->Uri;
  232. string Path = Get.Host + Get.Path; // To account for relative paths
  233. string keyID;
  234. vector<string> GoodSigners;
  235. vector<string> BadSigners;
  236. // a worthless signature is a expired or revoked one
  237. vector<string> WorthlessSigners;
  238. vector<string> NoPubKeySigners;
  239. FetchResult Res;
  240. Res.Filename = Itm->DestFile;
  241. URIStart(Res);
  242. // Run gpgv on file, extract contents and get the key ID of the signer
  243. string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
  244. GoodSigners, BadSigners, WorthlessSigners,
  245. NoPubKeySigners);
  246. if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
  247. {
  248. string errmsg;
  249. // In this case, something bad probably happened, so we just go
  250. // with what the other method gave us for an error message.
  251. if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
  252. errmsg = msg;
  253. else
  254. {
  255. if (!BadSigners.empty())
  256. {
  257. errmsg += _("The following signatures were invalid:\n");
  258. for (vector<string>::iterator I = BadSigners.begin();
  259. I != BadSigners.end(); I++)
  260. errmsg += (*I + "\n");
  261. }
  262. if (!WorthlessSigners.empty())
  263. {
  264. errmsg += _("The following signatures were invalid:\n");
  265. for (vector<string>::iterator I = WorthlessSigners.begin();
  266. I != WorthlessSigners.end(); I++)
  267. errmsg += (*I + "\n");
  268. }
  269. if (!NoPubKeySigners.empty())
  270. {
  271. errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
  272. for (vector<string>::iterator I = NoPubKeySigners.begin();
  273. I != NoPubKeySigners.end(); I++)
  274. errmsg += (*I + "\n");
  275. }
  276. }
  277. // this is only fatal if we have no good sigs or if we have at
  278. // least one bad signature. good signatures and NoPubKey signatures
  279. // happen easily when a file is signed with multiple signatures
  280. if(GoodSigners.empty() or !BadSigners.empty())
  281. return _error->Error("%s", errmsg.c_str());
  282. }
  283. // Just pass the raw output up, because passing it as a real data
  284. // structure is too difficult with the method stuff. We keep it
  285. // as three separate vectors for future extensibility.
  286. Res.GPGVOutput = GoodSigners;
  287. Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
  288. Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
  289. URIDone(Res);
  290. if (_config->FindB("Debug::Acquire::gpgv", false))
  291. {
  292. std::clog << "gpgv succeeded\n";
  293. }
  294. return true;
  295. }
  296. int main()
  297. {
  298. setlocale(LC_ALL, "");
  299. GPGVMethod Mth;
  300. return Mth.Run();
  301. }