gpgv.cc 10.0 KB

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