gpgv.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. int j;
  91. for(j=0;Args[j] != NULL; j++)
  92. std::cerr << Args[j] << " ";
  93. std::cerr << std::endl;
  94. }
  95. int nullfd = open("/dev/null", O_RDONLY);
  96. close(fd[0]);
  97. // Redirect output to /dev/null; we read from the status fd
  98. dup2(nullfd, STDOUT_FILENO);
  99. dup2(nullfd, STDERR_FILENO);
  100. // Redirect the pipe to the status fd (3)
  101. dup2(fd[1], 3);
  102. putenv("LANG=");
  103. putenv("LC_ALL=");
  104. putenv("LC_MESSAGES=");
  105. execvp(gpgvpath.c_str(), (char **)Args);
  106. exit(111);
  107. }
  108. close(fd[1]);
  109. pipein = fdopen(fd[0], "r");
  110. // Loop over the output of gpgv, and check the signatures.
  111. size_t buffersize = 64;
  112. char *buffer = (char *) malloc(buffersize);
  113. size_t bufferoff = 0;
  114. while (1)
  115. {
  116. int c;
  117. // Read a line. Sigh.
  118. while ((c = getc(pipein)) != EOF && c != '\n')
  119. {
  120. if (bufferoff == buffersize)
  121. buffer = (char *) realloc(buffer, buffersize *= 2);
  122. *(buffer+bufferoff) = c;
  123. bufferoff++;
  124. }
  125. if (bufferoff == 0 && c == EOF)
  126. break;
  127. *(buffer+bufferoff) = '\0';
  128. bufferoff = 0;
  129. if (_config->FindB("Debug::Acquire::gpgv", false))
  130. std::cerr << "Read: " << buffer << std::endl;
  131. // Push the data into three separate vectors, which
  132. // we later concatenate. They're kept separate so
  133. // if we improve the apt method communication stuff later
  134. // it will be better.
  135. if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
  136. {
  137. if (_config->FindB("Debug::Acquire::gpgv", false))
  138. std::cerr << "Got BADSIG! " << std::endl;
  139. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  140. }
  141. if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
  142. {
  143. if (_config->FindB("Debug::Acquire::gpgv", false))
  144. std::cerr << "Got NO_PUBKEY " << std::endl;
  145. NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  146. }
  147. if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0)
  148. {
  149. char *sig = buffer + sizeof(GNUPGPREFIX);
  150. char *p = sig + sizeof("VALIDSIG");
  151. while (*p && isxdigit(*p))
  152. p++;
  153. *p = 0;
  154. if (_config->FindB("Debug::Acquire::gpgv", false))
  155. std::cerr << "Got VALIDSIG, key ID:" << sig << std::endl;
  156. GoodSigners.push_back(string(sig));
  157. }
  158. }
  159. fclose(pipein);
  160. waitpid(pid, &status, 0);
  161. if (_config->FindB("Debug::Acquire::gpgv", false))
  162. {
  163. std::cerr <<"gpgv exited\n";
  164. }
  165. if (WEXITSTATUS(status) == 0)
  166. {
  167. if (GoodSigners.empty())
  168. return "Internal error: Good signature, but could not determine key fingerprint?!";
  169. return NULL;
  170. }
  171. else if (WEXITSTATUS(status) == 1)
  172. {
  173. return "At least one invalid signature was encountered.";
  174. }
  175. else if (WEXITSTATUS(status) == 111)
  176. {
  177. return (string("Could not execute ") + gpgvpath +
  178. string(" to verify signature (is gnupg installed?)")).c_str();
  179. }
  180. else
  181. {
  182. return "Unknown error executing gpgv";
  183. }
  184. }
  185. bool GPGVMethod::Fetch(FetchItem *Itm)
  186. {
  187. URI Get = Itm->Uri;
  188. string Path = Get.Host + Get.Path; // To account for relative paths
  189. string keyID;
  190. vector<string> GoodSigners;
  191. vector<string> BadSigners;
  192. vector<string> NoPubKeySigners;
  193. FetchResult Res;
  194. Res.Filename = Itm->DestFile;
  195. URIStart(Res);
  196. // Run gpgv on file, extract contents and get the key ID of the signer
  197. const char *msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
  198. GoodSigners, BadSigners, NoPubKeySigners);
  199. if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
  200. {
  201. string errmsg;
  202. // In this case, something bad probably happened, so we just go
  203. // with what the other method gave us for an error message.
  204. if (BadSigners.empty() && NoPubKeySigners.empty())
  205. errmsg = msg;
  206. else
  207. {
  208. if (!BadSigners.empty())
  209. {
  210. errmsg += "The following signatures were invalid:\n";
  211. for (vector<string>::iterator I = BadSigners.begin();
  212. I != BadSigners.end(); I++)
  213. errmsg += (*I + "\n");
  214. }
  215. if (!NoPubKeySigners.empty())
  216. {
  217. errmsg += "The following signatures couldn't be verified because the public key is not available:\n";
  218. for (vector<string>::iterator I = NoPubKeySigners.begin();
  219. I != NoPubKeySigners.end(); I++)
  220. errmsg += (*I + "\n");
  221. }
  222. }
  223. return _error->Error(errmsg.c_str());
  224. }
  225. // Transfer the modification times
  226. struct stat Buf;
  227. if (stat(Path.c_str(),&Buf) != 0)
  228. return _error->Errno("stat","Failed to stat %s", Path.c_str());
  229. struct utimbuf TimeBuf;
  230. TimeBuf.actime = Buf.st_atime;
  231. TimeBuf.modtime = Buf.st_mtime;
  232. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  233. return _error->Errno("utime","Failed to set modification time");
  234. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  235. return _error->Errno("stat","Failed to stat");
  236. // Return a Done response
  237. Res.LastModified = Buf.st_mtime;
  238. Res.Size = Buf.st_size;
  239. // Just pass the raw output up, because passing it as a real data
  240. // structure is too difficult with the method stuff. We keep it
  241. // as three separate vectors for future extensibility.
  242. Res.GPGVOutput = GoodSigners;
  243. Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
  244. Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
  245. URIDone(Res);
  246. if (_config->FindB("Debug::Acquire::gpgv", false))
  247. {
  248. std::cerr <<"gpgv suceeded\n";
  249. }
  250. return true;
  251. }
  252. int main()
  253. {
  254. GPGVMethod Mth;
  255. return Mth.Run();
  256. }