debmetaindex.cc 32 KB

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