You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1317 lines
47 KiB

  1. #include <config.h>
  2. #include <apt-pkg/acquire-item.h>
  3. #include <apt-pkg/aptconfiguration.h>
  4. #include <apt-pkg/configuration.h>
  5. #include <apt-pkg/debindexfile.h>
  6. #include <apt-pkg/debmetaindex.h>
  7. #include <apt-pkg/error.h>
  8. #include <apt-pkg/fileutl.h>
  9. #include <apt-pkg/gpgv.h>
  10. #include <apt-pkg/hashes.h>
  11. #include <apt-pkg/macros.h>
  12. #include <apt-pkg/metaindex.h>
  13. #include <apt-pkg/pkgcachegen.h>
  14. #include <apt-pkg/sourcelist.h>
  15. #include <apt-pkg/strutl.h>
  16. #include <apt-pkg/tagfile.h>
  17. #include <algorithm>
  18. #include <map>
  19. #include <sstream>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <apti18n.h>
  26. static std::string transformFingergrpints(std::string finger) /*{{{*/
  27. {
  28. std::transform(finger.begin(), finger.end(), finger.begin(), ::toupper);
  29. if (finger.length() == 40)
  30. {
  31. if (finger.find_first_not_of("0123456789ABCDEF") == std::string::npos)
  32. return finger;
  33. }
  34. else if (finger.length() == 41)
  35. {
  36. auto bang = finger.find_first_not_of("0123456789ABCDEF");
  37. if (bang == 40 && finger[bang] == '!')
  38. return finger;
  39. }
  40. return "";
  41. }
  42. /*}}}*/
  43. static std::string transformFingergrpintsWithFilenames(std::string const &finger) /*{{{*/
  44. {
  45. // no check for existence as we could be chrooting later or such things
  46. if (finger.empty() == false && finger[0] == '/')
  47. return finger;
  48. return transformFingergrpints(finger);
  49. }
  50. /*}}}*/
  51. static std::string NormalizeSignedBy(std::string SignedBy, bool const SupportFilenames) /*{{{*/
  52. {
  53. // we could go all fancy and allow short/long/string matches as gpgv/apt-key does,
  54. // but fingerprints are harder to fake than the others and this option is set once,
  55. // not interactively all the time so easy to type is not really a concern.
  56. std::transform(SignedBy.begin(), SignedBy.end(), SignedBy.begin(), [](char const c) {
  57. return (isspace(c) == 0) ? c : ',';
  58. });
  59. auto fingers = VectorizeString(SignedBy, ',');
  60. auto const isAnEmptyString = [](std::string const &s) { return s.empty(); };
  61. fingers.erase(std::remove_if(fingers.begin(), fingers.end(), isAnEmptyString), fingers.end());
  62. if (unlikely(fingers.empty()))
  63. return "";
  64. if (SupportFilenames)
  65. std::transform(fingers.begin(), fingers.end(), fingers.begin(), transformFingergrpintsWithFilenames);
  66. else
  67. std::transform(fingers.begin(), fingers.end(), fingers.begin(), transformFingergrpints);
  68. if (std::any_of(fingers.begin(), fingers.end(), isAnEmptyString))
  69. return "";
  70. std::stringstream os;
  71. std::copy(fingers.begin(), fingers.end() - 1, std::ostream_iterator<std::string>(os, ","));
  72. os << *fingers.rbegin();
  73. return os.str();
  74. }
  75. /*}}}*/
  76. class APT_HIDDEN debReleaseIndexPrivate /*{{{*/
  77. {
  78. public:
  79. struct APT_HIDDEN debSectionEntry
  80. {
  81. std::string const sourcesEntry;
  82. std::string const Name;
  83. std::vector<std::string> const Targets;
  84. std::vector<std::string> const Architectures;
  85. std::vector<std::string> const Languages;
  86. bool const UsePDiffs;
  87. std::string const UseByHash;
  88. };
  89. std::vector<debSectionEntry> DebEntries;
  90. std::vector<debSectionEntry> DebSrcEntries;
  91. metaIndex::TriState CheckValidUntil;
  92. time_t ValidUntilMin;
  93. time_t ValidUntilMax;
  94. metaIndex::TriState CheckDate;
  95. time_t DateMaxFuture;
  96. time_t NotBefore;
  97. std::vector<std::string> Architectures;
  98. std::vector<std::string> NoSupportForAll;
  99. std::vector<std::string> SupportedComponents;
  100. std::map<std::string, std::string> const ReleaseOptions;
  101. debReleaseIndexPrivate(std::map<std::string, std::string> const &Options) : CheckValidUntil(metaIndex::TRI_UNSET), ValidUntilMin(0), ValidUntilMax(0), CheckDate(metaIndex::TRI_UNSET), DateMaxFuture(0), NotBefore(0), ReleaseOptions(Options) {}
  102. };
  103. /*}}}*/
  104. // ReleaseIndex::MetaIndex* - display helpers /*{{{*/
  105. std::string debReleaseIndex::MetaIndexInfo(const char *Type) const
  106. {
  107. std::string Info = ::URI::ArchiveOnly(URI) + ' ';
  108. if (Dist[Dist.size() - 1] == '/')
  109. {
  110. if (Dist != "/")
  111. Info += Dist;
  112. }
  113. else
  114. Info += Dist;
  115. Info += " ";
  116. Info += Type;
  117. return Info;
  118. }
  119. std::string debReleaseIndex::Describe() const
  120. {
  121. return MetaIndexInfo("Release");
  122. }
  123. std::string debReleaseIndex::MetaIndexFile(const char *Type) const
  124. {
  125. return _config->FindDir("Dir::State::lists") +
  126. URItoFileName(MetaIndexURI(Type));
  127. }
  128. static std::string constructMetaIndexURI(std::string URI, std::string const &Dist, char const * const Type)
  129. {
  130. if (Dist == "/")
  131. ;
  132. else if (Dist[Dist.size()-1] == '/')
  133. URI += Dist;
  134. else
  135. URI += "dists/" + Dist + "/";
  136. return URI + Type;
  137. }
  138. std::string debReleaseIndex::MetaIndexURI(const char *Type) const
  139. {
  140. return constructMetaIndexURI(URI, Dist, Type);
  141. }
  142. /*}}}*/
  143. // ReleaseIndex Con- and Destructors /*{{{*/
  144. debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist, std::map<std::string, std::string> const &Options) :
  145. metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate(Options))
  146. {}
  147. debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist, bool const pTrusted, std::map<std::string, std::string> const &Options) :
  148. metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate(Options))
  149. {
  150. Trusted = pTrusted ? TRI_YES : TRI_NO;
  151. }
  152. debReleaseIndex::~debReleaseIndex() {
  153. if (d != NULL)
  154. delete d;
  155. }
  156. /*}}}*/
  157. // ReleaseIndex::GetIndexTargets /*{{{*/
  158. static void GetIndexTargetsFor(char const * const Type, std::string const &URI, std::string const &Dist,
  159. std::vector<debReleaseIndexPrivate::debSectionEntry> const &entries,
  160. std::vector<IndexTarget> &IndexTargets, std::map<std::string, std::string> const &ReleaseOptions)
  161. {
  162. bool const flatArchive = (Dist[Dist.length() - 1] == '/');
  163. std::string const baseURI = constructMetaIndexURI(URI, Dist, "");
  164. std::string DefCompressionTypes;
  165. {
  166. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  167. if (types.empty() == false)
  168. {
  169. std::ostringstream os;
  170. std::copy(types.begin(), types.end()-1, std::ostream_iterator<std::string>(os, " "));
  171. os << *types.rbegin();
  172. DefCompressionTypes = os.str();
  173. }
  174. }
  175. std::string DefKeepCompressedAs;
  176. {
  177. std::vector<APT::Configuration::Compressor> comps = APT::Configuration::getCompressors();
  178. if (comps.empty() == false)
  179. {
  180. std::sort(comps.begin(), comps.end(),
  181. [](APT::Configuration::Compressor const &a, APT::Configuration::Compressor const &b) { return a.Cost < b.Cost; });
  182. std::ostringstream os;
  183. for (auto const &c : comps)
  184. if (c.Cost != 0)
  185. os << c.Extension.substr(1) << ' ';
  186. DefKeepCompressedAs = os.str();
  187. }
  188. DefKeepCompressedAs += "uncompressed";
  189. }
  190. std::vector<std::string> const NativeArchs = { _config->Find("APT::Architecture"), "implicit:all" };
  191. bool const GzipIndex = _config->FindB("Acquire::GzipIndexes", false);
  192. for (std::vector<debReleaseIndexPrivate::debSectionEntry>::const_iterator E = entries.begin(); E != entries.end(); ++E)
  193. {
  194. for (std::vector<std::string>::const_iterator T = E->Targets.begin(); T != E->Targets.end(); ++T)
  195. {
  196. #define APT_T_CONFIG_STR(X, Y) _config->Find(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y))
  197. #define APT_T_CONFIG_BOOL(X, Y) _config->FindB(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y))
  198. std::string const tplMetaKey = APT_T_CONFIG_STR(flatArchive ? "flatMetaKey" : "MetaKey", "");
  199. std::string const tplShortDesc = APT_T_CONFIG_STR("ShortDescription", "");
  200. std::string const tplLongDesc = "$(SITE) " + APT_T_CONFIG_STR(flatArchive ? "flatDescription" : "Description", "");
  201. std::string const tplIdentifier = APT_T_CONFIG_STR("Identifier", *T);
  202. bool const IsOptional = APT_T_CONFIG_BOOL("Optional", true);
  203. bool const KeepCompressed = APT_T_CONFIG_BOOL("KeepCompressed", GzipIndex);
  204. bool const DefaultEnabled = APT_T_CONFIG_BOOL("DefaultEnabled", true);
  205. bool const UsePDiffs = APT_T_CONFIG_BOOL("PDiffs", E->UsePDiffs);
  206. std::string const UseByHash = APT_T_CONFIG_STR("By-Hash", E->UseByHash);
  207. std::string const CompressionTypes = APT_T_CONFIG_STR("CompressionTypes", DefCompressionTypes);
  208. std::string KeepCompressedAs = APT_T_CONFIG_STR("KeepCompressedAs", "");
  209. std::string const FallbackOf = APT_T_CONFIG_STR("Fallback-Of", "");
  210. #undef APT_T_CONFIG_BOOL
  211. #undef APT_T_CONFIG_STR
  212. if (tplMetaKey.empty())
  213. continue;
  214. if (KeepCompressedAs.empty())
  215. KeepCompressedAs = DefKeepCompressedAs;
  216. else
  217. {
  218. std::vector<std::string> const defKeep = VectorizeString(DefKeepCompressedAs, ' ');
  219. std::vector<std::string> const valKeep = VectorizeString(KeepCompressedAs, ' ');
  220. std::vector<std::string> keep;
  221. for (auto const &val : valKeep)
  222. {
  223. if (val.empty())
  224. continue;
  225. if (std::find(defKeep.begin(), defKeep.end(), val) == defKeep.end())
  226. continue;
  227. keep.push_back(val);
  228. }
  229. if (std::find(keep.begin(), keep.end(), "uncompressed") == keep.end())
  230. keep.push_back("uncompressed");
  231. std::ostringstream os;
  232. std::copy(keep.begin(), keep.end()-1, std::ostream_iterator<std::string>(os, " "));
  233. os << *keep.rbegin();
  234. KeepCompressedAs = os.str();
  235. }
  236. for (std::vector<std::string>::const_iterator L = E->Languages.begin(); L != E->Languages.end(); ++L)
  237. {
  238. if (*L == "none" && tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
  239. continue;
  240. for (std::vector<std::string>::const_iterator A = E->Architectures.begin(); A != E->Architectures.end(); ++A)
  241. {
  242. for (auto const &NativeArch: NativeArchs)
  243. {
  244. constexpr static auto BreakPoint = "$(NATIVE_ARCHITECTURE)";
  245. // available in templates
  246. std::map<std::string, std::string> Options;
  247. Options.insert(ReleaseOptions.begin(), ReleaseOptions.end());
  248. if (tplMetaKey.find("$(COMPONENT)") != std::string::npos)
  249. Options.emplace("COMPONENT", E->Name);
  250. if (tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
  251. Options.emplace("LANGUAGE", *L);
  252. if (tplMetaKey.find("$(ARCHITECTURE)") != std::string::npos)
  253. Options.emplace("ARCHITECTURE", (*A == "implicit:all") ? "all" : *A);
  254. else if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos)
  255. Options.emplace("ARCHITECTURE", (NativeArch == "implicit:all") ? "all" : NativeArch);
  256. if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos)
  257. Options.emplace("NATIVE_ARCHITECTURE", (NativeArch == "implicit:all") ? "all" : NativeArch);
  258. std::string MetaKey = tplMetaKey;
  259. std::string ShortDesc = tplShortDesc;
  260. std::string LongDesc = tplLongDesc;
  261. std::string Identifier = tplIdentifier;
  262. for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
  263. {
  264. std::string const varname = "$(" + O->first + ")";
  265. MetaKey = SubstVar(MetaKey, varname, O->second);
  266. ShortDesc = SubstVar(ShortDesc, varname, O->second);
  267. LongDesc = SubstVar(LongDesc, varname, O->second);
  268. Identifier = SubstVar(Identifier, varname, O->second);
  269. }
  270. {
  271. auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) {
  272. return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) &&
  273. E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T == IT.Option(IndexTarget::CREATED_BY);
  274. });
  275. if (dup != IndexTargets.end())
  276. {
  277. if (tplMetaKey.find(BreakPoint) == std::string::npos)
  278. break;
  279. continue;
  280. }
  281. }
  282. {
  283. auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) {
  284. return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) &&
  285. E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T != IT.Option(IndexTarget::CREATED_BY);
  286. });
  287. if (dup != IndexTargets.end())
  288. {
  289. std::string const dupT = dup->Option(IndexTarget::CREATED_BY);
  290. std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY);
  291. //TRANSLATOR: an identifier like Packages; Releasefile key indicating
  292. // a file like main/binary-amd64/Packages; another identifier like Contents;
  293. // filename and linenumber of the sources.list entry currently parsed
  294. _error->Warning(_("Target %s wants to acquire the same file (%s) as %s from source %s"),
  295. T->c_str(), MetaKey.c_str(), dupT.c_str(), dupEntry.c_str());
  296. if (tplMetaKey.find(BreakPoint) == std::string::npos)
  297. break;
  298. continue;
  299. }
  300. }
  301. {
  302. auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &T) {
  303. return MetaKey == T.MetaKey && baseURI == T.Option(IndexTarget::BASE_URI) &&
  304. E->sourcesEntry != T.Option(IndexTarget::SOURCESENTRY);
  305. });
  306. if (dup != IndexTargets.end())
  307. {
  308. std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY);
  309. if (T->find("legacy") == std::string::npos)
  310. {
  311. //TRANSLATOR: an identifier like Packages; Releasefile key indicating
  312. // a file like main/binary-amd64/Packages; filename and linenumber of
  313. // two sources.list entries
  314. _error->Warning(_("Target %s (%s) is configured multiple times in %s and %s"),
  315. T->c_str(), MetaKey.c_str(), dupEntry.c_str(), E->sourcesEntry.c_str());
  316. }
  317. if (tplMetaKey.find(BreakPoint) == std::string::npos)
  318. break;
  319. continue;
  320. }
  321. }
  322. // not available in templates, but in the indextarget
  323. Options.insert(std::make_pair("IDENTIFIER", Identifier));
  324. Options.insert(std::make_pair("TARGET_OF", Type));
  325. Options.insert(std::make_pair("CREATED_BY", *T));
  326. Options.insert(std::make_pair("FALLBACK_OF", FallbackOf));
  327. Options.insert(std::make_pair("PDIFFS", UsePDiffs ? "yes" : "no"));
  328. Options.insert(std::make_pair("BY_HASH", UseByHash));
  329. Options.insert(std::make_pair("DEFAULTENABLED", DefaultEnabled ? "yes" : "no"));
  330. Options.insert(std::make_pair("COMPRESSIONTYPES", CompressionTypes));
  331. Options.insert(std::make_pair("KEEPCOMPRESSEDAS", KeepCompressedAs));
  332. Options.insert(std::make_pair("SOURCESENTRY", E->sourcesEntry));
  333. bool IsOpt = IsOptional;
  334. {
  335. auto const arch = Options.find("ARCHITECTURE");
  336. if (arch != Options.end() && arch->second == "all")
  337. {
  338. // one of them must be implicit:all then
  339. if (*A != "all" && NativeArch != "all")
  340. IsOpt = true;
  341. else // user used arch=all explicitly
  342. Options.emplace("Force-Support-For-All", "yes");
  343. }
  344. }
  345. IndexTarget Target(
  346. MetaKey,
  347. ShortDesc,
  348. LongDesc,
  349. baseURI + MetaKey,
  350. IsOpt,
  351. KeepCompressed,
  352. Options
  353. );
  354. IndexTargets.push_back(Target);
  355. if (tplMetaKey.find(BreakPoint) == std::string::npos)
  356. break;
  357. }
  358. if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
  359. break;
  360. }
  361. if (tplMetaKey.find("$(LANGUAGE)") == std::string::npos)
  362. break;
  363. }
  364. }
  365. }
  366. }
  367. std::vector<IndexTarget> debReleaseIndex::GetIndexTargets() const
  368. {
  369. std::vector<IndexTarget> IndexTargets;
  370. GetIndexTargetsFor("deb-src", URI, Dist, d->DebSrcEntries, IndexTargets, d->ReleaseOptions);
  371. GetIndexTargetsFor("deb", URI, Dist, d->DebEntries, IndexTargets, d->ReleaseOptions);
  372. return IndexTargets;
  373. }
  374. /*}}}*/
  375. void debReleaseIndex::AddComponent(std::string const &sourcesEntry, /*{{{*/
  376. bool const isSrc, std::string const &Name,
  377. std::vector<std::string> const &Targets,
  378. std::vector<std::string> const &Architectures,
  379. std::vector<std::string> Languages,
  380. bool const usePDiffs, std::string const &useByHash)
  381. {
  382. if (Languages.empty() == true)
  383. Languages.push_back("none");
  384. debReleaseIndexPrivate::debSectionEntry const entry = {
  385. sourcesEntry, Name, Targets, Architectures, Languages, usePDiffs, useByHash
  386. };
  387. if (isSrc)
  388. d->DebSrcEntries.push_back(entry);
  389. else
  390. d->DebEntries.push_back(entry);
  391. }
  392. /*}}}*/
  393. bool debReleaseIndex::Load(std::string const &Filename, std::string * const ErrorText)/*{{{*/
  394. {
  395. LoadedSuccessfully = TRI_NO;
  396. FileFd Fd;
  397. if (OpenMaybeClearSignedFile(Filename, Fd) == false)
  398. return false;
  399. pkgTagFile TagFile(&Fd, Fd.Size());
  400. if (Fd.IsOpen() == false || Fd.Failed())
  401. {
  402. if (ErrorText != NULL)
  403. strprintf(*ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
  404. return false;
  405. }
  406. pkgTagSection Section;
  407. const char *Start, *End;
  408. if (TagFile.Step(Section) == false)
  409. {
  410. if (ErrorText != NULL)
  411. strprintf(*ErrorText, _("No sections in Release file %s"), Filename.c_str());
  412. return false;
  413. }
  414. // FIXME: find better tag name
  415. SupportsAcquireByHash = Section.FindB("Acquire-By-Hash", false);
  416. SetOrigin(Section.FindS("Origin"));
  417. SetLabel(Section.FindS("Label"));
  418. SetVersion(Section.FindS("Version"));
  419. Suite = Section.FindS("Suite");
  420. Codename = Section.FindS("Codename");
  421. SetReleaseNotes(Section.FindS("Release-Notes"));
  422. {
  423. std::string const archs = Section.FindS("Architectures");
  424. if (archs.empty() == false)
  425. d->Architectures = VectorizeString(archs, ' ');
  426. }
  427. {
  428. std::string const targets = Section.FindS("No-Support-for-Architecture-all");
  429. if (targets.empty() == false)
  430. d->NoSupportForAll = VectorizeString(targets, ' ');
  431. }
  432. for (auto const &comp: VectorizeString(Section.FindS("Components"), ' '))
  433. {
  434. if (comp.empty())
  435. continue;
  436. auto const pos = comp.find_last_of('/');
  437. if (pos != std::string::npos) // e.g. security.debian.org uses this style
  438. d->SupportedComponents.push_back(comp.substr(pos + 1));
  439. d->SupportedComponents.push_back(std::move(comp));
  440. }
  441. {
  442. decltype(pkgCache::ReleaseFile::Flags) flags = 0;
  443. Section.FindFlag("NotAutomatic", flags, pkgCache::Flag::NotAutomatic);
  444. signed short defaultpin = 500;
  445. if ((flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic)
  446. {
  447. Section.FindFlag("ButAutomaticUpgrades", flags, pkgCache::Flag::ButAutomaticUpgrades);
  448. if ((flags & pkgCache::Flag::ButAutomaticUpgrades) == pkgCache::Flag::ButAutomaticUpgrades)
  449. defaultpin = 100;
  450. else
  451. defaultpin = 1;
  452. }
  453. SetDefaultPin(defaultpin);
  454. }
  455. bool FoundHashSum = false;
  456. bool FoundStrongHashSum = false;
  457. auto const SupportedHashes = HashString::SupportedHashes();
  458. for (int i=0; SupportedHashes[i] != NULL; i++)
  459. {
  460. if (!Section.Find(SupportedHashes[i], Start, End))
  461. continue;
  462. std::string Name;
  463. std::string Hash;
  464. unsigned long long Size;
  465. while (Start < End)
  466. {
  467. if (!parseSumData(Start, End, Name, Hash, Size))
  468. return false;
  469. HashString const hs(SupportedHashes[i], Hash);
  470. if (Entries.find(Name) == Entries.end())
  471. {
  472. metaIndex::checkSum *Sum = new metaIndex::checkSum;
  473. Sum->MetaKeyFilename = Name;
  474. Sum->Size = Size;
  475. Sum->Hashes.FileSize(Size);
  476. APT_IGNORE_DEPRECATED_PUSH
  477. Sum->Hash = hs;
  478. APT_IGNORE_DEPRECATED_POP
  479. Entries[Name] = Sum;
  480. }
  481. Entries[Name]->Hashes.push_back(hs);
  482. FoundHashSum = true;
  483. if (FoundStrongHashSum == false && hs.usable() == true)
  484. FoundStrongHashSum = true;
  485. }
  486. }
  487. bool AuthPossible = false;
  488. if(FoundHashSum == false)
  489. _error->Warning(_("No Hash entry in Release file %s"), Filename.c_str());
  490. else if(FoundStrongHashSum == false)
  491. _error->Warning(_("No Hash entry in Release file %s which is considered strong enough for security purposes"), Filename.c_str());
  492. else
  493. AuthPossible = true;
  494. std::string const StrDate = Section.FindS("Date");
  495. if (RFC1123StrToTime(StrDate.c_str(), Date) == false)
  496. {
  497. _error->Warning( _("Invalid '%s' entry in Release file %s"), "Date", Filename.c_str());
  498. Date = 0;
  499. }
  500. bool CheckDate = _config->FindB("Acquire::Check-Date", true);
  501. if (d->CheckDate == metaIndex::TRI_NO)
  502. CheckDate = false;
  503. else if (d->CheckDate == metaIndex::TRI_YES)
  504. CheckDate = true;
  505. if (CheckDate)
  506. {
  507. auto const Label = GetLabel();
  508. // get the user settings for this archive
  509. time_t MaxFuture = d->DateMaxFuture;
  510. if (MaxFuture == 0)
  511. {
  512. MaxFuture = _config->FindI("Acquire::Max-FutureTime", 10);
  513. if (Label.empty() == false)
  514. MaxFuture = _config->FindI(("Acquire::Max-FutureTime::" + Label).c_str(), MaxFuture);
  515. }
  516. d->NotBefore = Date - MaxFuture;
  517. bool CheckValidUntil = _config->FindB("Acquire::Check-Valid-Until", true);
  518. if (d->CheckValidUntil == metaIndex::TRI_NO)
  519. CheckValidUntil = false;
  520. else if (d->CheckValidUntil == metaIndex::TRI_YES)
  521. CheckValidUntil = true;
  522. if (CheckValidUntil == true)
  523. {
  524. std::string const StrValidUntil = Section.FindS("Valid-Until");
  525. // if we have a Valid-Until header in the Release file, use it as default
  526. if (StrValidUntil.empty() == false)
  527. {
  528. if (RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
  529. {
  530. if (ErrorText != NULL)
  531. strprintf(*ErrorText, _("Invalid '%s' entry in Release file %s"), "Valid-Until", Filename.c_str());
  532. return false;
  533. }
  534. }
  535. auto const Label = GetLabel();
  536. // get the user settings for this archive and use what expires earlier
  537. time_t MaxAge = d->ValidUntilMax;
  538. if (MaxAge == 0)
  539. {
  540. MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
  541. if (Label.empty() == false)
  542. MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
  543. }
  544. time_t MinAge = d->ValidUntilMin;
  545. if (MinAge == 0)
  546. {
  547. MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
  548. if (Label.empty() == false)
  549. MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
  550. }
  551. if (MinAge != 0 || ValidUntil != 0 || MaxAge != 0)
  552. {
  553. if (MinAge != 0 && ValidUntil != 0)
  554. {
  555. time_t const min_date = Date + MinAge;
  556. if (ValidUntil < min_date)
  557. ValidUntil = min_date;
  558. }
  559. if (MaxAge != 0 && Date != 0)
  560. {
  561. time_t const max_date = Date + MaxAge;
  562. if (ValidUntil == 0 || ValidUntil > max_date)
  563. ValidUntil = max_date;
  564. }
  565. }
  566. }
  567. }
  568. /* as the Release file is parsed only after it was verified, the Signed-By field
  569. does not effect the current, but the "next" Release file */
  570. auto Sign = Section.FindS("Signed-By");
  571. if (Sign.empty() == false)
  572. {
  573. SignedBy = NormalizeSignedBy(Sign, false);
  574. if (SignedBy.empty() && ErrorText != NULL)
  575. strprintf(*ErrorText, _("Invalid '%s' entry in Release file %s"), "Signed-By", Filename.c_str());
  576. }
  577. if (AuthPossible)
  578. LoadedSuccessfully = TRI_YES;
  579. return AuthPossible;
  580. }
  581. /*}}}*/
  582. time_t debReleaseIndex::GetNotBefore() const /*{{{*/
  583. {
  584. return d->NotBefore;
  585. }
  586. /*}}}*/
  587. metaIndex * debReleaseIndex::UnloadedClone() const /*{{{*/
  588. {
  589. if (Trusted == TRI_NO)
  590. return new debReleaseIndex(URI, Dist, false, d->ReleaseOptions);
  591. else if (Trusted == TRI_YES)
  592. return new debReleaseIndex(URI, Dist, true, d->ReleaseOptions);
  593. else
  594. return new debReleaseIndex(URI, Dist, d->ReleaseOptions);
  595. }
  596. /*}}}*/
  597. bool debReleaseIndex::parseSumData(const char *&Start, const char *End, /*{{{*/
  598. std::string &Name, std::string &Hash, unsigned long long &Size)
  599. {
  600. Name = "";
  601. Hash = "";
  602. Size = 0;
  603. /* Skip over the first blank */
  604. while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r')
  605. && Start < End)
  606. Start++;
  607. if (Start >= End)
  608. return false;
  609. /* Move EntryEnd to the end of the first entry (the hash) */
  610. const char *EntryEnd = Start;
  611. while ((*EntryEnd != '\t' && *EntryEnd != ' ')
  612. && EntryEnd < End)
  613. EntryEnd++;
  614. if (EntryEnd == End)
  615. return false;
  616. Hash.append(Start, EntryEnd-Start);
  617. /* Skip over intermediate blanks */
  618. Start = EntryEnd;
  619. while (*Start == '\t' || *Start == ' ')
  620. Start++;
  621. if (Start >= End)
  622. return false;
  623. EntryEnd = Start;
  624. /* Find the end of the second entry (the size) */
  625. while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
  626. && EntryEnd < End)
  627. EntryEnd++;
  628. if (EntryEnd == End)
  629. return false;
  630. Size = strtoull (Start, NULL, 10);
  631. /* Skip over intermediate blanks */
  632. Start = EntryEnd;
  633. while (*Start == '\t' || *Start == ' ')
  634. Start++;
  635. if (Start >= End)
  636. return false;
  637. EntryEnd = Start;
  638. /* Find the end of the third entry (the filename) */
  639. while ((*EntryEnd != '\t' && *EntryEnd != ' ' &&
  640. *EntryEnd != '\n' && *EntryEnd != '\r')
  641. && EntryEnd < End)
  642. EntryEnd++;
  643. Name.append(Start, EntryEnd-Start);
  644. Start = EntryEnd; //prepare for the next round
  645. return true;
  646. }
  647. /*}}}*/
  648. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll)/*{{{*/
  649. {
  650. #define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X), false, false, d->ReleaseOptions)
  651. pkgAcqMetaClearSig * const TransactionManager = new pkgAcqMetaClearSig(Owner,
  652. APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"), this);
  653. #undef APT_TARGET
  654. // special case for --print-uris
  655. if (GetAll)
  656. for (auto const &Target: GetIndexTargets())
  657. if (Target.Option(IndexTarget::FALLBACK_OF).empty())
  658. new pkgAcqIndex(Owner, TransactionManager, Target);
  659. return true;
  660. }
  661. /*}}}*/
  662. // ReleaseIndex::Set* TriState options /*{{{*/
  663. bool debReleaseIndex::SetTrusted(TriState const pTrusted)
  664. {
  665. if (Trusted == TRI_UNSET)
  666. Trusted = pTrusted;
  667. else if (Trusted != pTrusted)
  668. // TRANSLATOR: The first is an option name from sources.list manpage, the other two URI and Suite
  669. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Trusted", URI.c_str(), Dist.c_str());
  670. return true;
  671. }
  672. bool debReleaseIndex::SetCheckValidUntil(TriState const pCheckValidUntil)
  673. {
  674. if (d->CheckValidUntil == TRI_UNSET)
  675. d->CheckValidUntil = pCheckValidUntil;
  676. else if (d->CheckValidUntil != pCheckValidUntil)
  677. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Check-Valid-Until", URI.c_str(), Dist.c_str());
  678. return true;
  679. }
  680. bool debReleaseIndex::SetValidUntilMin(time_t const Valid)
  681. {
  682. if (d->ValidUntilMin == 0)
  683. d->ValidUntilMin = Valid;
  684. else if (d->ValidUntilMin != Valid)
  685. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Min-ValidTime", URI.c_str(), Dist.c_str());
  686. return true;
  687. }
  688. bool debReleaseIndex::SetValidUntilMax(time_t const Valid)
  689. {
  690. if (d->ValidUntilMax == 0)
  691. d->ValidUntilMax = Valid;
  692. else if (d->ValidUntilMax != Valid)
  693. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Max-ValidTime", URI.c_str(), Dist.c_str());
  694. return true;
  695. }
  696. bool debReleaseIndex::SetCheckDate(TriState const pCheckDate)
  697. {
  698. if (d->CheckDate == TRI_UNSET)
  699. d->CheckDate = pCheckDate;
  700. else if (d->CheckDate != pCheckDate)
  701. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Check-Date", URI.c_str(), Dist.c_str());
  702. return true;
  703. }
  704. bool debReleaseIndex::SetDateMaxFuture(time_t const DateMaxFuture)
  705. {
  706. if (d->DateMaxFuture == 0)
  707. d->DateMaxFuture = DateMaxFuture;
  708. else if (d->DateMaxFuture != DateMaxFuture)
  709. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Date-Max-Future", URI.c_str(), Dist.c_str());
  710. return true;
  711. }
  712. bool debReleaseIndex::SetSignedBy(std::string const &pSignedBy)
  713. {
  714. if (SignedBy.empty() == true && pSignedBy.empty() == false)
  715. {
  716. SignedBy = NormalizeSignedBy(pSignedBy, true);
  717. if (SignedBy.empty())
  718. _error->Error(_("Invalid value set for option %s regarding source %s %s (%s)"), "Signed-By", URI.c_str(), Dist.c_str(), "not a fingerprint");
  719. }
  720. else
  721. {
  722. auto const normalSignedBy = NormalizeSignedBy(pSignedBy, true);
  723. if (normalSignedBy != SignedBy)
  724. return _error->Error(_("Conflicting values set for option %s regarding source %s %s: %s != %s"), "Signed-By", URI.c_str(), Dist.c_str(), SignedBy.c_str(), normalSignedBy.c_str());
  725. }
  726. return true;
  727. }
  728. /*}}}*/
  729. // ReleaseIndex::IsTrusted /*{{{*/
  730. bool debReleaseIndex::IsTrusted() const
  731. {
  732. if (Trusted == TRI_YES)
  733. return true;
  734. else if (Trusted == TRI_NO)
  735. return false;
  736. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  737. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  738. return true;
  739. if (FileExists(MetaIndexFile("Release.gpg")))
  740. return true;
  741. return FileExists(MetaIndexFile("InRelease"));
  742. }
  743. /*}}}*/
  744. bool debReleaseIndex::IsArchitectureSupported(std::string const &arch) const/*{{{*/
  745. {
  746. if (d->Architectures.empty())
  747. return true;
  748. return std::find(d->Architectures.begin(), d->Architectures.end(), arch) != d->Architectures.end();
  749. }
  750. /*}}}*/
  751. bool debReleaseIndex::IsArchitectureAllSupportedFor(IndexTarget const &target) const/*{{{*/
  752. {
  753. if (target.Options.find("Force-Support-For-All") != target.Options.end())
  754. return true;
  755. if (IsArchitectureSupported("all") == false)
  756. return false;
  757. if (d->NoSupportForAll.empty())
  758. return true;
  759. return std::find(d->NoSupportForAll.begin(), d->NoSupportForAll.end(), target.Option(IndexTarget::CREATED_BY)) == d->NoSupportForAll.end();
  760. }
  761. /*}}}*/
  762. bool debReleaseIndex::HasSupportForComponent(std::string const &component) const/*{{{*/
  763. {
  764. if (d->SupportedComponents.empty())
  765. return true;
  766. return std::find(d->SupportedComponents.begin(), d->SupportedComponents.end(), component) != d->SupportedComponents.end();
  767. }
  768. /*}}}*/
  769. std::vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() /*{{{*/
  770. {
  771. if (Indexes != NULL)
  772. return Indexes;
  773. Indexes = new std::vector<pkgIndexFile*>();
  774. bool const istrusted = IsTrusted();
  775. for (auto const &T: GetIndexTargets())
  776. {
  777. std::string const TargetName = T.Option(IndexTarget::CREATED_BY);
  778. if (TargetName == "Packages")
  779. Indexes->push_back(new debPackagesIndex(T, istrusted));
  780. else if (TargetName == "Sources")
  781. Indexes->push_back(new debSourcesIndex(T, istrusted));
  782. else if (TargetName == "Translations")
  783. Indexes->push_back(new debTranslationsIndex(T));
  784. }
  785. return Indexes;
  786. }
  787. /*}}}*/
  788. std::map<std::string, std::string> debReleaseIndex::GetReleaseOptions()
  789. {
  790. return d->ReleaseOptions;
  791. }
  792. static bool ReleaseFileName(debReleaseIndex const * const That, std::string &ReleaseFile)/*{{{*/
  793. {
  794. ReleaseFile = That->MetaIndexFile("InRelease");
  795. bool releaseExists = false;
  796. if (FileExists(ReleaseFile) == true)
  797. releaseExists = true;
  798. else
  799. {
  800. ReleaseFile = That->MetaIndexFile("Release");
  801. if (FileExists(ReleaseFile))
  802. releaseExists = true;
  803. }
  804. return releaseExists;
  805. }
  806. /*}}}*/
  807. bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/*{{{*/
  808. {
  809. std::string ReleaseFile;
  810. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  811. ::URI Tmp(URI);
  812. if (Gen.SelectReleaseFile(ReleaseFile, Tmp.Host) == false)
  813. return _error->Error("Problem with SelectReleaseFile %s", ReleaseFile.c_str());
  814. if (releaseExists == false)
  815. return true;
  816. FileFd Rel;
  817. // Beware: The 'Release' file might be clearsigned in case the
  818. // signature for an 'InRelease' file couldn't be checked
  819. if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false)
  820. return false;
  821. // Store the IMS information
  822. pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
  823. pkgCacheGenerator::Dynamic<pkgCache::RlsFileIterator> DynFile(File);
  824. // Rel can't be used as this is potentially a temporary file
  825. struct stat Buf;
  826. if (stat(ReleaseFile.c_str(), &Buf) != 0)
  827. return _error->Errno("fstat", "Unable to stat file %s", ReleaseFile.c_str());
  828. File->Size = Buf.st_size;
  829. File->mtime = Buf.st_mtime;
  830. pkgTagFile TagFile(&Rel, Rel.Size());
  831. pkgTagSection Section;
  832. if (Rel.IsOpen() == false || Rel.Failed() || TagFile.Step(Section) == false)
  833. return false;
  834. std::string data;
  835. #define APT_INRELEASE(TYPE, TAG, STORE) \
  836. data = Section.FindS(TAG); \
  837. if (data.empty() == false) \
  838. { \
  839. map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
  840. if (storage == 0) return false; \
  841. STORE = storage; \
  842. }
  843. APT_INRELEASE(MIXED, "Suite", File->Archive)
  844. APT_INRELEASE(VERSIONNUMBER, "Version", File->Version)
  845. APT_INRELEASE(MIXED, "Origin", File->Origin)
  846. APT_INRELEASE(MIXED, "Codename", File->Codename)
  847. APT_INRELEASE(MIXED, "Label", File->Label)
  848. #undef APT_INRELEASE
  849. Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
  850. Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
  851. Section.FindFlag("Packages-Require-Authorization", File->Flags, pkgCache::Flag::PackagesRequireAuthorization);
  852. return true;
  853. }
  854. /*}}}*/
  855. // ReleaseIndex::FindInCache - Find this index /*{{{*/
  856. pkgCache::RlsFileIterator debReleaseIndex::FindInCache(pkgCache &Cache, bool const ModifyCheck) const
  857. {
  858. std::string ReleaseFile;
  859. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  860. pkgCache::RlsFileIterator File = Cache.RlsFileBegin();
  861. for (; File.end() == false; ++File)
  862. {
  863. if (File->FileName == 0 || ReleaseFile != File.FileName())
  864. continue;
  865. // empty means the file does not exist by "design"
  866. if (ModifyCheck == false || (releaseExists == false && File->Size == 0))
  867. return File;
  868. struct stat St;
  869. if (stat(File.FileName(),&St) != 0)
  870. {
  871. if (_config->FindB("Debug::pkgCacheGen", false))
  872. std::clog << "ReleaseIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
  873. return pkgCache::RlsFileIterator(Cache);
  874. }
  875. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  876. {
  877. if (_config->FindB("Debug::pkgCacheGen", false))
  878. std::clog << "ReleaseIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
  879. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  880. << ") doesn't match for " << File.FileName() << std::endl;
  881. return pkgCache::RlsFileIterator(Cache);
  882. }
  883. return File;
  884. }
  885. return File;
  886. }
  887. /*}}}*/
  888. class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/
  889. {
  890. static std::vector<std::string> getDefaultSetOf(std::string const &Name,
  891. std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues)
  892. {
  893. auto const val = Options.find(Name);
  894. if (val != Options.end())
  895. return VectorizeString(val->second, ',');
  896. return defaultValues;
  897. }
  898. static std::vector<std::string> applyPlusMinusOptions(std::string const &Name,
  899. std::map<std::string, std::string> const &Options, std::vector<std::string> &&Values)
  900. {
  901. auto val = Options.find(Name + "+");
  902. if (val != Options.end())
  903. {
  904. std::vector<std::string> const plus = VectorizeString(val->second, ',');
  905. std::copy_if(plus.begin(), plus.end(), std::back_inserter(Values), [&Values](std::string const &v) {
  906. return std::find(Values.begin(), Values.end(), v) == Values.end();
  907. });
  908. }
  909. if ((val = Options.find(Name + "-")) != Options.end())
  910. {
  911. std::vector<std::string> const minus = VectorizeString(val->second, ',');
  912. Values.erase(std::remove_if(Values.begin(), Values.end(), [&minus](std::string const &v) {
  913. return std::find(minus.begin(), minus.end(), v) != minus.end();
  914. }), Values.end());
  915. }
  916. return Values;
  917. }
  918. static std::vector<std::string> parsePlusMinusOptions(std::string const &Name,
  919. std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues)
  920. {
  921. return applyPlusMinusOptions(Name, Options, getDefaultSetOf(Name, Options, defaultValues));
  922. }
  923. static std::vector<std::string> parsePlusMinusArchOptions(std::string const &Name,
  924. std::map<std::string, std::string> const &Options)
  925. {
  926. auto Values = getDefaultSetOf(Name, Options, APT::Configuration::getArchitectures());
  927. // all is a very special architecture users shouldn't be concerned with explicitly
  928. // but if the user does, do not override the choice
  929. auto const val = Options.find(Name + "-");
  930. if (val != Options.end())
  931. {
  932. std::vector<std::string> const minus = VectorizeString(val->second, ',');
  933. if (std::find(minus.begin(), minus.end(), "all") != minus.end())
  934. return applyPlusMinusOptions(Name, Options, std::move(Values));
  935. }
  936. Values = applyPlusMinusOptions(Name, Options, std::move(Values));
  937. if (std::find(Values.begin(), Values.end(), "all") == Values.end())
  938. Values.push_back("implicit:all");
  939. return Values;
  940. }
  941. static std::vector<std::string> parsePlusMinusTargetOptions(char const * const Name,
  942. std::map<std::string, std::string> const &Options)
  943. {
  944. std::vector<std::string> const alltargets = _config->FindVector(std::string("Acquire::IndexTargets::") + Name, "", true);
  945. std::vector<std::string> deftargets;
  946. deftargets.reserve(alltargets.size());
  947. std::copy_if(alltargets.begin(), alltargets.end(), std::back_inserter(deftargets), [&](std::string const &t) {
  948. std::string c = "Acquire::IndexTargets::";
  949. c.append(Name).append("::").append(t).append("::DefaultEnabled");
  950. return _config->FindB(c, true);
  951. });
  952. std::vector<std::string> mytargets = parsePlusMinusOptions("target", Options, deftargets);
  953. for (auto const &target : alltargets)
  954. {
  955. std::map<std::string, std::string>::const_iterator const opt = Options.find(target);
  956. if (opt == Options.end())
  957. continue;
  958. auto const idMatch = [&](std::string const &t) {
  959. return target == _config->Find(std::string("Acquire::IndexTargets::") + Name + "::" + t + "::Identifier", t);
  960. };
  961. if (StringToBool(opt->second))
  962. std::copy_if(alltargets.begin(), alltargets.end(), std::back_inserter(mytargets), idMatch);
  963. else
  964. mytargets.erase(std::remove_if(mytargets.begin(), mytargets.end(), idMatch), mytargets.end());
  965. }
  966. // if we can't order it in a 1000 steps we give up… probably a cycle
  967. for (auto i = 0; i < 1000; ++i)
  968. {
  969. bool Changed = false;
  970. for (auto t = mytargets.begin(); t != mytargets.end(); ++t)
  971. {
  972. std::string const fallback = _config->Find(std::string("Acquire::IndexTargets::") + Name + "::" + *t + "::Fallback-Of");
  973. if (fallback.empty())
  974. continue;
  975. auto const faller = std::find(mytargets.begin(), mytargets.end(), fallback);
  976. if (faller == mytargets.end() || faller < t)
  977. continue;
  978. Changed = true;
  979. auto const tv = *t;
  980. mytargets.erase(t);
  981. mytargets.emplace_back(tv);
  982. break;
  983. }
  984. if (Changed == false)
  985. break;
  986. }
  987. // remove duplicates without changing the order (in first appearance)
  988. {
  989. std::set<std::string> seenOnce;
  990. mytargets.erase(std::remove_if(mytargets.begin(), mytargets.end(), [&](std::string const &t) {
  991. return seenOnce.insert(t).second == false;
  992. }), mytargets.end());
  993. }
  994. return mytargets;
  995. }
  996. metaIndex::TriState GetTriStateOption(std::map<std::string, std::string>const &Options, char const * const name) const
  997. {
  998. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  999. if (opt != Options.end())
  1000. return StringToBool(opt->second, false) ? metaIndex::TRI_YES : metaIndex::TRI_NO;
  1001. return metaIndex::TRI_DONTCARE;
  1002. }
  1003. static time_t GetTimeOption(std::map<std::string, std::string>const &Options, char const * const name)
  1004. {
  1005. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  1006. if (opt == Options.end())
  1007. return 0;
  1008. return strtoull(opt->second.c_str(), NULL, 10);
  1009. }
  1010. static bool GetBoolOption(std::map<std::string, std::string> const &Options, char const * const name, bool const defVal)
  1011. {
  1012. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  1013. if (opt == Options.end())
  1014. return defVal;
  1015. return StringToBool(opt->second, defVal);
  1016. }
  1017. static std::vector<std::string> GetMapKeys(std::map<std::string, std::string> const &Options)
  1018. {
  1019. std::vector<std::string> ret;
  1020. ret.reserve(Options.size());
  1021. for (auto &&O: Options)
  1022. ret.emplace_back(O.first);
  1023. std::sort(ret.begin(), ret.end());
  1024. return ret;
  1025. }
  1026. static bool MapsAreEqual(std::map<std::string, std::string> const &OptionsA,
  1027. std::map<std::string, std::string> const &OptionsB,
  1028. std::string const &URI, std::string const &Dist)
  1029. {
  1030. auto const KeysA = GetMapKeys(OptionsA);
  1031. auto const KeysB = GetMapKeys(OptionsB);
  1032. auto const m = std::mismatch(KeysA.begin(), KeysA.end(), KeysB.begin());
  1033. if (m.first != KeysA.end())
  1034. {
  1035. if (std::find(KeysB.begin(), KeysB.end(), *m.first) == KeysB.end())
  1036. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), m.first->c_str(), "<set>", "<unset>");
  1037. else
  1038. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), m.second->c_str(), "<set>", "<unset>");
  1039. }
  1040. if (m.second != KeysB.end())
  1041. {
  1042. if (std::find(KeysA.begin(), KeysA.end(), *m.second) == KeysA.end())
  1043. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), m.first->c_str(), "<set>", "<unset>");
  1044. else
  1045. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), m.second->c_str(), "<set>", "<unset>");
  1046. }
  1047. for (auto&& key: KeysA)
  1048. {
  1049. if (key == "BASE_URI" || key == "REPO_URI" || key == "SITE" || key == "RELEASE")
  1050. continue;
  1051. auto const a = OptionsA.find(key);
  1052. auto const b = OptionsB.find(key);
  1053. if (unlikely(a == OptionsA.end() || b == OptionsB.end()) || a->second != b->second)
  1054. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), key.c_str(), URI.c_str(), Dist.c_str());
  1055. }
  1056. return true;
  1057. }
  1058. static debReleaseIndex * GetDebReleaseIndexBy(std::vector<metaIndex *> &List, std::string const &URI,
  1059. std::string const &Dist, std::map<std::string, std::string> const &Options)
  1060. {
  1061. std::map<std::string, std::string> ReleaseOptions{{
  1062. {"BASE_URI", constructMetaIndexURI(URI, Dist, "")},
  1063. {"REPO_URI", URI},
  1064. {"SITE", ::URI::ArchiveOnly(URI)},
  1065. {"RELEASE", (Dist == "/") ? "" : Dist},
  1066. }};
  1067. if (GetBoolOption(Options, "allow-insecure", _config->FindB("Acquire::AllowInsecureRepositories")))
  1068. ReleaseOptions.emplace("ALLOW_INSECURE", "true");
  1069. if (GetBoolOption(Options, "allow-weak", _config->FindB("Acquire::AllowWeakRepositories")))
  1070. ReleaseOptions.emplace("ALLOW_WEAK", "true");
  1071. if (GetBoolOption(Options, "allow-downgrade-to-insecure", _config->FindB("Acquire::AllowDowngradeToInsecureRepositories")))
  1072. ReleaseOptions.emplace("ALLOW_DOWNGRADE_TO_INSECURE", "true");
  1073. auto InReleasePath = Options.find("inrelease-path");
  1074. if (InReleasePath != Options.end())
  1075. ReleaseOptions.emplace("INRELEASE_PATH", InReleasePath->second);
  1076. debReleaseIndex * Deb = nullptr;
  1077. std::string const FileName = URItoFileName(constructMetaIndexURI(URI, Dist, "Release"));
  1078. for (auto const &I: List)
  1079. {
  1080. // We only worry about debian entries here
  1081. if (strcmp(I->GetType(), "deb") != 0)
  1082. continue;
  1083. auto const D = dynamic_cast<debReleaseIndex*>(I);
  1084. if (unlikely(D == nullptr))
  1085. continue;
  1086. /* This check ensures that there will be only one Release file
  1087. queued for all the Packages files and Sources files it
  1088. corresponds to. */
  1089. if (URItoFileName(D->MetaIndexURI("Release")) == FileName)
  1090. {
  1091. if (MapsAreEqual(ReleaseOptions, D->GetReleaseOptions(), URI, Dist) == false)
  1092. return nullptr;
  1093. Deb = D;
  1094. break;
  1095. }
  1096. }
  1097. // No currently created Release file indexes this entry, so we create a new one.
  1098. if (Deb == nullptr)
  1099. {
  1100. Deb = new debReleaseIndex(URI, Dist, ReleaseOptions);
  1101. List.push_back(Deb);
  1102. }
  1103. return Deb;
  1104. }
  1105. protected:
  1106. bool CreateItemInternal(std::vector<metaIndex *> &List, std::string const &URI,
  1107. std::string const &Dist, std::string const &Section,
  1108. bool const &IsSrc, std::map<std::string, std::string> const &Options) const
  1109. {
  1110. auto const Deb = GetDebReleaseIndexBy(List, URI, Dist, Options);
  1111. if (Deb == nullptr)
  1112. return false;
  1113. bool const UsePDiffs = GetBoolOption(Options, "pdiffs", _config->FindB("Acquire::PDiffs", true));
  1114. std::string UseByHash = _config->Find("APT::Acquire::By-Hash", "yes");
  1115. UseByHash = _config->Find("Acquire::By-Hash", UseByHash);
  1116. {
  1117. std::string const host = ::URI(URI).Host;
  1118. if (host.empty() == false)
  1119. {
  1120. UseByHash = _config->Find("APT::Acquire::" + host + "::By-Hash", UseByHash);
  1121. UseByHash = _config->Find("Acquire::" + host + "::By-Hash", UseByHash);
  1122. }
  1123. std::map<std::string, std::string>::const_iterator const opt = Options.find("by-hash");
  1124. if (opt != Options.end())
  1125. UseByHash = opt->second;
  1126. }
  1127. auto const entry = Options.find("sourceslist-entry");
  1128. Deb->AddComponent(
  1129. entry->second,
  1130. IsSrc,
  1131. Section,
  1132. parsePlusMinusTargetOptions(Name, Options),
  1133. parsePlusMinusArchOptions("arch", Options),
  1134. parsePlusMinusOptions("lang", Options, APT::Configuration::getLanguages(true)),
  1135. UsePDiffs,
  1136. UseByHash
  1137. );
  1138. if (Deb->SetTrusted(GetTriStateOption(Options, "trusted")) == false ||
  1139. Deb->SetCheckValidUntil(GetTriStateOption(Options, "check-valid-until")) == false ||
  1140. Deb->SetValidUntilMax(GetTimeOption(Options, "valid-until-max")) == false ||
  1141. Deb->SetValidUntilMin(GetTimeOption(Options, "valid-until-min")) == false ||
  1142. Deb->SetCheckDate(GetTriStateOption(Options, "check-date")) == false ||
  1143. Deb->SetDateMaxFuture(GetTimeOption(Options, "date-max-future")) == false)
  1144. return false;
  1145. std::map<std::string, std::string>::const_iterator const signedby = Options.find("signed-by");
  1146. if (signedby == Options.end())
  1147. {
  1148. bool alreadySet = false;
  1149. std::string filename;
  1150. if (ReleaseFileName(Deb, filename))
  1151. {
  1152. auto OldDeb = Deb->UnloadedClone();
  1153. _error->PushToStack();
  1154. OldDeb->Load(filename, nullptr);
  1155. bool const goodLoad = _error->PendingError() == false;
  1156. _error->RevertToStack();
  1157. if (goodLoad)
  1158. {
  1159. if (OldDeb->GetValidUntil() > 0)
  1160. {
  1161. time_t const invalid_since = time(NULL) - OldDeb->GetValidUntil();
  1162. if (invalid_since <= 0)
  1163. {
  1164. Deb->SetSignedBy(OldDeb->GetSignedBy());
  1165. alreadySet = true;
  1166. }
  1167. }
  1168. }
  1169. delete OldDeb;
  1170. }
  1171. if (alreadySet == false && Deb->SetSignedBy("") == false)
  1172. return false;
  1173. }
  1174. else
  1175. {
  1176. if (Deb->SetSignedBy(signedby->second) == false)
  1177. return false;
  1178. }
  1179. return true;
  1180. }
  1181. debSLTypeDebian(char const * const Name, char const * const Label) : Type(Name, Label)
  1182. {
  1183. }
  1184. };
  1185. /*}}}*/
  1186. class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian /*{{{*/
  1187. {
  1188. public:
  1189. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  1190. std::string const &Dist, std::string const &Section,
  1191. std::map<std::string, std::string> const &Options) const APT_OVERRIDE
  1192. {
  1193. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  1194. }
  1195. debSLTypeDeb() : debSLTypeDebian("deb", "Debian binary tree")
  1196. {
  1197. }
  1198. };
  1199. /*}}}*/
  1200. class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian /*{{{*/
  1201. {
  1202. public:
  1203. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  1204. std::string const &Dist, std::string const &Section,
  1205. std::map<std::string, std::string> const &Options) const APT_OVERRIDE
  1206. {
  1207. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  1208. }
  1209. debSLTypeDebSrc() : debSLTypeDebian("deb-src", "Debian source tree")
  1210. {
  1211. }
  1212. };
  1213. /*}}}*/
  1214. APT_HIDDEN debSLTypeDeb _apt_DebType;
  1215. APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;