gpgv.cc 8.7 KB

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