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.
 
 
 
 
 
 

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