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.
 
 
 
 
 
 

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