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

1067 lines
36 KiB

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