gpgv.cc 8.4 KB

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