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.
 
 
 
 
 
 

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