gpgv.cc 7.7 KB

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