debmetaindex.cc 44 KB

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