debmetaindex.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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 (Fd.IsOpen() == false || Fd.Failed())
  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. // Store the IMS information
  586. pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
  587. pkgCacheGenerator::Dynamic<pkgCache::RlsFileIterator> DynFile(File);
  588. // Rel can't be used as this is potentially a temporary file
  589. struct stat Buf;
  590. if (stat(ReleaseFile.c_str(), &Buf) != 0)
  591. return _error->Errno("fstat", "Unable to stat file %s", ReleaseFile.c_str());
  592. File->Size = Buf.st_size;
  593. File->mtime = Buf.st_mtime;
  594. pkgTagFile TagFile(&Rel, Rel.Size());
  595. pkgTagSection Section;
  596. if (Rel.IsOpen() == false || Rel.Failed() || TagFile.Step(Section) == false)
  597. return false;
  598. std::string data;
  599. #define APT_INRELEASE(TYPE, TAG, STORE) \
  600. data = Section.FindS(TAG); \
  601. if (data.empty() == false) \
  602. { \
  603. map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
  604. if (storage == 0) return false; \
  605. STORE = storage; \
  606. }
  607. APT_INRELEASE(MIXED, "Suite", File->Archive)
  608. APT_INRELEASE(VERSIONNUMBER, "Version", File->Version)
  609. APT_INRELEASE(MIXED, "Origin", File->Origin)
  610. APT_INRELEASE(MIXED, "Codename", File->Codename)
  611. APT_INRELEASE(MIXED, "Label", File->Label)
  612. #undef APT_INRELEASE
  613. Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
  614. Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
  615. return true;
  616. }
  617. /*}}}*/
  618. // ReleaseIndex::FindInCache - Find this index /*{{{*/
  619. pkgCache::RlsFileIterator debReleaseIndex::FindInCache(pkgCache &Cache, bool const ModifyCheck) const
  620. {
  621. std::string ReleaseFile;
  622. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  623. pkgCache::RlsFileIterator File = Cache.RlsFileBegin();
  624. for (; File.end() == false; ++File)
  625. {
  626. if (File->FileName == 0 || ReleaseFile != File.FileName())
  627. continue;
  628. // empty means the file does not exist by "design"
  629. if (ModifyCheck == false || (releaseExists == false && File->Size == 0))
  630. return File;
  631. struct stat St;
  632. if (stat(File.FileName(),&St) != 0)
  633. {
  634. if (_config->FindB("Debug::pkgCacheGen", false))
  635. std::clog << "ReleaseIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
  636. return pkgCache::RlsFileIterator(Cache);
  637. }
  638. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  639. {
  640. if (_config->FindB("Debug::pkgCacheGen", false))
  641. std::clog << "ReleaseIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
  642. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  643. << ") doesn't match for " << File.FileName() << std::endl;
  644. return pkgCache::RlsFileIterator(Cache);
  645. }
  646. return File;
  647. }
  648. return File;
  649. }
  650. /*}}}*/
  651. static std::vector<std::string> parsePlusMinusOptions(std::string const &Name, /*{{{*/
  652. std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues)
  653. {
  654. std::map<std::string, std::string>::const_iterator val = Options.find(Name);
  655. std::vector<std::string> Values;
  656. if (val != Options.end())
  657. Values = VectorizeString(val->second, ',');
  658. else
  659. Values = defaultValues;
  660. if ((val = Options.find(Name + "+")) != Options.end())
  661. {
  662. std::vector<std::string> const plus = VectorizeString(val->second, ',');
  663. std::copy_if(plus.begin(), plus.end(), std::back_inserter(Values), [&Values](std::string const &v) {
  664. return std::find(Values.begin(), Values.end(), v) == Values.end();
  665. });
  666. }
  667. if ((val = Options.find(Name + "-")) != Options.end())
  668. {
  669. std::vector<std::string> const minus = VectorizeString(val->second, ',');
  670. Values.erase(std::remove_if(Values.begin(), Values.end(), [&minus](std::string const &v) {
  671. return std::find(minus.begin(), minus.end(), v) != minus.end();
  672. }), Values.end());
  673. }
  674. return Values;
  675. }
  676. /*}}}*/
  677. class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/
  678. {
  679. metaIndex::TriState GetTriStateOption(std::map<std::string, std::string>const &Options, char const * const name) const
  680. {
  681. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  682. if (opt != Options.end())
  683. return StringToBool(opt->second, false) ? metaIndex::TRI_YES : metaIndex::TRI_NO;
  684. return metaIndex::TRI_DONTCARE;
  685. }
  686. time_t GetTimeOption(std::map<std::string, std::string>const &Options, char const * const name) const
  687. {
  688. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  689. if (opt == Options.end())
  690. return 0;
  691. return strtoull(opt->second.c_str(), NULL, 10);
  692. }
  693. protected:
  694. bool CreateItemInternal(std::vector<metaIndex *> &List, std::string const &URI,
  695. std::string const &Dist, std::string const &Section,
  696. bool const &IsSrc, std::map<std::string, std::string> const &Options) const
  697. {
  698. debReleaseIndex *Deb = NULL;
  699. for (std::vector<metaIndex *>::const_iterator I = List.begin();
  700. I != List.end(); ++I)
  701. {
  702. // We only worry about debian entries here
  703. if (strcmp((*I)->GetType(), "deb") != 0)
  704. continue;
  705. /* This check insures that there will be only one Release file
  706. queued for all the Packages files and Sources files it
  707. corresponds to. */
  708. if ((*I)->GetURI() == URI && (*I)->GetDist() == Dist)
  709. {
  710. Deb = dynamic_cast<debReleaseIndex*>(*I);
  711. if (Deb != NULL)
  712. break;
  713. }
  714. }
  715. // No currently created Release file indexes this entry, so we create a new one.
  716. if (Deb == NULL)
  717. {
  718. Deb = new debReleaseIndex(URI, Dist);
  719. List.push_back(Deb);
  720. }
  721. std::vector<std::string> const alltargets = _config->FindVector(std::string("Acquire::IndexTargets::") + Name, "", true);
  722. std::vector<std::string> deftargets;
  723. deftargets.reserve(alltargets.size());
  724. std::copy_if(alltargets.begin(), alltargets.end(), std::back_inserter(deftargets), [&](std::string const &t) {
  725. std::string c = "Acquire::IndexTargets::";
  726. c.append(Name).append("::").append(t).append("::DefaultEnabled");
  727. return _config->FindB(c, true);
  728. });
  729. std::vector<std::string> mytargets = parsePlusMinusOptions("target", Options, deftargets);
  730. for (auto const &target : alltargets)
  731. {
  732. std::map<std::string, std::string>::const_iterator const opt = Options.find(target);
  733. if (opt == Options.end())
  734. continue;
  735. auto const tarItr = std::find(mytargets.begin(), mytargets.end(), target);
  736. bool const optValue = StringToBool(opt->second);
  737. if (optValue == true && tarItr == mytargets.end())
  738. mytargets.push_back(target);
  739. else if (optValue == false && tarItr != mytargets.end())
  740. mytargets.erase(std::remove(mytargets.begin(), mytargets.end(), target), mytargets.end());
  741. }
  742. bool UsePDiffs = _config->FindB("Acquire::PDiffs", true);
  743. {
  744. std::map<std::string, std::string>::const_iterator const opt = Options.find("pdiffs");
  745. if (opt != Options.end())
  746. UsePDiffs = StringToBool(opt->second);
  747. }
  748. auto const entry = Options.find("sourceslist-entry");
  749. Deb->AddComponent(
  750. entry->second,
  751. IsSrc,
  752. Section,
  753. mytargets,
  754. parsePlusMinusOptions("arch", Options, APT::Configuration::getArchitectures()),
  755. parsePlusMinusOptions("lang", Options, APT::Configuration::getLanguages(true)),
  756. UsePDiffs
  757. );
  758. if (Deb->SetTrusted(GetTriStateOption(Options, "trusted")) == false ||
  759. Deb->SetCheckValidUntil(GetTriStateOption(Options, "check-valid-until")) == false ||
  760. Deb->SetValidUntilMax(GetTimeOption(Options, "valid-until-max")) == false ||
  761. Deb->SetValidUntilMin(GetTimeOption(Options, "valid-until-min")) == false)
  762. return false;
  763. std::map<std::string, std::string>::const_iterator const signedby = Options.find("signed-by");
  764. if (signedby == Options.end())
  765. {
  766. if (Deb->SetSignedBy("") == false)
  767. return false;
  768. }
  769. else
  770. {
  771. if (Deb->SetSignedBy(signedby->second) == false)
  772. return false;
  773. }
  774. return true;
  775. }
  776. debSLTypeDebian(char const * const Name, char const * const Label) : Type(Name, Label)
  777. {
  778. }
  779. };
  780. /*}}}*/
  781. class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian /*{{{*/
  782. {
  783. public:
  784. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  785. std::string const &Dist, std::string const &Section,
  786. std::map<std::string, std::string> const &Options) const APT_OVERRIDE
  787. {
  788. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  789. }
  790. debSLTypeDeb() : debSLTypeDebian("deb", "Debian binary tree")
  791. {
  792. }
  793. };
  794. /*}}}*/
  795. class APT_HIDDEN debSLTypeDebSrc : 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, true, Options);
  803. }
  804. debSLTypeDebSrc() : debSLTypeDebian("deb-src", "Debian source tree")
  805. {
  806. }
  807. };
  808. /*}}}*/
  809. APT_HIDDEN debSLTypeDeb _apt_DebType;
  810. APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;