debmetaindex.cc 37 KB

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