gpgv.cc 8.3 KB

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