gpgv.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <config.h>
  2. #include <apt-pkg/acquire-method.h>
  3. #include <apt-pkg/configuration.h>
  4. #include <apt-pkg/error.h>
  5. #include <apt-pkg/gpgv.h>
  6. #include <apt-pkg/strutl.h>
  7. #include <apt-pkg/fileutl.h>
  8. #include <ctype.h>
  9. #include <errno.h>
  10. #include <stddef.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/wait.h>
  15. #include <unistd.h>
  16. #include <iostream>
  17. #include <string>
  18. #include <vector>
  19. #include <apti18n.h>
  20. using std::string;
  21. using std::vector;
  22. #define GNUPGPREFIX "[GNUPG:]"
  23. #define GNUPGBADSIG "[GNUPG:] BADSIG"
  24. #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
  25. #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
  26. #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
  27. #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
  28. #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
  29. #define GNUPGNODATA "[GNUPG:] NODATA"
  30. class GPGVMethod : public pkgAcqMethod
  31. {
  32. private:
  33. string VerifyGetSigners(const char *file, const char *outfile,
  34. vector<string> &GoodSigners,
  35. vector<string> &BadSigners,
  36. vector<string> &WorthlessSigners,
  37. vector<string> &NoPubKeySigners);
  38. protected:
  39. virtual bool Fetch(FetchItem *Itm);
  40. virtual bool Configuration(string Message);
  41. public:
  42. GPGVMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
  43. };
  44. bool GPGVMethod::Configuration(string Message)
  45. {
  46. if (pkgAcqMethod::Configuration(Message) == false)
  47. return false;
  48. DropPrivsOrDie();
  49. return true;
  50. }
  51. string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
  52. vector<string> &GoodSigners,
  53. vector<string> &BadSigners,
  54. vector<string> &WorthlessSigners,
  55. vector<string> &NoPubKeySigners)
  56. {
  57. bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
  58. if (Debug == true)
  59. std::clog << "inside VerifyGetSigners" << std::endl;
  60. int fd[2];
  61. if (pipe(fd) < 0)
  62. return "Couldn't create pipe";
  63. pid_t pid = fork();
  64. if (pid < 0)
  65. return string("Couldn't spawn new process") + strerror(errno);
  66. else if (pid == 0)
  67. ExecGPGV(outfile, file, 3, fd);
  68. close(fd[1]);
  69. FILE *pipein = fdopen(fd[0], "r");
  70. // Loop over the output of apt-key (which really is gnupg), and check the signatures.
  71. size_t buffersize = 64;
  72. char *buffer = (char *) malloc(buffersize);
  73. size_t bufferoff = 0;
  74. while (1)
  75. {
  76. int c;
  77. // Read a line. Sigh.
  78. while ((c = getc(pipein)) != EOF && c != '\n')
  79. {
  80. if (bufferoff == buffersize)
  81. {
  82. char* newBuffer = (char *) realloc(buffer, buffersize *= 2);
  83. if (newBuffer == NULL)
  84. {
  85. free(buffer);
  86. return "Couldn't allocate a buffer big enough for reading";
  87. }
  88. buffer = newBuffer;
  89. }
  90. *(buffer+bufferoff) = c;
  91. bufferoff++;
  92. }
  93. if (bufferoff == 0 && c == EOF)
  94. break;
  95. *(buffer+bufferoff) = '\0';
  96. bufferoff = 0;
  97. if (Debug == true)
  98. std::clog << "Read: " << buffer << std::endl;
  99. // Push the data into three separate vectors, which
  100. // we later concatenate. They're kept separate so
  101. // if we improve the apt method communication stuff later
  102. // it will be better.
  103. if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
  104. {
  105. if (Debug == true)
  106. std::clog << "Got BADSIG! " << std::endl;
  107. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  108. }
  109. if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
  110. {
  111. if (Debug == true)
  112. std::clog << "Got NO_PUBKEY " << std::endl;
  113. NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  114. }
  115. if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
  116. {
  117. if (Debug == true)
  118. std::clog << "Got NODATA! " << std::endl;
  119. BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  120. }
  121. if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
  122. {
  123. if (Debug == true)
  124. std::clog << "Got KEYEXPIRED! " << std::endl;
  125. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  126. }
  127. if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
  128. {
  129. if (Debug == true)
  130. std::clog << "Got REVKEYSIG! " << std::endl;
  131. WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
  132. }
  133. if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
  134. {
  135. char *sig = buffer + sizeof(GNUPGPREFIX);
  136. char *p = sig + sizeof("GOODSIG");
  137. while (*p && isxdigit(*p))
  138. p++;
  139. *p = 0;
  140. if (Debug == true)
  141. std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
  142. GoodSigners.push_back(string(sig));
  143. }
  144. }
  145. fclose(pipein);
  146. free(buffer);
  147. int status;
  148. waitpid(pid, &status, 0);
  149. if (Debug == true)
  150. {
  151. ioprintf(std::clog, "gpgv exited with status %i\n", WEXITSTATUS(status));
  152. }
  153. if (WEXITSTATUS(status) == 0)
  154. {
  155. if (GoodSigners.empty())
  156. return _("Internal error: Good signature, but could not determine key fingerprint?!");
  157. return "";
  158. }
  159. else if (WEXITSTATUS(status) == 1)
  160. return _("At least one invalid signature was encountered.");
  161. else if (WEXITSTATUS(status) == 111)
  162. return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
  163. else if (WEXITSTATUS(status) == 112)
  164. {
  165. // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
  166. std::string errmsg;
  167. //TRANSLATORS: %s is a single techy word like 'NODATA'
  168. strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
  169. return errmsg;
  170. }
  171. else
  172. return _("Unknown error executing apt-key");
  173. }
  174. bool GPGVMethod::Fetch(FetchItem *Itm)
  175. {
  176. URI Get = Itm->Uri;
  177. string Path = Get.Host + Get.Path; // To account for relative paths
  178. string keyID;
  179. vector<string> GoodSigners;
  180. vector<string> BadSigners;
  181. // a worthless signature is a expired or revoked one
  182. vector<string> WorthlessSigners;
  183. vector<string> NoPubKeySigners;
  184. FetchResult Res;
  185. Res.Filename = Itm->DestFile;
  186. URIStart(Res);
  187. // Run apt-key on file, extract contents and get the key ID of the signer
  188. string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(),
  189. GoodSigners, BadSigners, WorthlessSigners,
  190. NoPubKeySigners);
  191. if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
  192. {
  193. string errmsg;
  194. // In this case, something bad probably happened, so we just go
  195. // with what the other method gave us for an error message.
  196. if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
  197. errmsg = msg;
  198. else
  199. {
  200. if (!BadSigners.empty())
  201. {
  202. errmsg += _("The following signatures were invalid:\n");
  203. for (vector<string>::iterator I = BadSigners.begin();
  204. I != BadSigners.end(); ++I)
  205. errmsg += (*I + "\n");
  206. }
  207. if (!WorthlessSigners.empty())
  208. {
  209. errmsg += _("The following signatures were invalid:\n");
  210. for (vector<string>::iterator I = WorthlessSigners.begin();
  211. I != WorthlessSigners.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. // this is only fatal if we have no good sigs or if we have at
  223. // least one bad signature. good signatures and NoPubKey signatures
  224. // happen easily when a file is signed with multiple signatures
  225. if(GoodSigners.empty() or !BadSigners.empty())
  226. return _error->Error("%s", errmsg.c_str());
  227. }
  228. // Just pass the raw output up, because passing it as a real data
  229. // structure is too difficult with the method stuff. We keep it
  230. // as three separate vectors for future extensibility.
  231. Res.GPGVOutput = GoodSigners;
  232. Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
  233. Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
  234. URIDone(Res);
  235. if (_config->FindB("Debug::Acquire::gpgv", false))
  236. {
  237. std::clog << "apt-key succeeded\n";
  238. }
  239. return true;
  240. }
  241. int main()
  242. {
  243. setlocale(LC_ALL, "");
  244. GPGVMethod Mth;
  245. return Mth.Run();
  246. }