gpgv.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // -*- mode: cpp; mode: fold -*-
  2. // Include Files /*{{{*/
  3. #include<config.h>
  4. #include<apt-pkg/configuration.h>
  5. #include<apt-pkg/error.h>
  6. #include<apt-pkg/strutl.h>
  7. #include<apt-pkg/fileutl.h>
  8. #include<apt-pkg/gpgv.h>
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <fcntl.h>
  14. #include <sys/wait.h>
  15. #include <unistd.h>
  16. #include <stddef.h>
  17. #include <algorithm>
  18. #include <fstream>
  19. #include <iostream>
  20. #include <sstream>
  21. #include <string>
  22. #include <vector>
  23. #include <apti18n.h>
  24. /*}}}*/
  25. static char * GenerateTemporaryFileTemplate(const char *basename) /*{{{*/
  26. {
  27. std::string out;
  28. std::string tmpdir = GetTempDir();
  29. strprintf(out, "%s/%s.XXXXXX", tmpdir.c_str(), basename);
  30. return strdup(out.c_str());
  31. }
  32. /*}}}*/
  33. // ExecGPGV - returns the command needed for verify /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* Generating the commandline for calling gpg is somehow complicated as
  36. we need to add multiple keyrings and user supplied options.
  37. Also, as gpg has no options to enforce a certain reduced style of
  38. clear-signed files (=the complete content of the file is signed and
  39. the content isn't encoded) we do a divide and conquer approach here
  40. and split up the clear-signed file in message and signature for gpg.
  41. And as a cherry on the cake, we use our apt-key wrapper to do part
  42. of the lifting in regards to merging keyrings. Fun for the whole family.
  43. */
  44. static bool iovprintf(std::ostream &out, const char *format,
  45. va_list &args, ssize_t &size) {
  46. char *S = (char*)malloc(size);
  47. ssize_t const n = vsnprintf(S, size, format, args);
  48. if (n > -1 && n < size) {
  49. out << S;
  50. free(S);
  51. return true;
  52. } else {
  53. if (n > -1)
  54. size = n + 1;
  55. else
  56. size *= 2;
  57. }
  58. free(S);
  59. return false;
  60. }
  61. static void APT_PRINTF(4) apt_error(std::ostream &outterm, int const statusfd, int fd[2], const char *format, ...)
  62. {
  63. std::ostringstream outstr;
  64. std::ostream &out = (statusfd == -1) ? outterm : outstr;
  65. va_list args;
  66. ssize_t size = 400;
  67. while (true) {
  68. bool ret;
  69. va_start(args,format);
  70. ret = iovprintf(out, format, args, size);
  71. va_end(args);
  72. if (ret == true)
  73. break;
  74. }
  75. if (statusfd != -1)
  76. {
  77. auto const errtag = "[APTKEY:] ERROR ";
  78. outstr << '\n';
  79. auto const errtext = outstr.str();
  80. if (FileFd::Write(fd[1], errtag, strlen(errtag)) == false ||
  81. FileFd::Write(fd[1], errtext.data(), errtext.size()) == false)
  82. outterm << errtext << std::flush;
  83. }
  84. }
  85. void ExecGPGV(std::string const &File, std::string const &FileGPG,
  86. int const &statusfd, int fd[2], std::string const &key)
  87. {
  88. #define EINTERNAL 111
  89. std::string const aptkey = _config->Find("Dir::Bin::apt-key", CMAKE_INSTALL_FULL_BINDIR "/apt-key");
  90. bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
  91. struct exiter {
  92. std::vector<const char *> files;
  93. void operator ()(int code) APT_NORETURN {
  94. std::for_each(files.begin(), files.end(), unlink);
  95. exit(code);
  96. }
  97. } local_exit;
  98. std::vector<const char *> Args;
  99. Args.reserve(11);
  100. Args.push_back("/bin/sh");
  101. Args.push_back(aptkey.c_str());
  102. Args.push_back("--quiet");
  103. Args.push_back("--readonly");
  104. if (key.empty() == false)
  105. {
  106. if (key[0] == '/')
  107. {
  108. Args.push_back("--keyring");
  109. Args.push_back(key.c_str());
  110. }
  111. else
  112. {
  113. Args.push_back("--keyid");
  114. Args.push_back(key.c_str());
  115. }
  116. }
  117. Args.push_back("verify");
  118. char statusfdstr[10];
  119. if (statusfd != -1)
  120. {
  121. Args.push_back("--status-fd");
  122. snprintf(statusfdstr, sizeof(statusfdstr), "%i", statusfd);
  123. Args.push_back(statusfdstr);
  124. }
  125. Configuration::Item const *Opts;
  126. Opts = _config->Tree("Acquire::gpgv::Options");
  127. if (Opts != 0)
  128. {
  129. Opts = Opts->Child;
  130. for (; Opts != 0; Opts = Opts->Next)
  131. {
  132. if (Opts->Value.empty() == true)
  133. continue;
  134. Args.push_back(Opts->Value.c_str());
  135. }
  136. }
  137. enum { DETACHED, CLEARSIGNED } releaseSignature = (FileGPG != File) ? DETACHED : CLEARSIGNED;
  138. std::vector<std::string> dataHeader;
  139. char * sig = NULL;
  140. char * data = NULL;
  141. char * conf = nullptr;
  142. // Dump the configuration so apt-key picks up the correct Dir values
  143. {
  144. conf = GenerateTemporaryFileTemplate("apt.conf");
  145. if (conf == nullptr) {
  146. apt_error(std::cerr, statusfd, fd, "Couldn't create tempfile names for passing config to apt-key");
  147. local_exit(EINTERNAL);
  148. }
  149. int confFd = mkstemp(conf);
  150. if (confFd == -1) {
  151. apt_error(std::cerr, statusfd, fd, "Couldn't create temporary file %s for passing config to apt-key", conf);
  152. local_exit(EINTERNAL);
  153. }
  154. local_exit.files.push_back(conf);
  155. std::ofstream confStream(conf);
  156. close(confFd);
  157. _config->Dump(confStream);
  158. confStream.close();
  159. setenv("APT_CONFIG", conf, 1);
  160. }
  161. if (releaseSignature == DETACHED)
  162. {
  163. Args.push_back(FileGPG.c_str());
  164. Args.push_back(File.c_str());
  165. }
  166. else // clear-signed file
  167. {
  168. sig = GenerateTemporaryFileTemplate("apt.sig");
  169. data = GenerateTemporaryFileTemplate("apt.data");
  170. if (sig == NULL || data == NULL)
  171. {
  172. apt_error(std::cerr, statusfd, fd, "Couldn't create tempfile names for splitting up %s", File.c_str());
  173. local_exit(EINTERNAL);
  174. }
  175. int const sigFd = mkstemp(sig);
  176. int const dataFd = mkstemp(data);
  177. if (dataFd != -1)
  178. local_exit.files.push_back(data);
  179. if (sigFd != -1)
  180. local_exit.files.push_back(sig);
  181. if (sigFd == -1 || dataFd == -1)
  182. {
  183. apt_error(std::cerr, statusfd, fd, "Couldn't create tempfiles for splitting up %s", File.c_str());
  184. local_exit(EINTERNAL);
  185. }
  186. FileFd signature;
  187. signature.OpenDescriptor(sigFd, FileFd::WriteOnly, true);
  188. FileFd message;
  189. message.OpenDescriptor(dataFd, FileFd::WriteOnly, true);
  190. if (signature.Failed() == true || message.Failed() == true ||
  191. SplitClearSignedFile(File, &message, &dataHeader, &signature) == false)
  192. {
  193. apt_error(std::cerr, statusfd, fd, "Splitting up %s into data and signature failed", File.c_str());
  194. local_exit(112);
  195. }
  196. Args.push_back(sig);
  197. Args.push_back(data);
  198. }
  199. Args.push_back(NULL);
  200. /* concat the args into a string and try to run it like a shell
  201. script to mitigate *OS 11 sandbox issues */
  202. std::stringstream ss;
  203. int j = 0;
  204. for (std::vector<const char *>::const_iterator a = Args.begin(); *a != NULL; ++a)
  205. {
  206. if(j != 0)
  207. ss << " ";
  208. ss << *a;
  209. j++;
  210. }
  211. std::string ArgString = ss.str();
  212. if (Debug == true)
  213. {
  214. std::clog << "Preparing to exec: ";
  215. for (std::vector<const char *>::const_iterator a = Args.begin(); *a != NULL; ++a)
  216. std::clog << " " << *a;
  217. std::clog << std::endl;
  218. }
  219. if (statusfd != -1)
  220. {
  221. int const nullfd = open("/dev/null", O_WRONLY);
  222. close(fd[0]);
  223. // Redirect output to /dev/null; we read from the status fd
  224. if (statusfd != STDOUT_FILENO)
  225. dup2(nullfd, STDOUT_FILENO);
  226. if (statusfd != STDERR_FILENO)
  227. dup2(nullfd, STDERR_FILENO);
  228. // Redirect the pipe to the status fd (3)
  229. dup2(fd[1], statusfd);
  230. putenv((char *)"LANG=");
  231. putenv((char *)"LC_ALL=");
  232. putenv((char *)"LC_MESSAGES=");
  233. }
  234. // We have created tempfiles we have to clean up
  235. // and we do an additional check, so fork yet another time …
  236. pid_t pid = ExecFork();
  237. if(pid < 0) {
  238. apt_error(std::cerr, statusfd, fd, "Fork failed for %s to check %s", Args[0], File.c_str());
  239. local_exit(EINTERNAL);
  240. }
  241. if(pid == 0)
  242. {
  243. if (statusfd != -1)
  244. dup2(fd[1], statusfd);
  245. execlp("sh", "sh", "-c", ArgString.c_str(), NULL); //run as a shell script instead
  246. //execvp(Args[0], (char **) &Args[0]);
  247. apt_error(std::cerr, statusfd, fd, "Couldn't execute %s to check %s", Args[0], File.c_str());
  248. local_exit(EINTERNAL);
  249. }
  250. // Wait and collect the error code - taken from WaitPid as we need the exact Status
  251. int Status;
  252. while (waitpid(pid,&Status,0) != pid)
  253. {
  254. if (errno == EINTR)
  255. continue;
  256. apt_error(std::cerr, statusfd, fd, _("Waited for %s but it wasn't there"), "apt-key");
  257. local_exit(EINTERNAL);
  258. }
  259. // check if it exit'ed normally …
  260. if (WIFEXITED(Status) == false)
  261. {
  262. apt_error(std::cerr, statusfd, fd, _("Sub-process %s exited unexpectedly"), "apt-key");
  263. local_exit(EINTERNAL);
  264. }
  265. // … and with a good exit code
  266. if (WEXITSTATUS(Status) != 0)
  267. {
  268. // we forward the statuscode, so don't generate a message on the fd in this case
  269. apt_error(std::cerr, -1, fd, _("Sub-process %s returned an error code (%u)"), "apt-key", WEXITSTATUS(Status));
  270. local_exit(WEXITSTATUS(Status));
  271. }
  272. // everything fine
  273. local_exit(0);
  274. }
  275. /*}}}*/
  276. // SplitClearSignedFile - split message into data/signature /*{{{*/
  277. bool SplitClearSignedFile(std::string const &InFile, FileFd * const ContentFile,
  278. std::vector<std::string> * const ContentHeader, FileFd * const SignatureFile)
  279. {
  280. FILE *in = fopen(InFile.c_str(), "r");
  281. if (in == NULL)
  282. return _error->Errno("fopen", "can not open %s", InFile.c_str());
  283. bool found_message_start = false;
  284. bool found_message_end = false;
  285. bool skip_until_empty_line = false;
  286. bool found_signature = false;
  287. bool first_line = true;
  288. char *buf = NULL;
  289. size_t buf_size = 0;
  290. while (getline(&buf, &buf_size, in) != -1)
  291. {
  292. _strrstrip(buf);
  293. if (found_message_start == false)
  294. {
  295. if (strcmp(buf, "-----BEGIN PGP SIGNED MESSAGE-----") == 0)
  296. {
  297. found_message_start = true;
  298. skip_until_empty_line = true;
  299. }
  300. }
  301. else if (skip_until_empty_line == true)
  302. {
  303. if (strlen(buf) == 0)
  304. skip_until_empty_line = false;
  305. // save "Hash" Armor Headers, others aren't allowed
  306. else if (ContentHeader != NULL && strncmp(buf, "Hash: ", strlen("Hash: ")) == 0)
  307. ContentHeader->push_back(buf);
  308. }
  309. else if (found_signature == false)
  310. {
  311. if (strcmp(buf, "-----BEGIN PGP SIGNATURE-----") == 0)
  312. {
  313. found_signature = true;
  314. found_message_end = true;
  315. if (SignatureFile != NULL)
  316. {
  317. SignatureFile->Write(buf, strlen(buf));
  318. SignatureFile->Write("\n", 1);
  319. }
  320. }
  321. else if (found_message_end == false) // we are in the message block
  322. {
  323. // we don't have any fields which need dash-escaped,
  324. // but implementations are free to encode all lines …
  325. char const * dashfree = buf;
  326. if (strncmp(dashfree, "- ", 2) == 0)
  327. dashfree += 2;
  328. if(first_line == true) // first line does not need a newline
  329. first_line = false;
  330. else if (ContentFile != NULL)
  331. ContentFile->Write("\n", 1);
  332. else
  333. continue;
  334. if (ContentFile != NULL)
  335. ContentFile->Write(dashfree, strlen(dashfree));
  336. }
  337. }
  338. else if (found_signature == true)
  339. {
  340. if (SignatureFile != NULL)
  341. {
  342. SignatureFile->Write(buf, strlen(buf));
  343. SignatureFile->Write("\n", 1);
  344. }
  345. if (strcmp(buf, "-----END PGP SIGNATURE-----") == 0)
  346. found_signature = false; // look for other signatures
  347. }
  348. // all the rest is whitespace, unsigned garbage or additional message blocks we ignore
  349. }
  350. fclose(in);
  351. if (buf != NULL)
  352. free(buf);
  353. if (found_signature == true)
  354. return _error->Error("Signature in file %s wasn't closed", InFile.c_str());
  355. // if we haven't found any of them, this an unsigned file,
  356. // so don't generate an error, but splitting was unsuccessful none-the-less
  357. if (first_line == true && found_message_start == false && found_message_end == false)
  358. return false;
  359. // otherwise one missing indicates a syntax error
  360. else if (first_line == true || found_message_start == false || found_message_end == false)
  361. return _error->Error("Splitting of file %s failed as it doesn't contain all expected parts %i %i %i", InFile.c_str(), first_line, found_message_start, found_message_end);
  362. return true;
  363. }
  364. /*}}}*/
  365. bool OpenMaybeClearSignedFile(std::string const &ClearSignedFileName, FileFd &MessageFile) /*{{{*/
  366. {
  367. char * const message = GenerateTemporaryFileTemplate("fileutl.message");
  368. int const messageFd = mkstemp(message);
  369. if (messageFd == -1)
  370. {
  371. free(message);
  372. return _error->Errno("mkstemp", "Couldn't create temporary file to work with %s", ClearSignedFileName.c_str());
  373. }
  374. // we have the fd, thats enough for us
  375. unlink(message);
  376. free(message);
  377. MessageFile.OpenDescriptor(messageFd, FileFd::ReadWrite | FileFd::BufferedWrite, true);
  378. if (MessageFile.Failed() == true)
  379. return _error->Error("Couldn't open temporary file to work with %s", ClearSignedFileName.c_str());
  380. _error->PushToStack();
  381. bool const splitDone = SplitClearSignedFile(ClearSignedFileName, &MessageFile, NULL, NULL);
  382. bool const errorDone = _error->PendingError();
  383. _error->MergeWithStack();
  384. if (splitDone == false)
  385. {
  386. MessageFile.Close();
  387. if (errorDone == true)
  388. return false;
  389. // we deal with an unsigned file
  390. MessageFile.Open(ClearSignedFileName, FileFd::ReadOnly);
  391. }
  392. else // clear-signed
  393. {
  394. if (MessageFile.Seek(0) == false)
  395. return _error->Errno("lseek", "Unable to seek back in message for file %s", ClearSignedFileName.c_str());
  396. }
  397. return MessageFile.Failed() == false;
  398. }
  399. /*}}}*/